Skip to content

Feature Request: Making order = np.argsort(device_channel_indices) optional #4565

Open

Description

@chiyu1203

Dear Spikeinterface community,
I am asking for this feature in the _set_probes() as I would like to feed my previous dataset sorted by kilosort standalone into an analyser with the new function read_kilosort_as_analyzer #4366
The problem is that my probe map, coming from probe_interface library is not sorted by channels. While using spikeinterface to do spike sorting, it is fine to sort them by channels. However, when using read_kilosort_as_analyzer, it will check whether channels location used in the kilosort4 phy folder matches with its counterpart in the Recording (e.g. OpenEphysBinaryRecordingExtractor). Since kilosort4 does not sort the map, there is always a mismatch between the two.

Image

or

Image

I can certainly create a set of probe maps and sort them by channels first before using kilosort standalone to do spike sorting so that I will never to worry about this mismatch. However, would it be possible to make order = np.argsort(device_channel_indices) an option to skip so that both Recording and either my original probe.prb or kilosort's channel_map.npy, channel_locations.npy, and channel_shanks.npy are equally unsorted?

Or if you have any suggestion about circuvment the mismatch in channels location when using read_kilosort_as_analyzer?

Many thanks in advance.

Activity

  1. chrishalcrow commented on May 1, 2026

    @chrishalcrow
    Member

    Hi @chiyu1203 , sorry I've been meaning to look at this. It's possible that I'm using channel_map incorrectly in the kilosort PR. I'll take a closer look next week!

  2. chiyu1203 commented on May 4, 2026

    @chiyu1203
    Author

    @chrishalcrow thank you! Take your time! I was thinking either doing some work in _set_probes() or in read_kilosort_as_analyzer to ensure the channels are sorted the same way but yeah you probably know what I meant.

  3. chrishalcrow commented on May 11, 2026

    @chrishalcrow
    Member

    Hello, I tried to update PR #4529 so that the probe attached to the recording gets rewired to whatever is in the probe.prb file. Can you check if this solves the problem?

  4. chiyu1203 commented on May 11, 2026

    @chiyu1203
    Author

    Hello, Thank you very much for the update! I have tried that with and without .prb file in the phy folder. Both worked fine (I have not valided the final output of the sorting analyser, but I think that part is fixed).
    On the other hand, there is a minor issue related to recording.get_probe().set_device_channel_indices(new_device_channel_indices) in the read_kilosort_as_analyzer (Line 394).

    I would received this following error if I attached 2-shanked probe information to the recording from a .prb file, instead of a .json file.

    Image

    Exception has occurred: AssertionError
    there are several probe use .get_probes() or get_probegroup()
    File "C:\Users\neuroPC\Documents\GitHub\spikeinterface\src\spikeinterface\core\baserecordingsnippets.py", line 245, in get_probe
    assert len(probes) == 1, "there are several probe use .get_probes() or get_probegroup()"
    ^^^^^^^^^^^^^^^^
    File "C:\Users\neuroPC\Documents\GitHub\spikeinterface\src\spikeinterface\extractors\phykilosortextractors.py", line 394, in read_kilosort_as_analyzer
    recording.get_probe().set_device_channel_indices(new_device_channel_indices)
    ^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\neuroPC\Documents\GitHub\ephys\spike_curation.py", line 357, in si2phy
    sorting_analyzer=se.read_kilosort_as_analyzer(kilosort_result_folder_name,recording_for_analysis)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\neuroPC\Documents\GitHub\ephys\spike_curation.py", line 500, in
    _=si2phy(thisDir, json_file)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
    AssertionError: there are several probe use .get_probes() or get_probegroup()

    The foundamental mechanism behind that seems to because of the API read_prb. Here is a discussion about that.

    In my code, this is how I read 2-shanked probe information from the .prb file

    stacked_probes =  pi.read_prb(probe_map_file_name)
    raw_rec = raw_rec.set_probegroup(stacked_probes,group_mode='by_shank')
    

    These codes, together with additional lines to add shank id to the probegroup, are fine to group my recording without a problem.

    For me, the way to get around that error is to use
    recording.get_probegroup().set_global_device_channel_indices(new_device_channel_indices) at Line 394 in the read_kilosort_as_analyzer
    However, I am not familiar with .set_global_device_channel_indices() so not sure if this is the right way to fix this.

    Note: in the case where the 2-shanked information comes from .json file, I use

    stacked_probes = pi.read_probeinterface(probe_map_file_name)
    raw_rec = raw_rec.set_probe(stacked_probes,group_mode='by_shank')
    

    No error returned, no matter if I changed the Line 394 from recording.get_probe().set_device_channel_indices(new_device_channel_indices) to recording.get_probegroup().set_global_device_channel_indices(new_device_channel_indices) or not

  5. h-mayorquin commented on May 18, 2026

    @h-mayorquin
    Contributor

    This type of error is what I am talking about when I have argued against silently setting device_channel_indices to arange(N) in #4397: the code should not have this type of side effects. It is an undeclared modification of the probe that we do for the internal benefit of SpikeInterface (which is actually not really necessary, see #4553), but it is surprising to the user. The user had their own mapping that they wanted to keep, and we are losing that information here.

  6. chiyu1203 commented on May 22, 2026

    @chiyu1203
    Author

    @h-mayorquin thanks for the insight! I do not have any idea how to solve this issue, either working on a quick fix for this particular function or re-organising those basis in the Spikeinterface. @chrishalcrow Do you have any preference?

  7. chrishalcrow commented on May 25, 2026

    @chrishalcrow
    Member

    Hello, I've updated the PR with @h-mayorquin's recommendation. Let us know if it works!
    We will probably need to come back to this after the other probe issues are resolved.

  8. chiyu1203 commented on May 26, 2026

    @chiyu1203
    Author

    Thank you so much!! I have tested the updated PR already. It worked well and fixed my problem so I will close this issue for now.

  9. chrishalcrow commented on May 26, 2026

    @chrishalcrow
    Member

    Great!! We'll try and get the PR merged into main soon

  10. chrishalcrow commented on Jul 22, 2026

    @chrishalcrow
    Member

    Hi @chiyu1203

    We realised there was a much easier way to get the recording to use the right channel map: we now apply the channel map to the spikeinterface recording using select_channels. This is simpler and doesn't touch the probe wiring (which can get complicated internally).

    Can you re-try the open PR (https://github.com/SpikeInterface/spikeinterface/pull/4529/changes) on your data?

    Sorry this has taken a while, I just keep forgetting about it. Feel free to tag me any time to try and hurry me up!

  11. chiyu1203 commented on Jul 22, 2026

    @chiyu1203
    Author

    @chrishalcrow No worries! I have tested the new 4529 with the code

    sorting_analyzer=se.read_kilosort_as_analyzer(kilosort_result_folder_name,recording_for_analysis)
    sorting_analyzer.set_temporary_recording(recording_for_analysis)
    

    This returned the following error at the step of set_temporary_recording

    Image

    Do you know where the bug might come from? I have lost the track of this PR 4529 already.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions