Analysis for one subject with tSSSΒΆ

Run the analysis.

import os.path as op
import sys

import numpy as np

import mne

sys.path.append(op.join('..', '..', 'processing'))
from library.config import (study_path, meg_dir, ylim,
                            set_matplotlib_defaults)  # noqa: E402

set_matplotlib_defaults()

Configuration

subjects_dir = op.join(study_path, 'subjects')

subject = "sub003"
subject_dir = op.join(meg_dir, subject)
st_duration = 1

Continuous data

raw_fname = op.join(study_path, 'ds117', subject, 'MEG', 'run_01_raw.fif')
raw_filt_fname = op.join(subject_dir,
                         'run_01_filt_tsss_%d_raw.fif' % st_duration)

raw = mne.io.read_raw_fif(raw_fname)
raw_filt = mne.io.read_raw_fif(raw_filt_fname)

Out:

Opening raw data file /tsi/doctorants/data_gramfort/dgw_faces_reproduce/ds117/sub003/MEG/run_01_raw.fif...
    Read a total of 8 projection items:
        mag_ssp_upright.fif : PCA-mags-v1 (1 x 306)  idle
        mag_ssp_upright.fif : PCA-mags-v2 (1 x 306)  idle
        mag_ssp_upright.fif : PCA-mags-v3 (1 x 306)  idle
        mag_ssp_upright.fif : PCA-mags-v4 (1 x 306)  idle
        mag_ssp_upright.fif : PCA-mags-v5 (1 x 306)  idle
        grad_ssp_upright.fif : PCA-grad-v1 (1 x 306)  idle
        grad_ssp_upright.fif : PCA-grad-v2 (1 x 306)  idle
        grad_ssp_upright.fif : PCA-grad-v3 (1 x 306)  idle
    Range : 222200 ... 765599 =    202.000 ...   695.999 secs
Ready.
Current compensation grade : 0
Opening raw data file /tsi/doctorants/data_gramfort/dgw_faces_reproduce/MEG/sub003/run_01_filt_tsss_1_raw.fif...
    Range : 222200 ... 765599 =    202.000 ...   695.999 secs
Ready.
Current compensation grade : 0

Filtering 04. Filter using MNE-python.

raw.plot_psd(n_fft=8192, average=False, xscale='log', show=False)
raw_filt.plot_psd(n_fft=8192, average=False, xscale='log')
  • ../../_images/sphx_glr_plot_tsss_analysis_001.png
  • ../../_images/sphx_glr_plot_tsss_analysis_002.png

Out:

Effective window size : 7.447 (s)
Effective window size : 7.447 (s)
Effective window size : 7.447 (s)
Effective window size : 7.447 (s)
Effective window size : 7.447 (s)
Effective window size : 7.447 (s)

Events 02. Extract events from the stimulus channel. Epochs 06. Construct epochs.

eve_fname = op.join(subject_dir, 'run_01-eve.fif')
epo_fname = op.join(subject_dir, '%s-tsss_%d-epo.fif' % (subject, st_duration))

events = mne.read_events(eve_fname)
fig = mne.viz.plot_events(events, show=False)
fig.suptitle('Events from run 01')

epochs = mne.read_epochs(epo_fname)
epochs.plot_drop_log()
  • ../../_images/sphx_glr_plot_tsss_analysis_003.png
  • ../../_images/sphx_glr_plot_tsss_analysis_004.png

Out:

Reading /tsi/doctorants/data_gramfort/dgw_faces_reproduce/MEG/sub003/sub003-tsss_1-epo.fif ...
    Read a total of 1 projection items:
        Average EEG reference (1 x 70) active
    Found the data of interest:
        t =    -200.00 ...    2900.00 ms
        0 CTF compensation matrices available
681 matching events found
Created an SSP operator (subspace dimension = 1)
681 matching events found
Created an SSP operator (subspace dimension = 1)
1 projection items activated

Evoked responses 07. Evoked data

ave_fname = op.join(subject_dir, '%s-tsss_%d-ave.fif' % (subject, st_duration))
evoked = mne.read_evokeds(ave_fname)
famous_evo, scrambled_evo, unfamiliar_evo, contrast_evo, faces_evo = evoked[:5]

Out:

Reading /tsi/doctorants/data_gramfort/dgw_faces_reproduce/MEG/sub003/sub003-tsss_1-ave.fif ...
    Read a total of 1 projection items:
        Average EEG reference (1 x 70) active
    Found the data of interest:
        t =    -200.00 ...    2900.00 ms (famous)
        0 CTF compensation matrices available
        nave = 234 - aspect type = 100
Projections have already been applied. Setting proj attribute to True.
No baseline correction applied
    Read a total of 1 projection items:
        Average EEG reference (1 x 70) active
    Found the data of interest:
        t =    -200.00 ...    2900.00 ms (scrambled)
        0 CTF compensation matrices available
        nave = 211 - aspect type = 100
Projections have already been applied. Setting proj attribute to True.
No baseline correction applied
    Read a total of 1 projection items:
        Average EEG reference (1 x 70) active
    Found the data of interest:
        t =    -200.00 ...    2900.00 ms (unfamiliar)
        0 CTF compensation matrices available
        nave = 236 - aspect type = 100
Projections have already been applied. Setting proj attribute to True.
No baseline correction applied
    Read a total of 1 projection items:
        Average EEG reference (1 x 70) active
    Found the data of interest:
        t =    -200.00 ...    2900.00 ms (contrast)
        0 CTF compensation matrices available
        nave = 146 - aspect type = 100
Projections have already been applied. Setting proj attribute to True.
No baseline correction applied
    Read a total of 1 projection items:
        Average EEG reference (1 x 70) active
    Found the data of interest:
        t =    -200.00 ...    2900.00 ms (faces)
        0 CTF compensation matrices available
        nave = 470 - aspect type = 100
Projections have already been applied. Setting proj attribute to True.
No baseline correction applied
    Read a total of 1 projection items:
        Average EEG reference (1 x 70) active
    Found the data of interest:
        t =    -200.00 ...    2900.00 ms (faces_eq)
        0 CTF compensation matrices available
        nave = 211 - aspect type = 100
Projections have already been applied. Setting proj attribute to True.
No baseline correction applied
    Read a total of 1 projection items:
        Average EEG reference (1 x 70) active
    Found the data of interest:
        t =    -200.00 ...    2900.00 ms (scrambled_eq)
        0 CTF compensation matrices available
        nave = 211 - aspect type = 100
Projections have already been applied. Setting proj attribute to True.
No baseline correction applied

Faces

faces_evo.plot(spatial_colors=True, gfp=True, ylim=ylim,
               window_title='Faces %s' % subject)
../../_images/sphx_glr_plot_tsss_analysis_005.png

Famous

famous_evo.plot(spatial_colors=True, gfp=True, ylim=ylim,
                window_title='Famous %s' % subject)
../../_images/sphx_glr_plot_tsss_analysis_006.png

Scrambled

scrambled_evo.plot(spatial_colors=True, gfp=True, ylim=ylim,
                   window_title='Scrambled %s' % subject)
../../_images/sphx_glr_plot_tsss_analysis_007.png

Unfamiliar

unfamiliar_evo.plot(spatial_colors=True, gfp=True, ylim=ylim,
                    window_title='Unfamiliar %s' % subject)
../../_images/sphx_glr_plot_tsss_analysis_008.png

Faces - scrambled

contrast_evo.plot(spatial_colors=True, gfp=True, ylim=ylim,
                  window_title='Faces - scrambled %s' % subject)
../../_images/sphx_glr_plot_tsss_analysis_009.png

Topomaps

times = np.arange(0.05, 0.3, 0.025)
famous_evo.plot_topomap(times=times, title='Famous %s' % subject,
                        show=False)
scrambled_evo.plot_topomap(times=times, title='Scrambled %s' % subject,
                           show=False)
unfamiliar_evo.plot_topomap(times=times, title='Unfamiliar %s' % subject,
                            show=False)
contrast_evo.plot_topomap(times=times, title='Faces - scrambled %s' % subject,
                          show=True)
  • ../../_images/sphx_glr_plot_tsss_analysis_010.png
  • ../../_images/sphx_glr_plot_tsss_analysis_011.png
  • ../../_images/sphx_glr_plot_tsss_analysis_012.png
  • ../../_images/sphx_glr_plot_tsss_analysis_013.png

ICA (ECG)

ica_fname = op.join(subject_dir, 'run_concat-tsss_%d-ica.fif'
                    % (st_duration,))
ica = mne.preprocessing.read_ica(ica_fname)

ecg_scores = np.load(
    op.join(subject_dir, '%s-tsss_%d-ecg-scores.npy'
            % (subject, st_duration)))
ica.plot_scores(ecg_scores, show=False, title='ICA ECG scores')
ecg_evoked = mne.read_evokeds(
    op.join(subject_dir, '%s-tsss_%d-ecg-ave.fif'
            % (subject, st_duration)))[0]
ica.plot_sources(ecg_evoked, title='ECG evoked', show=True)
  • ../../_images/sphx_glr_plot_tsss_analysis_014.png
  • ../../_images/sphx_glr_plot_tsss_analysis_015.png

Out:

Reading /tsi/doctorants/data_gramfort/dgw_faces_reproduce/MEG/sub003/run_concat-tsss_1-ica.fif ...
Now restoring ICA solution ...
Ready.
Reading /tsi/doctorants/data_gramfort/dgw_faces_reproduce/MEG/sub003/sub003-tsss_1-ecg-ave.fif ...
    Read a total of 1 projection items:
        Average EEG reference (1 x 70)  idle
    Found the data of interest:
        t =    -300.00 ...     300.00 ms (999)
        0 CTF compensation matrices available
        nave = 2716 - aspect type = 100
Created an SSP operator (subspace dimension = 1)
1 projection items activated
SSP projectors applied...
No baseline correction applied

ICA (EOG)

eog_scores = np.load(
    op.join(subject_dir, '%s-tsss_%d-eog-scores.npy'
            % (subject, st_duration)))
ica.plot_scores(eog_scores, show=False, title='ICA EOG scores')
eog_evoked = mne.read_evokeds(
    op.join(subject_dir, '%s-tsss_%d-eog-ave.fif'
            % (subject, st_duration)))[0]
ica.plot_sources(eog_evoked, title='EOG evoked', show=True)
  • ../../_images/sphx_glr_plot_tsss_analysis_016.png
  • ../../_images/sphx_glr_plot_tsss_analysis_017.png

Out:

Reading /tsi/doctorants/data_gramfort/dgw_faces_reproduce/MEG/sub003/sub003-tsss_1-eog-ave.fif ...
    Read a total of 1 projection items:
        Average EEG reference (1 x 70)  idle
    Found the data of interest:
        t =    -500.00 ...     500.00 ms (998)
        0 CTF compensation matrices available
        nave = 1307 - aspect type = 100
Created an SSP operator (subspace dimension = 1)
1 projection items activated
SSP projectors applied...
No baseline correction applied

Covariance 07. Evoked data.

cov_fname = op.join(subject_dir,
                    '%s-tsss_%d-cov.fif' % (subject, st_duration))
cov = mne.read_cov(cov_fname)
mne.viz.plot_cov(cov, faces_evo.info)
rank_dict = dict(
    meg=raw_filt.copy().load_data().pick_types(eeg=False).estimate_rank())
for kind in ('meg', 'eeg'):
    type_dict = dict(meg=False)
    type_dict.update({kind: True})
    fig = faces_evo.copy().apply_baseline().pick_types(
        **type_dict).plot_white(cov, rank=rank_dict if kind == 'meg' else {})
    for ax, ylabel in zip(fig.axes, ('Whitened\n%s (AU)' % (kind.upper(),),
                                     'GFP ($\chi^2$)')):
        ax.set(ylabel=ylabel)
    fig.axes[-1].set(title='', ylim=[0, 20])
    fig.axes[-1].legend(loc='lower center')
    fig.set_size_inches(3.5, 3, forward=True)
    fig.tight_layout()
    fig.savefig(op.join('..', 'figures', '%s-tsss_%d-plot_white_%s.pdf'
                        % (subject, st_duration, kind)))
  • ../../_images/sphx_glr_plot_tsss_analysis_018.png
  • ../../_images/sphx_glr_plot_tsss_analysis_019.png
  • ../../_images/sphx_glr_plot_tsss_analysis_020.png
  • ../../_images/sphx_glr_plot_tsss_analysis_021.png

Out:

376 x 376 full covariance (kind = 1) found.
    Read a total of 1 projection items:
        Average EEG reference (1 x 70) active
Reading 0 ... 543399  =      0.000 ...   493.999 secs...
estimated rank (mag + grad): 72
Applying baseline correction (mode: mean)
SSS has been applied to data. Showing mag and grad whitening jointly.
Setting small MEG eigenvalues to zero.
Not doing PCA for MEG.
Total rank is 72
Applying baseline correction (mode: mean)
estimated rank (eeg): 70
    Created an SSP operator (subspace dimension = 1)
Setting small EEG eigenvalues to zero.
Not doing PCA for EEG.
Total rank is 69

Total running time of the script: ( 1 minutes 39.740 seconds)

Gallery generated by Sphinx-Gallery