Skip to content

read_ngrid: read from DID file objects and file-like objects; little-endian - #14

Merged
stevevanhooser merged 1 commit into
mainfrom
claude/read-ngrid-fileobj
Nov 13, 2026
Merged

stevevanhooser merged 1 commit into
mainfrom
claude/read-ngrid-fileobj

Conversation

@stevevanhooser

Copy link
Copy Markdown
Contributor

Problem

ndi.fun.data.read_ngrid read file objects with np.fromfile, which needs a real OS file handle. It therefore failed on:

  • DID-python file objects (did.file.Fileobj, ReadOnlyFileobj, and the binary document objects database_openbinarydoc returns), raising AttributeError: … no attribute 'flush';
  • any other file-like object, e.g. io.BytesIO, raising io.UnsupportedOperation: fileno.

This is the Python counterpart of the NDI-matlab readngrid bug, which is fixed in a companion PR in VH-Lab/NDI-matlab. There, a read-only file object was reopened with 'r', which such objects reject.

Fix

  • DID file objects are read with fread. An object that is already open is read and left open for its owner to close; one that isn't open is opened here and closed after reading.
  • Other binary file-like objects are read with read() and left open.
  • Paths may be str or os.PathLike.
  • Byte order: data are read little-endian, as MATLAB's writengrid writes, rather than in the machine's native byte order. Reshape is column-major, as before.
  • 'char' was mapped to the np.char module, which isn't a dtype; it is now 8-bit unsigned.
  • A short file raises ValueError giving the element count. An unknown data_type still reads as double, now with a warning.

Tests

New test_read_ngrid.py in tests/ndi/unittest/fun and tests/nditests/unittest/fun (the two test trees are kept identical):

  • path round trips (2-D, 3-D, PathLike, int16);
  • a hand-built column-major little-endian file;
  • BytesIO;
  • unopened and already-open DID ReadOnlyFileobj;
  • wrong size, missing file, unknown type.

All 10 pass locally with DID-python main. The file-object tests fail on the previous implementation, so they reproduce the bug. The existing test_fun_basics.test_ngrid still passes.


Generated by Claude Code

…endian

read_ngrid used np.fromfile for file objects, which needs a real OS file
handle, so it failed on DID-python file objects (Fileobj,
ReadOnlyFileobj, and the binary documents database_openbinarydoc
returns: no 'flush'/'fileno') and on any file-like object. The same bug
in NDI-matlab's readngrid (reopening a read-only file object with 'r')
is fixed alongside.

- DID file objects are read with fread: an object that is already open
  is read and left open for its owner; one that is not open is opened
  here and closed after. Other binary file-like objects are read with
  read() and left open. Paths may be str or os.PathLike.
- Data are read little-endian, as MATLAB's writengrid writes, instead of
  in the machine's native byte order; reshape is column-major as before.
- 'char' mapped to the np.char module (not a dtype); now 8-bit unsigned.
- A short file raises ValueError with the element count; an unknown
  data_type still reads as double, now with a warning.
- New tests (tests/ndi and tests/nditests): path round trips (2-D, 3-D,
  PathLike, int16), a hand-built column-major little-endian file,
  BytesIO, unopened and open DID ReadOnlyFileobj, wrong size, missing
  file, unknown type. All pass locally; the file-object tests fail on the
  previous implementation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011v7R5tiX9iaian1z6QbUNX
@stevevanhooser
stevevanhooser merged commit 31b809a into main Nov 13, 2026
2 checks passed
@stevevanhooser
stevevanhooser deleted the claude/read-ngrid-fileobj branch November 13, 2026 21:01
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