Skip to content

Commit 102310b

Browse files
committed
Fix remaining divide by zero
1 parent 9c8fd95 commit 102310b

File tree

2 files changed

+21
-14
lines changed

2 files changed

+21
-14
lines changed

tests/test_diffraction_objects.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ def test_dump(tmp_path, mocker):
404404

405405

406406
@pytest.mark.parametrize(
407-
"do_init_args, expected_do_dict",
407+
"do_init_args, expected_do_dict, divide_by_zero_warning_expected",
408408
[
409409
( # instantiate just array attributes
410410
{
@@ -433,6 +433,7 @@ def test_dump(tmp_path, mocker):
433433
"dmax": np.float64(np.inf),
434434
"wavelength": 4.0 * np.pi,
435435
},
436+
True
436437
),
437438
( # instantiate just array attributes
438439
{
@@ -462,11 +463,16 @@ def test_dump(tmp_path, mocker):
462463
"dmax": np.float64(np.inf),
463464
"wavelength": 4.0 * np.pi,
464465
},
466+
False
465467
),
466468
],
467469
)
468-
def test_init_valid(do_init_args, expected_do_dict):
469-
actual_do_dict = DiffractionObject(**do_init_args).__dict__
470+
def test_init_valid(do_init_args, expected_do_dict, divide_by_zero_warning_expected):
471+
if divide_by_zero_warning_expected:
472+
with pytest.warns(RuntimeWarning, match="divide by zero encountered in divide"):
473+
actual_do_dict = DiffractionObject(**do_init_args).__dict__
474+
else:
475+
actual_do_dict = DiffractionObject(**do_init_args).__dict__
470476
diff = DeepDiff(
471477
actual_do_dict, expected_do_dict, ignore_order=True, significant_digits=13, exclude_paths="root['_id']"
472478
)

tests/test_transforms.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -223,30 +223,31 @@ def test_tth_to_d_invalid(wavelength, tth, expected_error_type, expected_error_m
223223

224224

225225
@pytest.mark.parametrize(
226-
"wavelength, d, expected_tth",
226+
"wavelength, d, expected_tth, divide_by_zero_warning_expected",
227227
[
228228
# UC1: Empty d values, no wavelength, return empty arrays
229-
(None, np.empty((0)), np.empty((0))),
229+
(None, np.empty((0)), np.empty((0)), False),
230230
# UC2: Empty d values, wavelength specified, return empty arrays
231-
(4 * np.pi, np.empty((0)), np.empty(0)),
231+
(4 * np.pi, np.empty((0)), np.empty(0), False),
232232
# UC3: User specified valid d values, no wavelength, return empty arrays
233-
(
234-
None,
235-
np.array([1, 0.8, 0.6, 0.4, 0.2, 0]),
236-
np.array([0, 1, 2, 3, 4, 5]),
237-
),
233+
(None, np.array([1, 0.8, 0.6, 0.4, 0.2, 0]), np.array([0, 1, 2, 3, 4, 5]), True),
238234
# UC4: User specified valid d values (with wavelength)
239235
(
240236
4 * np.pi,
241237
np.array([4 * np.pi, 4 / np.sqrt(2) * np.pi, 4 / np.sqrt(3) * np.pi]),
242238
np.array([60.0, 90.0, 120.0]),
239+
False
243240
),
244241
],
245242
)
246-
def test_d_to_tth(wavelength, d, expected_tth, wavelength_warning_msg):
247-
if wavelength is None:
243+
def test_d_to_tth(wavelength, d, expected_tth, divide_by_zero_warning_expected, wavelength_warning_msg):
244+
if wavelength is None and not divide_by_zero_warning_expected:
248245
with pytest.warns(UserWarning, match=re.escape(wavelength_warning_msg)):
249246
actual_tth = d_to_tth(d, wavelength)
247+
elif wavelength is None and divide_by_zero_warning_expected:
248+
with pytest.warns(UserWarning, match=re.escape(wavelength_warning_msg)):
249+
with pytest.warns(RuntimeWarning, match="divide by zero encountered in divide"):
250+
actual_tth = d_to_tth(d, wavelength)
250251
else:
251252
actual_tth = d_to_tth(d, wavelength)
252253

@@ -259,7 +260,7 @@ def test_d_to_tth(wavelength, d, expected_tth, wavelength_warning_msg):
259260
# UC1: user specified invalid d values that result in tth > 180 degrees
260261
(4 * np.pi, np.array([1.2, 1, 0.8, 0.6, 0.4, 0.2]), ValueError),
261262
# UC2: user specified a wrong wavelength that result in tth > 180 degrees
262-
(100, np.array([1, 0.8, 0.6, 0.4, 0.2, 0]), ValueError),
263+
(100, np.array([1.2, 1, 0.8, 0.6, 0.4, 0.2]), ValueError),
263264
],
264265
)
265266
def test_d_to_tth_bad(wavelength, d, expected_error_type, invalid_q_or_d_or_wavelength_error_msg):

0 commit comments

Comments
 (0)