-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_idps.py
251 lines (237 loc) · 9.28 KB
/
generate_idps.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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"""
Generate subject IDPs in TSV format from output of DEMISTIFI pipeline
"""
import argparse
import csv
import logging
import os
import sys
import pandas as pd
LOG = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
MEAN_PARAM="Interquartile mean"
#MEAN_PARAM="Mean"
class ArgumentParser(argparse.ArgumentParser):
def __init__(self, **kwargs):
argparse.ArgumentParser.__init__(self, prog="cmore_idps", add_help=True, **kwargs)
self.add_argument("--input", required=True, help="Input directory containing subject dirs")
self.add_argument("--statspath", help="Subject dir subfolder containing stats", default="stats")
self.add_argument("--output", help="Output filename", default="cmore_idps.csv")
# IDP definition. Mapping from:
# - organ -> available segmentations
# - segmentation name -> grid dataset
# - grid dataset -> Tuple of (parameter, scan, method)
#
# If grid dataset is empty, assumed to be same as segmentation grid
#
# Segmentation datasets follow naming convention:
# seg_<organ>_<segmentation name> (e.g. seg_liver_dixon)
#
# Re-gridded segmentations follow naming convention:
# seg_<organ>_<segmentation>_regrid_<grid dataset> (e.g. seg_liver_dixon_regrid_pancreas_gre)
#
# Parameter maps follow naming convention
# <variable>_<grid dataset>_[<method>] (e.g. t2star_pancreas_gre_presco)
#
# Note that parameters may be derived from maps targeting a particular
# organ but can also provide data for segmentations targeting a
# different organ
#
IDPDEF = {
"kidney_medulla_l" : {
"t1" : {
"" : [
("t1", "")
],
"t2star" : [
("t2star", "exp"),
("r2star", "exp"),
("t2star", "loglin"),
("r2star", "loglin"),
]
}
},
"kidney_cortex_l" : {
"t1" : {
"" : [
("t1", "")
],
"t2star" : [
("t2star", "exp"),
("r2star", "exp"),
("t2star", "loglin"),
("r2star", "loglin"),
]
}
},
"kidney_medulla_r" : {
"t1" : {
"" : [
("t1", "")
],
"t2star" : [
("t2star", "exp"),
("r2star", "exp"),
("t2star", "loglin"),
("r2star", "loglin"),
]
}
},
"kidney_cortex_r" : {
"t1" : {
"" : [
("t1", "")
],
"t2star" : [
("t2star", "exp"),
("r2star", "exp"),
("t2star", "loglin"),
("r2star", "loglin"),
]
}
},
"kidney_l" : {
"t2w" : {
"" : []
}
},
"kidney_r" : {
"t2w" : {
"" : []
}
},
"kidney" : {
"t2w" : {
"" : []
}
},
}
def get_seg_vols(options, subjid):
vols_tsv = os.path.join(options.input, subjid, options.statspath, "seg_volumes.tsv")
LOG.debug(f"Looking for segmentation volumes for subject {subjid} in {vols_tsv}")
if os.path.exists(vols_tsv):
return pd.read_csv(vols_tsv, sep="\t", index_col=0)
else:
LOG.warning(f"No volumes file: {vols_tsv}")
return pd.DataFrame()
def get_seg_vol(df, organ, seg, grid):
col_name = f"seg_{organ}_{seg}"
if grid:
col_name += f"_regrid_{grid}"
if col_name in list(df.columns):
n, vol = df[col_name]
if n == 0:
return "", ""
else:
return int(n), vol
else:
LOG.warning(f"No volume found for segmentation: {col_name}")
return "", ""
def strip_repeats(col_names):
new_col_names = []
for idx, col_name in enumerate(col_names):
if idx > 0 and col_names[idx-1] == col_name:
new_col_names.append("")
else:
new_col_names.append(col_name)
return new_col_names
def main():
options = ArgumentParser().parse_args()
subjids = [f for f in os.listdir(options.input) if os.path.isdir(os.path.join(options.input, f))]
#subjids = subjids[1:2]
out_idpcols, out_idplist = ["subjid",], []
organ_values, seg_values, grid_values, param_values, measure_values = [""], [""], [""], [""], ["subjid"]
for subj_idx, subjid in enumerate(sorted(subjids)):
subj_idpvals = [subjid,]
loaded_stats = {}
seg_vols_df = get_seg_vols(options, subjid)
LOG.info(f"Processing subject {subjid}")
LOG.debug(f"Found segmentation volumes for {seg_vols_df.columns}")
for organ, organdef in IDPDEF.items():
LOG.debug(f"Organ: {organ}")
for seg, segdef in organdef.items():
LOG.debug(f"Segmentation: {seg}")
for grid, params in segdef.items():
LOG.debug(f"Grid: {grid}")
n, vol = get_seg_vol(seg_vols_df, organ, seg, grid)
if subj_idx == 0:
output_col_name = f"{organ}_{seg}_{grid}"
out_idpcols.append(output_col_name + "_n")
out_idpcols.append(output_col_name + "_vol")
measure_values.append("n")
measure_values.append("vol")
for i in range(2):
organ_values.append(organ)
seg_values.append(seg)
grid_values.append(grid)
param_values.append("mask")
subj_idpvals.append(n)
subj_idpvals.append(vol)
LOG.debug(f"N, volume: {n} {vol}")
for paramdef in params:
param, method = paramdef
LOG.debug(f"Parameter: {paramdef}")
stats_dataset = f"{organ}_{seg}_stats.tsv"
if stats_dataset not in loaded_stats:
stats_tsv = os.path.join(options.input, subjid, options.statspath, stats_dataset)
if os.path.exists(stats_tsv):
LOG.debug(f"Loading stats from: {stats_tsv}")
loaded_stats[stats_dataset] = pd.read_csv(stats_tsv, sep="\s*\t\s*", engine="python", index_col=0)
else:
LOG.warning(f"No stats file: {stats_tsv}")
loaded_stats[stats_dataset] = pd.DataFrame()
col_name = param
param_name = param
#if grid:
# col_name += f"_{grid}"
if method:
col_name += f"_{method}"
param_name += f"_{method}"
LOG.debug(f"Looking for column: {col_name}")
if not n:
LOG.debug(f"Segmentation empty")
mean, std, median = "", "", ""
elif col_name in list(loaded_stats[stats_dataset].columns):
mean = loaded_stats[stats_dataset][col_name][MEAN_PARAM]
std = loaded_stats[stats_dataset][col_name]["Std"]
median = loaded_stats[stats_dataset][col_name]["Median"]
else:
LOG.debug(f"Not found: {col_name}")
mean, std, median = "", "", ""
if subj_idx == 0:
output_col_name = f"{organ}_{seg}_{col_name}"
out_idpcols.append(output_col_name + "_mean")
out_idpcols.append(output_col_name + "_std")
out_idpcols.append(output_col_name + "_median")
measure_values.append("mean")
measure_values.append("std")
measure_values.append("median")
for i in range(3):
organ_values.append(organ)
seg_values.append(seg)
grid_values.append(grid)
param_values.append(param_name)
subj_idpvals.append(mean)
subj_idpvals.append(std)
subj_idpvals.append(median)
out_idplist.append(subj_idpvals)
#df_out = pd.DataFrame(out_idplist, columns=out_idpcols)
#df_out.set_index("subjid", inplace=True)
#print(df_out)
#df_out.to_csv(options.output)
organ_values = strip_repeats(organ_values)
seg_values = strip_repeats(seg_values)
grid_values = strip_repeats(grid_values)
param_values = strip_repeats(param_values)
with open(options.output, 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
writer.writerow(out_idpcols)
writer.writerow(organ_values)
writer.writerow(seg_values)
writer.writerow(grid_values)
writer.writerow(param_values)
writer.writerow(measure_values)
for subj_values in out_idplist:
writer.writerow(subj_values)
if __name__ == "__main__":
main()