Skip to content

Can't compose units from equivalent units that have prefixes #4104

Closed

Description

@embray

For example, the following works:

>>> u.m.compose(units=[u.pc])
[Unit("3.24078e-17 pc")]

but this does not work:

>>> u.m.compose(units=[u.kpc])
ERROR: UnitsError: Cannot represent unit m in terms of the given units [astropy.units.core]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/internal/1/root/src/astropy/astropy/astropy/units/core.py", line 1226, in compose
    max_depth=max_depth, depth=0, cached_results={}))
  File "/internal/1/root/src/astropy/astropy/astropy/units/core.py", line 1099, in _compose
    raise result
astropy.units.core.UnitsError: Cannot represent unit m in terms of the given units

Or even more trivially:

>>> u.m.compose(units=[u.km])
ERROR: UnitsError: Cannot represent unit m in terms of the given units [astropy.units.core]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/internal/1/root/src/astropy/astropy/astropy/units/core.py", line 1226, in compose
    max_depth=max_depth, depth=0, cached_results={}))
  File "/internal/1/root/src/astropy/astropy/astropy/units/core.py", line 1099, in _compose
    raise result
astropy.units.core.UnitsError: Cannot represent unit m in terms of the given units

Of course, in the above examples the use of compose appears to be overkill, but this comes up in more general cases as well.

Activity

  1. embray commented on Aug 28, 2015

    @embray
    MemberAuthor

    Nevermind, I didn't notice the include_prefix_units argument:

    >>> u.m.compose(units=[u.kpc], include_prefix_units=True)
    [Unit("3.24078e-20 kpc")]

    So not really a bug. But I'm tempted to say that if the units list explicitly contains prefix units (suggesting that I explicitly wanted them), include_prefix_units should be automatic. But my suspicion is that there were internal reasons (such as passing in the si module as an argument?) for filtering out prefix units by default.

    Maybe instead it should only filter out prefix units if that unit's base unit was included.

  2. mhvk commented on Aug 11, 2016

    @mhvk
    Contributor

    Looking at old units issues, I'm slightly torn about this one. There is an argument to be made that if you put in a specific set of units, that is what should be used. Of course, all that is needed is to do something like:

            if include_prefix_units is None:
                include_prefix_units = units is not None
    

    Anyone who is interested in having this should feel free to submit a PR.

  3. added a commit that references this issue on Dec 9, 2017
  4. fastakash commented on Dec 10, 2017

    @fastakash
    Contributor

    @mhvk I made the change as you suggested and initially thought the test failure I was seeing was not related to my change!
    Before the change:

    >>> u.Pa.to_system(u.cgs)
    [Unit("10 P / s"), Unit("10 Ba")]
    

    After the change:

    >>> u.Pa.to_system(u.cgs)
    [Unit("daP / s"), Unit("1e-23 YP / s"), Unit("1e-20 ZP / s"), Unit("1e-17 EP / s"), Unit("1e-14 PP / s"), Unit("1e-11 TP / s"), Unit("1e-08 GP / s"), Unit("1e-05 MP / s"), Unit("0.01 kP / s"), Unit("0.1 hP / s"), Unit("10 P / s"), Unit("100 dP / s"), Unit("1000 cP / s"), Unit("10000 mP / s"), Unit("1e+07 uP / s"), Unit("1e+10 nP / s"), Unit("1e+13 pP / s"), Unit("1e+16 fP / s"), Unit("1e+19 aP / s"), Unit("1e+22 zP / s"), Unit("1e+25 yP / s"), Unit("daBa"), Unit("1e-23 YBa"), Unit("1e-20 ZBa"), Unit("1e-17 EBa"), Unit("1e-14 PBa"), Unit("1e-11 TBa"), Unit("1e-08 GBa"), Unit("1e-05 MBa"), Unit("0.01 kBa"), Unit("0.1 hBa"), Unit("10 Ba"), Unit("100 dBa"), Unit("1000 cBa"), Unit("10000 mBa"), Unit("1e+07 uBa"), Unit("1e+10 nBa"), Unit("1e+13 pBa"), Unit("1e+16 fBa"), Unit("1e+19 aBa"), Unit("1e+22 zBa"), Unit("1e+25 yBa")]
    

    I will continue to research, I am still very new to astropy. This is/was going to be my first project! Please let me know if there is a way to address this or what code I should study. I am still happy that I got all the github steps working. I am hoping to pick up another task during the holidays!

  5. fastakash commented on Dec 16, 2017

    @fastakash
    Contributor

    I made changes so that if the unit specified is a list or a tuple (and not a module etc.), then include_prefix_units is turned to True. As suspected by @embray , this was originally causing a problem when the input unit was a module such as u.cgs.

    I hope you can review/comment!

  6. added a commit that references this issue on Jan 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions