Kilosort icon indicating copy to clipboard operation
Kilosort copied to clipboard

BUG: cluster_templates function returns vastly different waveforms from the one in `templates.npy

Open teristam opened this issue 1 year ago • 5 comments

Describe the issue:

Hi,

I try to get the template waveform of a cluster both from the new cluster_templates function and the original templates.npy. I notice the waveform returned by both are vastly different from each others:

Image

I understand that the results from cluster_templates are scaled according to the spike amplitude, but even the best channel calculated within the cluster_templates() is different from the results from get_best_channels()

Upon looking closer, I notice that in

https://github.com/MouseLand/Kilosort/blob/db107d7e4939bcdd094f579269ce772355f0d9a8/kilosort/data_tools.py#L149-L150

The spike_idx refers to a list of cluster id, but in

https://github.com/MouseLand/Kilosort/blob/db107d7e4939bcdd094f579269ce772355f0d9a8/kilosort/data_tools.py#L185

spike_idx actually refers to the index of detected spikes.

Is there a potential issue there?

Reproduce the bug:


Error message:


Version information:

4.0.27

teristam avatar Mar 14 '25 13:03 teristam

Thanks for catching that, there was indeed a mixup there. It's fixed with the latest changes, you can pull those now if you installed from source. It might be a day or two before they're available through pypi.

jacobpennington avatar Mar 14 '25 18:03 jacobpennington

Thank you for looking into this!

However, I notice now that the result from cluster_templates is no longer scaled according to the amplitude of each spikes, different from what the doc of the function is saying.

The 'template' is scaled separately for each spike based on its amplitude.

Is this the expected behaviour?

teristam avatar Mar 18 '25 13:03 teristam

How are you determining they are "no longer scaled?" They are scaled based on the values in amplitudes.npy, in data_tools.get_templates (line 102):

scaled = amplitudes[spike_idx, np.newaxis, np.newaxis] * temps

jacobpennington avatar Mar 18 '25 18:03 jacobpennington

Sorry, my bad. I got confused about the get_templates and cluster_templates functions.

teristam avatar Mar 18 '25 18:03 teristam

Hi,

I am re-opening this issue because my current problem seems to be related. I am using the cluster templates as a way to measure how close the spikes of a cluster are to the centroid of the cluster. My understanding is that the cluster templates are scaled in a way that should have similar amplitudes to the original spikes. However, when I plot the raw/mean spike waveforms together with the cluster template, they seem to have very different amplitudes. I got my spikes from get_spike_waveforms.:

Image

Even in the original example in the doc (https://kilosort.readthedocs.io/en/latest/tutorials/plotting_example.html), the mean spike and template seems to be very different from each other in amplitude?

What is the best way to get a spike template that has similar amplitude to individual spikes?

teristam avatar Mar 19 '25 16:03 teristam