-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntendedFor.py
More file actions
executable file
·56 lines (49 loc) · 1.87 KB
/
IntendedFor.py
File metadata and controls
executable file
·56 lines (49 loc) · 1.87 KB
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
#!/usr/bin/python
import json
import os
from glob import glob
import numpy
import subprocess
"""
This script is designed to determine which field maps apply to discrete fMRI scans
Author - Timothy J Hendrickson
"""
def setup(subject_path):
if "ses" in os.listdir(subject_path)[0]:
for item in (os.listdir(subject_path)):
session_path = subject_path + '/'+ item
IntendedFor(session_path)
IntendedFor(subject_path)
def IntendedFor(data_path):
for fmap in sorted(glob(data_path+'/fmap/*.json')):
# change fmap file permissions to allow write access
os.system("chmod 660 " + fmap)
with open(fmap, 'r') as f:
fmap_json = json.load(f)
f.close()
if "IntendedFor" in fmap_json:
del fmap_json["IntendedFor"]
shim_fmap = fmap_json["ShimSetting"]
patient_pos_fmap = fmap_json["ImageOrientationPatientDICOM"]
func_list = []
for func in sorted(glob(data_path+'/func/*bold.json')):
with open(func, 'r') as g:
func_json = json.load(g)
shim_func = func_json["ShimSetting"]
patient_pos_func = func_json["ImageOrientationPatientDICOM"]
g.close()
if shim_fmap == shim_func:
func_nii = glob(data_path+'/func/' +func.split('/')[-1].split('.')[0]+".nii*")[0]
if "ses" in data_path:
func_nii = "/".join(func_nii.split("/")[-3:])
func_list.append(func_nii)
else:
func_nii = "/".join(func_nii.split("/")[-2:])
func_list.append(func_nii)
entry = {"IntendedFor": func_list}
fmap_json.update(entry)
with open(fmap, 'w') as f:
json.dump(fmap_json, f)
f.close()
# change file permissions to read only
os.system("chmod 444 " + fmap)