NumPy Recfunctions and Structured Quantities
See original GitHub issueDescription
The numpy numpy.lib.recfunctions
(numpy v1.16+) module provides functions for working with structured arrays.
Quantity should support these both when there’s a single unit and when the unit is structured. For simple units this should be easy. For structured units the behavior should probably depend on whether all the fields have the same unit or not.
Pulling in @mhvk.
Examples
>>> m_nu = u.Quantity(
... np.array([(0, 0, 0.6)], dtype=[("nu1", float, ()), ("nu2", float, ()), ("nu3", float, ())]),
... u.StructuredUnit(("eV", "eV", "eV")))
>>> m_nu
[(0,0,0.6)](eV,eV,eV)
>>> import numpy.lib.recfunctions as rfn
>>> rfn.structured_to_unstructured(m_nu)
WARNING: function 'structured_to_unstructured' is not known to astropy's Quantity. Will run it anyway, hoping it will treat ndarray subclasses correctly. Please raise an issue at https://github.com/astropy/astropy/issues. [astropy.units.quantity]
[[0, 0, 0.6]](eV,eV,eV)
This is right if the structured units were different, but wrong since they are the same. The structured unit should decay into an unstructured unit.
e.g.
>>> rfn.structured_to_unstructured(m_nu.value * u.eV)
WARNING: function 'structured_to_unstructured' is not known to astropy's Quantity. Will run it anyway, hoping it will treat ndarray subclasses correctly. Please raise an issue at https://github.com/astropy/astropy/issues. [astropy.units.quantity]
[[0, 0, 0.6]]eV
In any case, structured_to_unstructured
should be known to astropy’s Quantity.
Lastly, the following should either not decay the structured unit or error. I was leaning towards the former, but now I’m feeling the latter. This is not something that is unstructurable.
thing = u.Quantity(
np.array([(0, 0, 0.6)], dtype=[("x", float, ()), ("y", float, ()), ("z", float, ())]),
u.StructuredUnit(("eV", "km", "s"))
)
rfn.structured_to_unstructured(thing)
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top GitHub Comments
Ah, got it: then I guess most important is to address @pllim’s comment with some actual examples – and future test cases!
For the record, can you please provide the minversion of numpy with this desired feature?