mne使用教程(一)

01 read_raw_crop_filter

envs:

  • matplotlib 3.4.3
  • pathlib 1.0.1
  • mne 0.23.0
import matplotlib
import pathlib
import mne
import warnings
warnings.filterwarnings("ignore")
#Display interactive images
matplotlib.use('Qt5Agg')
sample_data_dir = mne.datasets.sample.data_path()
sample_data_dir = pathlib.Path(sample_data_dir)
sample_data_dir

WindowsPath(‘C:/Users/***/mne_data/MNE-sample-data’)

Load some raw data!

raw_path = sample_data_dir / 'MEG' / 'sample' / 'sample_audvis_raw.fif'
raw = mne.io.read_raw_fif(raw_path, preload=True)
raw

Let’s visualize the raw data!

raw.plot();

Extract events from the STIM channels

events = mne.find_events(raw)
event_id = {
'Auditory/Left': 1,
'Auditory/Right': 2,
'Visual/Left': 3,
'Visual/Right': 4,
'Smiley': 5,
'Button': 32
}
event_id

Out[72]:
{‘Auditory/Left’: 1,
‘Auditory/Right’: 2,
‘Visual/Left’: 3,
‘Visual/Right’: 4,
‘Smiley’: 5,
‘Button’: 32}

len(events[events[:,2]==32])

16

# Number of visual events
len(events[(events[:,2]==event_id['Visual/Left']) | (events[:,2]==event_id['Visual/Right'])])

144

Plot the raw data again,but add event markers

raw.plot(events=events, event_id=events_id);

Gather some info about the data

raw.info

acq_pars: ACQch001 110113 ACQch002 110112 ACQch003 110111 ACQch004 110122 …
bads: 7 items (MEG 2531, EEG 053, MEG 2131, MEG 1931, MEG 2021, MEG …)
ch_names: MEG 0113, MEG 0112, MEG 0111, MEG 0122, MEG 0123, MEG 0121, MEG …
chs: 204 GRAD, 102 MAG, 9 STIM, 60 EEG, 1 EOG
custom_ref_applied: False
description: acquisition (megacq) VectorView system at NMR-MGH
dev_head_t: MEG device -> head transform
dig: 146 items (3 Cardinal, 4 HPI, 61 EEG, 78 Extra)
events: 1 item (list)
experimenter: MEG
file_id: 4 items (dict)
highpass: 0.1 Hz
hpi_meas: 1 item (list)
hpi_results: 1 item (list)
lowpass: 172.2 Hz
meas_date: 2002-12-03 19:01:10 UTC
meas_id: 4 items (dict)
nchan: 376
proj_id: 1 item (ndarray)
proj_name: test
projs: PCA-v1: off, PCA-v2: off, PCA-v3: off
sfreq: 600.6 Hz>

raw.info['meas_date']

datetime.datetime(2002, 12, 3, 19, 1, 10, 720100, tzinfo=datetime.timezone.utc)

raw.info['sfreq']

600.614990234375

raw.info['bads']

[‘MEG 2531’,
‘EEG 053’,
‘MEG 2131’,
‘MEG 1931’,
‘MEG 2021’,
‘MEG 2311’,
‘MEG 2141’]

raw.ch_names[:10]

[‘MEG 0113’,
‘MEG 0112’,
‘MEG 0111’,
‘MEG 0122’,
‘MEG 0123’,
‘MEG 0121’,
‘MEG 0132’,
‘MEG 0133’,
‘MEG 0131’,
‘MEG 0143’]

raw.info['chs'][0]

{‘scanno’: 1,
‘logno’: 113,
‘kind’: 1,
‘range’: 0.00030517578125,
‘cal’: 3.1600000394149674e-09,
‘coil_type’: 3012,
‘loc’: array([-0.1066 , 0.0464 , -0.0604 , -0.0127 , 0.0057 ,
-0.99990302, -0.186801 , -0.98240298, -0.0033 , -0.98232698,
0.18674099, 0.013541 ]),
‘unit’: 201,
‘unit_mul’: 0,
‘ch_name’: ‘MEG 0113’,
‘coord_frame’: 1 (FIFFV_COORD_DEVICE)}

Visualize the sensor locations

raw.plot_sensors(ch_type='eeg');```
```python
raw.plot_sensors(kind='3d', ch_type='eeg');

Mark channels as bad

raw.info['bads']

[‘MEG 2531’,
‘EEG 053’,
‘MEG 2131’,
‘MEG 1931’,
‘MEG 2021’,
‘MEG 2311’,
‘MEG 2141’]

raw.info['bads'] += ['EEG 051']
raw.plot_sensors(ch_type='eeg');

Select only a subset of the channels

raw_eeg = raw.copy().pick_types(meg=False, eeg=True, eog=True, exclude=[])
len(raw_eeg.ch_names)

61

raw.info

acq_pars: ACQch001 110113 ACQch002 110112 ACQch003 110111 ACQch004 110122 …
bads: 2 items (MEG 2443, EEG 053)
ch_names: MEG 0113, MEG 0112, MEG 0111, MEG 0122, MEG 0123, MEG 0121, MEG …
chs: 204 GRAD, 102 MAG, 9 STIM, 60 EEG, 1 EOG
custom_ref_applied: False
description: acquisition (megacq) VectorView system at NMR-MGH
dev_head_t: MEG device -> head transform
dig: 146 items (3 Cardinal, 4 HPI, 61 EEG, 78 Extra)
events: 1 item (list)
experimenter: MEG
file_id: 4 items (dict)
highpass: 0.1 Hz
hpi_meas: 1 item (list)
hpi_results: 1 item (list)
lowpass: 172.2 Hz
meas_date: 2002-12-03 19:01:10 UTC
meas_id: 4 items (dict)
nchan: 376
proj_id: 1 item (ndarray)
proj_name: test
projs: PCA-v1: off, PCA-v2: off, PCA-v3: off
sfreq: 600.6 Hz>

raw_eeg.plot(events=events,event_id=event_id);

Crop and filter the data

raw_eeg_cropped = raw_eeg.copy().crop(tmax=100)
raw_eeg_cropped.times[-1]

99.99916914588277

raw_eeg_cropped_filtered = raw_eeg_cropped.copy().filter(l_freq=0.1, h_freq=40)
raw_eeg_cropped.plot(events=events, event_id=event_id, title='Unfiltered')
raw_eeg_cropped_filtered.plot(events=events,event_id=event_id, title='Filtered');
import matplotlib.pyplot as plt
fig, ax = plt.subplots(2)
raw_eeg_cropped.plot_psd(ax=ax[0], show=False)
raw_eeg_cropped_filtered.plot_psd(ax=ax[1], show=False)
ax[0].set_title('PSD before filtering')
ax[1].set_title('PSD after filtering')
ax[1].set_xlabel('Frequency (Hz)')
fig.set_tight_layout(True)
plt.show()

Save the data

raw_eeg_cropped_filtered.save(pathlib.Path('out_data') / 'eeg_cropped_filt_raw.fif',
overwrite=True)

摘自B站:https://www.bilibili.com/video/BV1ui4y1w7aR/?spm_id_from=333.788.recommend_more_video.1

2021/8/22

你可能感兴趣的:(代码,python)