-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcmd_parcellotron.py
168 lines (144 loc) · 6.5 KB
/
cmd_parcellotron.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
# -*- coding: utf-8 -*-
import argparse
import numpy as np
import time
import os
from sklearn.cluster import KMeans
import model.parcellobject as pa
import model.matrix_transformations as mt
import model.similarity_matrices as sm
import model.parcellation_methods as pm
parser = argparse.ArgumentParser(description="Calculate the parcellation of\
brain images")
# The available modalities
modality_arr = ['Tracto_4D', 'Tracto_mat']
sim_mat_arr = ['covariance', 'correlation']
mat_transform_arr = ['log2', 'zscore', 'log2_zscore', 'rank', 'none']
parcellation_method_arr = ['KMeans', 'PCA']
rotation_arr = ['quartimax', 'varimax']
# I need subcommands
# https://docs.python.org/3/library/argparse.html#module-argparse
group = parser.add_mutually_exclusive_group()
group.add_argument('-s', '--subject', type=str, help="the subject folder path")
group.add_argument('-g', '--group', type=str,
help="the root folder path, containing the subject folders")
group.add_argument('-lo', '--loop', type=str,
help="the root folder path, containing the subject folders.\
The parcellation will be performed on every subjects")
# parser.add_argument("subj_path", type=str, help="the subject folder path")
parser.add_argument("-sp", "--seed_pref", type=str, default='',
help="A prefix to find a particular seedROIs/seedMask file")
parser.add_argument("-tp", "--target_pref", type=str, default='',
help="A prefix to find a particular targetMask file")
parser.add_argument("modality", type=str, help="the input modality",
choices=modality_arr)
parser.add_argument('-rs', '--ROIs_size', type=int, default=0,
help='The seed ROIs size in cubic millimeter (Only for \
Tracto_mat)')
sub_parsers = parser.add_subparsers(help='Choose the parcellation method',
dest='parcellation_method')
parser_PCA = sub_parsers.add_parser('PCA', help='Parcellate your data using \
the PCA algorithm')
parser_PCA.add_argument('-r', '--rotation', help='Select the factor rotation',
type=str, default='quartimax', choices=rotation_arr)
parser_KMeans = sub_parsers.add_parser('KMeans', help="Parcellate your data \
using the KMeans algorithm")
parser_KMeans.add_argument('num_clu', help='Choose the number of cluster you \
want to find with the KMeans', type=int)
parser.add_argument("-sim", "--similarity_matrix", type=str,
dest="similarity_matrix",
help="type of similarity_matrix you want",
choices=sim_mat_arr, default=None)
parser.add_argument("-t", "--transform", type=str,
help="the transformation(s) to apply to the similarity \
matrix", choices=mat_transform_arr)
# parser.add_argument("parcellation_method", type=str,
# help="the parcellation methods to use",
# choices=parcellation_method_arr)
# group = parser.add_mutually_exclusive_group()
# group.add_argument("-v", "--verbose", action="store_true")
# group.add_argument("-q", "--quiet", action="store_true")
# parser.add_argument("y", type=int, help="the exponent")
# answer = args.x**args.y
args = parser.parse_args()
# Mettre la méthode the parcellisation avant la séléction de la transformation et de
# la similarity. Distance matrix il faut le disable
def parcellate_obj(group_level, files_arr, mod, size, transformation,
sim_mat, method, param_parcellate, seed_pref='',
target_pref=''):
for dir in files_arr:
if mod == 'Tracto_4D':
subj_obj = pa.Tracto_4D(dir,
group_level=group_level,
seed_pref=seed_pref,
target_pref=target_pref)
elif mod == "Tracto_mat":
subj_obj = pa.Tracto_mat(dir, size, group_level=group_level,
seed_pref=seed_pref,
target_pref=target_pref)
else:
raise Exception(mod + " is not yet implemented")
mat_2D = subj_obj.co_mat_2D
import view.display_intermediates as di
# di.mat_visualization(mat_2D)
tr_mat = subj_obj.mat_transform(transformation, mat_2D)
# di.mat_visualization(subj_obj.tr_mat_2D)
subj_obj.similarity(sim_mat, subj_obj.tr_mat_2D)
# di.mat_visualization(subj_obj.sim_mat)
labels = subj_obj.parcellate(method, subj_obj.sim_mat, param_parcellate)
if not group_level:
subj_obj.write_clusters()
print("FINISHED !")
# We launch the right function on the parameters
print(args)
def filter_args(args):
path = args.subject
group = args.group
loop = args.loop
if group != None:
group_level = True
else:
group_level = False
if group != None or loop != None:
files_arr = [dir for dir in sorted(
os.listdir(path)) if dir != '_group_level']
elif path != None:
files_arr = [path]
else:
raise Exception("file structure option unkown")
roisize = args.ROIs_size
mod = args.modality
tr = args.transform
sim = args.similarity_matrix
meth = args.parcellation_method
if 'rotation' in args:
param_parcellate = args.rotation
else:
param_parcellate = int(args.num_clu)
seed = args.seed_pref
tar = args.target_pref
if tr == None:
if meth == 'PCA':
tr = 'log2_zscore'
else:
tr = 'log2'
if sim == None:
if meth == 'PCA':
sim = 'covariance'
else:
sim = 'correlation'
return_arr = [group_level, files_arr, mod, roisize, tr, sim, meth,
param_parcellate, seed, tar]
return return_arr
# The parcellation method will determine the default values of transformations
# and similarity_matrix
# subj_labels = parcellate_obj(args.subject,
# args.group,
# args.modality,
# args.transform,
# args.similarity_matrix,
# args.parcellation_method,
# param_parcellate,
# args.seed_pref,
# args.target_pref)
subj_labels = parcellate_obj(*filter_args(args))