-
Notifications
You must be signed in to change notification settings - Fork 21
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]]), | ||
), | ||
], | ||
) | ||
|
@@ -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": | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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": | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 inx_values_not_equal_emsg