-
Notifications
You must be signed in to change notification settings - Fork 12
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
Wrong units when using da.integrate()
#205
Comments
Oh, I just saw that you briefly mention
Does that mean there is a workaround that allows the integrate method to work correctly? It is not so clear to me how I would have to do this. |
there's nothing like that yet, unfortunately. I completely missed the interaction with the coordinates when writing the tests for unit-aware operations in For now, the best we can do is to mention this in the page, and add a wrapper method to the accessor (so |
Note: this is more general, e.g. multiplying with a coordinate also does not propagate the dimensions, e.g. we should be able to do: import xarray
import pint_xarray
import pint
import numpy
ureg = pint.UnitRegistry(system="atomic", force_ndarray_like=True)
unit = ureg.bohr
x = numpy.linspace(-5, 5, 501) * unit
alpha = 1 * unit ** -2
psi = (2 * alpha / numpy.pi) ** 0.25 * numpy.exp(-alpha * x ** 2)
da = xarray.DataArray(data=psi, coords=dict(x=x))
norm = (da.conj() * da).integrate("x")
assert norm == ureg.Quantity(1.0 * ureg.dimensionless)
center = (da.conj() * da["x"] * da).integrate("x")
assert center == ureg.Quantity(0.0 * unit)
spread = (da.conj() * da["x"] * da["x"] * da).integrate("x")
assert spread == ureg.Quantity(0.25 * unit ** 2) |
in both cases you're running into the issue that dimension coordinates cannot have units (at least until #162 is merged, which is close but not quite there yet). In other words, |
Xarray has the nice
xarray.DataArray.integrate
method to integrate a data array over one of its coordinates. I automatically accounts for the values of the coordinates. The integration results should have units of the original array times the units of the coordinates. However, if I integrate a unit-aware Dataarray, it does not change its units. Here is a small example:I guess the problem is that only the data array is unit-aware, but not its coordinate. If this is not an easy thing to fix,it would be good to strip the unit completely or at least warn the user that the unit may be incorrect.
The text was updated successfully, but these errors were encountered: