-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
2D models created with units cannot be called with a dimensionless 0.0 #11313
Comments
Not necessarily. In magnitude, zero mag in one system isn't zero in another system. |
@olebole - the issue likely is that a unit is attached to the input because the model has units, and for a plain 0 that would be dimensionless. And In principle, it may be possible to change this, though there is some advantage to being explicit (as @pllim notes, I should perhaps add that the main reason we allow 0 to have any unit is for comparisons: |
The intention with units of models is to be explicit. If a model was initialized with units it expects inputs with units. |
@nden I think 0 is a special case, as usually the unit doesn't give additional information: 0" is the same as 0°, and I would rather support the "be strict in what you return, be generous in what you accept" rule. Requiring to be explicit in units when referring to the coordinate origin seems a bit too strict to me. I'd think the question is rather how much effort it takes to solve this; I agree that it is a rather unimportant problem. But when it takes more time to document the behaviour properly than to fix it, maybe the fix is better? |
@olebole This has come up in different contexts (not specifically the case with 0 as input) - should one be able to fit a model with units to arrays or fit a model without units to quantities, and the corresponding evaluation questions. I think the general rule needs to be documented . |
I think, 0 (and ±∞) is a special case, because it is not uncommon to use it without a unit. For other values, I think the unit should be mandatory, for a simple reason: For example, a Gaussian with σ=1nm is the same as a Gaussian with σ=10Å; so I would expect that they behave the same. The value of |
The problem is that there are exceptions to 0 being a "special case", for example temperature and magnitude (as @pllim noted). Models (really In most cases, the unit is dropped from a 0, as a notational connivence because we normally work with units which are all convertible to on-another with [linear maps)(https://en.wikipedia.org/wiki/Linear_map). This means 0 is always fixed ( As you do point out, in the majority of cases we do have this nice property for units so there are a few approaches to allowing a dimensionless 0 to be used in a model which defines units:
I think the best thing to do is to leave models as they are and just add a note to the documentation. In any case this should be better documented. |
After re-reading the stacktrace, I think the problem is not in the model, but in the units themself: >>> import numpy as np
>>> import astropy.units as u
>>> np.exp(0)
1.0
>>> 1*u.arcsec**-2 + 0
<Quantity 1. 1 / arcsec2>
>>> np.exp(0*u.arcsec**-2)
Traceback (most recent call last):
File "[…]/units/quantity_helper/helpers.py", line 32, in get_converter
scale = from_unit._to(to_unit)
File "[…]/units/core.py", line 950, in _to
raise UnitConversionError(
astropy.units.core.UnitConversionError: 'Unit("1 / arcsec2")' is not a scaled version of 'Unit(dimensionless)'
During handling of the above exception, another exception occurred:
[…] I think that the confusion is that some operations (like >>> import numpy as np
>>> import astropy.units as u
>>> 1*u.Celsius + 22*u.Celsius
<Quantity 23. deg_C>
>>> 1*u.Celsius - 22*u.Celsius
<Quantity -21. deg_C> The |
@olebole - the units system always respects units, so |
Description
If an argument in a 2D model (that was created with units) is zero, one gets an exception.
Expected behavior
I would expect that a zero (0.0) does not need to be armed with a unit, since this is the case in other places as well, f.e.
Actual behavior
While this is usually easy to avoid, it is somehow unexpected.
Steps to Reproduce
See above.
System Details
(all from Debian Testing)
The text was updated successfully, but these errors were encountered: