Repository navigation
read_ngrid: read from DID file objects and file-like objects; little-endian - #14
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
ndi.fun.data.read_ngridread file objects withnp.fromfile, which needs a real OS file handle. It therefore failed on:did.file.Fileobj,ReadOnlyFileobj, and the binary document objectsdatabase_openbinarydocreturns), raisingAttributeError: … no attribute 'flush';io.BytesIO, raisingio.UnsupportedOperation: fileno.This is the Python counterpart of the NDI-matlab
readngridbug, 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
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.read()and left open.stroros.PathLike.writengridwrites, rather than in the machine's native byte order. Reshape is column-major, as before.'char'was mapped to thenp.charmodule, which isn't a dtype; it is now 8-bit unsigned.ValueErrorgiving the element count. An unknowndata_typestill reads as double, now with a warning.Tests
New
test_read_ngrid.pyintests/ndi/unittest/funandtests/nditests/unittest/fun(the two test trees are kept identical):PathLike, int16);BytesIO;ReadOnlyFileobj;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_ngridstill passes.Generated by Claude Code