Skip to content

Port nd2 reader to the 1.x reader API and expose it in the GUI - #1272

Open
rysk-t wants to merge 4 commits into
MouseLand:mainfrom
rysk-t:rysk-t/nd2_input
Open

rysk-t wants to merge 4 commits into
MouseLand:mainfrom
rysk-t:rysk-t/nd2_input

Conversation

@rysk-t

@rysk-t rysk-t commented Sep 9, 2026

Copy link
Copy Markdown

Summary

  • nd2_to_binary still had the 0.x reader(settings) signature and called the stubbed utils.init_settings / find_files_open_binaries, so input_format="nd2" raised TypeError from run_s2p's files_to_binary dispatch. It is now ported to the (dbs, settings, reg_file, reg_file_chan2) contract used by the tiff/h5 readers: reads dbs[0]["file_list"], writes to the opened binaries, fills Ly/Lx/nframes/frames_per_file/frames_per_folder/meanImg(_chan2), saves each plane's db.npy/settings.npy and returns dbs.
  • Reads in batch_size chunks through the lazy ND2File.to_dask() view instead of asarray(), so multi-GB recordings are not loaded into memory at once; any axis order is reordered to T×Z×C×Y×X.
  • Pixel values: uint16 are halved to int16 like the tiff/h5 readers; uint8/int16 are written directly; int32 is halved only when the result fits int16; other dtypes raise a clear ValueError instead of wrapping.
  • Clear errors instead of opaque failures: ImportError when the optional nd2 package is missing (the ND2 flag was never checked), ValueError when the file's Z/C sizes disagree with nplanes/nchannels, when more than two channels are requested, or when the file has unsupported axes (e.g. multi-position P).
  • Adds 'nd2' to the input_format choices in parameters.py (the GUI builds its dropdown from that list, so nd2 could not be selected — regression of FEATURE: Add .nd2 input format in the GUI #951 after the rungui refactor) and to the generated docs/parameters.md.
  • Also raises a clear ImportError from dcimg_to_binary when the optional dcimg package is missing (previously a NameError deep in the conversion loop).

rysk-t and others added 4 commits September 9, 2026 22:27
nd2_to_binary still used the 0.x settings-only signature and the stubbed
utils.init_settings / find_files_open_binaries helpers, so selecting
input_format="nd2" raised TypeError from run_s2p's files_to_binary dispatch.
Follows the same pattern as the tiff/h5 readers:

- read dbs[0]["file_list"], write to the opened reg_file handles, save each
  plane's db/settings and return dbs
- read in batch_size chunks (counted as interleaved frames like the other
  readers) through the lazy dask view instead of asarray(), so multi-GB
  recordings are not loaded into memory at once
- reorder any axis order to T x Z x C x Y x X and reject unsupported axes
- convert uint16 by // 2 and uint8/int16 directly like tiff.py; int32 is
  halved only when the result fits int16, and other pixel dtypes raise a
  clear ValueError instead of wrapping
- raise a clear ImportError when the nd2 package is missing (the ND2 flag
  was never checked before), and a ValueError when the file's Z/C counts
  disagree with nplanes/nchannels or more than two channels are requested

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SKRtLiLxWPAP8NPQquyp7Q
The GUI builds the "Input format" combo box by parsing the bracketed list in
the input_format description string, which was never updated when the nd2
reader was registered in files_to_binary. Regression of MouseLand#951 introduced by
the rungui refactor.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SKRtLiLxWPAP8NPQquyp7Q
The DCIMG flag was set on import failure but never checked, so a missing
package surfaced as NameError deep inside the conversion loop.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SKRtLiLxWPAP8NPQquyp7Q
Drives nd2_to_binary through a fake ND2File with Z,T,C,Y,X axis order, two
planes and two channels, and checks the plane-count, dtype and int32-range
validation. The fake is installed with raising=False so the tests also run
where the optional nd2 package is not installed.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SKRtLiLxWPAP8NPQquyp7Q
@rysk-t
rysk-t marked this pull request as ready for review September 9, 2026 14:20

This branch has not been deployed

No deployments
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.

1 participant