Skip to content

Ensure freq_axis metadata propagation during NumPy ufunc evaluation#147

Open
vishwas-droid wants to merge 1 commit intosunpy:mainfrom
vishwas-droid:ufunc-freq-axis-clean
Open

Ensure freq_axis metadata propagation during NumPy ufunc evaluation#147
vishwas-droid wants to merge 1 commit intosunpy:mainfrom
vishwas-droid:ufunc-freq-axis-clean

Conversation

@vishwas-droid
Copy link

Spectrum objects were losing their freq_axis metadata when passed through NumPy ufuncs
(e.g., np.sqrt, np.log, arithmetic operations such as spec + 2).

Although the resulting object remained a Spectrum instance, the associated metadata
was silently dropped, which could lead to incorrect downstream scientific interpretation.

What This PR Does

This PR implements a custom __array_ufunc__ override to:

  • Execute NumPy ufuncs on plain ndarrays
  • Wrap the result back into a Spectrum
  • Safely propagate freq_axis metadata (deep copy)
  • Avoid recursion and ndarray subclass pitfalls

Tests

Regression tests have been added to ensure:

  • freq_axis is preserved after unary ufuncs (e.g., np.sqrt)
  • freq_axis is preserved after binary operations (e.g., spec + 5)

These tests prevent future silent metadata loss.

Rationale

Preserving metadata integrity is critical in scientific workflows, especially
for radio spectrogram analysis where frequency alignment is essential.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant