-
Notifications
You must be signed in to change notification settings - Fork 2
/
fnirsapp_qr.py
executable file
·329 lines (254 loc) · 11.6 KB
/
fnirsapp_qr.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
#!/usr/bin/env python3
import pandas as pd
import matplotlib
import mne
import argparse
from mne_bids import BIDSPath, read_raw_bids, get_entity_vals
from glob import glob
import os.path as op
from pathlib import Path
from mne.preprocessing.nirs import optical_density
from mne_nirs.preprocessing import peak_power, scalp_coupling_index_windowed
from mne_nirs.visualisation import plot_timechannel_quality_metric
import matplotlib.pyplot as plt
from itertools import compress
import os
import subprocess
from mne.utils import logger
from pathlib import Path
from datetime import datetime
import json
import hashlib
from pprint import pprint
matplotlib.use('agg')
__version__ = "v0.3.8"
def fnirsapp_qr(command, env={}):
merged_env = os.environ
merged_env.update(env)
process = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, shell=True,
env=merged_env)
while True:
line = process.stdout.readline()
line = str(line, 'utf-8')[:-1]
print(line)
if line == '' and process.poll() != None:
break
if process.returncode != 0:
raise Exception("Non zero return code: %d" % process.returncode)
parser = argparse.ArgumentParser(description='Quality Reports')
parser.add_argument('--input-datasets', default="/bids_dataset", type=str,
help='The directory with the input dataset '
'formatted according to the BIDS standard.')
parser.add_argument('--output-location', default="/bids_dataset/derivatives/fnirs-apps-quality-reports",
type=str, help='The directory where the output files should be stored.')
parser.add_argument('--subject-label',
help='The label(s) of the participant(s) that should be '
'analyzed. The label corresponds to '
'sub-<subject-label> from the BIDS spec (so it does '
'not include "sub-"). If this parameter is not provided '
'all subjects should be analyzed. Multiple participants '
'can be specified with a space separated list.',
nargs="+")
parser.add_argument('--task-label',
help='The label(s) of the tasks(s) that should be '
'analyzed. If this parameter is not provided '
'all tasks should be analyzed. Multiple tasks '
'can be specified with a space separated list.',
nargs="+")
parser.add_argument('--session-label',
help='The label(s) of the session(s) that should be '
'analyzed. The label corresponds to '
'ses-<session-label> from the BIDS spec (so it does '
'not include "ses-"). If this parameter is not provided '
'all sessions should be analyzed. Multiple sessions '
'can be specified with a space separated list.',
nargs="+")
parser.add_argument('--sci-threshold', type=float, default=0.0,
help='Threshold below which a channel is marked as bad.')
parser.add_argument('--pp-threshold', type=float, default=0.0,
help='Threshold below which a channel is marked as bad.')
parser.add_argument('-v', '--version', action='version',
version='BIDS-App Scalp Coupling Index version '
f'{__version__}')
args = parser.parse_args()
def create_report(app_name=None, pargs=None):
exec_rep = dict()
exec_rep["ExecutionStart"] = datetime.now().isoformat()
exec_rep["ApplicationName"] = app_name
exec_rep["ApplicationVersion"] = __version__
exec_rep["Arguments"] = vars(pargs)
return exec_rep
exec_files = dict()
exec_rep =create_report(app_name="fNIRS-Apps: Quality Reports", pargs=args)
mne.set_log_level("INFO")
logger.info("\n")
########################################
# Extract parameters
########################################
logger.info("Extracting subject metadata.")
subs = []
if args.subject_label:
logger.info(" Subject data provided as input argument.")
subs = args.subject_label
else:
logger.info(" Subject data will be extracted from data.")
subs = get_entity_vals(args.input_datasets, 'subject')
logger.info(f" Subjects: {subs}")
logger.info("Extracting session metadata.")
sess = []
if args.session_label:
logger.info(" Session data provided as input argument.")
sess = args.session_label
else:
logger.info(" Session data will be extracted from data.")
sess = get_entity_vals(args.input_datasets, 'session')
if len(sess) == 0:
sess = [None]
logger.info(f" Sessions: {sess}")
logger.info("Extracting tasks metadata.")
tasks = []
if args.task_label:
logger.info(" Task data provided as input argument.")
tasks = args.task_label
else:
logger.info(" Session data will be extracted from data.")
tasks = get_entity_vals(args.input_datasets, 'task')
logger.info(f" Tasks: {tasks}")
########################################
# Report Sections
########################################
def plot_raw(raw, report):
logger.debug(" Creating raw plot")
fig1 = raw.plot(n_channels=len(raw.ch_names),
duration=raw.times[-1],
show_scrollbars=False, clipping=None)
msg = "Plot of the raw signal"
report.add_figure(fig=fig1, caption=msg, title="Raw Waveform")
return raw, report
def summarise_triggers(raw, report):
logger.debug(" Creating trigger summary")
events, event_dict = mne.events_from_annotations(raw, verbose=False)
fig2 = mne.viz.plot_events(events, event_id=event_dict,
sfreq=raw.info['sfreq'])
report.add_figure(fig=fig2, title="Triggers")
return raw, report
def summarise_montage(raw, report):
logger.debug(" Creating montage summary")
fig3 = raw.plot_sensors()
msg = f"Montage of sensors." \
f"Bad channels are marked in red: {raw.info['bads']}"
report.add_figure(fig=fig3, title="Montage", caption=msg)
return raw, report
def summarise_sci(raw, report, threshold=0.8):
logger.debug(" Creating SCI summary")
if raw.info['lowpass'] < 1.5:
print("SCI calculation being run over limited frequency range, check if valid.")
h_trans_bandwidth = 0.2
h_freq = 1.5 - raw.info['lowpass'] - 0.05
else:
h_freq = 1.5
h_trans_bandwidth = 0.3
sci = mne.preprocessing.nirs.scalp_coupling_index(raw, h_freq=h_freq, h_trans_bandwidth=h_trans_bandwidth)
raw.info['bads'] = list(compress(raw.ch_names, sci < threshold))
fig, ax = plt.subplots()
ax.hist(sci)
ax.set(xlabel='Scalp Coupling Index', ylabel='Count', xlim=[0, 1])
ax.axvline(linewidth=4, color='r', x=threshold)
msg = f"Scalp coupling index with threshold at {threshold}." \
f"Results in bad channels {raw.info['bads']}"
report.add_figure(fig=fig, caption=msg, title="Scalp Coupling Index")
return raw, report
def summarise_sci_window(raw, report, threshold=0.8):
logger.debug(" Creating windowed SCI summary")
if raw.info['lowpass'] < 1.5:
print("SCI calculation being run over limited frequency range, check if valid.")
h_trans_bandwidth = 0.2
h_freq = 1.5 - raw.info['lowpass'] - 0.05
else:
h_freq = 1.5
h_trans_bandwidth = 0.3
_, scores, times = scalp_coupling_index_windowed(raw, time_window=60, h_freq=h_freq, h_trans_bandwidth=h_trans_bandwidth)
fig = plot_timechannel_quality_metric(raw, scores, times,
threshold=threshold,
title="Scalp Coupling Index "
"Quality Evaluation")
msg = "Windowed SCI."
report.add_figure(fig=fig, title="SCI Windowed", caption=msg)
return raw, report
def summarise_pp(raw, report, threshold=0.8):
logger.debug(" Creating peak power summary")
if raw.info['lowpass'] < 1.5:
print("PP calculation being run over limited frequency range, check if valid.")
h_trans_bandwidth = 0.2
h_freq = 1.5 - raw.info['lowpass'] - 0.05
else:
h_freq = 1.5
h_trans_bandwidth = 0.3
_, scores, times = peak_power(raw, time_window=10, h_freq=h_freq, h_trans_bandwidth=h_trans_bandwidth)
fig = plot_timechannel_quality_metric(raw, scores, times,
threshold=threshold,
title="Peak Power "
"Quality Evaluation")
msg = "Windowed Peak Power."
report.add_figure(fig=fig, title="Peak Power", caption=msg)
return raw, report
def summarise_odpsd(raw, report):
logger.debug(" Creating PSD plot")
fig, ax = plt.subplots(ncols=2, figsize=(15, 8))
raw.plot_psd(ax=ax[0])
raw.plot_psd(ax=ax[1], average=True)
ax[1].set_title("Average +- std")
msg = "PSD of the optical density signal."
report.add_figure(fig=fig, title="OD PSD", caption=msg)
return raw, report
########################################
# Report script
########################################
def run_report(path, path_out):
report = mne.Report(verbose=True, raw_psd=True)
report.parse_folder(f"{path.directory}", render_bem=False, raw_butterfly=False)
raw = read_raw_bids(path)
raw, report = plot_raw(raw, report)
raw, report = summarise_triggers(raw, report)
raw = optical_density(raw)
raw, report = summarise_odpsd(raw, report)
raw, report = summarise_sci_window(raw, report, threshold=args.sci_threshold)
raw, report = summarise_pp(raw, report, threshold=args.pp_threshold)
raw, report = summarise_sci(raw, report, threshold=args.sci_threshold)
raw, report = summarise_montage(raw, report)
report.save(path_out, overwrite=True, open_browser=False)
return 1
########################################
# Main script
########################################
logger.info(" ")
Path(f"{args.output_location}").mkdir(parents=True, exist_ok=True)
for sub in subs:
for task in tasks:
for ses in sess:
logger.info(f"Processing: sub-{sub}/ses-{ses}/task-{task}")
exec_files[f"sub-{sub}_ses-{ses}_task-{task}"] = dict()
in_path = BIDSPath(subject=sub, task=task, session=ses,
root=f"{args.input_datasets}",
datatype="nirs", suffix="nirs",
extension=".snirf")
exec_files[f"sub-{sub}_ses-{ses}_task-{task}"]["FileName"] = str(in_path.fpath)
exec_files[f"sub-{sub}_ses-{ses}_task-{task}"]["FileHash"] = hashlib.md5(open(in_path.fpath, 'rb').read()).hexdigest()
out_path = BIDSPath(subject=sub, task=task, session=ses,
root=f"{args.output_location}",
datatype="nirs", suffix="qualityReport",
extension=".html", check=False)
if op.exists(in_path):
logger.info(f" Found file: {in_path}")
out_path.fpath.parent.mkdir(exist_ok=True, parents=True)
run_report(in_path, out_path)
else:
logger.info(f" No file exists: {in_path}")
exec_rep["Files"] = exec_files
exec_path = f"{args.input_datasets}/execution"
exec_rep["ExecutionEnd"] = datetime.now().isoformat()
Path(exec_path).mkdir(parents=True, exist_ok=True)
with open(f"{exec_path}/{exec_rep['ExecutionStart'].replace(':', '-')}-quality_reports.json", "w") as fp:
json.dump(exec_rep, fp)
pprint(exec_rep)