Skip to content

Replace deprecated np.matrix with ndarray in H23 functions#820

Merged
Swanson-Hysell merged 2 commits into
masterfrom
fix/numpy-deprecations
Feb 28, 2026
Merged

Replace deprecated np.matrix with ndarray in H23 functions#820
Swanson-Hysell merged 2 commits into
masterfrom
fix/numpy-deprecations

Conversation

@Swanson-Hysell

Copy link
Copy Markdown
Member

Summary

  • Replace all np.matrix usage in the Heslop et al. (2023) common mean direction test functions (form_Mhat, form_Ghat, form_Q, find_CMDT_CR, find_T, find_CR) with standard ndarray operations (np.asarray, .T, .conj().T, @ operator)
  • Fix '%.1f' % array formatting bug in common_mean_watson() and float() on 1-element array in lat_from_pole()
  • Add numerical stability guard in find_T for singular covariance matrices from degenerate bootstrap samples

Context

np.matrix is deprecated and broken in modern numpy, causing ValueError: setting an array element with a sequence in form_Ghat. This broke common_mean_bootstrap_H23, mean_bootstrap_confidence, and reversal_test_bootstrap_H23. The common_mean_watson and lat_from_pole fixes address separate but related numpy array/scalar handling issues.

… (2023) functions

np.matrix is deprecated and broken in modern numpy, causing failures in
common_mean_bootstrap_H23, mean_bootstrap_confidence, and
reversal_test_bootstrap_H23.

Changes in pmag.py:
- form_Mhat, form_Ghat, form_Q, find_CMDT_CR, find_T, find_CR: replace
  np.matrix() with np.asarray(), .getT() with .T, .getH() with .conj().T,
  and matrix * with @ operator
- find_T: add condition number guard for singular covariance matrices
  that arise from degenerate bootstrap samples

Changes in ipmag.py:
- common_mean_bootstrap_H23: replace .getH()* with .conj().T @
- mean_bootstrap_confidence: filter degenerate bootstrap samples before
  computing quantile
- common_mean_watson: fix '%.1f' % array formatting (use angle[0])
- lat_from_pole: fix float() on 1-element array (use paleo_lat[0])

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the Heslop et al. (2023) common mean direction test implementation to remove deprecated np.matrix usage and standardize the linear algebra on ndarray, while also addressing a couple of numpy scalar/formatting edge cases and adding a guard for degenerate bootstrap covariance matrices.

Changes:

  • Replace np.matrix-based operations in H23 helper functions with ndarray + @/.T/.conj().T.
  • Fix numpy array/scalar handling in common_mean_watson() printing and lat_from_pole() return casting.
  • Add a stability guard in find_T() and filter degenerate bootstrap samples in mean_bootstrap_confidence().

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
pmagpy/pmag.py Reworks H23 linear algebra helpers to use ndarray semantics; adds conditioning-based degeneracy guard in find_T.
pmagpy/ipmag.py Filters non-finite bootstrap statistics; updates matrix multiplications to @; fixes scalar formatting/casting for angle/latitude helpers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pmagpy/ipmag.py
Comment thread pmagpy/pmag.py
Comment thread pmagpy/ipmag.py
Comment thread pmagpy/pmag.py
Add guard for empty T_b array after filtering non-finite bootstrap
values in mean_bootstrap_confidence(). Add np.isfinite check on Ghat
in find_T() to catch non-finite matrix entries before computing the
condition number.
@Swanson-Hysell

Copy link
Copy Markdown
Member Author

Addressed Copilot review feedback in cb78423:

  • ipmag.py mean_bootstrap_confidence(): Added explicit check for empty T_b array after filtering non-finite values, raising a clear ValueError rather than letting np.quantile fail on an empty array.
  • pmag.py find_T(): Added np.isfinite(Ghat).all() guard before the condition number check to catch matrices containing inf/nan entries.
  • Did not adopt the np.linalg.solve suggestion — keeping the explicit inv(Ghat) preserves direct correspondence with Equation 6 of Heslop et al. (2023).
  • Unit tests for the degenerate cases can potentially be added in a separate branch where we are migrating tests to pytest.

@Swanson-Hysell Swanson-Hysell merged commit 0a74e40 into master Feb 28, 2026
2 checks passed
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.

2 participants