Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# 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)
###############################################################################
# Filtering :ref:`sphx_glr_auto_scripts_04-python_filtering.py`.
raw.plot_psd(n_fft=8192, average=False, xscale='log', show=False)
raw_filt.plot_psd(n_fft=8192, average=False, xscale='log')
###############################################################################
# Events :ref:`sphx_glr_auto_scripts_02-extract_events.py`.
# Epochs :ref:`sphx_glr_auto_scripts_06-make_epochs.py`.
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')
###############################################################################
# Configuration
subjects_dir = op.join(study_path, 'subjects')
subject = "sub%03d" % int(14)
subject_dir = op.join(meg_dir, subject)
###############################################################################
# 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_sss_highpass-%sHz_raw.fif' % l_freq)
raw = mne.io.read_raw_fif(raw_fname)
raw_filt = mne.io.read_raw_fif(raw_filt_fname)
###############################################################################
# Filtering :ref:`sphx_glr_auto_scripts_02-python_filtering.py`.
raw.plot_psd(n_fft=2048, n_overlap=1024)
raw_filt.plot_psd()
###############################################################################
# Events :ref:`sphx_glr_auto_scripts_03-run_extract_events.py`.
# Epochs :ref:`sphx_glr_auto_scripts_05-make_epochs.py`.
eve_fname = op.join(subject_dir, 'run_01_filt_sss-eve.fif')
epo_fname = op.join(subject_dir,
'%s_highpass-%sHz-epo.fif' % (subject, l_freq))
events = mne.read_events(eve_fname)
fig = mne.viz.plot_events(events, show=False)
fig.suptitle('Events from run 01')
import os.path as op
import subprocess
import mne
from mne.viz import plot_events
from mne.realtime import FieldTripClient, RtEpochs
from mne.utils import running_subprocess
import matplotlib.pyplot as plt
print(__doc__)
# user should provide info and list of bad channels because
# FieldTrip header object does not provide them
data_path = mne.datasets.sample.data_path()
info = mne.io.read_info(op.join(data_path, 'MEG', 'sample',
'sample_audvis_raw.fif'))
# select the left-auditory condition
event_id, tmin, tmax = 1, -0.2, 0.5
# user must provide list of bad channels because
# FieldTrip header object does not provide that
bads = ['MEG 2443', 'EEG 053']
_, ax = plt.subplots(2, 1, figsize=(8, 8)) # create subplots
speedup = 10
command = ["neuromag2ft", "--file",
"{}/MEG/sample/sample_audvis_raw.fif".format(data_path),
"--speed", str(speedup)]
with running_subprocess(command, after='kill',
def analyze_rest(subject, args, hcp_params, run_index=0, calc_rest_from_raw=False, calc_rest_from_epochs=True):
flags = {}
if not op.isfile(meg.RAW):
raw = read_raw_data(run_index, hcp_params, 1, 60)
raw.save(meg.RAW)
else:
raw = mne.io.read_raw_fif(meg.RAW)
meg.COR = op.join(op.join(HCP_DIR, 'hcp-meg', subject, '{}-head_mri-trans.fif'.format(subject)))
epo_fname = meg.EPO.format(cond='all')
evo_fname = meg.EVO.format(cond='all')
if not op.isfile(epo_fname) or not op.isfile(evo_fname):
epochs = hcp.read_epochs(run_index=run_index, **hcp_params)
evoked = epochs.average()
epochs.save(epo_fname)
mne.write_evokeds(evo_fname, evoked)
else:
epochs = mne.read_epochs(epo_fname)
meg.calc_fwd_inv_wrapper(subject, args)
args.snr = 1.0 # use smaller SNR for raw data
# args.overwrite_labels_data = True
# args.n_jobs = 1
if calc_rest_from_raw:
meg.calc_labels_avg_for_rest_wrapper(args, raw)
###############################################################################
# Configuration
subjects_dir = op.join(study_path, 'subjects')
subject = "sub%03d" % int(18)
subject_dir = op.join(meg_dir, subject)
###############################################################################
# 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_sss_highpass-%sHz_raw.fif' % l_freq)
raw = mne.io.read_raw_fif(raw_fname)
raw_filt = mne.io.read_raw_fif(raw_filt_fname)
###############################################################################
# Filtering :ref:`sphx_glr_auto_scripts_02-python_filtering.py`.
raw.plot_psd(n_fft=2048, n_overlap=1024)
raw_filt.plot_psd()
###############################################################################
# Events :ref:`sphx_glr_auto_scripts_03-run_extract_events.py`.
# Epochs :ref:`sphx_glr_auto_scripts_05-make_epochs.py`.
eve_fname = op.join(subject_dir, 'run_01_filt_sss-eve.fif')
epo_fname = op.join(subject_dir,
'%s_highpass-%sHz-epo.fif' % (subject, l_freq))
events = mne.read_events(eve_fname)
fig = mne.viz.plot_events(events, show=False)
fig.suptitle('Events from run 01')
print(__doc__)
data_path = sample.data_path()
###############################################################################
# Set parameters and read data
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
event_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'
tmin, tmax = -0.2, 0.5
event_id = dict(aud_l=1, vis_l=3)
# Setup for reading the raw data
raw = io.Raw(raw_fname, preload=True)
raw.filter(2, None, method='iir') # replace baselining with high-pass
events = mne.read_events(event_fname)
raw.info['bads'] = ['MEG 2443'] # set bad channels
picks = mne.pick_types(raw.info, meg='grad', eeg=False, stim=False, eog=False,
exclude='bads')
# Read epochs
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=False,
picks=picks, baseline=None, preload=True)
labels = epochs.events[:, -1]
evoked = epochs.average()
###############################################################################
# Decoding in sensor space using a MDM
# correct nan values
data = raw_gdf.get_data()
for i_chan in range(data.shape[0]):
# first set to nan, than replace nans by nanmean.
this_chan = data[i_chan]
data[i_chan] = np.where(
this_chan == np.min(this_chan), np.nan, this_chan
)
mask = np.isnan(data[i_chan])
chan_mean = np.nanmean(data[i_chan])
data[i_chan, mask] = chan_mean
gdf_events = mne.events_from_annotations(raw_gdf)
raw_gdf = mne.io.RawArray(data, raw_gdf.info, verbose="WARNING")
# remember gdf events
raw_gdf.info["gdf_events"] = gdf_events
return raw_gdf
def load_raw(bad_channels=[], l_freq=None, h_freq=None):
# read the data
raw_fname, raw_exist = locating_file(RAW, glob_pattern='*raw.fif')
if not raw_exist:
return None
raw = mne.io.read_raw_fif(raw_fname, preload=True)
if not op.isfile(INFO):
utils.save(raw.info, INFO)
if len(bad_channels) > 0:
raw.info['bads'] = args.bad_channels
if l_freq or h_freq:
raw = raw.filter(l_freq, h_freq)
return raw
import mne
from mne import io
from mne.connectivity import spectral_connectivity, seed_target_indices
from mne.datasets import sample
from mne.time_frequency import AverageTFR
print(__doc__)
###############################################################################
# Set parameters
data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
event_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw-eve.fif'
# Setup for reading the raw data
raw = io.read_raw_fif(raw_fname)
events = mne.read_events(event_fname)
# Add a bad channel
raw.info['bads'] += ['MEG 2443']
# Pick MEG gradiometers
picks = mne.pick_types(raw.info, meg='grad', eeg=False, stim=False, eog=True,
exclude='bads')
# Create epochs for left-visual condition
event_id, tmin, tmax = 3, -0.2, 0.5
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, picks=picks,
baseline=(None, 0), reject=dict(grad=4000e-13, eog=150e-6),
preload=True)
# Use 'MEG 2343' as seed
import numpy as np
import matplotlib.pyplot as plt
import mne
from mne.datasets import sample
from sklearn.decomposition import PCA
from picard import picard
print(__doc__)
###############################################################################
# Generate sample EEG data
data_path = sample.data_path()
raw_fname = data_path + '/MEG/sample/sample_audvis_filt-0-40_raw.fif'
raw = mne.io.read_raw_fif(raw_fname, preload=True)
raw.filter(1, 40, n_jobs=1) # 1Hz high pass is often helpful for fitting ICA
picks = mne.pick_types(raw.info, meg=False, eeg=True, eog=False,
stim=False, exclude='bads')
random_state = 0
data = raw[picks, :][0]
data = data[:, ::2] # decimate a bit
# Center
data -= np.mean(data, axis=1, keepdims=True)
# Apply PCA for dimension reduction and whitenning.
n_components = 30
pca = PCA(n_components=n_components, whiten=True, svd_solver='full')