-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathmne_compute_proj_ecg.py
331 lines (302 loc) · 8.12 KB
/
mne_compute_proj_ecg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
r"""Compute SSP/PCA projections for ECG artifacts.
Examples
--------
.. code-block:: console
$ mne compute_proj_ecg -i sample_audvis_raw.fif -c "MEG 1531" -a \
--l-freq 1 --h-freq 100 \
--rej-grad 3000 --rej-mag 4000 --rej-eeg 100
"""
# Authors: The MNE-Python contributors.
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
import os
import sys
import mne
def run():
"""Run command."""
from mne.commands.utils import get_optparser
parser = get_optparser(__file__)
parser.add_option(
"-i", "--in", dest="raw_in", help="Input raw FIF file", metavar="FILE"
)
parser.add_option(
"--tmin",
dest="tmin",
type="float",
help="Time before event in seconds",
default=-0.2,
)
parser.add_option(
"--tmax",
dest="tmax",
type="float",
help="Time after event in seconds",
default=0.4,
)
parser.add_option(
"-g",
"--n-grad",
dest="n_grad",
type="int",
help="Number of SSP vectors for gradiometers",
default=2,
)
parser.add_option(
"-m",
"--n-mag",
dest="n_mag",
type="int",
help="Number of SSP vectors for magnetometers",
default=2,
)
parser.add_option(
"-e",
"--n-eeg",
dest="n_eeg",
type="int",
help="Number of SSP vectors for EEG",
default=2,
)
parser.add_option(
"--l-freq",
dest="l_freq",
type="float",
help="Filter low cut-off frequency in Hz",
default=1,
)
parser.add_option(
"--h-freq",
dest="h_freq",
type="float",
help="Filter high cut-off frequency in Hz",
default=100,
)
parser.add_option(
"--ecg-l-freq",
dest="ecg_l_freq",
type="float",
help="Filter low cut-off frequency in Hz used for ECG event detection",
default=5,
)
parser.add_option(
"--ecg-h-freq",
dest="ecg_h_freq",
type="float",
help="Filter high cut-off frequency in Hz used for ECG event detection",
default=35,
)
parser.add_option(
"-p",
"--preload",
dest="preload",
help="Temporary file used during computation (to save memory)",
default=True,
)
parser.add_option(
"-a",
"--average",
dest="average",
action="store_true",
help="Compute SSP after averaging",
default=False,
)
parser.add_option(
"--proj", dest="proj", help="Use SSP projections from a fif file.", default=None
)
parser.add_option(
"--filtersize",
dest="filter_length",
type="int",
help="Number of taps to use for filtering",
default=2048,
)
parser.add_option(
"-j",
"--n-jobs",
dest="n_jobs",
type="int",
help="Number of jobs to run in parallel",
default=1,
)
parser.add_option(
"-c",
"--channel",
dest="ch_name",
help="Channel to use for ECG detection (Required if no ECG found)",
default=None,
)
parser.add_option(
"--rej-grad",
dest="rej_grad",
type="float",
help="Gradiometers rejection parameter in fT/cm (peak to peak amplitude)",
default=2000,
)
parser.add_option(
"--rej-mag",
dest="rej_mag",
type="float",
help="Magnetometers rejection parameter in fT (peak to peak amplitude)",
default=3000,
)
parser.add_option(
"--rej-eeg",
dest="rej_eeg",
type="float",
help="EEG rejection parameter in µV (peak to peak amplitude)",
default=50,
)
parser.add_option(
"--rej-eog",
dest="rej_eog",
type="float",
help="EOG rejection parameter in µV (peak to peak amplitude)",
default=250,
)
parser.add_option(
"--avg-ref",
dest="avg_ref",
action="store_true",
help="Add EEG average reference proj",
default=False,
)
parser.add_option(
"--no-proj",
dest="no_proj",
action="store_true",
help="Exclude the SSP projectors currently in the fiff file",
default=False,
)
parser.add_option(
"--bad",
dest="bad_fname",
help="Text file containing bad channels list (one per line)",
default=None,
)
parser.add_option(
"--event-id",
dest="event_id",
type="int",
help="ID to use for events",
default=999,
)
parser.add_option(
"--event-raw",
dest="raw_event_fname",
help="raw file to use for event detection",
default=None,
)
parser.add_option(
"--tstart",
dest="tstart",
type="float",
help="Start artifact detection after tstart seconds",
default=0.0,
)
parser.add_option(
"--qrsthr",
dest="qrs_threshold",
type="string",
help="QRS detection threshold. Between 0 and 1. Can "
"also be 'auto' for automatic selection",
default="auto",
)
options, args = parser.parse_args()
raw_in = options.raw_in
if raw_in is None:
parser.print_help()
sys.exit(1)
tmin = options.tmin
tmax = options.tmax
n_grad = options.n_grad
n_mag = options.n_mag
n_eeg = options.n_eeg
l_freq = options.l_freq
h_freq = options.h_freq
ecg_l_freq = options.ecg_l_freq
ecg_h_freq = options.ecg_h_freq
average = options.average
preload = options.preload
filter_length = options.filter_length
n_jobs = options.n_jobs
ch_name = options.ch_name
reject = dict(
grad=1e-13 * float(options.rej_grad),
mag=1e-15 * float(options.rej_mag),
eeg=1e-6 * float(options.rej_eeg),
eog=1e-6 * float(options.rej_eog),
)
avg_ref = options.avg_ref
no_proj = options.no_proj
bad_fname = options.bad_fname
event_id = options.event_id
proj_fname = options.proj
raw_event_fname = options.raw_event_fname
tstart = options.tstart
qrs_threshold = options.qrs_threshold
if qrs_threshold != "auto":
try:
qrs_threshold = float(qrs_threshold)
except ValueError:
raise ValueError('qrsthr must be "auto" or a float')
if bad_fname is not None:
with open(bad_fname) as fid:
bads = [w.rstrip() for w in fid.readlines()]
print(f"Bad channels read : {bads}")
else:
bads = []
if raw_in.endswith("_raw.fif") or raw_in.endswith("-raw.fif"):
prefix = raw_in[:-8]
else:
prefix = raw_in[:-4]
ecg_event_fname = prefix + "_ecg-eve.fif"
if average:
ecg_proj_fname = prefix + "_ecg_avg-proj.fif"
else:
ecg_proj_fname = prefix + "_ecg-proj.fif"
raw = mne.io.read_raw_fif(raw_in, preload=preload)
if raw_event_fname is not None:
raw_event = mne.io.read_raw_fif(raw_event_fname)
else:
raw_event = raw
flat = None
projs, events = mne.preprocessing.compute_proj_ecg(
raw,
raw_event,
tmin,
tmax,
n_grad,
n_mag,
n_eeg,
l_freq,
h_freq,
average,
filter_length,
n_jobs,
ch_name,
reject,
flat,
bads,
avg_ref,
no_proj,
event_id,
ecg_l_freq,
ecg_h_freq,
tstart,
qrs_threshold,
copy=False,
)
raw.close()
if raw_event_fname is not None:
raw_event.close()
if proj_fname is not None:
print(f"Including SSP projections from : {proj_fname}")
# append the ecg projs, so they are last in the list
projs = mne.read_proj(proj_fname) + projs
if isinstance(preload, str) and os.path.exists(preload):
os.remove(preload)
print(f"Writing ECG projections in {ecg_proj_fname}")
mne.write_proj(ecg_proj_fname, projs)
print(f"Writing ECG events in {ecg_event_fname}")
mne.write_events(ecg_event_fname, events)
mne.utils.run_command_if_main()