Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 19
fix: compare values and shape of xarray values before applying operations between 2 DiffractionObjects#299
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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> |
| 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]]), | ||
ContributorAuthor 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 | ||
ContributorAuthor 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": | ||
Uh oh!
There was an error while loading. Please reload this page.
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 messageinstead of getting the expected error message inx_values_not_equal_emsg