Skip to content

Conversation

@MGAMZ
Copy link
Contributor

@MGAMZ MGAMZ commented Jan 5, 2026

This pull request makes a small correction to the logic for handling incomplete data batches in kilosort/io.py.

Specifically, it ensures that the correct variable is decremented when the last batch is smaller than expected.

The bug was caught with the following traceback:

Traceback (most recent call last):
  File "/home/zhangyiqin/miniforge3/envs/dbci/lib/python3.13/site-packages/spikeinterface/sorters/basesorter.py", line 270, in run_from_folder
    SorterClass._run_from_folder(sorter_output_folder, sorter_params, verbose)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zhangyiqin/miniforge3/envs/dbci/lib/python3.13/site-packages/spikeinterface/sorters/external/kilosort4.py", line 341, in _run_from_folder
    ops = compute_preprocessing(ops=ops, device=device, tic0=tic0, file_object=file_object)
  File "/home/zhangyiqin/miniforge3/envs/dbci/lib/python3.13/site-packages/kilosort/run_kilosort.py", line 616, in compute_preprocessing
    bfile = io.BinaryFiltered(ops['filename'], n_chan_bin, fs, NT, nt, twav_min,
                              chan_map, hp_filter, device=device, do_CAR=do_CAR,
    ...<2 lines>...
                              shift=shift, scale=scale, file_object=file_object,
                              batch_downsampling=batch_downsampling)
  File "/home/zhangyiqin/miniforge3/envs/dbci/lib/python3.13/site-packages/kilosort/io.py", line 978, in __init__
    super().__init__(filename, n_chan_bin, fs, NT, nt, nt0min, device,
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                     dtype=dtype, tmin=tmin, tmax=tmax, shift=shift,
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                     scale=scale, file_object=file_object,
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                     batch_downsampling=batch_downsampling)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/zhangyiqin/miniforge3/envs/dbci/lib/python3.13/site-packages/kilosort/io.py", line 654, in __init__
    self.n_batches -= 1
    ^^^^^^^^^^^^^^
AttributeError: 'BinaryFiltered' object has no attribute 'n_batches'. Did you mean: 'n_batches_raw'?

Potential Risk

I am not sure if n_batches has some special usage under other scenarios. But the fix is unlikely to introduce BC as self.n_batches is soon overrided by self.set_downsampling

@MGAMZ MGAMZ changed the title [WIP] Fix attribute not exist error in io Fix attribute not exist error in io Jan 5, 2026
@MGAMZ MGAMZ marked this pull request as ready for review January 5, 2026 09:36
Copilot AI review requested due to automatic review settings January 5, 2026 09:36
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes an AttributeError bug in the BinaryRWFile.__init__ method where the code attempted to decrement a non-existent attribute when handling incomplete data batches. The fix ensures that n_batches_raw is decremented instead of the yet-to-be-created n_batches attribute.

  • Corrects variable name from self.n_batches to self.n_batches_raw when adjusting for small last batches
  • Prevents AttributeError that occurred during preprocessing when the last batch is too small
  • Aligns with the proper initialization order where n_batches is created later by set_downsampling()

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov-commenter
Copy link

codecov-commenter commented Jan 14, 2026

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (3705cea) to head (284edca).
⚠️ Report is 125 commits behind head on main.

Files with missing lines Patch % Lines
kilosort/io.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff           @@
##            main   #1018    +/-   ##
======================================
  Coverage   0.00%   0.00%            
======================================
  Files         33      34     +1     
  Lines       5640    6053   +413     
======================================
- Misses      5640    6053   +413     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jacobpennington jacobpennington merged commit ce938a8 into MouseLand:main Jan 14, 2026
13 checks passed
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.

3 participants