Skip to content

Commit 312eb8f

Browse files
Update handling of incorrect units
1 parent c5f284d commit 312eb8f

File tree

7 files changed

+49
-4
lines changed

7 files changed

+49
-4
lines changed

src/easydynamics/sample_model/components/damped_harmonic_oscillator.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,15 @@ def evaluate(self, x: Union[Numeric, sc.Variable]) -> Union[float, np.ndarray]:
101101
if isinstance(x, sc.Variable):
102102
x_in = x.values
103103
if self._unit is not None and x.unit != self._unit:
104+
try:
105+
self.convert_unit(x.unit.name)
106+
except Exception as e:
107+
raise ValueError(
108+
f"Input x has unit {x.unit}, but DHO component has unit {self._unit}. Failed to convert DHO to {x.unit}."
109+
) from e
104110
warnings.warn(
105111
f"Input x has unit {x.unit}, but DHO component has unit {self._unit}. Converting DHO to {x.unit}."
106112
)
107-
self.convert_unit(x.unit.name)
108113
else:
109114
x_in = x
110115
return (

src/easydynamics/sample_model/components/gaussian.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,16 @@ def evaluate(self, x: Union[Numeric, sc.Variable]) -> Union[float, np.ndarray]:
9999
if isinstance(x, sc.Variable):
100100
x_in = x.values
101101
if self._unit is not None and x.unit != self._unit:
102+
try:
103+
self.convert_unit(x.unit.name)
104+
except Exception as e:
105+
raise ValueError(
106+
f"Input x has unit {x.unit}, but Gaussian component has unit {self._unit}. Failed to convert Gaussian to {x.unit}."
107+
) from e
108+
102109
warnings.warn(
103110
f"Input x has unit {x.unit}, but Gaussian component has unit {self._unit}. Converting Gaussian to {x.unit}."
104111
)
105-
self.convert_unit(x.unit.name)
106112
else:
107113
x_in = x
108114
return (

src/easydynamics/sample_model/components/lorentzian.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,15 @@ def evaluate(self, x: Union[Numeric, sc.Variable]) -> Union[float, np.ndarray]:
102102
if isinstance(x, sc.Variable):
103103
x_in = x.values
104104
if self._unit is not None and x.unit != self._unit:
105+
try:
106+
self.convert_unit(x.unit.name)
107+
except Exception as e:
108+
raise ValueError(
109+
f"Input x has unit {x.unit}, but Lorentzian component has unit {self._unit}. Failed to convert Lorentzian to {x.unit}."
110+
) from e
105111
warnings.warn(
106112
f"Input x has unit {x.unit}, but Lorentzian component has unit {self._unit}. Converting Lorentzian to {x.unit}."
107113
)
108-
self.convert_unit(x.unit.name)
109114
else:
110115
x_in = x
111116
return self.area.value * (

src/easydynamics/sample_model/components/voigt.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,15 @@ def evaluate(self, x: Union[Numeric, sc.Variable]) -> Union[float, np.ndarray]:
129129
if isinstance(x, sc.Variable):
130130
x_in = x.values
131131
if self._unit is not None and x.unit != self._unit:
132+
try:
133+
self.convert_unit(x.unit.name)
134+
except Exception as e:
135+
raise ValueError(
136+
f"Input x has unit {x.unit}, but Voigt component has unit {self._unit}. Failed to convert Voigt to {x.unit}."
137+
) from e
132138
warnings.warn(
133139
f"Input x has unit {x.unit}, but Voigt component has unit {self._unit}. Converting Voigt to {x.unit}."
134140
)
135-
self.convert_unit(x.unit.name)
136141
else:
137142
x_in = x
138143
return self.area.value * voigt_profile(

tests/unit_tests/sample_model/components/test_gaussian.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ def test_evaluate_with_different_unit(self, gaussian: Gaussian):
9494
)
9595
np.testing.assert_allclose(expected, expected_result, rtol=1e-5)
9696

97+
def test_evaluate_with_incompatible_unit(self, gaussian: Gaussian):
98+
x = sc.array(dims=["x"], values=[0.0, 500.0, 1000.0], unit="nm")
99+
with pytest.raises(
100+
ValueError,
101+
match="Input x has unit nm, but Gaussian component has unit meV. Failed to convert Gaussian to nm.",
102+
):
103+
gaussian.evaluate(x)
104+
97105
def test_center_is_fixed_if_set_to_None(self):
98106
test_gaussian = Gaussian(
99107
name="TestGaussian", area=2.0, center=None, width=0.6, unit="meV"

tests/unit_tests/sample_model/components/test_lorentzian.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ def test_evaluate_with_different_unit(self, lorentzian: Lorentzian):
9696
)
9797
np.testing.assert_allclose(expected, expected_result, rtol=1e-5)
9898

99+
def test_evaluate_with_incompatible_unit(self, lorentzian: Lorentzian):
100+
x = sc.array(dims=["x"], values=[0.0, 500.0, 1000.0], unit="nm")
101+
with pytest.raises(
102+
ValueError,
103+
match="Input x has unit nm, but Lorentzian component has unit meV. Failed to convert Lorentzian to nm.",
104+
):
105+
lorentzian.evaluate(x)
106+
99107
def test_center_is_fixed_if_set_to_None(self):
100108
test_lorentzian = Lorentzian(
101109
name="TestLorentzian", area=2.0, center=None, width=0.6, unit="meV"

tests/unit_tests/sample_model/components/test_voigt.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,14 @@ def test_evaluate_with_different_unit(self, voigt: Voigt):
189189
)
190190
np.testing.assert_allclose(expected, expected_result, rtol=1e-5)
191191

192+
def test_evaluate_with_incompatible_unit(self, voigt: Voigt):
193+
x = sc.array(dims=["x"], values=[0.0, 500.0, 1000.0], unit="nm")
194+
with pytest.raises(
195+
ValueError,
196+
match="Input x has unit nm, but Voigt component has unit meV. Failed to convert Voigt to nm.",
197+
):
198+
voigt.evaluate(x)
199+
192200
def test_center_is_fixed_if_set_to_None(self):
193201
test_voigt = Voigt(
194202
name="TestVoigt",

0 commit comments

Comments
 (0)