Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: compare values and shape of xarray values before applying operations between 2 DiffractionObjects #299

Merged
merged 1 commit into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/no-news.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* No news added: just making sure the previous add, mul, div, and sub tests are working as expected.

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
11 changes: 7 additions & 4 deletions src/diffpy/utils/diffraction_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
XQUANTITIES = ANGLEQUANTITIES + DQUANTITIES + QQUANTITIES
XUNITS = ["degrees", "radians", "rad", "deg", "inv_angs", "inv_nm", "nm-1", "A-1"]

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

invalid_add_type_emsg = (
Expand Down Expand Up @@ -255,7 +256,9 @@ def _check_operation_compatibility(self, other):
raise TypeError(invalid_add_type_emsg)
if isinstance(other, DiffractionObject):
if self.all_arrays.shape != other.all_arrays.shape:
Copy link
Contributor Author

@bobleesj bobleesj Dec 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to first check the shape. Without this condition, the error message is ValueError: operands could not be broadcast together with shapes (0,3) (2,3) error message instead of getting the expected error message in x_values_not_equal_emsg

raise ValueError(y_grid_length_mismatch_emsg)
raise ValueError(x_values_not_equal_emsg)
if not np.allclose(self.all_arrays[:, [1, 2, 3]], other.all_arrays[:, [1, 2, 3]]):
raise ValueError(x_values_not_equal_emsg)

@property
def all_arrays(self):
Expand Down
7 changes: 4 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ def invalid_add_type_error_msg():


@pytest.fixture
def y_grid_size_mismatch_error_msg():
def x_values_not_equal_error_msg():
return (
"The two objects have different y-array lengths. "
"Please ensure the length of the y-value during initialization is identical."
"The two objects have different values in x arrays (my_do.all_arrays[:, [1, 2, 3]]). "
"Please ensure the x values of the two objects are identical by re-instantiating "
"the DiffractionObject with the correct x value inputs."
)
43 changes: 30 additions & 13 deletions tests/test_diffraction_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,28 +780,28 @@ def test_scalar_operations(operation, starting_yarray, scalar_value, expected_ya


@pytest.mark.parametrize(
"operation, " "expected_do_1_all_arrays_with_y_modified, " "expected_do_2_all_arrays_with_y_modified",
"operation, expected_do_1_all_arrays_with_y_modified, expected_do_2_all_arrays_with_y_modified",
[
# Test addition, subtraction, multiplication, and division of two DO objects
( # Test addition of two DO objects, expect combined yarray values
"add",
np.array([[2.0, 0.51763809, 30.0, 12.13818192], [4.0, 1.0, 60.0, 6.28318531]]),
np.array([[2.0, 6.28318531, 100.70777771, 1], [4.0, 3.14159265, 45.28748053, 2.0]]),
np.array([[2.0, 0.51763809, 30.0, 12.13818192], [4.0, 1.0, 60.0, 6.28318531]]),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add two DOs with identical xarrays, expect valid without error.

),
( # Test subtraction of two DO objects, expect differences in yarray values
"sub",
np.array([[0.0, 0.51763809, 30.0, 12.13818192], [0.0, 1.0, 60.0, 6.28318531]]),
np.array([[0.0, 6.28318531, 100.70777771, 1], [0.0, 3.14159265, 45.28748053, 2.0]]),
np.array([[0.0, 0.51763809, 30.0, 12.13818192], [0.0, 1.0, 60.0, 6.28318531]]),
),
( # Test multiplication of two DO objects, expect multiplication in yarray values
"mul",
np.array([[1.0, 0.51763809, 30.0, 12.13818192], [4.0, 1.0, 60.0, 6.28318531]]),
np.array([[1.0, 6.28318531, 100.70777771, 1], [4.0, 3.14159265, 45.28748053, 2.0]]),
np.array([[1.0, 0.51763809, 30.0, 12.13818192], [4.0, 1.0, 60.0, 6.28318531]]),
),
( # Test division of two DO objects, expect division in yarray values
"div",
np.array([[1.0, 0.51763809, 30.0, 12.13818192], [1.0, 1.0, 60.0, 6.28318531]]),
np.array([[1.0, 6.28318531, 100.70777771, 1], [1.0, 3.14159265, 45.28748053, 2.0]]),
np.array([[1.0, 0.51763809, 30.0, 12.13818192], [1.0, 1.0, 60.0, 6.28318531]]),
),
],
)
Expand All @@ -810,15 +810,14 @@ def test_binary_operator_on_do(
expected_do_1_all_arrays_with_y_modified,
expected_do_2_all_arrays_with_y_modified,
do_minimal_tth,
do_minimal_d,
):
do_1 = do_minimal_tth
do_2 = do_minimal_d
do_2 = do_minimal_tth
assert np.allclose(
do_1.all_arrays, np.array([[1.0, 0.51763809, 30.0, 12.13818192], [2.0, 1.0, 60.0, 6.28318531]])
)
assert np.allclose(
do_2.all_arrays, np.array([[1.0, 6.28318531, 100.70777771, 1], [2.0, 3.14159265, 45.28748053, 2.0]])
do_2.all_arrays, np.array([[1.0, 0.51763809, 30.0, 12.13818192], [2.0, 1.0, 60.0, 6.28318531]])
)

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


@pytest.mark.parametrize("operation", ["add", "sub", "mul", "div"])
def test_operator_invalid_yarray_length(operation, do_minimal, do_minimal_tth, y_grid_size_mismatch_error_msg):
# Add two DO objects with different yarray lengths, expect ValueError
def test_operator_invalid_xarray_values_not_equal(
operation, do_minimal_tth, do_minimal_d, x_values_not_equal_error_msg
):
# Add two DO objects with different xarray values but equal in shape, expect ValueError
do_1 = do_minimal_tth
do_2 = do_minimal_d
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do1 and do2 have differnet xarray values, expect invalid.

with pytest.raises(ValueError, match=re.escape(x_values_not_equal_error_msg)):
if operation == "add":
do_1 + do_2
elif operation == "sub":
do_1 - do_2
elif operation == "mul":
do_1 * do_2
elif operation == "div":
do_1 / do_2


@pytest.mark.parametrize("operation", ["add", "sub", "mul", "div"])
def test_operator_invalid_xarray_shape_not_equal(
operation, do_minimal, do_minimal_tth, x_values_not_equal_error_msg
):
# Add two DO objects with different xarrays shape, expect ValueError
do_1 = do_minimal
do_2 = do_minimal_tth
assert len(do_1.all_arrays[:, 0]) == 0
assert len(do_2.all_arrays[:, 0]) == 2
with pytest.raises(ValueError, match=re.escape(y_grid_size_mismatch_error_msg)):
with pytest.raises(ValueError, match=re.escape(x_values_not_equal_error_msg)):
if operation == "add":
do_1 + do_2
elif operation == "sub":
Expand Down
Loading