Skip to content

Instantly share code, notes, and snippets.

@agramfort
Created September 27, 2020 13:09
Show Gist options
  • Save agramfort/56b5c0d605cdc136cf3a7d31aad74d67 to your computer and use it in GitHub Desktop.
Save agramfort/56b5c0d605cdc136cf3a7d31aad74d67 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 20 19:45:49 2020
@author: zhanglingxi
"""
import os
import os.path as op
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
from nilearn import plotting
import mne
from mne.viz import (plot_sparse_source_estimates,
plot_dipole_locations, plot_dipole_amplitudes)
from mne.inverse_sparse import mixed_norm, make_stc_from_dipoles,gamma_map,tf_mixed_norm
from mne.minimum_norm import make_inverse_operator, apply_inverse
from mne import setup_volume_source_space, setup_source_space
from mne import make_forward_solution
from mne.io import read_raw_fif
from mne.minimum_norm import make_inverse_operator, apply_inverse_epochs
from mne.connectivity import spectral_connectivity
from mne.viz import circular_layout, plot_connectivity_circle
from mne.preprocessing import (create_eog_epochs, create_ecg_epochs,
compute_proj_ecg, compute_proj_eog,ICA,corrmap)
print(__doc__)
# data_path = r'E:\beishi1+3\beishi1+3'
# data_path = r'./'
subject = 'sub01wkl'
# data_dir = op.join(data_path, 'wangkailisub01', '171117')
# subjects_dir = op.join(data_path, 'subject01wkl')
# bem_dir = op.join(subjects_dir, subject, 'bem')
# # Set file names
fname_mixed_src = 'sub01wkl-oct-6-mixed-src.fif'
fname_aseg = 'aseg.mgz'
bem_dir = '.'
fname_model = op.join(bem_dir, 'sub01wkl-bem.fif')
fname_bem = op.join(bem_dir, 'sub01wkl-bem-sol.fif')
fname_evoked = op.join(bem_dir, 'sub01wkl-ave.fif')
fname_cov = op.join(bem_dir, 'sub01wkl-cov.fif')
fname_fwd= op.join(bem_dir, 'sub01wkl-fwd.fif')
forward = mne.read_forward_solution(fname_fwd)
evoked = mne.read_evokeds(fname_evoked , condition='tool', baseline=(None, 0))
cov = mne.read_cov(fname_cov)
# alpha parameter is between 0 and 100 (100 gives 0 active source)
alpha = 30. # general regularization parameter
###!!!I have tried 30 is too small,60 is too big to get vaulable label_ts
# l1_ratio parameter between 0 and 1 promotes temporal smoothness# (0 means no temporal regularization)
l1_ratio = 0.03 # temporal regularization parameter
snr = 3.0 # use smaller SNR for raw data
inv_method = 'dSPM' # sLORETA, MNE, dSPM
parc = 'aparc'
depth = None # loose orientation & depth weighting
loose = dict(surface=0.2, volume=1.)
lambda2 = 1.0 / snr ** 2
inverse_operator = make_inverse_operator(evoked.info, forward, cov,
loose=loose, depth=depth,
rank="info")
stc_dspm = apply_inverse(evoked, inverse_operator, lambda2,
inv_method, pick_ori=None)
src = inverse_operator['src']
dipoles, residual = tf_mixed_norm(
evoked, forward, cov, alpha=alpha, l1_ratio=l1_ratio, loose=loose,
depth=depth, maxit=200, tol=1e-6, weights_min=8.,
debias=True, wsize=16, tstep=4, window=0.05, return_as_dipoles=True,
return_residual=True, rank="info")
stc = mne.inverse_sparse.make_stc_from_dipoles(dipoles, forward['src'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment