Replace deprecated np.matrix with ndarray in H23 functions#820
Merged
Conversation
… (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])
Contributor
There was a problem hiding this comment.
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 withndarray+@/.T/.conj().T. - Fix numpy array/scalar handling in
common_mean_watson()printing andlat_from_pole()return casting. - Add a stability guard in
find_T()and filter degenerate bootstrap samples inmean_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.
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.
Member
Author
|
Addressed Copilot review feedback in cb78423:
|
This was referenced Feb 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
np.matrixusage 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)'%.1f' % arrayformatting bug incommon_mean_watson()andfloat()on 1-element array inlat_from_pole()find_Tfor singular covariance matrices from degenerate bootstrap samplesContext
np.matrixis deprecated and broken in modern numpy, causingValueError: setting an array element with a sequenceinform_Ghat. This brokecommon_mean_bootstrap_H23,mean_bootstrap_confidence, andreversal_test_bootstrap_H23. Thecommon_mean_watsonandlat_from_polefixes address separate but related numpy array/scalar handling issues.