Skip to content

Commit 65088f9

Browse files
authored
Merge pull request #299 from bobleesj/operation-test-warnings
fix: compare values and shape of `xarray` values before applying operations between 2 DiffractionObjects
2 parents 88d8d73 + 98c65dd commit 65088f9

File tree

4 files changed

+64
-20
lines changed

4 files changed

+64
-20
lines changed

Diff for: news/no-news.rst

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* No news added: just making sure the previous add, mul, div, and sub tests are working as expected.
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* <news item>
20+
21+
**Security:**
22+
23+
* <news item>

Diff for: src/diffpy/utils/diffraction_objects.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
XQUANTITIES = ANGLEQUANTITIES + DQUANTITIES + QQUANTITIES
1515
XUNITS = ["degrees", "radians", "rad", "deg", "inv_angs", "inv_nm", "nm-1", "A-1"]
1616

17-
y_grid_length_mismatch_emsg = (
18-
"The two objects have different y-array lengths. "
19-
"Please ensure the length of the y-value during initialization is identical."
17+
x_values_not_equal_emsg = (
18+
"The two objects have different values in x arrays (my_do.all_arrays[:, [1, 2, 3]]). "
19+
"Please ensure the x values of the two objects are identical by re-instantiating "
20+
"the DiffractionObject with the correct x value inputs."
2021
)
2122

2223
invalid_add_type_emsg = (
@@ -255,7 +256,9 @@ def _check_operation_compatibility(self, other):
255256
raise TypeError(invalid_add_type_emsg)
256257
if isinstance(other, DiffractionObject):
257258
if self.all_arrays.shape != other.all_arrays.shape:
258-
raise ValueError(y_grid_length_mismatch_emsg)
259+
raise ValueError(x_values_not_equal_emsg)
260+
if not np.allclose(self.all_arrays[:, [1, 2, 3]], other.all_arrays[:, [1, 2, 3]]):
261+
raise ValueError(x_values_not_equal_emsg)
259262

260263
@property
261264
def all_arrays(self):

Diff for: tests/conftest.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ def invalid_add_type_error_msg():
8181

8282

8383
@pytest.fixture
84-
def y_grid_size_mismatch_error_msg():
84+
def x_values_not_equal_error_msg():
8585
return (
86-
"The two objects have different y-array lengths. "
87-
"Please ensure the length of the y-value during initialization is identical."
86+
"The two objects have different values in x arrays (my_do.all_arrays[:, [1, 2, 3]]). "
87+
"Please ensure the x values of the two objects are identical by re-instantiating "
88+
"the DiffractionObject with the correct x value inputs."
8889
)

Diff for: tests/test_diffraction_objects.py

+30-13
Original file line numberDiff line numberDiff line change
@@ -780,28 +780,28 @@ def test_scalar_operations(operation, starting_yarray, scalar_value, expected_ya
780780

781781

782782
@pytest.mark.parametrize(
783-
"operation, " "expected_do_1_all_arrays_with_y_modified, " "expected_do_2_all_arrays_with_y_modified",
783+
"operation, expected_do_1_all_arrays_with_y_modified, expected_do_2_all_arrays_with_y_modified",
784784
[
785785
# Test addition, subtraction, multiplication, and division of two DO objects
786786
( # Test addition of two DO objects, expect combined yarray values
787787
"add",
788788
np.array([[2.0, 0.51763809, 30.0, 12.13818192], [4.0, 1.0, 60.0, 6.28318531]]),
789-
np.array([[2.0, 6.28318531, 100.70777771, 1], [4.0, 3.14159265, 45.28748053, 2.0]]),
789+
np.array([[2.0, 0.51763809, 30.0, 12.13818192], [4.0, 1.0, 60.0, 6.28318531]]),
790790
),
791791
( # Test subtraction of two DO objects, expect differences in yarray values
792792
"sub",
793793
np.array([[0.0, 0.51763809, 30.0, 12.13818192], [0.0, 1.0, 60.0, 6.28318531]]),
794-
np.array([[0.0, 6.28318531, 100.70777771, 1], [0.0, 3.14159265, 45.28748053, 2.0]]),
794+
np.array([[0.0, 0.51763809, 30.0, 12.13818192], [0.0, 1.0, 60.0, 6.28318531]]),
795795
),
796796
( # Test multiplication of two DO objects, expect multiplication in yarray values
797797
"mul",
798798
np.array([[1.0, 0.51763809, 30.0, 12.13818192], [4.0, 1.0, 60.0, 6.28318531]]),
799-
np.array([[1.0, 6.28318531, 100.70777771, 1], [4.0, 3.14159265, 45.28748053, 2.0]]),
799+
np.array([[1.0, 0.51763809, 30.0, 12.13818192], [4.0, 1.0, 60.0, 6.28318531]]),
800800
),
801801
( # Test division of two DO objects, expect division in yarray values
802802
"div",
803803
np.array([[1.0, 0.51763809, 30.0, 12.13818192], [1.0, 1.0, 60.0, 6.28318531]]),
804-
np.array([[1.0, 6.28318531, 100.70777771, 1], [1.0, 3.14159265, 45.28748053, 2.0]]),
804+
np.array([[1.0, 0.51763809, 30.0, 12.13818192], [1.0, 1.0, 60.0, 6.28318531]]),
805805
),
806806
],
807807
)
@@ -810,15 +810,14 @@ def test_binary_operator_on_do(
810810
expected_do_1_all_arrays_with_y_modified,
811811
expected_do_2_all_arrays_with_y_modified,
812812
do_minimal_tth,
813-
do_minimal_d,
814813
):
815814
do_1 = do_minimal_tth
816-
do_2 = do_minimal_d
815+
do_2 = do_minimal_tth
817816
assert np.allclose(
818817
do_1.all_arrays, np.array([[1.0, 0.51763809, 30.0, 12.13818192], [2.0, 1.0, 60.0, 6.28318531]])
819818
)
820819
assert np.allclose(
821-
do_2.all_arrays, np.array([[1.0, 6.28318531, 100.70777771, 1], [2.0, 3.14159265, 45.28748053, 2.0]])
820+
do_2.all_arrays, np.array([[1.0, 0.51763809, 30.0, 12.13818192], [2.0, 1.0, 60.0, 6.28318531]])
822821
)
823822

824823
if operation == "add":
@@ -856,13 +855,31 @@ def test_operator_invalid_type(do_minimal_tth, invalid_add_type_error_msg):
856855

857856

858857
@pytest.mark.parametrize("operation", ["add", "sub", "mul", "div"])
859-
def test_operator_invalid_yarray_length(operation, do_minimal, do_minimal_tth, y_grid_size_mismatch_error_msg):
860-
# Add two DO objects with different yarray lengths, expect ValueError
858+
def test_operator_invalid_xarray_values_not_equal(
859+
operation, do_minimal_tth, do_minimal_d, x_values_not_equal_error_msg
860+
):
861+
# Add two DO objects with different xarray values but equal in shape, expect ValueError
862+
do_1 = do_minimal_tth
863+
do_2 = do_minimal_d
864+
with pytest.raises(ValueError, match=re.escape(x_values_not_equal_error_msg)):
865+
if operation == "add":
866+
do_1 + do_2
867+
elif operation == "sub":
868+
do_1 - do_2
869+
elif operation == "mul":
870+
do_1 * do_2
871+
elif operation == "div":
872+
do_1 / do_2
873+
874+
875+
@pytest.mark.parametrize("operation", ["add", "sub", "mul", "div"])
876+
def test_operator_invalid_xarray_shape_not_equal(
877+
operation, do_minimal, do_minimal_tth, x_values_not_equal_error_msg
878+
):
879+
# Add two DO objects with different xarrays shape, expect ValueError
861880
do_1 = do_minimal
862881
do_2 = do_minimal_tth
863-
assert len(do_1.all_arrays[:, 0]) == 0
864-
assert len(do_2.all_arrays[:, 0]) == 2
865-
with pytest.raises(ValueError, match=re.escape(y_grid_size_mismatch_error_msg)):
882+
with pytest.raises(ValueError, match=re.escape(x_values_not_equal_error_msg)):
866883
if operation == "add":
867884
do_1 + do_2
868885
elif operation == "sub":

0 commit comments

Comments
 (0)