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 bug where Quantity subclasses fail Table operations #11792

Merged
merged 4 commits into from May 26, 2021

Conversation

taldcroft
Copy link
Member

Description

This is a follow-on to #11127 to address a bug where Table operations fail on Quantity columns where masking is required. Since #11127 masking is available with Quantity and so operations like hstack, vstack, and join should work. This bug was noted on Slack by @adrn.

I fixed a couple of somewhat-related issues that I noticed while in the code.

@taldcroft taldcroft added this to the v4.3 milestone May 26, 2021
@taldcroft taldcroft changed the title Fix bug where Quantity subclasses failed Table operations Fix bug where Quantity subclasses fail Table operations May 26, 2021
@taldcroft taldcroft force-pushed the table-masked-quantity-operations branch from 177f5d3 to e19a69b Compare May 26, 2021 14:22
@taldcroft taldcroft requested a review from mhvk May 26, 2021 14:22
Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

I'm really happy to see that things work so well, with relatively minimal changes!! I think one change can instead be pushed to Masked - if so, this would avoid problems for other classes too.

t1 = table.QTable([[1, 2, 3, 4], col], names=['a', 'b'])
t2 = table.QTable([[1, 2], col[:2]], names=['a', 'c'])

with pytest.raises(NotImplementedError) as err:
table.vstack([t1, t2], join_type='outer')
assert 'vstack requires masking' in str(err.value)
assert 'vstack unavailable' in str(err.value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Change this to pytest.raises(NotImplementedError, match='vstack unavailable')?

Copy link
Member Author

Choose a reason for hiding this comment

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

I noticed this old code, but was trying to keep the footprint small and not make changes just for style.

@@ -584,7 +584,8 @@ def __setitem__(self, item, value):
if isinstance(value, Longitude):
raise TypeError("A Longitude angle cannot be assigned to a Latitude angle")
# first check bounds
self._validate_angles(value)
if value is not np.ma.masked:
Copy link
Contributor

Choose a reason for hiding this comment

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

I had hoped to avoid having non-masked classes to be aware of masks at all, but to avoid it here would mean creating a specific MaskedLatitutde class in utils.masked, which seems overkill. But the problem here is general, that classes may not be able to deal with np.ma.masked. I'll have a quick look to see if I cannot fix it more easily in Masked itself...

Copy link
Member Author

Choose a reason for hiding this comment

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

That is an interesting question. In this case if there was going to be a MaskedLatitude class, wouldn't it belong in angles.py not in masked.py? I can imagine a MaskedLatitude class that just overrides _validate_angles to do nothing if value is np.ma.masked otherwise calls the super method.

In general classes might need some small tweaks like this to become masked, and these might be classes outside of astropy. What's the pattern for doing that?

Copy link
Contributor

Choose a reason for hiding this comment

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

So, my current idea is that the top-level MaskedLatitude class (which gets created by Masked(Latitude)) should really deal with this, i.e., define a __setitem__. I tried that locally and it works, but I need to think a little bit exactly how best to implement this... But if I don't get to it, I'm quite fine with just doing it here for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

In the spirit of "perfect is the enemy of good", I'd say to keep this as is for now. There will be other natural opportunities that come up to improve the implementation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, agreed.

astropy/coordinates/earth.py Outdated Show resolved Hide resolved
Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

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

OK, let's go the simple route here!

@taldcroft taldcroft merged commit c58ad0a into astropy:main May 26, 2021
@taldcroft taldcroft deleted the table-masked-quantity-operations branch May 26, 2021 18:24
@taldcroft
Copy link
Member Author

Thanks for the quick reviews @mhvk. 🤞 this makes it into 4.3.

@taldcroft
Copy link
Member Author

And back to a previous comment, I am also really impressed at how easy it was to fully support Quantity, once the monster of #11127 was done. 🎉

eteq pushed a commit that referenced this pull request Jun 11, 2021
…ions

Fix bug where Quantity subclasses fail Table operations
@eteq
Copy link
Member

eteq commented Jun 11, 2021

backported to v4.3.x in 38b2a88

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants