Skip to content

feat: DREDge motion correction (CatGT→DREDge→KS4) + uv/py3.14 env#5

Open
tabedzki wants to merge 3 commits into
mainfrom
feat/dredge-motion-correction
Open

feat: DREDge motion correction (CatGT→DREDge→KS4) + uv/py3.14 env#5
tabedzki wants to merge 3 commits into
mainfrom
feat/dredge-motion-correction

Conversation

@tabedzki

@tabedzki tabedzki commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

🤖 AI text below 🤖

Summary

Implements Option A from #4: adds DREDge motion correction (SpikeInterface correct_motion(preset="dredge")) as an opt-in preprocessing step in the CatGT → DREDge → Kilosort4 flow. DREDge is one more selectable preproc_tool, mirroring catgt; Kilosort keeps its direct kilosort.run_kilosort call. Also modernizes the environment to uv on Python 3.14.

Note: the correct keyword is preset="dredge" (which internally selects estimate_motion method dredge_ap) — not preset="dredge_ap".

How it works

  • run_dredge reads whatever the previous step produced (globs *ap.meta): the flattened CatGT output dir when chained, or raw SpikeGLX if DREDge runs alone. It writes a SpikeGLX-style *.ap.bin + copied *.ap.meta into dredge_output/, which becomes new_raw_data_directory, so the existing Kilosort callers read it unchanged.
  • KS4's internal drift corrector is disabled via nblocks=0 in the process paramset, so motion is corrected once (by DREDge). do_correction is not a KS4 parameter (verified: zero hits in the kilosort pkg) and is kept only as an inert, KS2/KS3-friendly key.
  • GPU: DREDge does not auto-detect a GPU (KS4 does), so run_dredge passes estimate_motion_kwargs={"device": ...} explicitly (cuda when available, cpu fallback) and frees CUDA memory before the sorter stage.

Changes

  • u19_sorting/config.py — register dredge in preproc_tools + preproc_tools_delete_post.
  • u19_sorting/preprocess_wrappers.pydredge branch in preprocess_main; dredge.run_dredge (read → correct_motionwrite_binary_recording + copy meta → persist motion/ → lazy-skip) and dredge_check_output.
  • pyproject.tomlrequires-python >= 3.14; spikeinterface>=0.104.8 (floor, no upper bound); floor ibllib/ONE-api at 4.0.1/3.4.1 to stop the resolver regressing to a stale pre-2024 snapshot; replace the invalid empty license with license-files; point Pyright at .venv. Re-locked so matplotlib/pyarrow resolve cp314 wheels. Retire requirements.txt / setup.py.

Verification

End-to-end on a synthetic drifting recording: exactly one ap.bin+ap.meta, non-trivial motion displacement (≈±30 µm, not the all-zero silent-failure mode), kilosort.io.find_binary locates the output, and a second run lazy-skips.

Linkage (merge together)

🤖 Generated with Claude Code

…CatGT

Adds DREDge (SpikeInterface correct_motion(preset="dredge_ap")) as an opt-in
preprocessing tool alongside CatGT, per BrainCogsEphysSorters#4 (Option A).

- config: register 'dredge' in preproc_tools + preproc_tools_delete_post
- preprocess_wrappers: dredge branch in preprocess_main; dredge.run_dredge
  reads the SpikeGLX run, runs correct_motion, and writes a SpikeGLX-style
  *.ap.bin + copied *.ap.meta into dredge_output/ (drop-in for CatGT output,
  so the Kilosort callers are unchanged). SpikeInterface import is deferred.
  Adds preprocess_has_tool() helper.
- sorter_wrappers: when DREDge ran, disable each Kilosort's internal drift
  correction to avoid double-correcting (KS4 nblocks=0+do_correction=False,
  KS3 nblocks=0, KS2 reorder=0).
- kilosortbatch.m: honor ops.nblocks==0 (was always calling datashift2(rez,1))
- deps: add spikeinterface[full]

Assisted-by: ClaudeCode:claude-opus-4.8
@tabedzki

Copy link
Copy Markdown
Contributor Author

🤖 AI text below 🤖

Companion tracking issue (orchestration side): BrainCOGS/U19-pipeline-python#94 — describes the DataJoint param inserts ('dredge' PreClusterMethod + ParamSet + step) needed to make DREDge selectable. No code change in that repo.

tabedzki added 2 commits July 9, 2026 12:04
correct_motion() has never accepted "dredge_ap" as a preset key. The valid
key is "dredge" (SpikeInterface's "Official Dredge preset"), which sets
estimate_motion_kwargs.method="dredge_ap" internally. Passing "dredge_ap"
would raise on every released version, including the 0.104.8 the lockfile
resolves to.

Pin a >=0.101 floor: that release introduced the Motion class and the
dredge preset. write_binary_recording's signature is unchanged across
0.101-0.104 for the kwargs we pass.

Assisted-by: ClaudeCode:claude-opus-4.8
…nize env

Reconcile the DREDge preprocessing step with the CatGT->DREDge->KS4 flow and
modernize the Python environment to uv on Python 3.14.

DREDge (u19_sorting/preprocess_wrappers.py):
- run_dredge reads whatever the previous step produced (the flattened CatGT
  output dir, or raw SpikeGLX if DREDge runs alone) by globbing *ap.meta, so it
  chains after CatGT without assuming the original run-folder layout.
- Pass the motion-estimation device explicitly (cuda when available, cpu
  fallback) since DREDge does not auto-detect a GPU the way KS4 does; free CUDA
  memory before handing off to the sorter.
- Persist motion_info + drift plots for verification; lazy-skip on requeue.
- Materialize a SpikeGLX-style *.ap.bin via write_binary_recording (+ copied
  *.ap.meta) so the existing Kilosort callers read it unchanged.

Environment:
- requires-python >= 3.14; spikeinterface>=0.104.8 (floor, no upper bound);
  floor ibllib/ONE-api at 4.0.1/3.4.1 to stop the resolver regressing to a
  stale pre-2024 snapshot; re-lock so matplotlib/pyarrow pick cp314 wheels.
- Replace invalid empty license field with license-files; point Pyright at .venv.
- config.py: register dredge in preproc_tools + preproc_tools_delete_post.

Verified end-to-end on a synthetic drifting recording: produces one
ap.bin+ap.meta, non-trivial motion displacement, KS4 find_binary locates it,
and the second run lazy-skips.

Closes #4
@tabedzki tabedzki changed the title feat: DREDge motion correction as a preprocessing alternative to CatGT feat: DREDge motion correction (CatGT→DREDge→KS4) + uv/py3.14 env Jul 10, 2026
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.

Integrate SpikeInterface (DREDge motion correction) + Kilosort version selection via Apptainer

1 participant