question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NumPy Recfunctions and Structured Quantities

See original GitHub issue

Description

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:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mhvkcommented, Oct 26, 2021

Ah, got it: then I guess most important is to address @pllim’s comment with some actual examples – and future test cases!

1reaction
pllimcommented, Oct 25, 2021

For the record, can you please provide the minversion of numpy with this desired feature?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Structured arrays — NumPy v1.25.dev0 Manual
recfunctions.repack_fields converts an aligned dtype or array to a packed one and vice versa. It takes either a dtype or structured ndarray as...
Read more >
numpy/recfunctions.py at main - GitHub
Collection of utilities to manipulate structured arrays. Most of these functions were initially implemented by John Hunter for. matplotlib.
Read more >
Convert structured array to regular NumPy array
@joris: Your array contains single-precision (32 bit) floating point numbers. To reinterpret the same memory as an unstructured array, use .view(np.float32) in ...
Read more >
Structured Units — Astropy v5.2
Numpy arrays can be structured arrays, where each element consists of multiple fields. These can be used with Quantity using a StructuredUnit ,...
Read more >
Python: module numpy.lib.recfunctions - PyOpenGL
Collection of utilities to manipulate structured arrays. Most of these functions were initially implemented by John Hunter for matplotlib. They have been ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found