MouseLand / Kilosort

Fast spike sorting with drift correction
https://kilosort.readthedocs.io/en/latest/
GNU General Public License v3.0
478 stars 248 forks source link

How to resolve NaNs and/or zeroes present in weights for spikedetect.run? #629

Closed NirvikNU closed 7 months ago

NirvikNU commented 7 months ago

Describe the issue:

I am using data from a 32 contact laminar probe data (150 micron spacing) in Kilosort4. The data is in int16 format. Kilosort3 was able to detect spikes and sort it successfully.

However, I repeatedly get this warning when running the same file with Kilosort 4: d:\kilosort\kilosort\kilosort\spikedetect.py:232: UserWarning: NaNs and/or zeroes present in weights for spikedetect.run, may need to adjust min_template_size and/or dminx for best results.

subsequent to this warning, the program terminates with the error: 0 spikes extracted in 33.26s; total 33.47s Traceback (most recent call last): File "d:\kilosort\kilosort\kilosort\gui\sorter.py", line 82, in run st, tF, Wall0, clu0 = detect_spikes(ops, self.device, bfile, tic0=tic0, File "d:\kilosort\kilosort\kilosort\run_kilosort.py", line 397, in detect_spikes raise ValueError('No spikes detected, cannot continue sorting.')

I have turned off drift correction, also tried playing with the parameters dmin, min_template_size, nearest_templates, but all the combinations I tried keep generating the same warning followed by the same error. Is there a systematic way to test the parameters to find the right combination?

jacobpennington commented 7 months ago

Hello,

Am I understanding correctly that this is 32 contacts along a single shank, i.e. all x-positions are the same? If so, I would try setting dmin = 300. Template centers are placed every dmin//2 microns, which allows for some overlap for densely spaced contacts, but in your case the default dmin = <median difference in y position> would center half of the templates in the large gaps between contacts.

I'm not sure what will work best for nearest_templates, but you will want something much smaller than the default. For linear probes, there will always be a single template center laterally. So, given how far apart the templates would be with dmin = 300, I would try nearest_templates = 3 so that only immediate neighbors are compared except at the ends. You could probably reduce nearest_chans to 3 as well for the same reason.

Increasing min_template_size to something like 20um might help with the NaN weight issue, but I would try the other parameters first.

NirvikNU commented 7 months ago

Thanks a lot! Changing nearest_templates = 3and dmin = 300 worked so as far now spikes are detected and the sorting is completed. However, I do still get the NaN issue. I tried increasing min_template_size to 20 but the warning still persists. Any suggestions as to why it still persists?

jacobpennington commented 7 months ago

@NirvikNU It happens when the distances between templates are really large compared to min_template_size. It doesn't necessarily need to be fixed, the warning is just there as a debugging tool in case no spikes are detected, and sometimes increasing min_template_size a bit will be enough to get rid of it. As long as the sorting finished and the results look reasonable (i.e. similar to your KS3 results), don't worry about it.

NirvikNU commented 7 months ago

I see. That makes sense. Thanks!