Skip to content

Commit

Permalink
pcmt
Browse files Browse the repository at this point in the history
  • Loading branch information
esoteric-ephemera authored and tsmathis committed Dec 3, 2024
1 parent b3d7eb8 commit d059931
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
13 changes: 8 additions & 5 deletions emmet-core/emmet/core/xas.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,18 @@ class XASDoc(SpectrumDoc):
..., title="Absorption Edge", description="The interaction edge for XAS."
)

@field_validator("spectrum",mode="before")
@field_validator("spectrum", mode="before")
@classmethod
def check_spectrum_non_positive_values(cls,v,eps=1.e-12) -> XAS:
if isinstance(v,dict):
def check_spectrum_non_positive_values(cls, v, eps=1.0e-12) -> XAS:
if isinstance(v, dict):
try:
v = XAS.from_dict(v)
except ValueError as exc:
if "Double check the intensities. Most of them are non-positive." in str(exc):
v["y"] = [y if y > 0. else abs(eps) for y in v["y"]]
if (
"Double check the intensities. Most of them are non-positive."
in str(exc)
):
v["y"] = [y if y > 0.0 else abs(eps) for y in v["y"]]
v = XAS.from_dict(v)
return v

Expand Down
10 changes: 5 additions & 5 deletions emmet-core/tests/test_xas.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

from emmet.core.xas import XASDoc

def test_xas_doc(test_dir):

def test_xas_doc(test_dir):
xas_dict = loadfn(test_dir / "xasdoc_nonpos_mp_626735.json.gz", cls=None)

# First show that there are non-positive intensities
non_pos_idx = [idx for idx, v in enumerate(xas_dict["spectrum"]["y"]) if v <= 0.]
non_pos_idx = [idx for idx, v in enumerate(xas_dict["spectrum"]["y"]) if v <= 0.0]
assert len(non_pos_idx) > 0

# Now show that XASDoc removes non-positive intensities and correctly serializes
xas = XASDoc(**xas_dict)
assert isinstance(xas.spectrum, XAS)
assert len(xas.spectrum.y[xas.spectrum.y <= 0.]) == 0
assert len(xas.spectrum.y[xas.spectrum.y <= 0.0]) == 0
assert all(
xas.spectrum.y[idx] > 0. and xas.spectrum.y[idx] == pytest.approx(0.)
xas.spectrum.y[idx] > 0.0 and xas.spectrum.y[idx] == pytest.approx(0.0)
for idx in non_pos_idx
)

assert isinstance(xas.absorbing_element,Element)
assert isinstance(xas.absorbing_element, Element)

0 comments on commit d059931

Please sign in to comment.