Skip to content

Add main_channel_id when running Kilosort - #4725

Draft
chrishalcrow wants to merge 1 commit into
SpikeInterface:mainfrom
chrishalcrow:add-kilosort-main-channel
Draft

Add main_channel_id when running Kilosort#4725
chrishalcrow wants to merge 1 commit into
SpikeInterface:mainfrom
chrishalcrow:add-kilosort-main-channel

Conversation

@chrishalcrow

@chrishalcrow chrishalcrow commented Jul 31, 2026

Copy link
Copy Markdown
Member

Starts work on #4692

We recently added the sorting property main_channel_id and the analyzer property main_channel_index which allows sorters to tell us which channel each unit is localized on.

It would be great to get this information from each sorter when it's run. Doing so means: 1) we don't need to guess the main channel => the results should be closer to the original sorter output 2) we can do an almost-instantaneous sparsity calculation.

But no external sorters actually give us the main_channel_id yet. One way to overcome this is to guess the main_channel_id straight after the sorting has completed (i.e. the end of run_sorter). At this point, we have access to whatever the sorter outputs, and we still have access to the recording. This PR implements that option.

One downside of this option is that we usually don't touch the sorter_output folder: that's usually just what the sorter outputs. I think this is the first time (for kilosort at least) that we're adding some additional information.

Other possible options:

  • we shouldn't guess the main_channel_id unless the sorter tells us what it is
  • we should guess it on load (difficult: we don't have access to the recording)

@chrishalcrow chrishalcrow added the sorters Related to sorters module label Jul 31, 2026
Comment on lines +460 to +471
if (results_dir / "templates.npy").is_file():
# Note: these are the whitened templates
templates = np.load(results_dir / "templates.npy")
# main channel indices are the argmax of the ptp of the templates
main_channel_indices = np.argmax(np.ptp(templates, axis=1), axis=1)
main_channel_ids = recording.channel_ids[main_channel_indices]
# save main_channel_ids
with open(results_dir / "cluster_main_channel_id.tsv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f, delimiter="\t")
writer.writerow(["cluster_id", "main_channel_id"])
for unit_index, item in enumerate(main_channel_ids):
writer.writerow([unit_index, item])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Smart! So they are loaded automatically as properties :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

sorters Related to sorters module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants