-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_mindcontrol.py
101 lines (92 loc) · 2.96 KB
/
auto_mindcontrol.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
#! python
import argparse
from pathlib import Path
import json
from bids.grabbids import BIDSLayout
import subprocess
import os
import shutil
parser = argparse.ArgumentParser(description='Autogenerate mindcontrol settings from a BIDS directory')
parser.add_argument('--server', action = 'store_true',
help='Start a simple file server in the bids directory.')
parser.add_argument('--meteor', action = 'store_true',
help='Start the meteor server that will serve mindcontrol.')
parser.add_argument('bids_dir', help='The directory with the input dataset '
'formatted according to the BIDS standard.')
args = parser.parse_args()
bids_dir = args.bids_dir
file_server = "http://127.0.0.1:3002/"
# Default settings for each module
default_module = {
"fields": [
{
"function_name": "get_qc_viewer",
"id": "name",
"name": "Image File"
},
{
"function_name": "get_qc_ave_field",
"id": "average_vote",
"name": "QC vote"
},
{
"function_name": None,
"id": "num_votes",
"name": "# votes"
},
{
"function_name": None,
"id": "quality_check.notes_QC",
"name": "Notes"
}
],
"metric_names": None,
"graph_type": None,
"staticURL": file_server,
"usePeerJS": False,
"logPainter": False,
"logContours": False,
"logPoints": True,
"qc_options": {"pass": 1, "fail": 1, "needs_edits": 0, "edited": 0, "assignTo": 0, "notes": 1, "confidence": 1}}
layout = BIDSLayout(bids_dir)
# Loop through files, adding them to the manifest
# add entity types to a set to be used to populate the
# autogenerated settings
manifest = []
entry_types = set()
for img in layout.get(extensions = ".nii.gz"):
img_dict = {}
img_dict["check_masks"] = [img.filename.replace(bids_dir,"")]
entry_types.add(img.type)
img_dict["entry_type"] = img.type
img_dict["metrics"] = {}
img_dict["name"] = os.path.split(img.filename)[1].split('.')[0]
img_dict["subject"] = 'sub-' + img.subject
img_dict["session"] = 'ses-' + img.session
manifest.append(img_dict)
# Populate modules list
modules = []
for et in entry_types:
et_module = default_module.copy()
et_module["name"] = et
et_module["entry_type"] = et
modules.append(et_module)
# autogenerated settings files
pub_set = {"startup_json": file_server+"startup.auto.json",
"load_if_empty": True,
"use_custom": True,
"needs_consent": False,
"modules": modules}
settings = {"public": pub_set}
# Write out file list to bids directory
with open(os.path.join(bids_dir,"startup.auto.json"), "w") as h:
json.dump(manifest,h)
# Write out the settings file to the mindcontrol directory
with open("settings.auto.json", "w") as h:
json.dump(settings,h)
if args.server:
shutil.copy2("start_static_server.py",bids_dir)
subprocess.Popen("python start_static_server.py", cwd = bids_dir, shell = True)
if args.meteor:
meteor = subprocess.Popen("meteor --settings settings.auto.json".split(' '))
meteor.wait()