astropy: Can't instantiate Quantity with logarithmic units.

I’m trying to create a Quantity that represents units of wavelength in log_10(Angstrom), so I figured dex(Angstrom) would work fine. It only sort of does.

>>> foo = np.linspace(3., 5., 10) * u.Unit("dex(Angstrom)")
>>> foo
<Dex [ 3.        , 3.22222222, 3.44444444, 3.66666667, 3.88888889,
       4.11111111, 4.33333333, 4.55555556, 4.77777778, 5.        ] dex(Angstrom)>
>>> foo.physical
<Quantity [   1000.        ,   1668.1005372 ,   2782.55940221,
              4641.58883361,   7742.63682681,  12915.49665015,
             21544.34690032,  35938.13663805,  59948.42503189, 100000.        ] Angstrom>
>>> bar = Quantity(np.linspace(3., 5., 10), unit=u.Unit("dex(Angstrom)"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/weaver/Documents/local/products/anaconda/envs/py3/lib/python3.5/site-packages/astropy/units/quantity.py", line 332, in __new__
    value._set_unit(value_unit)
  File "/Users/weaver/Documents/local/products/anaconda/envs/py3/lib/python3.5/site-packages/astropy/units/quantity.py", line 722, in _set_unit
    .format(type(self).__name__, UnitBase, type(unit)))
astropy.units.core.UnitTypeError: Quantity instances require <class 'astropy.units.core.UnitBase'> units, not <class 'astropy.units.function.logarithmic.DexUnit'> instances.

So why can’t DexUnit instances inherit from UnitBase? This is a pretty serious problem for me because I use dex(Angstrom) units all the time. For example, basically every SDSS spectrum file uses these units.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (19 by maintainers)

Most upvoted comments

Another thought: in context of the zen of python – There should be one-- and preferably only one --obvious way to do it – yet another suggestion is simply to state that multiplication with a unit is the right way to instantiate a Quantity (with perhaps still that long-promised context manager that allows one to avoid copies).

I feel like this is the same issue I brought up some time ago, which incidentally interrupted Marten’s vacation… https://github.com/astropy/astropy/issues/5178#issuecomment-232984105 😅