-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_rois-mmp_npz.py
More file actions
executable file
·216 lines (181 loc) · 10 KB
/
Copy pathcreate_rois-mmp_npz.py
File metadata and controls
executable file
·216 lines (181 loc) · 10 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
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
"""
-----------------------------------------------------------------------------------------
create_rois-mmp_npz.py
-----------------------------------------------------------------------------------------
Goal of the script:
Create 170k npz mmp atlas files and a tsv of rois numbers and names
-----------------------------------------------------------------------------------------
Input(s):
sys.argv[1]: main project directory
sys.argv[2]: project name (correspond to directory)
sys.argv[3]: group of shared data (e.g. 327)
-----------------------------------------------------------------------------------------
Output(s):
.npz masks for roi-mmp and roi-group-mmp for both 91k and 170k template
-----------------------------------------------------------------------------------------
To run LOCALLY with Inkscape installed:
1. cd to function
>> cd ~/disks/meso_H/projects/pRF_analysis/analysis_code/atlas
2. run python command
python create_rois-mmp_npz.py [main directory] [project name] [group]
-----------------------------------------------------------------------------------------
Exemple:
cd ~/disks/meso_H/projects/pRF_analysis/analysis_code/atlas
python create_rois-mmp_npz.py /Users/uriel/disks/meso_shared RetinoMaps 327
python create_rois-mmp_npz.py /Users/sinakling/disks/meso_shared amsterdam24 327
python create_rois-mmp_npz.py /scratch/mszinte/data amblyo7T_prf 327
-----------------------------------------------------------------------------------------
Written by Uriel Lascombes (uriel.lascombes@laposte.net)
-----------------------------------------------------------------------------------------
"""
# Stop warnings
import warnings
warnings.filterwarnings("ignore")
# Debug
import ipdb
deb = ipdb.set_trace
# General imports
import os
import sys
import shutil
import numpy as np
# Personal imports
sys.path.append("{}/../../analysis_code/utils".format(os.getcwd()))
from pycortex_rois_utils import *
from settings_utils import load_settings
from pycortex_utils import set_pycortex_config_file
# Inputs
main_dir = sys.argv[1]
project_dir = sys.argv[2]
group = sys.argv[3]
# Load settings
base_dir = os.path.abspath(os.path.join(os.getcwd(), "../../"))
settings_path = os.path.join(base_dir, project_dir, "settings.yml")
prf_settings_path = os.path.join(base_dir, project_dir, "prf-analysis.yml")
settings = load_settings([settings_path, prf_settings_path])
analysis_info = settings[0]
rois_group_mmp = analysis_info['rois-group-mmp']
formats = analysis_info['formats_conversion']
pycortex_subject_formats = analysis_info['pycortex_subject_formats']
# Set pycortex db and colormaps
cortex_dir = "{}/{}/derivatives/pp_data/cortex".format(main_dir, project_dir)
set_pycortex_config_file(cortex_dir)
for full_brain_format, cortex_format in formats.items():
subject = pycortex_subject_formats[cortex_format]
# Load rois mmp mask
roi_dir = '{}/{}/derivatives/pp_data/cortex/db/{}/rois'.format(
main_dir, project_dir, subject)
roi_mmp_brain_npz_fn = '{}/{}_{}_rois-mmp.npz'.format(
roi_dir, subject, cortex_format)
roi_mmp_lh_npz_fn = '{}/{}_hemi-L_{}_rois-mmp.npz'.format(
roi_dir, subject, cortex_format)
roi_mmp_rh_npz_fn = '{}/{}_hemi-R_{}_rois-mmp.npz'.format(
roi_dir, subject, cortex_format)
rois_mmp_masks_brain = dict(np.load(roi_mmp_brain_npz_fn))
rois_mmp_masks_lh = dict(np.load(roi_mmp_lh_npz_fn))
rois_mmp_masks_rh = dict(np.load(roi_mmp_rh_npz_fn))
# Load ful brain mask to extend rois masks
full_brain_to_cortex_mask_fn = '{}/{}/derivatives/pp_data/cortex/db/{}/masks/{}_cortex_mask.npz'.format(
main_dir, project_dir, subject, full_brain_format)
full_brain_to_cortex_mask = dict(np.load(full_brain_to_cortex_mask_fn))['brain_mask']
cortex_plus_to_cortex_mask_fn = '{}/{}/derivatives/pp_data/cortex/db/{}/masks/{}_cortex_mask.npz'.format(
main_dir, project_dir, subject, cortex_format)
cortex_plus_to_cortex_mask = dict(np.load(cortex_plus_to_cortex_mask_fn))['brain_mask']
# make full brain masks
rois_mmp_masks_full_lh_dict = {}
rois_mmp_masks_full_rh_dict = {}
rois_mmp_masks_full_brain_dict = {}
n_vert_lh = next(iter(rois_mmp_masks_lh.values())).shape[0]
n_vert_rh = next(iter(rois_mmp_masks_rh.values())).shape[0]
n_vert_full = full_brain_to_cortex_mask.shape[0]
# --- Left hemisphere ---
for roi_name, roi_mask in rois_mmp_masks_lh.items():
cortex_plus_to_cortex_mask_lh = cortex_plus_to_cortex_mask[:n_vert_lh]
roi_mask_cortex_lh = roi_mask[cortex_plus_to_cortex_mask_lh]
vertex_to_add_lh = np.full(n_vert_full - roi_mask_cortex_lh.shape[0], False)
rois_mmp_masks_full_lh_dict[roi_name] = np.concatenate([roi_mask_cortex_lh, vertex_to_add_lh])
# --- Right hemisphere ---
for roi_name, roi_mask in rois_mmp_masks_rh.items():
cortex_plus_to_cortex_mask_rh = cortex_plus_to_cortex_mask[n_vert_lh:]
roi_mask_cortex_rh = roi_mask[cortex_plus_to_cortex_mask_rh]
vertex_to_add_rh = np.full(n_vert_full - (roi_mask_cortex_rh.shape[0] + roi_mask_cortex_lh.shape[0]), False)
rois_mmp_masks_full_rh_dict[roi_name] = np.concatenate([np.full(roi_mask_cortex_lh.shape[0], False), roi_mask_cortex_rh, vertex_to_add_rh])
# --- Full brain ROIs ---
for roi_name, roi_mask in rois_mmp_masks_brain.items():
roi_mask_cortex_brain = roi_mask[cortex_plus_to_cortex_mask]
vertex_to_add_brain = np.full(n_vert_full - roi_mask_cortex_brain.shape[0], False)
rois_mmp_masks_full_brain_dict[roi_name] = np.concatenate([roi_mask_cortex_brain, vertex_to_add_brain])
# Export masks as npz
print('Saving roi mmp npz')
np.savez('{}/{}_hemi-L_{}_rois-mmp.npz'.format(
roi_dir, subject, full_brain_format), **rois_mmp_masks_full_lh_dict)
np.savez('{}/{}_hemi-R_{}_rois-mmp.npz'.format(
roi_dir, subject, full_brain_format), **rois_mmp_masks_full_rh_dict)
np.savez('{}/{}_{}_rois-mmp.npz'.format(
roi_dir, subject, full_brain_format), **rois_mmp_masks_full_brain_dict)
# make rois-mmp-group dict
rois_group_mmp_masks_lh = {}
rois_group_mmp_masks_rh = {}
rois_group_mmp_masks_brain = {}
for group_name, roi_list in rois_group_mmp.items():
masks_lh = [rois_mmp_masks_lh[roi] for roi in roi_list if roi in rois_mmp_masks_lh]
masks_rh = [rois_mmp_masks_rh[roi] for roi in roi_list if roi in rois_mmp_masks_rh]
masks_brain = [rois_mmp_masks_brain[roi] for roi in roi_list if roi in rois_mmp_masks_brain]
if len(masks_lh) > 0:
rois_group_mmp_masks_lh[group_name] = np.logical_or.reduce(masks_lh)
if len(masks_rh) > 0:
rois_group_mmp_masks_rh[group_name] = np.logical_or.reduce(masks_rh)
if len(masks_brain) > 0:
rois_group_mmp_masks_brain[group_name] = np.logical_or.reduce(masks_brain)
# Export masks as npz
print('Saving roi group mmp npz')
np.savez('{}/{}_hemi-L_{}_rois-group-mmp.npz'.format(
roi_dir, subject, cortex_format), **rois_group_mmp_masks_lh)
np.savez('{}/{}_hemi-R_{}_rois-group-mmp.npz'.format(
roi_dir, subject, cortex_format), **rois_group_mmp_masks_rh)
np.savez('{}/{}_{}_rois-group-mmp.npz'.format(
roi_dir, subject, cortex_format), **rois_group_mmp_masks_brain)
# make full brain masks
rois_group_mmp_masks_full_lh_dict = {}
rois_group_mmp_masks_full_rh_dict = {}
rois_group_mmp_masks_full_brain_dict = {}
n_vert_lh = next(iter(rois_mmp_masks_lh.values())).shape[0]
n_vert_rh = next(iter(rois_mmp_masks_rh.values())).shape[0]
# --- Left hemisphere ---
for roi_name, roi_mask in rois_group_mmp_masks_lh.items():
cortex_plus_to_cortex_mask_lh = cortex_plus_to_cortex_mask[:n_vert_lh]
roi_mask_cortex_lh = roi_mask[cortex_plus_to_cortex_mask_lh]
vertex_to_add_lh = np.full(n_vert_full - roi_mask_cortex_lh.shape[0], False)
rois_group_mmp_masks_full_lh_dict[roi_name] = np.concatenate([roi_mask_cortex_lh, vertex_to_add_lh])
# --- Right hemisphere ---
for roi_name, roi_mask in rois_group_mmp_masks_rh.items():
cortex_plus_to_cortex_mask_rh = cortex_plus_to_cortex_mask[n_vert_lh:]
roi_mask_cortex_rh = roi_mask[cortex_plus_to_cortex_mask_rh]
vertex_to_add_rh = np.full(n_vert_full - (roi_mask_cortex_rh.shape[0] + roi_mask_cortex_lh.shape[0]), False)
rois_group_mmp_masks_full_rh_dict[roi_name] = np.concatenate([np.full(roi_mask_cortex_lh.shape[0], False), roi_mask_cortex_rh, vertex_to_add_rh])
# --- Full brain ROIs ---
for roi_name, roi_mask in rois_group_mmp_masks_brain.items():
roi_mask_cortex_brain = roi_mask[cortex_plus_to_cortex_mask]
vertex_to_add_brain = np.full(n_vert_full - roi_mask_cortex_brain.shape[0], False)
rois_group_mmp_masks_full_brain_dict[roi_name] = np.concatenate([roi_mask_cortex_brain, vertex_to_add_brain])
# Export masks as npz
print('Saving roi group mmp npz')
np.savez('{}/{}_hemi-L_{}_rois-group-mmp.npz'.format(
roi_dir, subject, full_brain_format), **rois_group_mmp_masks_full_lh_dict)
np.savez('{}/{}_hemi-R_{}_rois-group-mmp.npz'.format(
roi_dir, subject, full_brain_format), **rois_group_mmp_masks_full_rh_dict)
np.savez('{}/{}_{}_rois-group-mmp.npz'.format(
roi_dir, subject, full_brain_format), **rois_group_mmp_masks_full_brain_dict)
# Project borders of rois on overlays
overlays_fn = '{}/db/{}/overlays.svg'.format(cortex_dir, subject)
overlays_rois_group_mmp_fn = '{}/db/{}/overlays_rois-group-mmp.svg'.format(cortex_dir, subject)
# Copy overlays
shutil.copy(overlays_fn, overlays_rois_group_mmp_fn)
# ROIs group MMP
rp = ROIpack(subject, '{}/{}_{}_rois-group-mmp.npz'.format(
roi_dir, subject, cortex_format))
rp.to_svg(filename=overlays_rois_group_mmp_fn, res=1024)
# Define permission cmd
print('Changing files permissions 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))