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

Ensure mask calculation is correct also for multi-op ufunc with scalars #15450

Merged
merged 1 commit into from Oct 10, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion astropy/utils/masked/core.py
Expand Up @@ -697,7 +697,9 @@
np.copyto(out, masks[0], where=where)
return out

out = np.logical_or(masks[0], masks[1], out=out, where=where)
# [...] at the end to ensure we have an array, not a scalar, and
# thus can be used for in-place changes in the loop.
out = np.logical_or(masks[0], masks[1], out=out, where=where)[...]

Check warning on line 702 in astropy/utils/masked/core.py

View check run for this annotation

Codecov / codecov/patch

astropy/utils/masked/core.py#L702

Added line #L702 was not covered by tests
for mask in masks[2:]:
np.logical_or(out, mask, out=out, where=where)
return out
Expand Down
22 changes: 22 additions & 0 deletions astropy/utils/masked/tests/test_functions.py
Expand Up @@ -5,6 +5,7 @@
coverage. Complete coverage of all numpy functions is done
with less detailed tests in test_function_helpers.
"""
import erfa.ufunc as erfa_ufunc
import numpy as np
import pytest
from numpy.testing import assert_array_equal
Expand Down Expand Up @@ -151,6 +152,27 @@ def test_3op_ufunc(self):
assert_array_equal(ma_mb.unmasked, expected_data)
assert_array_equal(ma_mb.mask, expected_mask)

def test_multi_op_ufunc(self):
mask = [True, False, False]
iy = Masked([2000, 2001, 2002], mask=mask)
im = Masked([1, 2, 3], mask=mask)
idy = Masked([10, 20, 25], mask=mask)
ihr = Masked([11, 12, 13], mask=[False, False, True])
imn = np.array([50, 51, 52])
isc = np.array([12.5, 13.6, 14.7])
result = erfa_ufunc.dtf2d("utc", iy, im, idy, ihr, imn, isc)
Copy link
Member

Choose a reason for hiding this comment

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

Are the later elements ever tested? See comments on the zip unpacking below.

# Also test scalar
result0 = erfa_ufunc.dtf2d("utc", iy[0], im[0], idy[0], ihr[0], imn[0], isc[0])
expected = erfa_ufunc.dtf2d(
"utc", iy.unmasked, im.unmasked, idy.unmasked, ihr.unmasked, imn, isc
)
expected_mask = np.array([True, False, True])
for res, res0, exp in zip(result, result0, expected):
Copy link
Member

Choose a reason for hiding this comment

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

Aren't these different lengths? So it's not iterating over all of result? As in won't this fail?

Suggested change
for res, res0, exp in zip(result, result0, expected):
for res, res0, exp in zip(result, result0, expected, strict=True):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is OK: all of result, result0, and expected are output from the ufunc, which returns a tuple of 3 elements, jd1, jd2, and status, each of which can be an array or scalar.

Copy link
Member

Choose a reason for hiding this comment

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

But then zipping them together means the zip only goes to end of the shortest iterator, which is result0, right? Equivalent to result[0]? So the for loop is only 1 element?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, result0 is a tuple with 3 entries, it is the same as (jd1[0], jd2[0], stat[0]) where (jd1, jd2, stat) would be in result. The ufunc always gives back a tuple of three (I think my nomenclature may have not been very helpful here!).

Copy link
Member

Choose a reason for hiding this comment

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

Ah! Good. So then each element of result0 is broadcast against each element of result and everything works. Gotcha.

assert_array_equal(res.unmasked, exp)
assert_array_equal(res.mask, expected_mask)
assert res0.unmasked == exp[0]
assert res0.mask == expected_mask[0]

@pytest.mark.parametrize("axis", (0, 1, None))
def test_add_reduce(self, axis):
ma_reduce = np.add.reduce(self.ma, axis=axis)
Expand Down
2 changes: 2 additions & 0 deletions docs/changes/utils/15450.bugfix.rst
@@ -0,0 +1,2 @@
Ufuncs with more than 2 operands (such as ``erfa.dtf2d``) now work
also if all inputs are scalars and more than two inputs have masks.
Comment on lines +1 to +2
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Ufuncs with more than 2 operands (such as ``erfa.dtf2d``) now work
also if all inputs are scalars and more than two inputs have masks.
Ufuncs with more than 2 operands (such as ``erfa.dtf2d``) now also
work if all inputs are scalars and more than two inputs have masks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to change this, but perhaps only if there is also a code change? I think there is no worry about zip.