|
1 | | -import matplotlib.pyplot as plt |
| 1 | +# MNE2MATLAB2MNE - reads in and preprocesses data using MNE, then moves to SPM to do specific preprocessing, then moves back to MNE for the rest of the analysis |
| 2 | + |
| 3 | + |
| 4 | +import matplotlib.pyplot as plt |
2 | 5 | import numpy as np |
3 | 6 | import mne |
4 | | -import sys |
5 | | -plt.ion() |
6 | | -sys.path.insert(1, r'C:\Users\ybezsudnova\Documents\Git\spm-python\utils\spm_mne_converter.py') |
| 7 | +from spm import Struct, spm_opm_amm |
| 8 | +from spm.utils import spm_2_mne_raw, mne_raw_2_spm |
7 | 9 |
|
8 | | -from spm_mne_converter import spm_2_mne_raw, mne_raw_2_spm |
9 | | -from spm import * |
| 10 | +plt.ion() |
10 | 11 |
|
11 | 12 | # 1. read in the data using mne |
12 | 13 | subject = "sub-002" |
|
17 | 18 | subjects_dir = data_path / "derivatives" / "freesurfer" / "subjects" |
18 | 19 |
|
19 | 20 | raw = mne.io.read_raw_fil(opm_file, verbose="error") |
20 | | -raw.crop(120, 210).load_data() |
| 21 | +raw.crop(120, 210).load_data() |
21 | 22 |
|
22 | 23 | # 2. Plot raw data using mne |
23 | 24 | picks = mne.pick_types(raw.info, meg=True) |
|
34 | 35 | set_kwargs = dict( |
35 | 36 | ylim=(-500, 500), xlim=time_ds[[0, -1]], xlabel="Time (s)", ylabel="Amplitude (pT)" |
36 | 37 | ) |
37 | | -ax.set(title="No preprocessing",**set_kwargs) |
| 38 | +ax.set(title="No preprocessing", **set_kwargs) |
38 | 39 | plt.show() |
39 | 40 |
|
40 | | -psd_kwargs = dict(fmax=100, n_fft=int(round(raw.info["sfreq"])*2*2)) |
| 41 | +psd_kwargs = dict(fmax=100, n_fft=int(round(raw.info["sfreq"]) * 2 * 2)) |
41 | 42 | raw.compute_psd(**psd_kwargs).plot(dB=True, amplitude=True) |
42 | 43 |
|
43 | 44 | # 2. Some preproceesing using mne |
44 | 45 | raw.notch_filter(np.arange(50, 251, 50), notch_widths=4) |
45 | 46 | raw.filter(1, 70, picks="meg") |
46 | 47 |
|
47 | | -ref_mag_name = [ch['ch_name'] for ch in raw.info['chs'] if ch['kind'] == mne.io.constants.FIFF.FIFFV_REF_MEG_CH] |
48 | | -raw.info['bads'] = ref_mag_name |
49 | | -#raw.info['bads'].extend(['G2-A4-RAD','G2-17-TAN']) |
| 48 | +ref_mag_name = [ |
| 49 | + ch["ch_name"] |
| 50 | + for ch in raw.info["chs"] |
| 51 | + if ch["kind"] == mne.io.constants.FIFF.FIFFV_REF_MEG_CH |
| 52 | +] |
| 53 | +raw.info["bads"] = ref_mag_name |
| 54 | +# raw.info['bads'].extend(['G2-A4-RAD','G2-17-TAN']) |
50 | 55 |
|
51 | 56 |
|
52 | 57 | # 3. MNE 2 SPM |
53 | | -data = mne_raw_2_spm(raw,'D:/opm-tutorial') |
| 58 | +data = mne_raw_2_spm(raw, "D:/opm-tutorial") |
54 | 59 | data |
55 | 60 |
|
56 | 61 | # 4. SPM prossesing |
57 | | -S = Struct() |
| 62 | +S = Struct() |
58 | 63 | S.D = data |
59 | 64 | mD = spm_opm_amm(S) |
60 | 65 |
|
|
65 | 70 |
|
66 | 71 |
|
67 | 72 | # 6. MNE plotting |
68 | | -psd_kwargs = dict(fmax=100, n_fft=int(round(cleaned_data.info["sfreq"])*2*2)) |
| 73 | +psd_kwargs = dict(fmax=100, n_fft=int(round(cleaned_data.info["sfreq"]) * 2 * 2)) |
69 | 74 | cleaned_data.compute_psd(**psd_kwargs).plot(dB=True, amplitude=True) |
70 | 75 |
|
71 | 76 |
|
|
83 | 88 | set_kwargs = dict( |
84 | 89 | ylim=(-500, 500), xlim=time_ds[[0, -1]], xlabel="Time (s)", ylabel="Amplitude (pT)" |
85 | 90 | ) |
86 | | -ax.set(title="No preprocessing",**set_kwargs) |
| 91 | +ax.set(title="No preprocessing", **set_kwargs) |
87 | 92 | plt.show() |
88 | 93 |
|
89 | 94 |
|
90 | | - |
91 | 95 | # 7. MNE epoching |
92 | | -events=[] |
93 | | -events = mne.find_events(cleaned_data, stim_channel ='NI-TRIG',min_duration=0.001) |
94 | | -events_dict = {'trigger':3} |
| 96 | +events = [] |
| 97 | +events = mne.find_events(cleaned_data, stim_channel="NI-TRIG", min_duration=0.001) |
| 98 | +events_dict = {"trigger": 3} |
95 | 99 |
|
96 | 100 | cleaned_data.filter(2, 40, picks="meg") |
97 | | -epochs = mne.Epochs(cleaned_data, |
98 | | - events, |
99 | | - tmin=-0.1, tmax=0.4, |
100 | | - event_id=events_dict, |
101 | | - detrend = 1, |
102 | | - baseline=[-0.05,0], |
103 | | - preload=True) |
| 101 | +epochs = mne.Epochs( |
| 102 | + cleaned_data, |
| 103 | + events, |
| 104 | + tmin=-0.1, |
| 105 | + tmax=0.4, |
| 106 | + event_id=events_dict, |
| 107 | + detrend=1, |
| 108 | + baseline=[-0.05, 0], |
| 109 | + preload=True, |
| 110 | +) |
104 | 111 |
|
105 | 112 |
|
106 | | -evoked = epochs['trigger'].average() |
| 113 | +evoked = epochs["trigger"].average() |
107 | 114 | evoked.plot() |
108 | 115 |
|
109 | 116 |
|
110 | | -radial_channels = [ch for ch in evoked.ch_names if ch.endswith('RAD')] # take on radial sensors |
| 117 | +radial_channels = [ |
| 118 | + ch for ch in evoked.ch_names if ch.endswith("RAD") |
| 119 | +] # take on radial sensors |
111 | 120 | evoked_radial = evoked.pick_channels(radial_channels) |
112 | 121 | fig = evoked_radial.plot_joint() |
113 | 122 |
|
114 | 123 |
|
115 | | - |
116 | | -tang_channels = [ch for ch in evoked.ch_names if ch.endswith('TAN')] # take on tangatial sensors |
| 124 | +tang_channels = [ |
| 125 | + ch for ch in evoked.ch_names if ch.endswith("TAN") |
| 126 | +] # take on tangatial sensors |
117 | 127 | evoked_tang = evoked.pick_channels(tang_channels) |
118 | 128 | evoked_tang.plot_joint() |
119 | | - |
120 | | - |
|
0 commit comments