Skip to content

Commit e902064

Browse files
Update tests
1 parent 4fd8b31 commit e902064

File tree

6 files changed

+91
-2
lines changed

6 files changed

+91
-2
lines changed

src/easydynamics/sample_model/components/polynomial.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ def evaluate(self, x: Union[Numeric, sc.Variable]) -> np.ndarray:
6666
)
6767
else:
6868
x_in = x
69+
70+
if any(np.isnan(x_in)):
71+
raise ValueError("Input x contains NaN values.")
72+
73+
if any(np.isinf(x_in)):
74+
raise ValueError("Input x contains infinite values.")
75+
6976
result = np.zeros_like(x_in, dtype=float)
7077
for i, param in enumerate(self.coefficients):
7178
result += param.value * np.power(x_in, i)

tests/unit_tests/sample_model/components/test_damped_harmonic_oscillator.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_area_property_setter(self, dho: DampedHarmonicOscillator):
9999

100100
def test_center_property_getter(self, dho: DampedHarmonicOscillator):
101101
# WHEN THEN EXPECT
102-
assert dho.center.value == 0.5
102+
assert dho.center.value == 1.5
103103

104104
def test_center_property_setter(self, dho: DampedHarmonicOscillator):
105105
# WHEN
@@ -111,7 +111,7 @@ def test_center_property_setter(self, dho: DampedHarmonicOscillator):
111111
dho.center = "invalid"
112112

113113
def test_width_property_getter(self, dho: DampedHarmonicOscillator):
114-
assert dho.width.value == 0.6
114+
assert dho.width.value == 0.3
115115

116116
def test_width_property_setter(self, dho: DampedHarmonicOscillator):
117117
# WHEN
@@ -170,6 +170,22 @@ def test_evaluate_with_incompatible_unit(self, dho: DampedHarmonicOscillator):
170170
):
171171
dho.evaluate(x)
172172

173+
def test_evaluate_with_nan_input(self, dho: DampedHarmonicOscillator):
174+
# WHEN
175+
x = np.array([0.0, np.nan, 1.0])
176+
177+
# THEN EXPECT
178+
with pytest.raises(ValueError, match="Input x contains NaN values."):
179+
dho.evaluate(x)
180+
181+
def test_evaluate_with_infinite_input(self, dho: DampedHarmonicOscillator):
182+
# WHEN
183+
x = np.array([0.0, np.inf, 1.0])
184+
185+
# THEN EXPECT
186+
with pytest.raises(ValueError, match="Input x contains infinite values."):
187+
dho.evaluate(x)
188+
173189
def test_get_parameters(self, dho: DampedHarmonicOscillator):
174190
params = dho.get_parameters()
175191
assert len(params) == 3

tests/unit_tests/sample_model/components/test_gaussian.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,22 @@ def test_evaluate_with_incompatible_unit(self, gaussian: Gaussian):
138138
):
139139
gaussian.evaluate(x)
140140

141+
def test_evaluate_with_nan_input(self, gaussian: Gaussian):
142+
# WHEN
143+
x = np.array([0.0, np.nan, 1.0])
144+
145+
# THEN EXPECT
146+
with pytest.raises(ValueError, match="Input x contains NaN values."):
147+
gaussian.evaluate(x)
148+
149+
def test_evaluate_with_infinite_input(self, gaussian: Gaussian):
150+
# WHEN
151+
x = np.array([0.0, np.inf, 1.0])
152+
153+
# THEN EXPECT
154+
with pytest.raises(ValueError, match="Input x contains infinite values."):
155+
gaussian.evaluate(x)
156+
141157
def test_center_is_fixed_if_set_to_None(self):
142158
# WHEN
143159
test_gaussian = Gaussian(

tests/unit_tests/sample_model/components/test_lorentzian.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,24 @@ def test_evaluate_with_incompatible_unit(self, lorentzian: Lorentzian):
140140
):
141141
lorentzian.evaluate(x)
142142

143+
def test_evaluate_with_nan_input(self, lorentzian: Lorentzian):
144+
# WHEN
145+
x = np.array([0.0, np.nan, 1.0])
146+
147+
# THEN EXPECT
148+
with pytest.raises(ValueError, match="Input x contains NaN values."):
149+
lorentzian.evaluate(x)
150+
151+
def test_evaluate_with_infinite_input(self, lorentzian: Lorentzian):
152+
# WHEN
153+
x = np.array([0.0, np.inf, 1.0])
154+
155+
# THEN EXPECT
156+
with pytest.raises(ValueError, match="Input x contains infinite values."):
157+
lorentzian.evaluate(x)
158+
159+
lorentzian.evaluate(x)
160+
143161
def test_center_is_fixed_if_set_to_None(self):
144162
# WHEN
145163
test_lorentzian = Lorentzian(

tests/unit_tests/sample_model/components/test_polynomial.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ def test_evaluate_with_different_unit_error(self, polynomial: Polynomial):
7575
):
7676
polynomial.evaluate(x)
7777

78+
def test_evaluate_with_nan_input(self, polynomial: Polynomial):
79+
# WHEN
80+
x = np.array([0.0, np.nan, 1.0])
81+
82+
# THEN EXPECT
83+
with pytest.raises(ValueError, match="Input x contains NaN values."):
84+
polynomial.evaluate(x)
85+
86+
def test_evaluate_with_infinite_input(self, polynomial: Polynomial):
87+
# WHEN
88+
x = np.array([0.0, np.inf, 1.0])
89+
90+
# THEN EXPECT
91+
with pytest.raises(ValueError, match="Input x contains infinite values."):
92+
polynomial.evaluate(x)
93+
7894
def test_degree(self, polynomial: Polynomial):
7995
# WHEN THEN EXPECT
8096
assert polynomial.degree() == 2

tests/unit_tests/sample_model/components/test_voigt.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,22 @@ def test_evaluate_with_incompatible_unit(self, voigt: Voigt):
219219
):
220220
voigt.evaluate(x)
221221

222+
def test_evaluate_with_nan_input(self, voigt: Voigt):
223+
# WHEN
224+
x = np.array([0.0, np.nan, 1.0])
225+
226+
# THEN EXPECT
227+
with pytest.raises(ValueError, match="Input x contains NaN values."):
228+
voigt.evaluate(x)
229+
230+
def test_evaluate_with_infinite_input(self, voigt: Voigt):
231+
# WHEN
232+
x = np.array([0.0, np.inf, 1.0])
233+
234+
# THEN EXPECT
235+
with pytest.raises(ValueError, match="Input x contains infinite values."):
236+
voigt.evaluate(x)
237+
222238
def test_center_is_fixed_if_set_to_None(self):
223239
# WHEN THEN
224240
test_voigt = Voigt(

0 commit comments

Comments
 (0)