Skip to content

File readers: accept pathlib paths and return native byte order arrays (MIB) - #330

Open
cophus wants to merge 1 commit into
electronmicroscopy:devfrom
cophus:mib-reader
Open

cophus wants to merge 1 commit into
electronmicroscopy:devfrom
cophus:mib-reader

Conversation

@cophus

@cophus cophus commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Fixes two problems with loading Quantum Detectors .mib files through RosettaSciIO.

  • pathlib.Path was rejected. The quantumdetector reader only accepts str or a bytes buffer. read_4dstem, read_2d and read_3d_spectroscopy now pass os.fspath(file_path) to the RosettaSciIO reader. This change is still harmless if RosettaSciIO fixes the reader upstream.
  • Torch could not use the loaded array. MIB pixels are stored big-endian, and the reader returns them as a view with the detector y axis flipped (a negative stride). Torch rejects both. The selected array is now converted at import to native byte order, without negative strides. Arrays that are already native and positively strided are not copied.

One test in tests/core/test_file_readers.py writes a small 16-bit MIB file, reads it with a Path, and checks that torch.from_numpy gives back the frames. Before this fix the test fails with the Path, and with a str it fails at the torch conversion.

🤖 Generated with Claude Code

RosettaSciIO's quantumdetector reader rejects pathlib.Path, so the
rsciio readers now pass os.fspath(file_path). MIB data comes back as a
big-endian view with a flipped axis, which torch cannot wrap; the
selected array is now converted to native byte order without negative
strides at import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@maclariz maclariz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Seems good to me. The use of fspath and a couple of other features is better and more compact than the version running in my own branch on my laptop that I've been using all day with yesterday's mib files. But the core concepts are the same. I suspect it is simply that your version of Claude model is slightly higher and newer than the one I was using and knows a few extra tricks for more compact coding.

I just noted something in a test that there seemed to be a 384 byte padding in the test file. This is exactly right for a Merlin 1R - single Medipix3 chip. It is different for a Merlin 4R - quad chip arrangement, which has a 784 byte padding. I have some of these in data from E02 at ePSIC, but cannot access this data today to test since it the server that data is on went down and I need IT support to bring it back up. As such, I'd be happy to accept that it will work with single-chip 256x256 pixel data, but would have to test later with 512x512 or 515x515 data from the quad.

@cophus

cophus commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks Ian. rosettasciio reads the header size from the third header field, so it handles both the 384-byte single-chip header and the 768-byte quad header. Our changes only convert the path to a string and the returned array to native byte order, so they apply to every MIB layout.

I checked this branch on synthetic quad files with 768-byte headers: 512x512 at 12-bit, 515x515 at 24-bit, and 512x512 at 6-bit. All of them load from a Path and match the written frames after torch.from_numpy. A check on your E02 quad data once the server is back would still be worthwhile, since real quad headers contain more fields than my synthetic ones.

@maclariz

maclariz commented Dec 6, 2026

Copy link
Copy Markdown
Collaborator

Fails to install with pip due to an error with h5py installation:

Building h5py requires pkg-config unless the HDF5 path is explicitly specified using the environment variable HDF5_DIR. For more information and details, see https://docs.h5py.org/en/stable/build.html#custom-installation

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