-
Notifications
You must be signed in to change notification settings - Fork 20
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
Refactor __add__
operation in DiffractionObject
and add tests
#285
Conversation
|
||
def __radd__(self, other): |
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.
radd i think we don't need?
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.
I think we may. Are you sure? Anyway, we can test and see.
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.
you are correct - added __radd__
back using __radd__ = __add__
please see a new test below for do + scalar
as well as scalar + do
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #285 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 8 8
Lines 408 446 +38
=========================================
+ Hits 408 446 +38
|
multiplied.on_q[1] = self.on_q[1] * other.on_q[1] | ||
return multiplied | ||
|
||
def __rmul__(self, other): |
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.
removed since identical as __mul__
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 should test and make sure it is ok to remove this. On balance, I probably want to leave the 'r' functionalities
tests/test_diffraction_objects.py
Outdated
|
||
|
||
@pytest.mark.parametrize( | ||
"LHS_all_arrays, RHS_all_arrays, expected_all_arrays_sum", |
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.
- test adding do1 + do2
tests/test_diffraction_objects.py
Outdated
|
||
|
||
@pytest.mark.parametrize( | ||
"starting_all_arrays, scalar_value, expected_all_arrays", |
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.
- test adding do + number
tests/test_diffraction_objects.py
Outdated
# Add a string to a DO object, expect TypeError, only scalar (int, float) allowed for addition | ||
do_LHS = do_minimal_tth | ||
with pytest.raises(TypeError, match=re.escape(invalid_add_type_error_msg)): | ||
do_LHS + "string_value" |
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.
string not allowed
tests/test_diffraction_objects.py
Outdated
do_LHS = do_minimal | ||
do_RHS = do_minimal_tth | ||
with pytest.raises(ValueError, match=re.escape(x_grid_size_mismatch_error_msg)): | ||
do_LHS + do_RHS |
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.
different lengths of xarray, not allowed
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.
@sbillinge ready for review
__add__
operation in DiffractionObject
and add tests__add__
operation in DiffractionObject
and add tests
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.
Please see inline.
|
||
def __radd__(self, other): |
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.
I think we may. Are you sure? Anyway, we can test and see.
multiplied.on_q[1] = self.on_q[1] * other.on_q[1] | ||
return multiplied | ||
|
||
def __rmul__(self, other): |
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 should test and make sure it is ok to remove this. On balance, I probably want to leave the 'r' functionalities
tests/test_diffraction_objects.py
Outdated
do = do_minimal_tth | ||
assert np.allclose(do.all_arrays, starting_all_arrays) | ||
do_sum_RHS = do + scalar_to_add | ||
do_sum_LHS = scalar_to_add + do |
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.
test for __radd__
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.
ready for review
right, radd
is back. rmul
is also back.
If this looks okay, then I will further implement the rest of the remaining operations
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.
please see inline comments.
invalid_add_type_emsg = ( | ||
"You may only add a DiffractionObject with another DiffractionObject or a scalar value. " | ||
"Please rerun by adding another DiffractionObject instance or a scalar value. " | ||
"e.g., my_do_1 + my_do_2 or my_do + 10" |
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.
for completeness, lets also add a radd as an example here. We could say "to add 10 to all intensities use..."
|
||
Examples | ||
-------- | ||
Add a scalar value to the xarrays of the DiffractionObject instance: |
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.
let's add the radd example here too.
tests/test_diffraction_objects.py
Outdated
@pytest.mark.parametrize( | ||
"starting_all_arrays, scalar_to_add, expected_all_arrays", | ||
[ | ||
# Test scalar addition to xarray values (q, tth, d) and expect no change to yarray values |
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.
the add operation is supposed to operate on the yarray leaving xarrays unaffected.
I see, I just refactored the earlier code which had xarrays adding up
I will fix these. |
Those were the y-arrays 😂. On_q[0] is the q and on_q[1] the intensities so it is more logical for users when plotting. |
>>> new_do = my_do_1 + my_do_2 | ||
""" | ||
|
||
self._check_operation_compatibility(other) |
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.
Created a private func that checks the validity of other
def _check_operation_compatibility(self, other):
if not isinstance(other, (DiffractionObject, int, float)):
raise TypeError(invalid_add_type_emsg)
if isinstance(other, DiffractionObject):
self_yarray = self.all_arrays[:, 0]
other_yarray = other.all_arrays[:, 0]
if len(self_yarray) != len(other_yarray):
raise ValueError(y_grid_length_mismatch_emsg)
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.
Nice! it may be more readable if we use
if shape(self.all_arrays) != shape(other.all_arrays)
to accomplish the same thing?
@sbillinge ready for review! - addressed all of the comments |
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.
I will merge this, but please see my comment on the private function. No need to make a special PR for that, but if you want, fix i tin any other PR....... or not.....
>>> new_do = my_do_1 + my_do_2 | ||
""" | ||
|
||
self._check_operation_compatibility(other) |
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.
Nice! it may be more readable if we use
if shape(self.all_arrays) != shape(other.all_arrays)
to accomplish the same thing?
Yup will do 👍 |
Starting with
__add__
first, and will make separate PRs for subtraction, multiplication, division, etc. after review