Skip to content

Bug: quantities (to vs. set_enabled_equivalencies) #2496

Closed

Description

@hamogu

Below, the same comparison is True is a I explicetly call .to(u/km/u.s, equivalencies=l2v) and False is I use the set_enabled_equivalencies context manager.

In [1]: import astropy.units as u

In [2]: l2v = u.doppler_optical(2800*u.angstrom)

In [3]: (3000 * u.Angstrom).to(u.km/u.s, equivalencies=l2v) > (500*u.km/u.s)
Out[3]: array(True, dtype=bool)

In [4]: with u.set_enabled_equivalencies(l2v):
           print (3000 * u.Angstrom) > (500*u.km/u.s)
   ...:     
False

Activity

  1. added this to the milestone on May 15, 2014
  2. changed the title [-]Bug: quantities (to vs. set_enabled equivalencies[/-] [+]Bug: quantities (to vs. set_enabled_equivalencies)[/+] on May 15, 2014
  3. modified the milestones: v0.4.1, on May 21, 2014
  4. mhvk commented on May 23, 2014

    @mhvk
    Contributor

    @hamogu - this is a tricky one, as build in quite deep in Quantity is that for arithmetic the only thing one has to do to go from one unit to another is to multiply with a given scale -- and that we can get this scale by just transforming 1 <old-unit> to new-unit. Of course, for your equivalency, this is not true: some addition/subtraction has to be done as well. As a result,

    with u.set_enabled_equivalencies(l2v):
        print (3000 * u.Angstrom - 500*u.km/u.s)
    

    does the same as

    3000*u.Angstrom - (1.*u.km/u.s).to(u.Angstrom, equivalencies=l2v)*500
    

    and both yield

    -1397004.6698973326 Angstrom
    

    (which is smaller than zero, and hence the comparison returns False).

    It's not immediately obvious to me how to change this, but I think we will have to let quantity_helpers return transformation functions rather than scales. I'll think more about this, but in the meantime: cc @mdboom, @astrofrog.

  5. modified the milestones: v0.4.2, v0.4.3 on Sep 23, 2014
  6. mhvk commented on Sep 24, 2014

    @mhvk
    Contributor

    Came back to this bug again due to the movement of the milestone. Do think this is a tricky one and still do not see any solution other than changing quantity_helper such that rather than returning scales, it returns transformation functions. As I've been working with Quantity quite a bit recently, I can try to implement this, but I would appreciate feedback on whether this is a good idea in the first place... @mdboom, @astrofrog?

  7. embray commented on Sep 24, 2014

    @embray
    Member

    I would also definitely like to see a solution to this. The use case you suggested, using set_enabled_equivalencies, is too beautiful to ignore (and rather ugly that it doesn't work). If no one else has any ideas I'll poke at this sometime when I have time (which sadly is not right now).

  8. embray commented on Sep 24, 2014

    @embray
    Member

    (Though I'll add, depending on what you do inside that with block this could also be potentially very dangerous, though I guess the idea is to...not call anything inside the with block that you don't know what it's doing :)

  9. removed this from the v0.4.3 milestone on Dec 2, 2014
  10. added 4 commits that reference this issue on Mar 12, 2015
    4fc209f
    49d09bc
    32e75a5
  11. mhvk commented on Mar 12, 2015

    @mhvk
    Contributor

    Finally time to solve this issue -- see #3586

  12. added this to the v1.0.2 milestone on Mar 12, 2015
  13. added a commit that references this issue on Mar 12, 2015
    8d877b6
  14. added a commit that references this issue on Mar 23, 2015
    5871e22
  15. added a commit that references this issue on Mar 31, 2015
    81a79d6
  16. added a commit that references this issue on Apr 13, 2015
    5501440
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions