Skip to content

[ENH] Add support for importing UCL/FIL OPM data - #11366

Merged
larsoner merged 20 commits into
mne-tools:mainfrom
georgeoneill:main
Jan 6, 2023
Merged

larsoner merged 20 commits into
mne-tools:mainfrom
georgeoneill:main

Conversation

@georgeoneill

@georgeoneill georgeoneill commented Dec 13, 2022 •

Copy link
Copy Markdown
Contributor

Reference issue

Closes #11257.

What does this implement/fix?

  • Adds means to import data as a RawUCL RawFIL object.
  • Adds a test to check data when imported matches established MATLAB-style importers
  • Updates testing dataset version to include the FIL data.

Open question

This PR has used UCL instead of FIL, to avoid confusion with FIF. However the corresponding mne-test-data uses FIL. Should we use FIL for consistency here?

@welcome

welcome Bot commented Dec 13, 2022

Copy link
Copy Markdown

Hello! 👋 Thanks for opening your first pull request here! ❤️ We will try to get back to you soon. 🚴🏽‍♂️

@larsoner

Copy link
Copy Markdown
Member

This PR has used UCL instead of FIL, to avoid confusion with FIF

I'm not too worried about FIL vs FIF in terms of confusion or completion. If FIL is a better name in principle then let's use it

@georgeoneill

Copy link
Copy Markdown
Contributor Author

Thanks, will change to FIL.

@larsoner

Copy link
Copy Markdown
Member

Pushed a tiny commit to fix a doc issue, and since I have write access to the repo we'll no longer need to approve CI workflow runs (first-time committers need CI approvals as a GH security measure)

@larsoner

Copy link
Copy Markdown
Member

Argh, GH is degraded at the moment:

Investigating - We are investigating reports of degraded performance for Git Operations and Pull Requests.
Dec 13, 2022 - 17:39 UTC

We'll have to wait maybe until tomorrow to see if there are legitimate failures.

@georgeoneill

georgeoneill commented Dec 13, 2022 •

Copy link
Copy Markdown
Contributor Author

Sorry, the class is RawFIL, not RawUCL! I'll make the changes in the documentation.

@larsoner

Copy link
Copy Markdown
Member

I wouldn't worry about compat/old yet -- we're releasing 1.3 this week, which means we'll bump our dependency minimums, and simplify_cells is available in SciPy 1.5, which is probably what we'll bump to.

If you really want to fix it now, another option is to not simplify them, or to use pymatreader, which might already do some simplification itself? But personally I would just wait...

@georgeoneill

Copy link
Copy Markdown
Contributor Author

I hadn't come across pymatreader before. It looks like it is a simple drop-in.

@georgeoneill

Copy link
Copy Markdown
Contributor Author

Apologies for the chaos. This is my first time using a CI'd repo, but It finally works. It appears to pass all checks on my local fork now. Unsure why the default test on here is failing.

@larsoner

Copy link
Copy Markdown
Member

Apologies for the chaos. This is my first time using a CI'd repo, but It finally works

Awesome! No need to apologize, CIs are almost always red for a while, even for experienced contributors.

It appears to pass all checks on my local fork now. Unsure why the default test on here is failing.

That's CircleCI -- as a security measure they won't run your commit because you've never committed to MNE-Python before. I just pushed an empty commit that should get all CIs running

@larsoner

Copy link
Copy Markdown
Member

@georgeoneill okay if I push a commit to add a dataset downloader for #11257 (comment) in this PR? Then you can add a hopefully short tutorial/example that shows how to get some auditory responses using your reader? I think it's the last major thing we'd want to add to this PR.

@georgeoneill

Copy link
Copy Markdown
Contributor Author

Of course!

@larsoner

Copy link
Copy Markdown
Member

Okay I:

  1. Adapted your top comment to say Closes #11257. so that it closes when we merge this (by GitHub magic 🪄)
  2. Added the stationary auditory dataset downloader and associated infrastructure
  3. Changed the logic in the reading code to use pathlib.Path because it's the way of the future (compared to os.path) and fixed a bug in the process on my system
  4. Added multi-reader support to mne.io.read_raw because the .bin extension is shared between FIL and Artemis
  5. Changed a while loop to a np.where -- it's shorter, more readable (IMO) and should be plenty fast even if there are hundreds of channels. There are a few other minor python-izations in there as well -- feel free to take a look at the commit if you're interested. Normally I would have left suggestions as review comments, but I was digging in the code anyway so just made the changes.

With this I think we are very close to being able to merge! What's left:

  1. @georgeoneill can you add content to the tutorial now? I just put in something very basic. To me the ideal first version would show some auditory evoked response. But if we can't get that far in this PR because we need something like homogeneous field correction (and that's hard to add), we can wait and just put in a placeholder for now.
  2. @georgeoneill @neurofractal feel free to update datasets_index.rst if you are so inclined -- you can see I was a bit lazy about it :)
  3. We need to add a doc/changes/latest.inc entry, but see below ...

FYI we are releasing 1.3 this Friday, so we should hold off on merging this (and adding a latest.inc entry) until next week. That gives us plenty of time to make the OPM tutorial more complete by the time we want to release 1.4 (probably early spring).

@larsoner

Copy link
Copy Markdown
Member

FYI if you click the "Check the rendered docs here!" you can see:

and other stuff like the rendered API pages.

@larsoner larsoner added this to the 1.4 milestone Dec 14, 2022
@georgeoneill

Copy link
Copy Markdown
Contributor Author

Hi @larsoner, that quite a lot of infrastructure you've put in. Thanks!

@neurofractal and I should be able to put something together for a basic, non-HFC, documentation until such a time that its been inplimented. Filters can do the heavy lifting for the time being. thumb's up emoji.

Also happy to wait for to merge after 1.3 release. Unless we get a tutorial together before then, of course.

@neurofractal

Copy link
Copy Markdown
Contributor

Lovely - happy to help write the tutorial and have a play around with the new code!

larsoner and others added 2 commits January 3, 2023 15:02
* upstream/main:
  [ENH, MRG] Add info save method (#11401)
  MAINT: Sanitize name lists (#11399)
  [ENH] Read annotation duration from SNIRF files. (#11397)
  1.4dev0 deprecations and TODOs (#11391)
  MRG: Use MNE-misc-data dataset version 0.24 (#11389)
  update installer links (#11387)
  bump version to dev
  release 1.3 (#11384)
  [BUG][MRG] Fix surface finding for mne.viz.Brain (#11361) [circle deploy]
  Replaced package mention ESINet by invertmeeg (#11381)
  Handle None-type for 'sex' in raw subject info before writing EDF (#11375)
  FIX: Safer numba import (#11377)
  [DRAFT][DOC] Adapt LCMV tutorial (#11378)
  [DOC] Adding make clean to contributing guide (#11379)
  Fix lingering topomap api (#11371)
  MAINT: Bump to 20.04 (#11374)
  Improve ICA component selection with the argument picks (#11369)
  [MRG] Fix missing PR number in changelog (#11365)
@georgeoneill

Copy link
Copy Markdown
Contributor Author

I've just updated the tutorial to do some synthetic gradiometry. It appears to work. Feel free to edit how the data is visualised as I suspect my plotting method isn't very memory efficient?

One thing which concerns me is when checking the shielding factor (measured as $20log_{10}(\frac{PSD_{1}}{PSD_{2}})$ after regression in both MNE (top plot) and SPM (bottom plot) I was suprised how different the two were. I'm not entirely up to speed with the minutae of MNE's spectral calculations (I think its welch's method vs SPM using an FFT). But I'm not sure that explains why the shielding factor doesn't settle to 0 after the ~5 Hz point).

image
image

@larsoner

larsoner commented Jan 5, 2023

Copy link
Copy Markdown
Member

I'll take a look and push a commit with a few plotting tweaks

@larsoner larsoner left a comment •

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.

FYI the > 5 Hz PSD stuff seemed to mostly have to do with the IIR .filter -- the roll-off of the default IIR filter isn't super sharp so there was enough content there for the regression to capture it. Switching to our default FIR implementation seems to help.

I also .crop the data now to ~2 minutes. We need to keep examples < 1.5 GB of data usage to keep CircleCI happy building the docs -- and also it's not a bad idea from an example/tutorial efficiency standpoint. Hopefully this will still allow some ERFs to be obtained eventually. If this is too difficult for you to work with, feel free to revert the .crop for now and I can figure out how to save memory once everything works fully

picks = pick_types(raw.info, eeg=True)
assert_array_equal(picks, np.arange(4, 12))
norms = np.linalg.norm(raw.get_data(picks), axis=1)
raw_reg, _ = regress_artifact(raw, picks=picks, picks_artifact='eog')

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.

@wmvanvliet this is where in the OPM example it used to fail during the .apply stage, saying some of the picks (e.g., 94) were too large for info, which only contained 86 channels following the pick_info call

Comment on lines -194 to +193
all_picks = np.unique(np.hstack((picks, picks_artifact)))
self.info_ = pick_info(inst.info, all_picks)
self.info_ = inst.info.copy()

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.

@wmvanvliet this was buggy -- if picks and/or picks_regression were numerical and did not begin at 0 and stay contiguous, this could lead to incorrect channels being picked at the apply stage. Imagine you had 5 MEG channels, then 4 EEG, then 1 EOG, and your picks are np.arange(5, 9) (i.e., EEG but numerical), the pick_info of the picks+picks_artifact would reduce info_ to only have 5 channels, but the numerical .picks attribute would be np.arange(5, 9), which is clearly out of range.

We could probably get away with some searchsorted stuff, but it would be a bit complex and annoying to get right. It seems easy/okay enough just to have some extra info['chs'] entries that won't ever be used...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Huh. In my mind, picks would always be a list of str. Didn't I enforce that somewhere?

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.

Eventually some sanity check converts them to str but by that time the bug has already occurred. But in the object itself they were still stored as a single string (which generally worked) or as an ndarray of int (which didn't), depending on what was passed originally to the constructor

I agree storing the ordered channel names and using pick_channels(...,ordered=True) would be a good way to avoid the bug and avoid needing to keep am complete info

@georgeoneill

Copy link
Copy Markdown
Contributor Author

A basic tutorial is complete, for now. The final evoked plot is passable for now (you can see the M100 at least), but the real denoising magic happens when HFC is applied.

@georgeoneill

georgeoneill commented Jan 5, 2023 •

Copy link
Copy Markdown
Contributor Author

FYI the > 5 Hz PSD stuff seemed to mostly have to do with the IIR .filter -- the roll-off of the default IIR filter isn't super sharp so there was enough content there for the regression to capture it. Switching to our default FIR implementation seems to help.

Interesting, we use an IIR (6th order Butterworth) in our SPM code, it seems to behave nicely for us. Filters are strange.

I also .crop the data now to ~2 minutes. We need to keep examples < 1.5 GB of data usage to keep CircleCI happy building the docs -- and also it's not a bad idea from an example/tutorial efficiency standpoint. Hopefully this will still allow some ERFs to be obtained eventually. If this is too difficult for you to work with, feel free to revert the .crop for now and I can figure out how to save memory once everything works fully

Thanks, this is fine for now. If we struggle later, we have the power to do provide a downsampled (and anti-aliased) version of the OPM data (in raw binary format). 6 kHz is unweildy at the best of times, especially when QuSpin rate their sensors' sensitivity rolls off at > 130 Hz!

@larsoner

larsoner commented Jan 6, 2023

Copy link
Copy Markdown
Member

Just pushed a tiny commit to set the Tutorial gallery thumbnail and get CIs to run, marking for merge when green. Thanks for this awesome addition @georgeoneill , hopefully the first of many 😄

@larsoner

larsoner commented Jan 6, 2023

Copy link
Copy Markdown
Member

If we struggle later, we have the power to do provide a downsampled (and anti-aliased) version of the OPM data (in raw binary format)

... and I meant to add that we should probably do this in some next iteration that adds HFC and/or head movement correction. If you can get me the files at some point, I can add them. But at this point there's no rush I think

@larsoner
larsoner enabled auto-merge (squash) January 6, 2023 20:33
@larsoner
larsoner merged commit d757026 into mne-tools:main Jan 6, 2023
@welcome

welcome Bot commented Jan 6, 2023

Copy link
Copy Markdown

🎉 Congrats on merging your first pull request! 🥳 Looking forward to seeing more from you in the future! 💪

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: Support reading FIL-OPM-MEG data

5 participants