Skip to content

Use quantity to value - #6137

Merged
bsipocz merged 15 commits into
astropy:masterfrom
mhvk:use-quantity-to-value
Jun 16, 2017
Merged

bsipocz merged 15 commits into
astropy:masterfrom
mhvk:use-quantity-to-value

Conversation

@mhvk

@mhvk mhvk commented May 30, 2017

Copy link
Copy Markdown
Contributor

fix #6136

Comment thread CHANGES.rst
``to_value(unit)`` method, since this is somewhat faster. Any subclasses
that overwrote ``.to``, should also overwrite ``.tovalue`` (or
possibly just the priviate ``._to_value`` method. (If you did this,
please let us know what was lacking that made this necessary!). [#6136]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

typos: tovalue → to_value, priviate → private

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Since travis and appveyor hadn't even started yet, I cancelled those builds and fixed this immediately

@taldcroft taldcroft left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A couple of questions, otherwise looks good to me.

Comment thread CHANGES.rst Outdated

- Internally, astropy replaced use of ``.to(unit).value`` with the new
``to_value(unit)`` method, since this is somewhat faster. Any subclasses
that overwrote ``.to``, should also overwrite ``.tovalue`` (or

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

typo .to_value

raise ValueError("Size Error: Arrays will be {}. Use "
"allow_huge=True to override this exception."
.format(human_file_size(array_size_C.to(u.byte).value)))
.format(human_file_size(array_size_C)))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I take it this is intended?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, human_file_size understands Quantity, so no need to convert here. I think this was just missed when human_file_size was changed.

Comment thread astropy/time/formats.py
# Possibly scaled unit any quantity-likes should be converted to
_unit = u.CompositeUnit(getattr(self, 'unit', 1.), [u.day], [1])
val1 = val1.to(_unit).value
val1 = u.Quantity(val1, copy=False).to_value(_unit)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm sure there is a reason, but why did you change in this way?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

At first, I just changed to to_value, but this failed, since one of the tests uses Column (which has a to method, but not a to_value). When I noticed that, I felt it was actually better not to assume having a unit goes with having to method either, but rather just let the Quantity initializer deal with different objects that have a .unit attribute.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK, now I understand. I've never liked duck-typing by attribute names.

@taldcroft

Copy link
Copy Markdown
Member

Question -- there was one tested instance where a both a Column and Quantity were acceptable inputs to a method. That's because Column behaves (a little bit) like a Quantity. So is it possible in the other 204 changes that this could happen as well? Does Column need to acquire a to_value method just in case??

Comment thread astropy/time/core.py Outdated
val = self._match_shape(val)
if hasattr(val, 'to'): # Matches Quantity but also TimeDelta.
val = val.to(u.second).value
val = val.to_value(u.second)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call on checking for other classes. This one is wrong (and the other setter too). Will correct.

@mhvk

mhvk commented May 30, 2017

Copy link
Copy Markdown
Contributor Author

Yes, good point. I went through the whole list again, and did find two instances where I should not have changed it: in the setters for delta_ut1_utc and delta_tdb_tt (though it was a match to TimeDelta rather than Column). Turns out those were not tested and failed even in current master. Anyway, this solves a small bug while we're at it!

@mhvk

mhvk commented May 30, 2017

Copy link
Copy Markdown
Contributor Author

p.s. The other changes are all in places where it is known that we're dealing with a Quantity(large majority; test cases, coordinate conversions, etc.), or where the docstring explicitly states that a Quantity is expected (very few).

@mhvk

mhvk commented May 31, 2017

Copy link
Copy Markdown
Contributor Author

I think this is ready to go in -- tests pass now. Any objections?

@taldcroft

Copy link
Copy Markdown
Member

So my only outstanding question is whether we give Column a to_value method that just returns self.quantity.to_value(unit, equivalencies)?

In essence allow astropy and other developers to write code that treats Column and Quantity as kindof-duck-like and use the faster/nicer to_value method.

This would allow undoing 167082e etc., and in some sense then the tests would already be written. 😄

@astrofrog

Copy link
Copy Markdown
Member

@mhvk - do you plan to wrap this up for 2.0?

@taldcroft

Copy link
Copy Markdown
Member

I don't think that the issue of a Column.to_value method is a lien on this PR getting merged. No existing code will break and the only implication is that if other (non-astropy-core) code migrates to the to_value method then it won't work on Column objects. I think we can address this for 3.0.

So, I'm 👍 on merge (of course rebase is needed).

@mhvk
mhvk force-pushed the use-quantity-to-value branch from acdfd67 to 6730efa Compare June 15, 2017 21:38
@mhvk

mhvk commented Jun 15, 2017

Copy link
Copy Markdown
Contributor Author

I was indeed not planning to add anything to here; we can decide later whether it is useful for Column and TimeDelta to have to_value too (see #6148).

Anyway, now rebased.

assert '...' in bigarrangle._repr_latex_()


def test_rotation_matrix_deprecation():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@mhvk - your rebase must have gone wrong, these tests were removed in #6170

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, duh, that was a conflict and I did the wrong thing. Will fix...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No worries. I see thumbs up above, if this is good to go once given the all green by CI, please add the merge-when-ci-passes label.

@bsipocz

bsipocz commented Jun 15, 2017 •

Copy link
Copy Markdown
Member

Cancelling travis and appveyor as this has genuine failures, see comment and the CircleCI log.

@mhvk
mhvk force-pushed the use-quantity-to-value branch from 6730efa to d34e9f3 Compare June 15, 2017 22:10
@mhvk mhvk added the zzz 💤 merge-when-ci-passes Do not use: We have auto-merge option now. label Jun 15, 2017
@bsipocz
bsipocz merged commit 0f0d40b into astropy:master Jun 16, 2017
@astrobot

Copy link
Copy Markdown

@bsipocz - thanks for merging this! However, I noticed the following issue with this pull request:

  • Changelog entry not present (or pull request number missing) and neither the Affects-dev nor the no-changelog-entry-needed label are set

Would it be possible to fix this? Thanks!

If you believe the above to be incorrect (which I - @astrobot - very much doubt) you can ping @astrofrog

@pllim

pllim commented Jun 16, 2017

Copy link
Copy Markdown
Member

Just for the record, change log is there but not this PR number.

Comment thread CHANGES.rst
``to_value(unit)`` method, since this is somewhat faster. Any subclasses
that overwrote ``.to``, should also overwrite ``.to_value`` (or
possibly just the private ``._to_value`` method. (If you did this,
please let us know what was lacking that made this necessary!). [#6136]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Typo in PR number that astrobot noted. I'll fix it before release.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It is actually the issue number this PR fixes, so not strictly a typo. 😄

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Then the issue is the missing PR number. Neither the bot, nor the release script care about issue numbers, but about the PR numbers :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry to have missed that...

@mhvk
mhvk deleted the use-quantity-to-value branch June 16, 2017 05:55
@dhomeier

dhomeier commented Jul 6, 2017

Copy link
Copy Markdown
Contributor

This did break some code of mine where I called spectral_density(wl) with a table Column wl.
It's easily enough fixed with a Quantity(wl), but the exception is a bit opaque, being raised three call levels down...

I realise that spectral_density has required a Quantity as input for some time, but perhaps it (and other function that have switched to the to_value method) should check the input type in the same way as for UnitBase.

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.

Replace instances of .to(unit).value with .to_value(unit)

8 participants