Skip to content

Fix numpy.matrix deprecation warnings in bootstrap routines#809

Closed
Copilot wants to merge 5 commits into
masterfrom
copilot/fix-deprecation-warnings-numpy
Closed

Fix numpy.matrix deprecation warnings in bootstrap routines#809
Copilot wants to merge 5 commits into
masterfrom
copilot/fix-deprecation-warnings-numpy

Conversation

Copilot AI commented Nov 6, 2025

Copy link
Copy Markdown

NumPy 1.25+ deprecated implicit scalar coercion from (1,1) matrices/arrays, causing thousands of warnings per bootstrap run. The warnings will become errors in future NumPy releases.

Changes

pmagpy/pmag.py

  • form_Ghat, form_Mhat, form_Q, find_CR, find_CMDT_CR: Replace np.matrix with np.ndarray, use @ operator for matmul
  • find_T: Return scalar via .item() instead of (1,1) array
  • Replace .getT().T and .getH().conj().T throughout

pmagpy/ipmag.py

  • common_mean_bootstrap_H23: Replace .getH() with .conj().T, explicit float conversion for Lmin_b[i]

Example

Before:

def form_Ghat(X, Mhat):
    X = np.matrix(X)
    Ghat = np.matrix(np.zeros((2,2)))
    for u in range(2):
        for v in range(2):
            for i in range(n):
                Ghat[u,v] += Mhat_T[:,u].getT()*X[:,i]*X[:,i].getT()*Mhat_T[:,v]  # ⚠️ Warning
    return Ghat

After:

def form_Ghat(X, Mhat):
    X = np.asarray(X)
    Ghat = np.zeros((2,2))
    for u in range(2):
        for v in range(2):
            for i in range(n):
                Ghat[u,v] += (Mhat_T[:,u] @ X[:,i]) * (X[:,i] @ Mhat_T[:,v])  # ✓ Clean
    return Ghat

No functional changes—purely API modernization to eliminate deprecated numpy.matrix usage.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • api.earthref.org
    • Triggering command: python3 pmagpy_tests/test_ipmag.py (dns block)
  • earthref.org
    • Triggering command: python3 pmagpy_tests/test_ipmag.py (dns block)
  • www2.earthref.org
    • Triggering command: python3 pmagpy_tests/test_ipmag.py (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>DeprecationWarnings in ipmag bootstrap routines due to numpy.matrix scalar coercion</issue_title>
<issue_description>Summary

Running mean_bootstrap_confidence, common_mean_bootstrap, common_mean_bootstrap_H23 now floods stderr with DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated…. These warnings come from writing (1, 1) numpy.matrix results into scalar slots. NumPy 1.25 deprecated that behavior and it will become an error in a future NumPy release, so the ipmag bootstrap utilities will eventually fail.

The hottest spots are:

  • pmagpy/pmag.py:13799 (form_Ghat) when accumulating a (1, 1) matrix into Ghat[u, v].
  • pmagpy/ipmag.py:431 (mean_bootstrap_confidence) when pmag.find_T(...) returns a matrix that is stored in T_b.
  • pmagpy/ipmag.py:1371 (common_mean_bootstrap_H23) when np.min(D_b) yields a (1, 1) matrix that is stored in Lmin_b.

Every Monte Carlo iteration repeats the warning, so callers see thousands of messages.


Observed Behavior

pmagpy/pmag.py:13799: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
pmagpy/ipmag.py:431: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)
pmagpy/ipmag.py:1371: DeprecationWarning: Conversion of an array with ndim > 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.)

Depending on num_sims, thousands of instances are printed to stderr.


Expected Behavior

Bootstrap routines should run without deprecation warnings on current NumPy releases, and continue to work once NumPy removes implicit numpy.matrix→scalar coercion.


Proposed Fix (high level)

  1. Replace numpy.matrix usage in form_Ghat, form_Mhat, etc. with plain ndarray calculations, extracting scalars explicitly via .item() / float() when needed.
  2. Update pmag.find_T and its call sites to return and store scalars or 1‑D arrays instead of (1, 1) matrices.
    </issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 4 commits November 6, 2025 20:51
- Replace numpy.matrix with ndarray in form_Ghat, form_Mhat, form_Q, find_T, find_CR, find_CMDT_CR
- Use .item() to explicitly extract scalars from matrix operations
- Replace .getT() with .T and .getH() with .conj().T
- Fix matrix multiplication to use @ operator instead of *
- Tested with NumPy 2.3.4 - no deprecation warnings

Co-authored-by: Swanson-Hysell <4332322+Swanson-Hysell@users.noreply.github.com>
- Ensure form_Q converts inputs to float to avoid in-place division errors
- All tests now pass without warnings

Co-authored-by: Swanson-Hysell <4332322+Swanson-Hysell@users.noreply.github.com>
Co-authored-by: Swanson-Hysell <4332322+Swanson-Hysell@users.noreply.github.com>
Co-authored-by: Swanson-Hysell <4332322+Swanson-Hysell@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix deprecation warnings in ipmag bootstrap routines Fix numpy.matrix deprecation warnings in bootstrap routines Nov 6, 2025
Copilot AI requested a review from Swanson-Hysell November 6, 2025 21:05
@Swanson-Hysell

Copy link
Copy Markdown
Member

This has been dealt with in a separate pull request that was just merged #820

@Swanson-Hysell Swanson-Hysell deleted the copilot/fix-deprecation-warnings-numpy branch February 28, 2026 13:19
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.

DeprecationWarnings in ipmag bootstrap routines due to numpy.matrix scalar coercion

2 participants