Skip to content

Commit 46c0649

Browse files
committed
Move utils to spm.utils
1 parent dc6cf24 commit 46c0649

File tree

5 files changed

+417
-293
lines changed

5 files changed

+417
-293
lines changed
Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
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
25
import numpy as np
36
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
79

8-
from spm_mne_converter import spm_2_mne_raw, mne_raw_2_spm
9-
from spm import *
10+
plt.ion()
1011

1112
# 1. read in the data using mne
1213
subject = "sub-002"
@@ -17,7 +18,7 @@
1718
subjects_dir = data_path / "derivatives" / "freesurfer" / "subjects"
1819

1920
raw = mne.io.read_raw_fil(opm_file, verbose="error")
20-
raw.crop(120, 210).load_data()
21+
raw.crop(120, 210).load_data()
2122

2223
# 2. Plot raw data using mne
2324
picks = mne.pick_types(raw.info, meg=True)
@@ -34,27 +35,31 @@
3435
set_kwargs = dict(
3536
ylim=(-500, 500), xlim=time_ds[[0, -1]], xlabel="Time (s)", ylabel="Amplitude (pT)"
3637
)
37-
ax.set(title="No preprocessing",**set_kwargs)
38+
ax.set(title="No preprocessing", **set_kwargs)
3839
plt.show()
3940

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))
4142
raw.compute_psd(**psd_kwargs).plot(dB=True, amplitude=True)
4243

4344
# 2. Some preproceesing using mne
4445
raw.notch_filter(np.arange(50, 251, 50), notch_widths=4)
4546
raw.filter(1, 70, picks="meg")
4647

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'])
5055

5156

5257
# 3. MNE 2 SPM
53-
data = mne_raw_2_spm(raw,'D:/opm-tutorial')
58+
data = mne_raw_2_spm(raw, "D:/opm-tutorial")
5459
data
5560

5661
# 4. SPM prossesing
57-
S = Struct()
62+
S = Struct()
5863
S.D = data
5964
mD = spm_opm_amm(S)
6065

@@ -65,7 +70,7 @@
6570

6671

6772
# 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))
6974
cleaned_data.compute_psd(**psd_kwargs).plot(dB=True, amplitude=True)
7075

7176

@@ -83,38 +88,41 @@
8388
set_kwargs = dict(
8489
ylim=(-500, 500), xlim=time_ds[[0, -1]], xlabel="Time (s)", ylabel="Amplitude (pT)"
8590
)
86-
ax.set(title="No preprocessing",**set_kwargs)
91+
ax.set(title="No preprocessing", **set_kwargs)
8792
plt.show()
8893

8994

90-
9195
# 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}
9599

96100
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+
)
104111

105112

106-
evoked = epochs['trigger'].average()
113+
evoked = epochs["trigger"].average()
107114
evoked.plot()
108115

109116

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
111120
evoked_radial = evoked.pick_channels(radial_channels)
112121
fig = evoked_radial.plot_joint()
113122

114123

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
117127
evoked_tang = evoked.pick_channels(tang_channels)
118128
evoked_tang.plot_joint()
119-
120-

spm/utils/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .spm_mne_converter import mne_raw_2_spm, spm_2_mne_raw
2+
3+
4+
__all__ = ["mne_raw_2_spm", "spm_2_mne_raw"]

0 commit comments

Comments
 (0)