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

Allow object arrays of mixed unit Quantities to be printed #3778

Merged
merged 5 commits into from May 29, 2015

Conversation

embray
Copy link
Member

@embray embray commented May 15, 2015

This partially addresses the issue raised in #3777 that printing/repr-ing an ndarray of dtype=object containing quantities of different units resulted in a crash.

I still think it would be nice to have a class specifically for this purpose as discussed in #3777, but this solves the immediate problem, and I will need this for further work.

…sn't create a new class but it does at least make arrays of Quantity objects printable without breaking or changing too much else. It adds a new UnitConversionError specifically for errors related to that, and is also a ValueError.
@embray embray added this to the v1.0.3 milestone May 15, 2015
embray added a commit that referenced this pull request May 15, 2015
@mhvk
Copy link
Contributor

mhvk commented May 16, 2015

@embray - the PR looks good to me. Just to be sure, though, is there a reason not just to base UnitsError itself on ValueError? (cc @mdboom). Or did you encounter cases where this would cause problems?

@embray
Copy link
Member Author

embray commented May 18, 2015

I think in principle UnitsError is a little too generic, as a base class for any and all exceptions involving units, to make it a ValueError. Of course, the use cases for something like ValueError are pretty broad and underspecified in the first place, but I figured keep the base units error more generic. I seem to recall at least one case where it also created some problems.

@embray
Copy link
Member Author

embray commented May 18, 2015

I think I should add one more note in the changelog, that this changes the exception raised by unit conversion errors (though it's still a subclass of the original exception class used, so it shouldn't break any existing code).

@@ -282,6 +283,8 @@ def _get_time_fmt(self, val, val2, format, scale):
try:
return FormatClass(val, val2, scale, self.precision,
self.in_subfmt, self.out_subfmt)
except UnitConversionError:
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think the addition here is necessary. Really, one is just testing whether a format works at all, and a blanket except would arguably have been fine.

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 could change that, but I don't think I'm comfortable with a bare except here, at least for now. I agree it's a bit awkward though.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it is fine just to remove the additional exception. I don't see how UnitConversionError could indicate anything else than "this format doesn't work, go on try the next".

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, let me clarify, the reason this needs to be caught and handled separately is that there were some tests where a UnitsError was expected to be raised. Now a UnitConversionError is raised, but since that's a subclass of ValueError it needs to be handled separately.

I can't remember exactly what the use case was but there is code expecting this to raise unit conversion errors so that they can be handled separately.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, yes, I think I now understand: that much be the interaction with Quantity in TimeDelta

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 double checked, and there are a few tests that fail without this line. For example TestTimeQuantity.test_invalid_quantity_input fails, because it tests instantiating a Time object from a quantity in units of meters. This should definitely result in a unit conversion error, instead of just being ignored (instead you end up getting an error "Input values did not match the format class jd", which is true in a sense, but it's not as useful as a unit error would be here.

@mhvk
Copy link
Contributor

mhvk commented May 22, 2015

@embray: on second review, one further small comment.

As for the name, I'm still not entirely sure it is needed to introduce the new UnitConversionError (there seems to be few normal UnitsError left!), but it is no big deal either.

The travis failure is about an open file, which seems strange, given that nothing was changed that could lead to this. I was about to just restart that test, but then thought it might be good if you had a look, since it is such an odd failure.

@embray
Copy link
Member Author

embray commented May 22, 2015

There are a few places where UnitsError is used that don't specifically have to do with conversion between units--or at least they didn't to me at first glance. Though if you could point it out that would be good.

I'm not wedded to the overall solution here and could do away with UnitConversionError if people prefer. I just like the specificity of it compared to a generic UnitsError.

@embray
Copy link
Member Author

embray commented May 29, 2015

Okay, since the build failure is just the /etc/hosts file open error that has nothing to do with this I'm going to go ahead and merge this for the sake of fixing the issue (one that I could use fixed for current development). If the UnitConversionError thing turns out to be a bust I'm happy to reconsider.

embray added a commit that referenced this pull request May 29, 2015
Allow object arrays of mixed unit Quantities to be printed
@embray embray merged commit 346e455 into astropy:master May 29, 2015
@embray embray deleted the units/quantity-array-repr branch May 29, 2015 19:15
embray added a commit that referenced this pull request May 29, 2015
Allow object arrays of mixed unit Quantities to be printed
Conflicts:
	astropy/time/core.py
dhomeier pushed a commit to dhomeier/astropy that referenced this pull request Aug 11, 2015
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

2 participants