Kilosort 2.5 Index in position 1 exceeds array bounds. Index must not exceed 32.
I have a similar problem to #3204. Unlike the issue, I am currently using 32 channels.
recording = se.BinaryRecordingExtractor(file_paths=[path], sampling_frequency=30000, num_channels=32, dtype='int16')
probe = generate_linear_probe(num_elec=32, ypitch=100.0, contact_shapes="circle", contact_shape_params={"radius":5})
probe.set_device_channel_indices(np.arange(recording.get_num_channels()))
assert recording.get_num_channels() == 32
assert recording.get_channel_locations().shape == (32, 2)
recording = recording.set_probe(probe, in_place=False)
sorting_KS2_5 = ss.run_sorter(sorter_name="kilosort2_5", recording=recording, remove_existing_folder=True,
folder=dirname, verbose=True, do_correction=False, preclust_threshold=6, freq_min=200.)
I double checked that the number of channels (i.e., recording.get_num_channels()) is 32. However, I am facing an error that "----------------------------------------Index in position 1 exceeds array bounds. Index must not exceed 32.\u001b[?1l\u001b>"
spikeinterface_log.json shows:
{
"sorter_name": "kilosort2_5",
"sorter_version": "git-f2e570c61b6e",
"datetime": "2025-08-25T20:20:52.738731",
"runtime_trace": [
"\u001b[?1h\u001b=",
"< M A T L A B (R) >",
"Copyright 1984-2024 The MathWorks, Inc.",
"R2024a (24.1.0.2537033) 64-bit (glnxa64)",
"February 21, 2024",
"",
"",
"To get started, type doc.",
"For product information, visit www.mathworks.com.",
"",
"\u001b[?1h\u001b=Time 0s. Computing whitening matrix..",
"Getting channel whitening matrix...",
"Channel-whitening matrix computed.",
"Time 8s. Loading raw data and applying filters...",
"Time 199s. Finished preprocessing 8493 batches.",
"Drift correction DISABLED",
"pitch is 100 um",
"----------------------------------------Index in position 1 exceeds array bounds. Index must not exceed 32.\u001b[?1l\u001b>"
],
"error": true,
"error_trace": [
"Traceback (most recent call last):",
" File \"/om2/user/jdhwang/anaconda3/envs/kilosort/lib/python3.9/site-packages/spikeinterface/sorters/basesorter.py\", line 270, in run_from_folder",
" SorterClass._run_from_folder(sorter_output_folder, sorter_params, verbose)",
" File \"/om2/user/jdhwang/anaconda3/envs/kilosort/lib/python3.9/site-packages/spikeinterface/sorters/external/kilosortbase.py\", line 217, in _run_from_folder",
" raise Exception(f\"{cls.sorter_name} returned a non-zero exit code\")",
" Exception: kilosort2_5 returned a non-zero exit code"
],
"run_time": null
}
For your information, I am using spikeinterface version 0.103.0
Have you tried running this in matlab by itself? That will tell us whether the problem is with the wrapper. It seems strange to me for sure. Maybe @alejoe91 has an idea?
Hi @zm711 Thank you for your response. I tried to run on matlab. However, it shows the same error without a specific part. It seems that it is related to a line rez = datashift2(rez, do_correction); % lat input is for shifting data
MATLAB is selecting SOFTWARE rendering.
< M A T L A B (R) >
Copyright 1984-2024 The MathWorks, Inc.
R2024a (24.1.0.2537033) 64-bit (glnxa64)
February 21, 2024
To get started, type doc.
For product information, visit www.mathworks.com.
k>> ilosort2_5_master('/om2/data/tmp/monkey_data/sorter_output', '/home/jdhwang/Kilosort')kilosort2_5_master('/om2/data/tmp/monkey_data/sorter_output', '/home/jdhwang/Kilosortkilosort2_5_master('/om2/data/tmp/monkey_data/sorter_output', '/home/jdhwang/Kilosort')
>>
>>
>> kilosort2_5_master('/om2/data/tmp/monkey_data/sorter_output', '/home/jdhwang/Kilosort')
Time 0s. Computing whitening matrix..
Getting channel whitening matrix...
Channel-whitening matrix computed.
Time 8s. Loading raw data and applying filters...
Time 185s. Finished preprocessing 8493 batches.
Drift correction ENABLED
pitch is 100 um
----------------------------------------Index in position 1 exceeds array bounds. Index must not exceed 32.
I also checked Kilsort repository and it seems that it is already discussed and the authors fixed it https://github.com/MouseLand/Kilosort/issues/318
Okay this seems like a matlab/kilosort problem but as final confirmation could you try sorting your data with one of our other sorters? So do
sorter_name = "spykingcircus2" # or "mountainsort5" or "trisdesclous2" etc?
sorting_KS2_5 = ss.run_sorter(sorter_name=sorter_name, recording=recording, remove_existing_folder=True,
folder=dirname, verbose=True, do_correction=False, preclust_threshold=6, freq_min=200.)
that way we can see if any sorting works.
Unfortunately we have noted in the past that Kilosort tends to depend on specific version of matlab because matlab internally changes how some functions work which causes some linear algebra operations to behave differently between versions. In the past it was best to run on < 2022 as that is roughly when they stopped updating the matlab versions. I think Kilosorts official recommendation would be to switch to Kilosort4 instead, but I would try
- test a different sorter to confirm this is a matlab ks2.5 issue
- try downgrading your matlab to an old version
Thanks for pinging @zm711
@jd730 what is the geometry of your probe? I don't think the issue is in datashift2 since you got the same error also when disabling motion correction.