Skip to content

Fix kcoords for non-int groups#3944

Merged
alejoe91 merged 5 commits into
SpikeInterface:mainfrom
alejoe91:fix_kcoords_in_ks4
May 23, 2025
Merged

Fix kcoords for non-int groups#3944
alejoe91 merged 5 commits into
SpikeInterface:mainfrom
alejoe91:fix_kcoords_in_ks4

Conversation

@alejoe91
Copy link
Copy Markdown
Member

Introduced by #3852, if the channel groups are not strings they cannot be cast to float. This makes a map to integers

@alejoe91 alejoe91 added bug Something isn't working sorters Related to sorters module labels May 21, 2025
@alejoe91 alejoe91 requested a review from zm711 May 21, 2025 16:23
xc = positions[:, 0]
yc = positions[:, 1]
kcoords = groups.astype(float)
unique_groups = np.unique(groups)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Remember the bug with np.unique, we should use set.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Right! Let me change that!

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.

What's the np.unique bug? We use it a lot!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I vaguely remember a potential overflow issue since numpy is returning np.int* instead of Python int (of course in case the elements are ints). @h-mayorquin how wrong am I?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hi, guys, sorry I should have been more clear but I actually don't remember. At some point we encountered some bug with np.unique with mixed types and I have been weary of using since most of the times a set just does the same.

They are enhancing the np.unique so maybe the old bug is gone but I don't have time to track it:

numpy/numpy#28375

kcoords = groups.astype(float)
unique_groups = np.unique(groups)
group_map = {group: idx for idx, group in enumerate(unique_groups)}
kcoords = np.array([group_map[group] for group in groups], dtype=int)
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.

I guess the other option here is

kcoords = np.array(group_map.values(), dtype=int)

This just makes it more readable for me, but doesn't help with the code so feel free to ignore.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@zm711 just realized that doesn't work. We need kcoords to be the same length as x and y. Reverted

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.

Hello, is this np.array gonna fail when the groups have a different number of electrodes in each one? i.e. when you have some configuration with 182 electodes on shank 1, 96 on shank 2 and 96 on shank 3.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No because it returns a flat list of inta from 0 to Ngroups-1

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.

my bad. I actually had the same thought as I was falling asleep last night and was going to post when I woke up!

Comment thread src/spikeinterface/sorters/external/kilosort4.py Outdated
Comment thread src/spikeinterface/sorters/external/kilosort4.py
Copy link
Copy Markdown
Collaborator

@h-mayorquin h-mayorquin left a comment

Choose a reason for hiding this comment

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

LGTM

@alejoe91 alejoe91 merged commit 46e40a1 into SpikeInterface:main May 23, 2025
36 checks passed
@alejoe91 alejoe91 deleted the fix_kcoords_in_ks4 branch March 20, 2026 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working sorters Related to sorters module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants