forked from dnacombo/app-filter-epo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
75 lines (60 loc) · 2.63 KB
/
main.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
# this app allows filtering epoched data.
# it loads the epoched data, applies a bandpass filter to it using the parameters specified in the config.json file
# it then saves the filtered data and plots the filter response
# it also saves a report of the filtered data
import os
import mne
import json
import helper
import matplotlib.pyplot as plt
from mne.viz import plot_filter, plot_ideal_filter
import re
#workaround for -- _tkinter.TclError: invalid command name ".!canvas"
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
# Load brainlife config.json
with open('config.json','r') as config_f:
config = helper.convert_parameters_to_None(json.load(config_f))
# == LOAD DATA ==
fname = config['epo']
epo = mne.read_epochs(fname, preload=True)
epo_orig = epo.copy()
sfreq = epo.info['sfreq']
f = mne.filter.create_filter(epo_orig.get_data(),
sfreq,
l_freq=config['l_freq'],
h_freq=config['h_freq'],
filter_length=config['filter_length'],
l_trans_bandwidth=config['l_trans_bandwidth'],
h_trans_bandwidth=config['h_trans_bandwidth'],
method=config['method'],
iir_params=config['iir_params'],
phase=config['phase'],
fir_window=config['fir_window'],
fir_design=config['fir_design'])
plt.figure()
fig=plot_filter(f,sfreq)
plt.savefig(os.path.join('out_dir_figs','filter_response.png'))
if config['notch']:
config['notch'] = [int(x) for x in re.split("\\W+",config['notch'])]
raw.notch_filter(freqs=config['notch'], picks=config['picks'])
epo.filter(picks=config['picks'],
l_freq=config['l_freq'],
h_freq=config['h_freq'],
filter_length=config['filter_length'],
l_trans_bandwidth=config['l_trans_bandwidth'],
h_trans_bandwidth=config['h_trans_bandwidth'],
method=config['method'],
iir_params=config['iir_params'],
phase=config['phase'],
fir_window=config['fir_window'],
fir_design=config['fir_design'],
skip_by_annotation=config['skip_by_annotation'],
pad=config['pad'])
report = mne.Report(title='Filtering report')
report.add_figure(fig, title='Filter')
report.add_epochs(epo_orig, 'Original unfiltered data', psd=True)
report.add_epochs(epo, 'Filtered data', psd=True)
report.save('out_dir_report/report_filter.html', overwrite=True)
epo.save('out_dir/meg-epo.fif',overwrite=True)