-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_rois_img.py
154 lines (129 loc) · 6.21 KB
/
make_rois_img.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
"""
-----------------------------------------------------------------------------------------
make_rois_img.py
-----------------------------------------------------------------------------------------
Goal of the script:
Make Cifti and Gifti object with rois
-----------------------------------------------------------------------------------------
Input(s):
sys.argv[1]: main project directory
sys.argv[2]: project name (correspond to directory)
sys.argv[2]: subject (e.g. sub-01)
sys.argv[3]: group (e.g. 327)
-----------------------------------------------------------------------------------------
Output(s):
Combined estimate nifti file and pRF derivative nifti file
-----------------------------------------------------------------------------------------
To run:
1. cd to function
>> cd ~/projects/[PROJECT]/analysis_code/postproc/prf/postfit
2. run python command
>> python make_rois_img.py [main directory] [project name] [subject] [group]
-----------------------------------------------------------------------------------------
Exemple:
cd ~/projects/pRF_analysis/analysis_code/postproc/prf/postfit
python make_rois_img.py /scratch/mszinte/data MotConf sub-01 327
python make_rois_img.py /scratch/mszinte/data MotConf sub-170k 327
python make_rois_img.py /scratch/mszinte/data RetinoMaps sub-01 327
python make_rois_img.py /scratch/mszinte/data RetinoMaps sub-170k 327
python make_rois_img.py /scratch/mszinte/data amblyo_prf sub-01 327
python make_rois_img.py /scratch/mszinte/data amblyo_prf sub-170k 327
-----------------------------------------------------------------------------------------
Written by Uriel Lascombes ([email protected])
Edited by Martin Szinte ([email protected])
-----------------------------------------------------------------------------------------
"""
# Stop warnings
import warnings
warnings.filterwarnings("ignore")
# debug
import ipdb
deb = ipdb.set_trace
# General imports
import os
import sys
import json
import numpy as np
import nibabel as nb
# personal imports
sys.path.append("{}/../../../utils".format(os.getcwd()))
from surface_utils import make_surface_image, load_surface
from pycortex_utils import get_rois, set_pycortex_config_file
# Inputs
main_dir = sys.argv[1]
project_dir = sys.argv[2]
subject = sys.argv[3]
group = sys.argv[4]
# Load settings
base_dir = os.path.abspath(os.path.join(os.getcwd(), "../../../../"))
settings_path = os.path.join(base_dir, project_dir, "settings.json")
with open(settings_path) as f:
json_s = f.read()
analysis_info = json.loads(json_s)
rois = analysis_info["rois"]
if subject == 'sub-170k': formats = ['170k']
else: formats = analysis_info['formats']
if subject == 'sub-170k': extensions = ['dtseries.nii']
else: extensions = analysis_info['extensions']
prf_task_name = analysis_info['prf_task_name']
# Set pycortex db and colormaps
cortex_dir = "{}/{}/derivatives/pp_data/cortex".format(main_dir, project_dir)
set_pycortex_config_file(cortex_dir)
# Create roi image files
for format_, extension in zip(formats, extensions):
print(format_)
rois_dir = '{}/{}/derivatives/pp_data/{}/{}/rois'.format(
main_dir, project_dir, subject, format_)
os.makedirs(rois_dir, exist_ok=True)
if format_ == 'fsnative':
# Load rois
roi_verts_dict_L, roi_verts_dict_R = get_rois(subject,
return_concat_hemis=False,
rois=rois,
mask=True,
atlas_name=None,
surf_size=None)
for hemi in ['hemi-L','hemi-R']:
if hemi == 'hemi-L':roi_verts_dict = roi_verts_dict_L
elif hemi == 'hemi-R':roi_verts_dict = roi_verts_dict_R
array_rois = np.zeros(len(next(iter(roi_verts_dict.values()))), dtype=int)
for i, (key, mask) in enumerate(roi_verts_dict.items(), 1):
array_rois[mask] = i
# Load data to have source img
data_dir = '{}/{}/derivatives/pp_data/{}/{}/prf/prf_derivatives'.format(
main_dir, project_dir, subject, format_)
data_fn = '{}_task-{}_{}_fmriprep_dct_avg_prf-deriv_gauss_gridfit.{}'.format(subject, prf_task_name, hemi, extension)
img, data = load_surface(fn='{}/{}'.format(data_dir, data_fn))
# Define filename
rois_fn = '{}_{}_rois.{}'.format(subject, hemi, extension)
# Saving file
array_rois = array_rois.reshape(1, -1)
rois_img = make_surface_image(data=array_rois, source_img=img, maps_names=['rois'])
nb.save(rois_img, '{}/{}'.format(rois_dir, rois_fn))
print('Saving {}/{}'.format(rois_dir, rois_fn))
elif format_ == '170k':
roi_verts_dict = get_rois(subject,
return_concat_hemis=True,
rois=rois,
mask=True,
atlas_name='mmp_group',
surf_size='170k')
array_rois = np.zeros(len(next(iter(roi_verts_dict.values()))), dtype=int)
for i, (key, mask) in enumerate(roi_verts_dict.items(), 1):
array_rois[mask] = i
# Load data to have source img
data_dir = '{}/{}/derivatives/pp_data/{}/{}/prf/prf_derivatives'.format(
main_dir, project_dir, subject, format_)
data_fn = '{}_task-{}_fmriprep_dct_avg_prf-deriv_gauss_gridfit.{}'.format(subject, prf_task_name, extension)
img, data = load_surface(fn='{}/{}'.format(data_dir, data_fn))
# Define filename
rois_fn = '{}_rois.{}'.format(subject, extension)
# Saving file
array_rois = array_rois.reshape(1, -1)
rois_img = make_surface_image(data=array_rois, source_img=img, maps_names=['rois'])
nb.save(rois_img, '{}/{}'.format(rois_dir, rois_fn))
print('Saving {}/{}'.format(rois_dir, rois_fn))
# Change permission
print('Changing permission in {}/{}'.format(main_dir, project_dir))
os.system("chmod -Rf 771 {}/{}".format(main_dir, project_dir))
os.system("chgrp -Rf {} {}/{}".format(group, main_dir, project_dir))