Skip to content

Commit 2505edc

Browse files
authored
Merge pull request #1380 from materialsproject/xas-serde
update XAS type adapter + sync main into develop
2 parents 4f96ff5 + a951bfd commit 2505edc

3 files changed

Lines changed: 16 additions & 4 deletions

File tree

.github/workflows/testing.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ jobs:
108108
EMMET_USE_EMMET_MODELS: true
109109
run: python${{ matrix.python-version }} -m pytest -n auto --cov=emmet --cov-report=xml ${{ matrix.package }}/tests
110110

111-
- uses: codecov/codecov-action@v5.5.1
111+
- uses: codecov/codecov-action@v5.5.2
112112
with:
113113
token: ${{ secrets.CODECOV_TOKEN }}
114114
file: ./coverage.xml

emmet-core/emmet/core/trajectory.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,10 +740,11 @@ def from_vasprun(
740740
):
741741
kwargs["time_step"] = vasprun.parameters.get("POTIM")
742742

743+
istep_attr = "md_data" if vasprun.incar.get("ML_LMLFF") else "ionic_steps"
743744
return cls._from_dict(
744745
{
745746
remap.get(k, k): [
746-
ionic_step.get(k) for ionic_step in vasprun.ionic_steps
747+
ionic_step.get(k) for ionic_step in getattr(vasprun, istep_attr, [])
747748
]
748749
for k in ionic_step_data
749750
},

emmet-core/emmet/core/types/pymatgen_types/xas_adapter.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from typing import Annotated, TypeVar
1+
from typing import Any, Annotated, TypeVar
22

3+
import numpy as np
34
from pydantic import BeforeValidator, WrapSerializer
45
from pymatgen.analysis.xas.spectrum import XAS
56
from typing_extensions import TypedDict
@@ -45,8 +46,18 @@ def pop_xas_empty_structure_fields(xas: XASTypeVar, eps: float = 1e-12):
4546
return xas
4647

4748

49+
def _serialize_xas(xas: XAS) -> dict[str, Any]:
50+
xas_dct = xas.as_dict()
51+
for k in ("x", "y"):
52+
if isinstance(xas_dct[k], np.ndarray):
53+
xas_dct[k] = xas_dct[k].tolist()
54+
return xas_dct
55+
56+
4857
XASType = Annotated[
4958
XASTypeVar,
5059
BeforeValidator(pop_xas_empty_structure_fields),
51-
WrapSerializer(lambda x, nxt, info: x.as_dict(), return_type=TypedXASSpectrumDict),
60+
WrapSerializer(
61+
lambda x, nxt, info: _serialize_xas(x), return_type=TypedXASSpectrumDict
62+
),
5263
]

0 commit comments

Comments
 (0)