ccpem / ccpem/mrcfile

DeprecationWarning: setting header.dtype is deprecated in NumPy 2.5

Open Beginner friendly
#75 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
86
Forks
24
PR merge metrics
No merged PRs in 30d

Description

## 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

```python
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 "", line 11, in
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:

```python
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.:

```python
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.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the NumPy 2.5 minimal reproduction and inspect mrcinterpreter.py in MrcInterpreter._read_header(), where header.dtype is assigned. Replace the deprecated mutation with an equivalent view-based approach, then confirm that opening the generated MRC file no longer raises a DeprecationWarning when warnings are treated as errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.