Skip to content

DeprecationWarning: setting header.dtype is deprecated in NumPy 2.5 #75

Description

@shuuul

Summary

Reading an MRC file with mrcfile.open() triggers a DeprecationWarning on NumPy 2.5 because MrcInterpreter._read_header() assigns to header.dtype on a structured/record array.

This is distinct from the NumPy 2.4 strides deprecation addressed in #73, and from the broader NumPy 2.4 compatibility report in #74.

Environment

  • mrcfile 1.5.4 (latest release)
  • numpy 2.5.0
  • Python 3.12.13

Minimal reproduction

import warnings
import numpy as np
import mrcfile

warnings.simplefilter("error", DeprecationWarning)

data = np.arange(24, dtype=np.float32).reshape(2, 3, 4)
with mrcfile.new("test.mrc", data=data, overwrite=True) as mrc:
    mrc.voxel_size = (1.2, 1.3, 1.4)
    mrc.header.origin = (5.0, 6.0, 7.0)

with mrcfile.open("test.mrc", permissive=True, mode="r") as mrc:
    _ = mrc.data

Warning / traceback

DeprecationWarning: Setting the dtype on a NumPy array has been deprecated in NumPy 2.5.
Instead of changing the dtype on an array x, create a new array with x.view(new_dtype)
Traceback (most recent call last):
  File "<stdin>", line 11, in <module>
  File ".../mrcfile/load_functions.py", line 145, in open
    return NewMrc(name, mode=mode, permissive=permissive,
  File ".../mrcfile/mrcfile.py", line 115, in __init__
    self._read(header_only)
  File ".../mrcfile/mrcinterpreter.py", line 170, in _read
    self._read_header()
  File ".../mrcfile/mrcinterpreter.py", line 221, in _read_header
    header.dtype = header.dtype.newbyteorder(byte_order)
  File ".../numpy/_core/records.py", line 462, in __setattr__
    ret = object.__setattr__(self, attr, val)

Root cause

In mrcinterpreter.py (_read_header), after determining byte order:

header.dtype = header.dtype.newbyteorder(byte_order)

NumPy 2.5 deprecates mutating .dtype on record arrays. The warning suggests creating a new view instead.

Suggested fix

Replace in-place dtype mutation with a view-based update, e.g.:

new_dtype = header.dtype.newbyteorder(byte_order)
header = header.view(new_dtype)

(or an equivalent approach that avoids assigning to header.dtype).

Impact

  • Currently a warning under default pytest settings.
  • With warnings.simplefilter("error", DeprecationWarning) (or stricter CI configs), reading MRC files fails.
  • Downstream packages that round-trip MRC files (e.g. via mrcfile.open after mrcfile.new) will see this on NumPy 2.5+.

Happy to help test a fix or open a PR if useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions