Skip to content

Commit

Permalink
Merge pull request #217 from materialsproject/fix-compositon-serializ…
Browse files Browse the repository at this point in the history
…ation

Fix oxidation doc composition
  • Loading branch information
shyamd authored Jun 27, 2021
2 parents 9638766 + 2cf79b2 commit a25168c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
14 changes: 6 additions & 8 deletions emmet-builders/emmet/builders/materials/oxidation_states.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from pymatgen.core import __version__ as pymatgen_version

from emmet.core.oxidation_states import OxidationStateDoc
from emmet.core.utils import jsanitize


class OxidationStatesBuilder(MapBuilder):
Expand Down Expand Up @@ -37,14 +38,11 @@ def __init__(

def unary_function(self, item):
structure = Structure.from_dict(item["structure"])
oxi_doc = OxidationStateDoc.from_structure(structure)
doc = oxi_doc.dict()

doc.update(
{
"pymatgen_version": pymatgen_version,
"successful": True,
}
mpid = item["material_id"]

oxi_doc = OxidationStateDoc.from_structure(
structure=structure, material_id=mpid
)
doc = jsanitize(oxi_doc.dict(), allow_bson=True)

return doc
6 changes: 4 additions & 2 deletions emmet-core/emmet/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ def from_composition(
if fields is None
else fields
)
composition = composition.remove_charges()

elsyms = sorted(set([e.symbol for e in composition.elements]))

data = {
"elements": elsyms,
"nelements": len(elsyms),
"composition": composition,
"composition_reduced": composition.reduced_composition,
"composition_reduced": composition.reduced_composition.remove_charges(),
"formula_pretty": composition.reduced_formula,
"formula_anonymous": composition.anonymized_formula,
"chemsys": "-".join(elsyms),
Expand Down Expand Up @@ -126,7 +128,7 @@ def from_structure(
if fields is None
else fields
)
comp = structure.composition
comp = structure.composition.remove_charges()
elsyms = sorted(set([e.symbol for e in comp.elements]))
symmetry = SymmetryData.from_structure(structure)

Expand Down
30 changes: 30 additions & 0 deletions tests/emmet-builders/test_oxidation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest
from maggma.stores import JSONStore, MemoryStore

from emmet.builders.materials.oxidation_states import OxidationStatesBuilder


@pytest.fixture(scope="session")
def fake_materias(test_dir):
entries = JSONStore(test_dir / "LiTiO2_batt.json", key="entry_id")
entries.connect()

materials_store = MemoryStore(key="material_id")
materials_store.connect()

for doc in entries.query():
materials_store.update(
{"material_id": doc["entry_id"], "structure": doc["structure"]}
)
return materials_store


def test_oxidation_store(fake_materias):
oxi_store = MemoryStore()
builder = OxidationStatesBuilder(
materials=fake_materias, oxidation_states=oxi_store
)
builder.run()

assert oxi_store.count() == 6
assert all([isinstance(d["composition"], dict) for d in oxi_store.query()])

0 comments on commit a25168c

Please sign in to comment.