-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: handle temporal discontinuities in Neuralynx .ncs
files
#12279
Conversation
First, to cut
I would make sure first that |
Thanks! Indeed, |
3939927
to
3dea552
Compare
@larsoner Fixed the bug on my end and added commits to detect gaps in Neo segments, infer missing samples, add them as 0's, and annotate as Example gapsFor context, this is a ~40-min recording of story listening. I cannot comment on whether these look like a typical Neuralynx ring buffer error or not since I have never eye-balled these before. But it seems they can be burst-like, interspersed with a very short (1 or 2 sample) valid samples (this is a 4Khz dataset, so timescale is stretched out to microsecond precision in plots). And these are very short, mostly microsecond-level gaps in the case of my datasets. Creating annotationsRight now, I add the all information for annotations in gap_annot = raw._raw_extras[0]["gap_annotations"]
annot = mne.Annotations(
onset=gap_annot["onset"],
duration=gap_annot["duration"],
description=gap_annot["descriptions"],
)
raw.set_annotations(annot) This requires the user to set annotations. Not sure, if it is preferred that annotations should be added under the hood. |
Yes I think the gaps should be automatically annotated. Then the user can decide what to do with them (including removing them if they want) |
4497eb9
to
a32ff97
Compare
@larsoner Example call and feedback on a local dataset with gaps detected:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me so far! One idea for a potential refactoring and a couple small cleanup ideas
@larsoner updated It seems CIs are failing due to some neo import issues (via Otherwise, a recap: Created 3 gaps (130 invalid samples) in 2 .ncs files, then read them in with Also added a line to |
@requires_testing_data | ||
def test_neuralynx_gaps(): | ||
"""Test gap detection.""" | ||
# ignore files with no gaps |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is one problem
# ignore files with no gaps | |
pytest.importorskip("neo") | |
# ignore files with no gaps |
but a worse problem is that it seems like neo
is not being installed on any of those CIs. I'll push a commit
Feel free to take out of draft mode if you're done then ping me @KristijanArmeni and I'll merge assuming CIs are green (other than the Windows pip-pre one, which is having unrelated issues) |
Thanks and done! @larsoner
I pulled this latest branch locally and |
Some are, for example:
is because I changed environment.yml in this PR to add neo, but used the wrong name. After I push a commit to fix this, all CIs should come back green other than windows pip-pre. |
.ncs
files.ncs
files
Yeah, looks like the CIs are green, now @larsoner. Thanks! (This branch still needs a merge from main, but other than that it seems good) |
Updated to latest |
* upstream/main: BUG: handle temporal discontinuities in Neuralynx `.ncs` files (mne-tools#12279) MAINT: Work around bad SciPy nightly wheels (mne-tools#12317) fix 404 link on devel landing page (mne-tools#12316) Switch from `epoch_data` to `data` for TFR array functions (mne-tools#12308) [pre-commit.ci] pre-commit autoupdate (mne-tools#12307) fix icon link colors (mne-tools#12301) Bump actions/download-artifact from 3 to 4 (mne-tools#12304) Bump github/codeql-action from 2 to 3 (mne-tools#12303) Bump actions/upload-artifact from 3 to 4 (mne-tools#12302)
* upstream/main: MAINT: Use towncrier for release notes (mne-tools#12299) MAINT: More [ci skip] MAINT: Add bot entry [ci skip] BUG: handle temporal discontinuities in Neuralynx `.ncs` files (mne-tools#12279) MAINT: Work around bad SciPy nightly wheels (mne-tools#12317)
…ools#12279) Co-authored-by: Eric Larson <[email protected]>
Reference issue
Addresses #12247
What does this implement/fix?
Tries do deal with temporal gaps in
.ncs
files. Conceptually,read_raw_neuralynx()
now checks for any temporal differences between consecutiveneo.Block[0].segments
start/stop times and if any of those are larger than the sampling period, call it a gap (i.e. missing > 1 sample). Then, infer the number of missing samples for each gap based on gap times, do the bookkeeping, generate arrays of 0's for each gap data segment and insert them between respective valid segments.I can read-in a large dataset locally (and
test_neuralyx.py
passes), but nowraw.plot()
will choke (see more below).Example behavior on a local dataset where largest discontinuity seems to be 30 msecs (sampling rate = 4kHz).
Note, that this assumes that all segments returned by Neo are valid data. I tried asking about his in neo github, but haven't heard back.
Additional information:
raw.plot() breaks
Essentially, calling .plot with some selection
raw.plot(start=10, duration=2)
will choke with the output below.My hunch is the reloading of chunks for plotting somehow interacts with my implementation, where
all_data
array constructed internally from neo segments + times of inferred gaps, won't match the number of samples of the array generated asblock = np.zeros(n_channels, stop-start)
.Not sure how to best troubleshoot this (I can't share the data I use locally to reproduce and the testing dataset can't be used for this since it's continuous), but hopefully there's something obvious I'm missing?