From 46d55ca2e9d199e02695d95a9a7a872291675217 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 11:51:16 +0000 Subject: [PATCH] read_ngrid: read from DID file objects and file-like objects; little-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 Claude-Session: https://claude.ai/code/session_011v7R5tiX9iaian1z6QbUNX --- src/ndi/fun/data/read_ngrid.py | 119 ++++++++++++------ tests/ndi/unittest/fun/test_read_ngrid.py | 109 ++++++++++++++++ .../nditests/unittest/fun/test_read_ngrid.py | 109 ++++++++++++++++ 3 files changed, 301 insertions(+), 36 deletions(-) create mode 100644 tests/ndi/unittest/fun/test_read_ngrid.py create mode 100644 tests/nditests/unittest/fun/test_read_ngrid.py diff --git a/src/ndi/fun/data/read_ngrid.py b/src/ndi/fun/data/read_ngrid.py index 60495c5..a76cc44 100644 --- a/src/ndi/fun/data/read_ngrid.py +++ b/src/ndi/fun/data/read_ngrid.py @@ -1,51 +1,98 @@ import numpy as np import os +import warnings + +# MATLAB's writengrid writes little-endian ('ieee-le'); read the same way on +# every platform rather than in the machine's native byte order. +_DTYPE_MAP = { + 'double': '