Skip to content

Commit 3634420

Browse files
author
Shyam Dwaraknath
authored
Merge pull request #201 from materialsproject/fix-oxidation
Fix oxidation document
2 parents b738071 + 21df762 commit 3634420

File tree

4 files changed

+28
-17
lines changed

4 files changed

+28
-17
lines changed

emmet-core/emmet/core/mpid.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,7 @@ def validate(cls, v):
7979
return v
8080
elif isinstance(v, str) and mpid_regex.fullmatch(v):
8181
return MPID(v)
82+
elif isinstance(v, int):
83+
return MPID(v)
8284

8385
raise ValueError("Invalid MPID Format")

emmet-core/emmet/core/oxidation_states.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,32 @@
33
from typing import Dict, List
44

55
import numpy as np
6-
from pydantic import BaseModel
6+
from pydantic import Field
77
from pymatgen.analysis.bond_valence import BVAnalyzer
88
from pymatgen.core import Structure
99
from pymatgen.core.periodic_table import Specie
1010
from typing_extensions import Literal
1111

12+
from emmet.core.material_property import PropertyDoc
13+
from emmet.core.mpid import MPID
1214

13-
class OxidationStateDoc(BaseModel):
1415

15-
possible_species: List[str]
16-
possible_valences: List[float]
17-
average_oxidation_states: Dict[str, float]
18-
method: Literal["BVAnalyzer", "oxi_state_guesses"]
19-
structure: Structure
16+
class OxidationStateDoc(PropertyDoc):
17+
"""Oxidation states computed from the structure"""
18+
19+
possible_species: List[str] = Field(
20+
description="Possible charged species in this material"
21+
)
22+
possible_valences: List[float] = Field(
23+
description="List of valences for each site in this material"
24+
)
25+
average_oxidation_states: Dict[str, float] = Field(
26+
description="Average oxidation states for each unique species"
27+
)
28+
method: str = Field(description="Method used to compute oxidation states")
2029

2130
@classmethod
22-
def from_structure(cls, structure: Structure):
31+
def from_structure(cls, structure: Structure, material_id: MPID, **kwargs): # type: ignore[override]
2332
structure.remove_oxidation_states()
2433
try:
2534
bva = BVAnalyzer()
@@ -49,12 +58,9 @@ def from_structure(cls, structure: Structure):
4958
"possible_species": list(possible_species),
5059
"possible_valences": valences,
5160
"average_oxidation_states": oxi_state_dict,
52-
"method": "BVAnalyzer",
53-
"structure": structure,
61+
"method": "Bond Valence Analysis",
5462
}
5563

56-
return cls(**d)
57-
5864
except Exception as e:
5965
logging.error("BVAnalyzer failed with: {}".format(e))
6066

@@ -76,12 +82,13 @@ def from_structure(cls, structure: Structure):
7682
"possible_species": list(possible_species),
7783
"possible_valences": valences,
7884
"average_oxidation_states": first_oxi_state_guess,
79-
"method": "oxi_state_guesses",
80-
"structure": structure,
85+
"method": "Oxidation State Guess",
8186
}
8287

83-
return cls(**d)
84-
8588
except Exception as e:
8689
logging.error("Oxidation state guess failed with: {}".format(e))
8790
raise e
91+
92+
return super().from_structure(
93+
structure=structure, material_id=material_id, **d, **kwargs
94+
)

tests/emmet-core/test_mpid.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ def test_mpid():
1616
== 3
1717
)
1818

19+
MPID(3)
20+
1921

2022
def test_to_str():
2123
assert str(MPID("mp-149")) == "mp-149"

tests/emmet-core/test_oxidation_states.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
def test_oxidation_state(structure: Structure):
3232
"""Very simple test to make sure this actually works"""
3333
print(f"Should work : {structure.composition}")
34-
doc = OxidationStateDoc.from_structure(structure)
34+
doc = OxidationStateDoc.from_structure(structure, material_id=33)
3535
assert doc is not None

0 commit comments

Comments
 (0)