Skip to content

MRG: Unify compute_source_psd, compute_source_psd_epochs - #5514

Merged
larsoner merged 24 commits into
mne-tools:masterfrom
dengemann:bstorm_rest
Nov 3, 2018
Merged

larsoner merged 24 commits into
mne-tools:masterfrom
dengemann:bstorm_rest

Conversation

@dengemann

Copy link
Copy Markdown
Member

This is the script I worked on during our small Biomag2018 sprint @larsoner @agramfort.
The analysis implements conceptually on my understanding of what @guiomar presented ruing our open science panel.
The good news is that something physiologically looking comes out.

The bad news is that I have to write "funny" code to make this happen and the data is quite difficult to analyze:

  1. As we know our default surface plotting is not prepared for heavy tailed data and moving distributions. So we have to crop stcs and compute local percentages, etc.

  2. I had to compute Welch PSDs in source space in an unelegant way in order to get relative source power. For comparison, our inverse PSD function gave quite meaningless results

  3. The data seems difficult. In many segments of the data the local PSD shows no pronounced alpha peak and the overall morphology of the PSD is driven by a few segments with clear oscillatory peaks in alpha and beta.

@bloyl have a look here and let me know what you think. We can work on this PR together in the next days and I can give you push access into my fork.

@guiomar I'd be curious if what I did here is about equivalent with your OMEGA analysis and if the results look plausible to you.

cc @dokato here you have finally the example + coreg info to do source localization of the Brainstorm resting state data.

Here are some plots from the script (and some interactively generated ones):

screenshot 2018-09-12 23 35 16

screenshot 2018-09-12 23 36 01

screenshot 2018-09-12 23 22 49

screenshot 2018-09-12 23 23 02

screenshot 2018-09-12 23 23 24

screenshot 2018-09-12 23 24 12

screenshot 2018-09-12 23 24 43

screenshot 2018-09-12 23 25 01

@bloyl

bloyl commented Sep 13, 2018

Copy link
Copy Markdown
Contributor

This looks more reasonable to me then what I was getting using compute_source_psd but it still doesn't seem to match what I see in brainstorm using a similar although not identical pipeline.

Below you'll find attached screen shots showing sensor PSD, topo and localized relative power, for 2 frequencies 8 and 35 Hz for both a brainstorm pipeline and this pipeline.

It seems like the sensor data is in agreement, but the source estimates look very different. In my opinion brainstorm more closely matches the topos at those frequencies.

@dengemann do you have access to brainstorm?

8 HZ

brainstorm_8hz_resting
mne_resting_8hz

35 Hz

brainstorm_35hz_resting
mne_resting_35hz

@bloyl

bloyl commented Sep 13, 2018

Copy link
Copy Markdown
Contributor

Does anyone have an example neuromag resting dataset?

perhaps this is a CTF specific issue?

@agramfort

agramfort commented Sep 13, 2018 via email

Copy link
Copy Markdown
Member

@guiomar

guiomar commented Sep 13, 2018

Copy link
Copy Markdown
Contributor

Hi Denis! Sorry, I'm quite busy till mid Oct, I'll carefully review it by then if it's not very late for you.
Cheers!!

@bloyl

bloyl commented Sep 13, 2018

Copy link
Copy Markdown
Contributor

@agramfort in this particular example neither pipelines marks bad segments.

Differences between pipelines.
-MNE has eog/ecg projects Brainstorm does not.
-MNE resampled at 150Hz and filterer (1,None). Brainstorm resampled at 300 and filtered at (0.3, 100)
-The overlap and windows for the welch PSDs are probably different.

The preprocessing pipelines in issue #5510 are more similar.

That being said I think the sensor PSDs do look very similar. The visual difference in the topos is mostly color map and edge interpolation I think.

The big difference to my eye is how different the source analysis looks give the similarities in the sensor profiles.

Look at the 8 Hz peak. Both topos (MNE and brainstorm) show 2 hot spots on the right hemisphere. Brainstorm puts the activity in roughly the right spot, middle of the right hemisphere, where as MNE has bilateral occipital activation.

@agramfort

Copy link
Copy Markdown
Member

ok I see. Yes https://user-images.githubusercontent.com/5585821/45498042-3c7a2900-b747-11e8-9bd1-c9ac2a388ae9.png is weird.

did you check with plot_alignment?

can we mess up the compensation info?

@dengemann

Copy link
Copy Markdown
Member Author

@bloyl do you want to suggest edits here that approximate the pipelines as much as possible? I think this is what we have to do.

@dengemann

Copy link
Copy Markdown
Member Author

Hi Denis! Sorry, I'm quite busy till mid Oct, I'll carefully review it by then if it's not very late for you.
Cheers!!

@guiomar Hi Guiomar, sure, no problem! Showing a cross-toolbox replication of analyses is still a good thing mid October. It is only important that we make progress before the next Biomag. Otherwise we cannot do our next panel session :)

@dengemann

Copy link
Copy Markdown
Member Author

@guiomar @agramfort @larsoner @SherazKhan I am just wondering if any licensing issue speaks against shipping one or two OMEGA datasets via MNE (making it download if user clicks to agree to license). This would probably be the best.

@bloyl

bloyl commented Sep 13, 2018

Copy link
Copy Markdown
Contributor

plot_alignment looks fine.

Regarding gradient compensations. It is worth noting that the resting (comp=3) and empty room(comp=0) have difference gradient compensations on.

That being said If i set grad_comp to either 3 or 0 ( raw_.apply_gradient_compensation) I don't see any marked difference in source localization.

@dengemann

Copy link
Copy Markdown
Member Author

plot_alignment looks fine.

Regarding gradient compensations. It is worth noting that the resting (comp=3) and empty room(comp=0) have difference gradient compensations on.

That being said If i set grad_comp to either 3 or 0 ( raw_.apply_gradient_compensation) I don't see any marked difference in source localization.

We need to systematically debug this. In principle we should be able to do logically exactly the same.

@bloyl

bloyl commented Sep 13, 2018

Copy link
Copy Markdown
Contributor

@dengemann : I don't know about licensing on the omega datasets.
I do know that this issue exists there as well as that is the data I looked at initially.

Do you know if this code/approach works with HCP resting data?

@dengemann

dengemann commented Sep 13, 2018 via email

Copy link
Copy Markdown
Member Author

@dengemann

dengemann commented Sep 13, 2018 via email

Copy link
Copy Markdown
Member Author

@bloyl

bloyl commented Sep 18, 2018

Copy link
Copy Markdown
Contributor

OK I think I have sorted the issue.

The problem is that both the new example and compute_source_psd return values in decibels. If i remove the psd = np.log10(psd) from the example and remove

psd = 10 * np.log10(psd)

I get much more biological looking maps that match much better with brainstrom figures of relative power.

While it doesn't make perfect sense. IE logs shouldn't change ordering, I think the combination of values much less than 1 and some scaling (maybe?) in the display code causes the behavior we are seeing.

Does this make sense to others?

@larsoner

Copy link
Copy Markdown
Member

Can you push a commit to how branch so we can see the effect?

Yes the log will have a compressive effect so it will make differences less drastic

@bloyl

bloyl commented Sep 18, 2018

Copy link
Copy Markdown
Contributor

I don't think i have push access to @dengemann 's branch. Even if i did i don't know how to push to it. Can i just push to the checked-out out PR?

@dengemann

dengemann commented Sep 18, 2018 via email

Copy link
Copy Markdown
Member Author

@bloyl

bloyl commented Sep 18, 2018

Copy link
Copy Markdown
Contributor

@dengemann thx. I'll try to figure out how to push right to the PR.

@larsoner in the mean time here is a modified example the demonstrates the difference. I just commented out psd = np.log10(psd). notice that at least at 35hz it not only compresses the scale but changes where the maxima are. This also might be tied into displaying relative powers.
https://gist.github.com/bloyl/c3ce58a506bd5d03d789c5079eafd0de

the output at 35hz is below. notice it better matches the epoch topos and the brainstorm responses from above.

screenshot from 2018-09-18 15-41-17

In thinking more about it the scaling issues (ie changing spatial peaks), might be our computing relative power in decibels?

@dengemann

Copy link
Copy Markdown
Member Author

@dengemann thx. I'll try to figure out how to push right to the PR.

You should have push access now.

Regarding the image you share what made me suspicious when I played with the data and scrolled through frequencies was that everything seemed to land in the visual cortex here regardless of the frequency band, with some small fluctuations.

But generally speaking, the OMEGA/Brainstorm results should be our guide.

@larsoner

larsoner commented Sep 18, 2018 •

Copy link
Copy Markdown
Member

Yes the "baseline correction" applied will matter

Keep in mind you never actually push to a PR, this is loose terminology. You push commits to branches. Requests to merge commits from one branch to another form PRs. So basically you need to push commits to whatever branch is listed in this PR, namely dengemann:bstorm_rest

@codecov

codecov Bot commented Sep 19, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #5514 into master will increase coverage by 0.01%.
The diff coverage is 91.33%.

@@            Coverage Diff            @@
##           master   #5514      +/-   ##
=========================================
+ Coverage   88.49%   88.5%   +0.01%     
=========================================
  Files         369     369              
  Lines       69074   69115      +41     
  Branches    11651   11654       +3     
=========================================
+ Hits        61124   61172      +48     
+ Misses       5090    5087       -3     
+ Partials     2860    2856       -4

@bloyl

bloyl commented Sep 19, 2018

Copy link
Copy Markdown
Contributor

ok I added a 2nd example showing what i think are some issues with using decibels for source psds.

the script generates this image showing that computing relative power using DB PSDs yeilds large peaks on the ventral surface.
screenshot from 2018-09-18 20-33-54
Also notice the lack of occipital beta in the DB version compared to the non DB version.

If you run the other example, you also see that the non-db version better matches the brainstorm output at the selected freqs (8 and 35 hz)

@agramfort

agramfort commented Sep 19, 2018 via email

Copy link
Copy Markdown
Member

@larsoner larsoner changed the title [WIP][ENH] First resting state example following OMEGA pipeline with Brainstorm data [MRG] First resting state example following OMEGA pipeline with Brainstorm data Oct 26, 2018
@larsoner larsoner added this to the 0.17 milestone Oct 26, 2018
@larsoner

Copy link
Copy Markdown
Member

I've simplified, optimized, and unified the two scripts to get the following output:

https://9957-1301584-gh.circle-artifacts.com/0/html/auto_examples/datasets/plot_brainstorm_rest_data.html

@bloyl @dengemann if you can both take a quick look and let me know if it still makes sense to you then I think we can merge this for 0.17.

@larsoner

Copy link
Copy Markdown
Member

... I noticed that there is something fishy where the result changes depending on whether you use MNE or dSPM (even when using `pick_ori='normal'), which should not be the case in this example since we use individual-source-point normalization that should cancel out the spatial normalization.

But we should maybe fix that separately.

@larsoner

larsoner commented Nov 2, 2018

Copy link
Copy Markdown
Member

Okay from talking to @dengemann we'll split this into two PRs:

  1. This one, which has API unification / bug fixes, and unit tests
  2. A follow-up one with an example -- hopefully the one that was just removed, assuming it can be replicated.

@dengemann is going to try to follow the now-removed example with mne-hcp to see if it works there, as it does here with Neuromag + OPM data (and what @ktavabi has seen with his data). So hopefully we can converge on that pipeline for 0.17 but no rush if we don't. In the meantime, these fixes will be nice to have for 0.17.

@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.

I annotated the major changes so that reviewers can understand more easily. There are also a number of very minor doc-building fixes here that are also part of #5649.

Comment thread doc/whats_new.rst
- Fix :func:`mne.io.Raw.plot_proj_topomap` by `Joan Massich`_
- Fix :func:`mne.io.Raw.plot_projs_topomap` by `Joan Massich`_

- Fix bug in :func:`mne.minimum_norm.compute_source_psd` where the ``stc.times`` output was scaled by 1000, by `Eric Larson`_

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.

Fix: .times was wrong before

stc.data = source_data
out = stc
if return_sensor:
out = (out, evoked)

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.

Fix: make compute_source_psd wrap to compute_source_psd_epochs. We get multitaper estimation for free, and simplify the codebase.

Enhancement: have return_sensor output so you get the Evoked and SourceEstimate.

if dB is None:
dB = True
warn('dB=True by default in 0.17 but will change to False in 0.18, '
'set it explicitly to avoid this warning', DeprecationWarning)

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.

Fix: Deprecate dB=True to unify with compute_source_psd_epochs (which does not even have a dB argument and outputs non-dB PSDs)

logger.info('Using %d tapers with bandwidth %0.1fHz %s'
% (n_tapers, bandwidth, extra))
if isinstance(bandwidth, string_types):
bandwidth = '%s windowing' % (bandwidth,)

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.

Enhancement: allow MTM estimation to take a string, meaning "use standard estimation". The advantage of this is that in the long run it will greatly simplify our code paths, especially if we go to implement CUDA-based multitaper estimation.

Comment thread mne/source_estimate.py
Returns
-------
stc : SourceEstimate | VectorSourceEstimate
The modified stc.

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.

Enh: Add missing SourceEstimate method

Comment thread mne/tests/test_surface.py
(read_info(fname_kit_raw), 304, 'KIT'),
(read_info(fname_bti_raw), 304, 'Magnes'),
(read_info(fname_ctf_raw), 348, 'CTF'),
(read_info(fname_ctf_raw), 343, 'CTF'),

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.

Enh: Improve CTF helmet position based on information from this thread. (And it looks more correct / no more head collisions in BST data, too.)

Comment thread mne/viz/_3d.py
title : str | None
Title for the figure. If None, the subject name will be used.

.. versionadded:: 0.17.0

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.

Enh: Allow plot_source_estimates to take a title kwarg as an alternative to auto-setting it

Comment thread mne/viz/topomap.py
for l in layout:
if not isinstance(l, Layout):
raise TypeError('All entries in layout list must be of type '
'Layout, got type %s' % (type(l),))

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.

Fix: Give more informative error

@larsoner

larsoner commented Nov 2, 2018

Copy link
Copy Markdown
Member

@dengemann is okay with the two-PR plan. So @agramfort if you're happy I think this should be good to merge.

Once this is in, I'll open a second PR where @dengemann and @bloyl can report back on how the Brainstorm pipeline works on their data

@larsoner larsoner changed the title [MRG] First resting state example following OMEGA pipeline with Brainstorm data [MRG] Unify compute_source_psd, compute_source_psd_epochs Nov 3, 2018
@larsoner larsoner changed the title [MRG] Unify compute_source_psd, compute_source_psd_epochs MRG: Unify compute_source_psd, compute_source_psd_epochs Nov 3, 2018
@agramfort

Copy link
Copy Markdown
Member

diff was a pain to read but LGTM.

@larsoner

larsoner commented Nov 3, 2018

Copy link
Copy Markdown
Member

diff was a pain to read but LGTM.

I tried to make your life a little bit easier with guiding review comments, but I'm not surprised it was still unpleasant. Thanks for looking

Since @dengemann said he was okay with this refactoring, okay to merge this?

@agramfort

agramfort commented Nov 3, 2018 via email

Copy link
Copy Markdown
Member

@larsoner
larsoner merged commit 9382ee8 into mne-tools:master Nov 3, 2018
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.

7 participants