Repository navigation
Description
Describe the bug
SpikeGLX may write data using "folder per probe" organization. Here is the description, from the SpikeGLX User Manual:
Note that there is an option on the
Save TabcalledFolder per probe.
If this is set, there is still a run folder for each g-indexrun-name_gN.
However, inside that there is also a subfolder for each probe that contains
all the t-indices for that g-index and that probe. A probe subfolder is
named likerun-name_gN/run-name_gN_imecM.
For example:
- run_dir/ (example: 3-1-2021/)
- gate_dir_g0/ (example: 3-1-2021_g0/)
...
- gate_dir_gN/
- probe_dir_imec0/ (example: 3-1-2021_g0_imec0/)
...
- probe_dir_imecN/
- trigger_file_gN_imecN_t0.lf.bin
- trigger_file_gN_imecN_t0.lf.meta
- trigger_file_gN_imecN_t0.ap.bin
- trigger_file_gN_imecN_t0.ap.meta
...
- trigger_file_gN_imecN_tN.lf.bin
- trigger_file_gN_imecN_tN.lf.meta
- trigger_file_gN_imecN_tN.ap.bin
- trigger_file_gN_imecN_tN.ap.meta
This issue applies when using the folder-per-probe layout and reading probes with a device index greater than 0. For example, trying to read the following file:
3-1-2021/3-1-2021_g0/3-1-2021_g0_imec1/3-1-2021_g0_imec1.ap.bin
will result in the following error:
AssertionError: stream_id imec1.ap is not in [np.str_('imec0.ap'), np.str_('imec0.lf')]
It should be checking for membership in [np.str_('imec1.ap'), np.str_('imec1.lf')], as that is the only probe in the directory.
The incorrect stream IDs parsed for this directory are generated in neo.rawio.spikeglxrawio.scan_files(), on line 405.
stream_name = f"{device_kind}{device_index}{stream_kind}"
This creates the string {imec}{0}{.ap} because device_index is 0, when it should be 1.
The reason that device_index is 0 is because of lines 388-398 in neo.rawio.spikeglxrawio.scan_files():
# TODO: handle one box case
info_list_imec = [info for info in info_list if info.get("device") != "nidq"]
unique_probe_tuples = {get_probe_tuple(info) for info in info_list_imec}
sorted_probe_keys = sorted(unique_probe_tuples)
probe_tuple_to_probe_index = {key: idx for idx, key in enumerate(sorted_probe_keys)}
for info in info_list:
if info.get("device") == "nidq":
info["device_index"] = "" # TODO: Handle multi nidq case, maybe use meta["typeNiEnabled"]
else:
info["device_index"] = probe_tuple_to_probe_index[get_probe_tuple(info)]
The problem is that you cannot enumerate the sorted probe keys on line 392 and expect that enumeration index to be the correct device index on line 398. In the case of folder per probe organization, where the the probe is imecN, the enumeration index will always be 0, but the device index should be N.
One way to solve this could be to parse the device index directly from the probe name or the info["device"] field, both of which should be "imecN":
info["device_index"] = int(re.split(r"(^[^\d]+)", info["device"])[-1])
If this index cannot be assigned directly to info["device_index"], it could at least be saved and used to offset the probe index from the probe_tuple_to_index dict. Someone with access to a wider variety of SpikeGLX outputs (non-folder-per-probe layouts, OneBox, etc) than I do should double-check!
This bug appears to have been introduced in #1608.
Environment:
- OS: Linux
- Python version
3.13.2 - Neo version
0.14.0 - NumPy version
2.1.3
Additional context
One other thing I noticed while tracking down this bug: On lines 530-534 in neo.rawio.spikeglxrawio.extract_stream_info, device and stream_kind are parsed twice each in quick succession: first from a regex, and then by just splitting the filename. Since they're obtained two different ways, might not hurt to assert that they match. Or just not bother recalculating them the second time, since they're already in hand? Nbd, just something that caught my eye.
Thank you!
Activity
zm711 commentedon Mar 16, 2025
@h-mayorquin and @alejoe91, you are probably best to handle this.
h-mayorquin commentedon Mar 16, 2025
Hi, @grahamfindlay
Would it be possible for you to share:
https://gist.github.com/h-mayorquin/b5abe6ae8e8484976c2d27e0bb04e2a2
Or can it be reproduced with any of the datasets that you previously shared?
https://gin.g-node.org/NeuralEnsemble/ephy_testing_data/src/master/spikeglx/multi_trigger_multi_gate
That way, we would avoid a regression like this again.
grahamfindlay commentedon Mar 18, 2025
Hi, @h-mayorquin
The error can be reproduced with several of the datasets I previously shared. Specifically, any of the ones that I labeled as using folder-per-probe organization in the README (i.e.,
-CI0,-CI2, and-CI4). See below for a minimal reproducible example that uses-CI0.The exact call that triggers the error is actually in
spikeinterface. Sorry, I totally failed to mention that. Example:The error is thrown in `spikeinterface.extractors.neoextractors.NeoBaseRecordingExtractor, line 218:
The reason is that
stream_idsis taken fromself.neo_reader.header["signal_streams"]["name"], and isarray(['imec0.ap', 'imec0.lf'], dtype='<U64').So, basically,
neowill happily create a reader and parse the header (incorrectly) without throwing an error, but thenspikeinterfaceraises the error when it tries to validate the user arguments against the incorrectly parsed metadata.So I don't think any changes should be required downstream in
spikeinterface.Hope that helps!
-Graham
zm711 commentedon Mar 19, 2025
Okay, so in this case I think we will also need to add a new test into our ci that parses the possible streams based on the files present and then ensures they actually exist. This is good to know Graham thanks.
h-mayorquin commentedon Mar 19, 2025
Hi, @grahamfindlay
This works just fine for me:
And it seems it detects the streams it should:
Can you help me see if I am making am mistake.
grahamfindlay commentedon Mar 19, 2025
This had me very confused for a minute!
It apparently matters whether the other probe is present: to load imec1, imec0 also has to be present. If you just
gin get-content 5-19-2022-CI0_g0_imec1, you will run into the error. But if you then dogin get-content 5-19-2022-CI0_g0_imec0, it will go away.grahamfindlay commentedon Mar 20, 2025
Incidentally, it also appears that you can elicit this error even if you have both probes downloaded and present in sibling directories, by passing the
probe_dirinstead of thegate_dirto theSpikeGLXRecordingExtractor, like so:From the documentation of the
SpikeGLXRecordingExtractorclass, it seems like this should be allowed:None of which implies that you couldn't just pass the probe folder to the constructor.
It's sort of the same situation with the docs for
SpikeGLXRawIO:Which would actually cause me to try passing the
probe_dirbefore trying thegate_dir, although shortly afterwards the same docstring does mention subfolders, which implies thegate_dir:h-mayorquin commentedon Mar 20, 2025
OK, I managed to reproduce this by moving only the folder containing the data from one probe and one gate and isolating it from the rest of your data:
and as you mention if you pass the data containing only one probe that triggers the error as well:
So with that we can work, but now that I have understood better I do have the question:
Is it that common that the data for probe 1 would be obtained without the accompanying data for probe 0? It would be good for me to understand how common this is before moving forward.
h-mayorquin commentedon Mar 20, 2025
This is the way it was before, but the problem is that sometimes people change the names of their files. Check this out:
#1606
There is an entry in the meta called
fileNamethat keeps the original name. Maybe it would be as simple as using that one.The fundamental problem is that the meta file does not contain the device_index. The calculation I'm using is here:
python-neo/neo/rawio/spikeglxrawio.py
Lines 379 to 386 in f05b92f
This approach is from @jenniferColonell, but it breaks down when you don't know if there were other ports, docks, or slots available, since those numbers aren't available when looking at the meta of a single probe.
I think the best solution is to add the fix using the original filename, but I'm a bit concerned that we're exchanging one type of brittleness (parsing
device_indexfrom the filename) for another (calculating it from all the available metadata). Both approaches have their failure modes, but I think we broke backward compatibility when we solved #1606, and it would be good to undo that regression.I will briefly discuss this in the meeting tomorrow and then push a fix. I've already got a test working on the CI with our current data. Let me know what do you think @grahamfindlay.
jenniferColonell commentedon Mar 20, 2025
I'm pretty sure I don't use slot/port/dock values anywhere, because they aren't generally important in the output data! Although they are included in the metadata (to trace hardware issues) they aren't meant to tell you the probe index for the run. Parsing the original filename is likely the best bet for handling this case -- unless it is actually data from the 3A era.
It's true -- if the user has changed the file names AND the name in the metadata, there will be an issue. But reading the data is vulnerable to all kinds of uncontrolled changes to the metadata.
grahamfindlay commentedon Mar 20, 2025
I think that the first case, where folder-per-probe organization was/is used to create the directory structure/names, but the data from the two probes actually do not exist in sibling directories, is probably very uncommon. In my case, it happened because one probe malfunctioned but continued to acquire data, and rather than keep the many terabytes of junk around, I just deleted the bad probe's data.
Being able to pass the
probe_dirinstead of thegate_diron the other hand, probably does make sense to support (or documentation could be changed to make it explicit thatgate_diris required, but this may be confusing or surprising for some users). Supportingprobe_dircould be as easy as just detecting it usingPath.parentto get thegate_dir. However, this would still maintain the limitation that you can't really load one probe without having the other around. So I think, ideally, it would be possible to just load one probe without parsing its sibling's meta files. After all, there isn't a fundamental reason I can think of why you should really have to touch, peek at, or even know about imec0 to load imec1 and work with it.I see what you mean about device index not being present in the metadata apart from the filename, and about trading one kind of brittleness for another. If it were me, and I had to pick, I would decline to support arbitrary file renaming, including stripping the device index, unless there was a really good reason for users to be doing it, especially if they could be un-doing it. I suppose you could check if the device index is present in the filename, and if not, fall back on the current method. I think the best solution sounds like the one you proposed: using the
fileNamemeta entry. If someone's going and arbitrarily renaming that, they're a psychopath! I don't think the official SpikeGLX suite of tools will even tolerate files whose names don't match what's in their metadata -- it's considered a failure of validation. But don't quote me on that.h-mayorquin commentedon Mar 20, 2025
Thanks for jumping in.
What I meant is that I once asked how to calculate the
probe_indexthat is included in the filename hereProbably my misunderstanding somewhere there.
h-mayorquin commentedon Mar 20, 2025
Thanks, @grahamfindlay that sounds reasonable to me. I just opened #1661 which should fix the error here.
Thanks for working with me through this fix.
jenniferColonell commentedon Mar 20, 2025
I haven't tried to unwind all of this issue...but wanted to add a couple notes:
(1) @grahamfindlay you're absolutely correct that it should be possible to handle selected probes within a run, with our without per-probe organization. All of the SGLX related tools (like CatGT) include a parameter to specify which probes to process/analyze. This will only become more important as people run experiments with more probes. It does depend on the file names being retained.
(2) @h-mayorquin I confirmed with Bill Karsh that the Filename tag in the metadata is the appropriate fallback if the names of the files have been changed (which we discourage!). Other SGLX tools do not parse that tag, they only parse the file names. That's good, because it means there generally won't be any reason to change that original filename in the metadata.
(3) If you are looking at historical data -- mostly from before 2018 -- it may be data from the 3A development probes. That hardware didn't allow for multiple probes per 'basestation', so there is no stream number in the filename. Here's Bill's official function for detecting a 3A vs. other probe type:
This function will be official way to fill in the modern part number for a probe and to set a flag for whether this is instead, 3A data. It parses metadata (kvp= key-value params).
This is a modern probe if it has tag imDatPrb_pn.
Otherwise this is 3A if it has any of {imProbeOpt , imProbeType , imDatPrbtype },
Otherwise this is unusable data.
QString PrbTbl::probeType( QString &pn, bool &is3A, const KVParams &kvp )
{
}
h-mayorquin commentedon Mar 20, 2025
@jenniferColonell the guidance on how to tell if the probe is from the oldest generation is very useful. Thanks again for your contribution here.
Any chance that
probe_indexcould be added to the metadata as a field? While it can be fetch from the original name if it needs be it seems better to have a canonical place for it on the metadata so there is a straightforward parsing of where the probe is within an experimental setup.Explicit (having it as a field) is better than implicit (having to parse it from the file name) and all of that.
jenniferColonell commentedon Mar 20, 2025
HI @h-mayorquin I had a chat with Bill about this, and he's not inclined to add to add another metadata item because the stream index really, truly is easily available from the original filename. I realize you're trying to predict users from every contingency -- but this one isn't too important. The stream index is in fact arbitrary -- if a user has renamed all their files and changed the metadata to match, it would be fine from an analysis perspective to simply create new stream indices.
The position in the experiments -- meaning which slot, port and dock -- are already recorded independently (as you know). All the properties which use the stream index during the experiment -- e.g. information from Pinpoint -- is stored in the metadata.
h-mayorquin commentedon Mar 20, 2025
Thanks for bringing it up @jenniferColonell .
ttngu207 commentedon Mar 25, 2025
I ran into the same problem. No filename or metadata changes.
Odd enough the same folder structure (ie "folder per probe") worked fine for me before. I suspect it may be related to the logic changes in #1608 (I'm just speculating here, I haven't looked further to confirm)
Looking forward to #1661 being merged to test this out again.
Thank you all!
h-mayorquin commentedon Mar 25, 2025
Thanks @ttngu207. Yes, this was a regression. It should be merged soon.
zm711 commentedon Mar 25, 2025
Yes @ttngu207 if you're willing to test out PR #1661 that would help us demonstrate this fixed the regression. It is always nice to have an end-user confirm that we've fixed the issue with real data vs our smaller testing files.
ttngu207 commentedon Mar 25, 2025
@zm711 , @h-mayorquin
Yes, I'm happy to test this out. I probably won't be able to share the data, but I can confirm if the fix works for the data, folder structure I'm working with. Will keep you posted
ttngu207 commentedon Mar 26, 2025
@zm711 @h-mayorquin
I'm confirming here that PR #1661 works! Some details:
spikeinterface0.102.2(usingneo` for spikeglx loading)neoversion0.14.0resulted in the error abovehttps://github.com/h-mayorquin/python-neo/tree/fix_spikeglx(neoversion0.15.0.dev0)zm711 commentedon Mar 26, 2025
Thanks @ttngu207 !
h-mayorquin commentedon Mar 26, 2025
I guess we can close this @grahamfindlay ?
zm711 commentedon Mar 27, 2025
I'll close. Feel free to reopen if needed.
ttngu207 commentedon Mar 28, 2025
Thanks @zm711 , any chance you can make a
0.14.1release soon?zm711 commentedon Mar 28, 2025
My hope is within the next two weeks :) The team is meeting next week so we will discuss, so as long as no one requests a delay!