forked from dnacombo/app-filter-raw
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
112 lines (95 loc) · 3.5 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
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
# this app allows filtering raw data.
# it loads the raw 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
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))
# == CONFIG PARAMETERS ==
fname = config['mne']
l_freq = config['l_freq']
h_freq = config['h_freq']
picks = config['picks']
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']
# == LOAD DATA ==
raw = mne.io.read_raw_fif(fname, preload=True)
raw_orig = raw.copy()
sfreq = raw.info['sfreq']
# Create filter
f = mne.filter.create_filter(raw_orig.get_data(),
sfreq,
l_freq = l_freq,
h_freq = h_freq )
'''
filter_length = filter_length,
l_trans_bandwidth = l_trans_bandwidth,
h_trans_bandwidth = h_trans_bandwidth,
method = method,
iir_params = iir_params,
phase = phase,
fir_window = fir_window,
fir_design = fir_design)
'''
plt.figure()
fig = plot_filter(f,sfreq)
plt.savefig(os.path.join('out_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'])
raw.filter( l_freq = l_freq,
h_freq = h_freq )
'''
raw.filter(
l_freq = l_freq,
h_freq = h_freq,
picks = picks,
filter_length = filter_length,
l_trans_bandwidth = l_trans_bandwidth,
h_trans_bandwidth = h_trans_bandwidth,
method = method,
iir_params = iir_params,
phase = phase,
fir_window = fir_window,
fir_design = fir_design
skip_by_annotation=config['skip_by_annotation'],
pad = pad)
raw.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 = skip_by_annotation,
pad = config['pad'])
'''
raw.save('out_dir/meg.fif',overwrite=True)
# == REPORT ==
report = mne.Report(title='Filtering report')
report.add_figure(fig, title='Filter')
report.add_raw(raw_orig, 'Original unfiltered data', psd=True)
report.add_raw(raw, 'Filtered data', psd=True)
report.save('out_report/report_filter.html', overwrite=True)