-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Add dex, dB units #1628
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
Add dex, dB units #1628
Conversation
|
A couple of things come to mind about this:
|
Yes, indeed! That is what I've been spending (far too much of) my time on. I'm still adding documentation and tests for the
Indeed, I should try to use that; as I'm already using, e.g., For the dimensionless case (relevant for this PR), would it be |
|
FYI @mhvk, you may have a hard sell (at least to me) on That said, this PR seems fine to me, and I agree with Mike that equivalencies provide a natural way to move between log and linear units. |
|
@eteq - I guess ultimately it will be a question of what tools you think are important to provide -- as noted by @mdboom above, it would be really good for astropy to provide both magnitude, decibels, and dex with physical units, since these are used regularly. Since these are essentially the same type of logarithmic quantities, it would be insane to have them use different implementations. As said in #1577, I do think that what I'm doing is the logical extension of the What I have so far provides little documentation on the API, but the usage would be very similar to that outlined in #1577, where one would typically initalise with p.s. As I'm essentially implementing the API described in #1577 is lacking, please let me know if you think it is lacking. |
|
Lets leave this open until we see how that is the correct behavior, but the thing that comes out is dimensionless, rather than having units of |
|
I think the current behaviour is correct, since here the user explicitly asks to actually take the |
Just to clarify: this PR does not depend on |
|
Added an equivalency, so one can now do the example above as in: which yields p.s. @mdboom - I still haven't gotten around to looking how vounit and fits deal with dimensionless for their |
|
Between this and https://github.com/mhvk/astropy/compare/units-logarithmic , I see a lot of overlap with #1577 . Should I hold off my |
|
@pllim - yes, I think so; we definitely need a |
|
@mdboom - I rebased this old PR to introduce dex and dB, as I hope to get to the follow-up with full functional units. It still has a Otherwise, the one outstanding issue is whether/how to deal with the link between |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you find this in the FITS spec somewhere? That's kind of an odd unit...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this unit will ever occur (and if it did, I'm not sure what I would make of it!), but it is defined by the prefixes, and thus is tested, and with dB being present, we have to make the short form of decibyte is not being created. Would happily implement another way to circumvent it (maybe in this case, just omitting it from the tests is better than cluttering the code?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way that this is handled elsewhere is to use the exclude_prefixes kwarg when the unit is defined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did that in the definition of byte -- so in our unit system dbyte cannot exist. But in FITS in principle it can, and that is the case I'm trying to capture here.
Maybe it is good to step back, and consider what is the actual problem: if I remove these special_cases, I get an error any time I do <unit>.to_string('fits'):
In [2]: u.a.to_string('fits')
ERROR: AttributeError: 'module' object has no attribute 'dbyte' [astropy.units.format.fits]
The difference with other excluded prefixes seems to be that one usually excludes them since the other unit exists. Hence, when, e.g., the code tries to get 'P'+'a', it will just overwrite names['Pa']=u.Pa, which is fine. But in my case, it will work for dB but not for dbyte since that particular unit does not exist. So, really I want to exclude the prefix d only for B, not also for byte.
Which perhaps suggests a solution: if I define B as a derived unit from byte, I can exclude the prefix d only where it matters. This works except that it breaks another test:
def test_megabit():
"""See #1543"""
assert u.Mbit is u.Mb
assert u.megabit is u.Mb
> assert u.Mbyte is u.MB
E assert Unit("Mbyte") is Unit("MB")
E + where Unit("Mbyte") = u.Mbyte
E + and Unit("MB") = u.MB
/data/mhvk/venv/astropy-dev/lib/python3.3/site-packages/astropy-0.4.dev6748-py3.3-linux-x86_64.egg/astropy/units/tests/test_units.py:516: AssertionError
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, what a pain. I wonder if we shouldn't rework how SI prefixes are handled. Rather than making prefixes= a boolean option when defining units, makes prefixes accept a dictionary mapping unit names/abbreviations to a list of allowed prefixes. For example, in the definition for byte:
prefixes = {'B': ['k', 'M', 'G', 'T', 'P', 'Y', 'Z', etc....],
'byte': ['k', 'M', 'G', etc...]}
There could be shortcuts available if all spellings of the unit take the same prefixes. There should also be a few constants defined like a list named SI_PREFIXES so that one can easily assign prefixes=SI_PREFIXES for units that accept all standard SI prefixes. sigh
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a pain, but then again, we have a mostly working system, so I think it doesn't hurt that much to just put in a hack for a few corner cases. Perhaps mine is not so bad? At least it is extensible. Alternatively, we could just replace
if hasattr(u, key):
names[key] = getattr(u, key)
and not unreasonably assume that nobody will ever have a fits file with units of dbyte.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another possibility is to do what was done for cds and define units specific to the format in their own module. As there is only one of those here, that seems like a bit of overkill, though...
I'm fine with this for now, now that I understand why it's here. Let's not put too much effort into a single unit that is very unlikely to ever be seen in the wild.
|
My concern is with introducing a unit syntax that ultimately is incompatible with the FITS standard. I hadn't expected it to be so difficult to get a response... This feature is implemented in wcslib, so perhaps we just look there at what it's doing -- that's probably as "authoritative" as we can expect, but I suspect many people simply aren't using this feature in FITS. If we proceed without that, I'd say we need a way to flag this as a "discretionary" feature in the meantime. Maybe that's to raise a warning when the (EDIT: I'm out today with weather-related school closures, or I'd look into this myself...) |
|
@mdboom - fair enough. At least Dr Chiappetti replied; hopefully his suggestion of posting to fitsbits will lead somewhere. |
|
@mdboom - ok, I'll add a comment the the fits part so it is clearer why this test is there. Aside: do you have any idea why the |
|
It's not clear why the coverage test is failing here and not on master. It's because |
|
@mdboom - thanks for another fix. I was indeed very puzzled that the test seemed problematic on my branch but not on master, although it was a change in the |
|
Here's what So there can only be one "function" per unit specification. When converting between units, (in
The VOUnit adds |
|
@mdboom - you're fix for the coverage problem actually causes an enormous number of new problems... Could you check? |
|
@mdboom - tests now failed for python3; I made a commit in which |
|
The |
|
Looks good, aside from my comments on the |
|
Is this obsolete in light of #1894? |
since these do not know dex
…ding for unicode units from CDS
|
@mdboom - as @wkerzendorf mentioned in #1894 that it would be quite useful to have
Let me know if you think there is anything missing. |
|
Travis throws an error, but it seems unrelated. Restart to be sure? |
|
Looks good. Like you say, we should get the output formatting right, too... I'm not sure why you suggest we delay that. Won't merging this pull request allow for units that when written to FITS won't be FITS compliant? |
|
No, this PR will not write non-compliant FITS; instead, I think that is fine in principle, since the standard is so unclear anyway. But I do want to implement it returning |
|
I see. That makes sense. |
|
Most bizarre, the same travis setup fails (one but last, numpy 1.6, python 3.2), and for a similar reason (EOF when importing module), but in a different package (was table last time). Any ideas? |
|
The travis error does seem to be spurious -- #1894, which is based on this PR, does pass all tests. |
|
@mhvk - this needs an entry in CHANGES.rst |
Forgotten CHANGES.rst entry for #1628
|
@astrofrog - OK, sorry to have forgotten that; now done (#2231) |
Beyond magnitude, there are two other logarithmic units commonly used in astronomy:
dex(e.g., metallicity [Fe/H], surface gravitylogg) anddB(radio astronomy, cable losses, etc.) This PR adds them, writingmaganddBas derived units ofdex. The latter causes a small problem for thecdsandvounitformats, since these knowmagand, in the case of vounit,dB, but do not knowdex. Hence,mag.decompose().to_string('vounit')fails. This PR simply skips the corresponding tests, as is done for the whole decompose test in FITS; better suggestions welcome.