Kilosort icon indicating copy to clipboard operation
Kilosort copied to clipboard

BUG: Error in template matching phase with the expanded size of a tensor.

Open Leonardodm00 opened this issue 8 months ago • 2 comments

Describe the issue:

I'm trying to apply Kilosort 4 to in-vitro data. The data has been recorded from the 3Brain device CorePlate™ 6W 38/60, featuring 2304 channels. The error arises during the spike detection phase likely during the template matching process. Apart from this, I cannot give you more information than what the log can.

kilosort4.log

Reproduce the bug:


Error message:

kilosort.spikedetect: Error in spikedetect.run on batch 0
Traceback (most recent call last):
  File "C:\Users\Admin\anaconda3\envs\SpikeInterfaceSorter\Lib\site-packages\kilosort\spikedetect.py", line 252, in run
    xy, imax, amp, adist = template_match(X, ops, iC, iC2, weigh, device=device)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\anaconda3\envs\SpikeInterfaceSorter\Lib\site-packages\kilosort\spikedetect.py", line 152, in template_match
    As[:, nb*t:nb*(t+1)] = Aa
    ~~^^^^^^^^^^^^^^^^^^
RuntimeError: The expanded size of the tensor (742) must match the existing size (743) at non-singleton dimension 1.  Target sizes: [11593, 742].  Tensor sizes: [11593, 743]
kilosort.spikedetect: Error in spikedetect.run on batch 0
Traceback (most recent call last):
  File "C:\Users\Admin\anaconda3\envs\SpikeInterfaceSorter\Lib\site-packages\kilosort\spikedetect.py", line 252, in run
    xy, imax, amp, adist = template_match(X, ops, iC, iC2, weigh, device=device)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\anaconda3\envs\SpikeInterfaceSorter\Lib\site-packages\kilosort\spikedetect.py", line 152, in template_match
    As[:, nb*t:nb*(t+1)] = Aa
    ~~^^^^^^^^^^^^^^^^^^
RuntimeError: The expanded size of the tensor (742) must match the existing size (743) at non-singleton dimension 1.  Target sizes: [11593, 742].  Tensor sizes: [11593, 743]
kilosort.run_kilosort: Encountered error in `run_kilosort`:
Traceback (most recent call last):
  File "C:\Users\Admin\anaconda3\envs\SpikeInterfaceSorter\Lib\site-packages\kilosort\run_kilosort.py", line 229, in run_kilosort
    ops, bfile, st0 = compute_drift_correction(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\anaconda3\envs\SpikeInterfaceSorter\Lib\site-packages\kilosort\run_kilosort.py", line 574, in compute_drift_correction
    ops, st = datashift.run(ops, bfile, device=device, progress_bar=progress_bar,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\anaconda3\envs\SpikeInterfaceSorter\Lib\site-packages\kilosort\datashift.py", line 200, in run
    st, _, ops  = spikedetect.run(
                  ^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\anaconda3\envs\SpikeInterfaceSorter\Lib\site-packages\kilosort\spikedetect.py", line 252, in run
    xy, imax, amp, adist = template_match(X, ops, iC, iC2, weigh, device=device)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\anaconda3\envs\SpikeInterfaceSorter\Lib\site-packages\kilosort\spikedetect.py", line 152, in template_match
    As[:, nb*t:nb*(t+1)] = Aa
    ~~^^^^^^^^^^^^^^^^^^
RuntimeError: The expanded size of the tensor (742) must match the existing size (743) at non-singleton dimension 1.  Target sizes: [11593, 742].  Tensor sizes: [11593, 743]
kilosort.run_kilosort: Encountered error in `run_kilosort`:
Traceback (most recent call last):
  File "C:\Users\Admin\anaconda3\envs\SpikeInterfaceSorter\Lib\site-packages\kilosort\run_kilosort.py", line 229, in run_kilosort
    ops, bfile, st0 = compute_drift_correction(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\anaconda3\envs\SpikeInterfaceSorter\Lib\site-packages\kilosort\run_kilosort.py", line 574, in compute_drift_correction
    ops, st = datashift.run(ops, bfile, device=device, progress_bar=progress_bar,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\anaconda3\envs\SpikeInterfaceSorter\Lib\site-packages\kilosort\datashift.py", line 200, in run
    st, _, ops  = spikedetect.run(
                  ^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\anaconda3\envs\SpikeInterfaceSorter\Lib\site-packages\kilosort\spikedetect.py", line 252, in run
    xy, imax, amp, adist = template_match(X, ops, iC, iC2, weigh, device=device)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\anaconda3\envs\SpikeInterfaceSorter\Lib\site-packages\kilosort\spikedetect.py", line 152, in template_match
    As[:, nb*t:nb*(t+1)] = Aa
    ~~^^^^^^^^^^^^^^^^^^
RuntimeError: The expanded size of the tensor (742) must match the existing size (743) at non-singleton dimension 1.  Target sizes: [11593, 742].  Tensor sizes: [11593, 743]

Version information:

Python version: 3.12.8 Kilosort version: 4 Operating system: Windows

Leonardodm00 avatar May 14 '25 15:05 Leonardodm00

Please start by upgrading Kilosort4 to the latest version. You can do that by running pip install --upgrade kilosort in your existing kilosort environment.

jacobpennington avatar May 14 '25 17:05 jacobpennington

I'm still encountering the same problem. I'm on purpose not using the GPU. As before I'm attaching the log

kilosort4.log

Leonardodm00 avatar May 15 '25 07:05 Leonardodm00

Sorry for the long delay, I must have missed this somehow. If you're still having problems with this, I would start by checking that the recording duration is correct. According to the log, your recording is only 5 seconds long (2 batches), which is going to cause all sorts of strange behavior.

jacobpennington avatar Aug 27 '25 17:08 jacobpennington

@jacobpennington I was getting this error often when using motion interpolation with dredge through spikeinterface with "force_zero" for the border mode, changing that to "force_extapolate" allowed it to run without this error. So I suspect it has something to do with the edge cases being set to zero. KS4.1.1 ran fine on the original catgt output, and the "force_extrapolate", but not with the "force_zero" in spikeinterface.sortingcomponents.motion.interpolate_motion. It was the only setting I changed.

peterfschade avatar Aug 28 '25 16:08 peterfschade

Okay. Just for reference, we always recommend running KS4 by itself (without spikeinterface or any other third-party software) if you you run into errors and need to debug. We can't help debug issues caused by other software, even if they use KS4 as part of the pipeline, as we're not involved with development for those packages.

jacobpennington avatar Aug 28 '25 16:08 jacobpennington

Yes I see the issue with 3rd party software, but wanted to add it in the hope the comment can be of use to someone else that is running into issues. I am running KS4 outside of spike interface but use spike interface for the motion correction only, saving the bin file and then running KS4 outside of spike-interface environment. I will add that this seems to be a new behavior interaction between the packages in KS 4.1.x, as I wasn't having this specific error before.

peterfschade avatar Aug 28 '25 17:08 peterfschade

Thanks. I just want to reiterate that sorting a 5 second recording is well outside the expected use-case for KS4. So, I would not be surprised if you continue running into errors. Or, if that's not actually the recording duration, then something else is wrong.

jacobpennington avatar Aug 28 '25 17:08 jacobpennington

@jacobpennington here is the log file from my recording with the error. No modifications made outside of running catGT.

kilosort4.log

peterfschade avatar Sep 04 '25 17:09 peterfschade

@jacobpennington, thanks for the reply. I found that the error likely originated from how I was setting the 'nt' parameter. The code loks as follow:

nt_ms = 3 # [ms] nt_s = nt_ms / 1000 # [s] other_params['nt'] = int(nt_s * samplingRate) + 1

When setting the 'nt_ms' to 2.5 or to any non integer number the above error arises. However now everything works perfectly!

Leonardodm00 avatar Sep 04 '25 19:09 Leonardodm00