You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect channel IDs in SpikeGadgetsRecordingExtractor #1215
Describe the bug
originally here SpikeInterface/spikeinterface#1260
Recordings from SpikeGadgets hardware have two possible channel maps: HWChan (a channel map defined by the amplifier) and Trodes channel map (for displaying on Trodes, the data acquisition software for SpikeGadgets). The mapping between these are in the header of the file that contains the recording (in .rec format). The channel IDs one gets with get_channel_ids from a SpikeGadgetsRecordingExtractor is in the Trodes order (though the channel names are HWChan names), which is sensible. But the order of the traces is completely scrambled (neither Trodes nor HWChan).
Thanks to the recently released version of Trodes that gives the location of data from each channel within a packet, I think I finally understand what is going on here.
As I mentioned previously, the issue is that the mapping between the voltage traces and the channel IDs is incorrect in the current version of SpikeGadgetsRecordingExtractor. When one does recording.get_channel_ids(), one gets a list of channel IDs that correspond to the hwChan of each channel in the order of appearance in the header of the .rec file. But this does not match the order of the voltage traces one gets from recording.get_traces(). I learned that this is because it is not the order in which the data is arranged in the packets streamed from the recording hardware. Rather, the data in packets are saved according to the following order:
hwChan ID of channel 0 of first chip, hwChan ID of channel 0 of second chip, hwChan ID of channel 0 of third chip, ..., hwChan ID of channel 0 of Nth chip,
hwChan ID of channel 1 of first chip, hwChan ID of channel 1 of second chip, ..., hwChan ID of channel 1 of Nth chip,
...
In other words, the order is chip-first, hwChan-second. Here, chip refers to the amplifier chip used to acquire data (usually part of the SpikeGadgets product) e.g. the 32-channel chip from Intan. For example, a 128-channel recording using these chips would have the data in the following hwChan order:
When I reordered the channels of a SpikeGadgetsRecordingExtractor with this (using the channel_slice trick) I get the correct mapping from channel IDs to the voltage traces (verified by comparing against the binary recording extracted from the rec file using trodesexport).
So to parse the data correctly, we need to know (1) how many channels there are and (2) how many channels there are per chip. Dividing (1) by (2) gives you the number of chips (assuming only one type of chip is used). Both (1) and (2) can be found by parsing the header of the rec file. Specifically, (1) can be found by parsing HardwareConfiguration field and (2) can be found by parsing SpikeConfiguration field.
Now that we understand the issue better, I think we can fix it. I can't promise to do this very soon so others should feel free to take this up, but may get to this eventually.
@samuelbray32 do you mean if the hwChan numbers in the workspace file are not continuous? hmm not sure. I would guess that the data packets would still come in in the hwChan numerical order without the missing hwChan. alternatively I suppose the missing hwChan could be there but filled with zero (I think this is unlikely because the file size should be smaller). let me ask spikegadgets folks. or we could do a little test in our lab.
It depends on whether or not you have the "saved displayed channels only" checkbox checked. If you do, then any channels not configured to be displayed are not saved in the rec file, and Trodes knows how to deal with it during playback and export...If you don't have it checked, then all channels are saved even if you didn't visualize them. The order is unchanged, except that the unused channels are removed.
Thanks for checking, that seems logical. I think that means there's an error in the linked PR in the case "saved displayed channels only" is on.
implemented solution assumes all hwChan are present in the binary stream and names the indexes accordingly
if one or more are hwChan missing from the binary stream due to saving options then the assigned channel names will be shifted relative to the corresponding binary data
I think a complete solution would need to know a list of all the hwChan saved in the files and censor the returned list based on that
@khl02007 does that make sense or am I missing something about the organization here?
@samuelbray32 yeah I think that's right, in that case the hwChans in the workspace file need to be identified and sorted (instead of assuming they are all there), and that should be used to fix the channel ids
Activity
alejoe91 commentedon Feb 16, 2023
@JuliaSprenger @samuelgarcia do you have time to look into this? I'm not familiar with SpikeGadgets.
Unless @khl02007 knows exactly what should be changed ;)
JuliaSprenger commentedon Apr 27, 2023
Sorry, I don't have any experience with this IO. @samuelgarcia Could you take over this one?
samuelgarcia commentedon Apr 27, 2023
Hi Kyu Julia and Alessio.
I think I get enhough solicitation to answer : "yes I will have look" but I do not promise anything about the delay...
khl02007 commentedon May 25, 2023
Thanks to the recently released version of Trodes that gives the location of data from each channel within a packet, I think I finally understand what is going on here.
As I mentioned previously, the issue is that the mapping between the voltage traces and the channel IDs is incorrect in the current version of
SpikeGadgetsRecordingExtractor. When one doesrecording.get_channel_ids(), one gets a list of channel IDs that correspond to thehwChanof each channel in the order of appearance in the header of the.recfile. But this does not match the order of the voltage traces one gets fromrecording.get_traces(). I learned that this is because it is not the order in which the data is arranged in the packets streamed from the recording hardware. Rather, the data in packets are saved according to the following order:In other words, the order is chip-first, hwChan-second. Here, chip refers to the amplifier chip used to acquire data (usually part of the SpikeGadgets product) e.g. the 32-channel chip from Intan. For example, a 128-channel recording using these chips would have the data in the following hwChan order:
When I reordered the channels of a
SpikeGadgetsRecordingExtractorwith this (using thechannel_slicetrick) I get the correct mapping from channel IDs to the voltage traces (verified by comparing against the binary recording extracted from the rec file using trodesexport).So to parse the data correctly, we need to know (1) how many channels there are and (2) how many channels there are per chip. Dividing (1) by (2) gives you the number of chips (assuming only one type of chip is used). Both (1) and (2) can be found by parsing the header of the rec file. Specifically, (1) can be found by parsing
HardwareConfigurationfield and (2) can be found by parsingSpikeConfigurationfield.Now that we understand the issue better, I think we can fix it. I can't promise to do this very soon so others should feel free to take this up, but may get to this eventually.
khl02007 commentedon Jun 28, 2023
@rly
demo of the workaround:
hwChan_1t_128ch.trodesconfthat sets the channel order of extract binary to increasing HWChan order (https://ucsf.box.com/s/18tpk3tclcagjk3wrj9p5qxrl9kzskyo)trodesexport -rec FILENAME.rec -reconfig hwChan_1t_128ch.trodesconf -kilosortrec_binaryandrec_sg_fixed(they should be the same)Fix ephys channel ids in spikegadgetsrawio
zm711 commentedon Jul 24, 2024
Fixed by #1496
samuelbray32 commentedon Jun 23, 2025
@khl02007 What's the expected behavior of this if not all the hardware channels were saved by the user into the .rec file?
e.g. A wire on one of the tetrodes broke and the experimenter did not save the data from that channel?
khl02007 commentedon Jun 23, 2025
@samuelbray32 do you mean if the hwChan numbers in the workspace file are not continuous? hmm not sure. I would guess that the data packets would still come in in the hwChan numerical order without the missing hwChan. alternatively I suppose the missing hwChan could be there but filled with zero (I think this is unlikely because the file size should be smaller). let me ask spikegadgets folks. or we could do a little test in our lab.
khl02007 commentedon Jun 23, 2025
@samuelbray32
From Mattias:
samuelbray32 commentedon Jun 23, 2025
Thanks for checking, that seems logical. I think that means there's an error in the linked PR in the case "saved displayed channels only" is on.
@khl02007 does that make sense or am I missing something about the organization here?
khl02007 commentedon Jun 23, 2025
@samuelbray32 yeah I think that's right, in that case the hwChans in the workspace file need to be identified and sorted (instead of assuming they are all there), and that should be used to fix the channel ids