-
Notifications
You must be signed in to change notification settings - Fork 154
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
Using Pint
for units
#1483
Comments
I would like to add here that I could see this as an enhancement for spatialdata. The two need not be opposed. I think it's super cool to be able to see units on arrays and is super useful for obvious readability reasons. |
In terms of the issues at play, I don't think merging two different things with Pint-typed arrays i.e., in |
This is also a use-case for ehrapy, where we're constantly dealing with various units for measurements |
Thanks very much @ilan-gold! I don't have much of an opinion about For your other items: 1: It's probably no surprise, but I advocate for the approach I took in #1481, in which all 3: I consider this code snippet, included in #1481 and duplicated here for convenience, to be purely an improvement: In [1]: import numpy as np
In [2]: from pint import UnitRegistry
In [3]: reg = UnitRegistry()
In [4]: coords_1 = np.array([[1, 2], [2, 1]]) * reg.mm
In [5]: coords_1
Out[5]:
array([[1, 2],
[2, 1]]) <Unit('millimeter')>
In [6]: coords_2 = np.array([[0, 3], [4, 0]]) * reg.um
In [7]: coords_2
Out[7]:
array([[0, 3],
[4, 0]]) <Unit('micrometer')>
In [8]: np.vstack([coords_1, coords_2])
Out[8]:
array([[1. , 2. ],
[2. , 1. ],
[0. , 0.003],
[0.004, 0. ]]) <Unit('millimeter')> |
Any other stakeholders, or opinions about the implementation in the draft PR? Does anyone else agree or disagree that these items should be considered separately:
I think a global unit registry instantiated by |
After reading your above thoughts, and also thinking about it a bit, I don't think this is such a big deal. Allowing concatenation is fine, because people have to opt-in to using units (and there is already some meaninglessness in the operation on occasion as you mention). I am more curious what perhaps should happen if one array has units and the other doesn't. What's the result then? I think this should be resolved in the PR. |
Independent of whether we even allow the operation, this problem would need to be resolved. |
Thanks for the feedback @ilan-gold! Combining arrays with and without units is not allowed -- Pint throws an exception. This: >>> import numpy as np
>>> from pint import UnitRegistry
>>> reg = UnitRegistry()
>>> a1 = np.array([[1, 2], [3, 4]])
>>> a2 = np.array([[0, 1], [2, 3]]) * reg.mm
>>> np.vstack([a1, a2]) results in:
|
Great @mruffalo - I think you have answered most of my questions then. I think I have a few comments open on the PR. Another thing I'd be curious about is how this works with dask and scipy sparse. Probably want to add an |
Please describe your wishes and possible alternatives to achieve the desired result.
Continuing the discussion from #1481, we are considering
Pint
as both an in-memory data structure and something serializable.It seems there are a few things that would have to be worked out?
settings
help override behavior as needed?SpatialData
concat
be which involve more than one object?cc @mruffalo @ivirshup @flying-sheep
The text was updated successfully, but these errors were encountered: