-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmap2atlas.py
514 lines (407 loc) · 19.5 KB
/
map2atlas.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
#! /usr/bin/env python3
# * map2atlas
# Vincent Koppelmans
# 2021-09-23
# * Libraries
import argparse
import os
import nibabel as nb
import numpy as np
from scipy import ndimage
from nipype.interfaces import freesurfer, fsl
# * Import arguments
# Gather arguments
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='map2atlas')
parser.add_argument('mapf',
help='The input image for which you want to know which '
'atlas ROIs it overlaps with.')
parser.add_argument('atlas',
help='Atlas from which you want to know which ROIs '
'overlap with your map file.')
parser.add_argument('outdir',
help='Output folder')
parser.add_argument('--othr1',
help='Minimal percent overlap of the ROI with the map '
'to be selected. E.g., an ROI needs to be filled for '
'at least x percent by the map before it will be '
'selected. Default is 50 percent.',
default=50,
choices=range(0, 101),
metavar='[0-100]',
type=float)
parser.add_argument('--othr2',
help='Minimal percent overlap of the map clusters and '
'the ROI to be selected. Default is 50 percent. E.g., '
'After the map is broken up into clusters, each '
'needs to fit for at least x percent inside an ROI '
'before that ROI is selected. Default = 50 percent',
default=50,
choices=range(0, 101),
metavar='[0-100]',
type=float)
parser.add_argument('--mthr',
help='Map threshold (lower, upper) level to create a '
'binary image from your map file. Default = 0,1.',
nargs=2,
default=[0, 1],
metavar=('low', 'high'))
parser.add_argument('--com',
help='Only select ROIs if their center-of-mass is '
'inside the map.',
default=False,
action='store_true')
parser.add_argument('--parts',
help='Run either part1, or part 1 and part2 of this '
'script (see under description). Default = both',
default="both",
choices=["p1", "both"])
args = parser.parse_args()
# * Empty Object Creator
class Scratch(object):
pass
# * Build map2atlas Object
class map2atlas:
def __init__(self):
# ** Environment
class arguments:
def __init__(self):
self.mapf = args.mapf
self.atlas = args.atlas
self.outdir = args.outdir
self.othr1 = float(args.othr1)
self.othr2 = float(args.othr2)
self.mthr = args.mthr
self.com = args.com
self.parts = args.parts
# Call: Prepare map data
self.args = arguments()
# ** Create output folder
os.makedirs(self.args.outdir, exist_ok=True)
# ** Prepare map data
class map_data:
def __init__(self, mapf, mthr, outdir):
# *** Load map data
self.img = nb.load(mapf)
# *** Threshold and binarize map data
self.binimg = self.img.get_fdata()
self.binimg = np.where(
self.binimg < float(mthr[1]), self.binimg, 0
)
self.binimg = np.where(self.binimg > float(mthr[0]), 1, 0)
# *** Save thresholded image
self.outimg = nb.Nifti1Image(
self.binimg, self.img.affine, self.img.header
)
self.outfile = outdir + '/map_t.nii.gz'
nb.save(self.outimg, self.outfile)
# Call: Prepare map data
self.map_data = map_data(
self.args.mapf, self.args.mthr, self.args.outdir
)
# ** Preapare atlas data
class atlas_data:
def __init__(self, odir, mapf, atlas):
# *** Load atlas data
self.img = nb.load(atlas)
# *** Reslice atlas image to map space
# nipype object for reslicing
self.outfile = odir + '/atlas_r.nii.gz'
self.reslice = freesurfer.MRIConvert()
self.reslice.inputs.in_file = atlas
self.reslice.inputs.out_file = self.outfile
self.reslice.inputs.reslice_like = mapf
self.reslice.inputs.resample_type = 'nearest'
self.reslice.terminal_output = 'none'
self.results = self.reslice.run()
# *** List unique ROI values in the atlas file
self.imgrdata = nb.load(self.outfile)
self.roinums = np.unique(self.imgrdata.get_fdata())
# Remove zero, because that is not a label
self.roinums = self.roinums[self.roinums != 0]
# Call: Prepare atlas data
self.atlas_data = atlas_data(
self.args.outdir, self.args.mapf, self.args.atlas
)
# ** -------------------------------------------------------------------
# ** PART 1: Select ROIs that sufficiently fit inside the map image
# ** -------------------------------------------------------------------
if args.parts == "p1" or args.parts == "both":
# ** Announce
print("PART 1: Selecting ROIs that sufficiently fit inside the map"
"image...")
# ** Function to test if atlas ROI overlaps with the map
def test_roi(i, atld, mapd, thr):
# *** Extract ROI (binary)
roibin = np.where(atld == i, 1, 0)
# *** Size of the ROI in voxels
roisize = np.sum(roibin)
# *** If the center of mass flag was enabled
if self.args.com:
# Calculate center of mass coordinates
roicom = ndimage.center_of_mass(roibin)
# Get the value of the COM coordinate from the map data
map_roicom = mapd[
round(roicom[0]), round(roicom[1]), round(roicom[2])
]
# *** Continue calculating the overlap of the ROI and the map
# Only if COM was not enabled, or when it was and the COM
# overlaps with the map data
if not self.args.com or map_roicom == 1:
# Use the ROI as a mask for the map.
map_masked = np.multiply(mapd, roibin)
# Count the number of voxels in this masked image
map_masked_size = np.sum(map_masked)
# Calculate what % this is of the original ROI size
map_masked_perc = (map_masked_size / roisize) * 100
# If this % passes the user threshold select this ROI
if map_masked_perc >= thr:
select_roi = 1
else:
select_roi = 0
# ...in all other cases, don't select this ROI
else:
select_roi = 0
map_masked_size = 0
map_masked_perc = 0
# Return output
return([select_roi, roisize, map_masked_size, map_masked_perc])
# Add function to main the object
self.test_roi = test_roi
# ** Output array to store which ROIs were selected
self.output1 = np.empty((np.size(self.atlas_data.roinums), 5))
# ** Function to run the overlap-test function for all ROIs
def run_test_roi(roinums, atld, mapd, thr):
# *** Start row index
outrow = 0
# *** Loop over ROIs
for i in roinums:
# **** Store the ROI number in the output matrix
self.output1[outrow, 0] = i
# **** Test for overlap
select_roi = self.test_roi(i, atld, mapd, thr)
# **** Store output in the output matrix
self.output1[outrow, 1:5] = select_roi
# **** Continue to the next row on the output matrix
outrow = outrow + 1
# Add function to the main object
self.run_test_roi = run_test_roi
# ** Run the funtion to do all overlap testing
self.run_test_roi(
self.atlas_data.roinums,
self.atlas_data.imgrdata.get_fdata(),
self.map_data.binimg,
self.args.othr1
)
# ** Save output matrix to output file in output folder
ofile = self.args.outdir + '/ROI_selectionR.csv'
np.savetxt(
ofile, self.output1, delimiter=',', fmt='%i,%i,%i,%i,%3.2f'
)
# ** Add header
# I don't want to use Pandas to include a header, so I will just add
# a header to the text file.
with open(ofile) as fp:
data = fp.read()
header = "ROI value,Select R,ROI size (v),Overlap (v),Overlap (%)"
with open(ofile, 'w') as fp:
fp.write(header + "\n" + data)
# ** -------------------------------------------------------------------
# ** PART 2: Select ROIs that contain a sufficiently large portion of
# ** the input image
# ** -------------------------------------------------------------------
if args.parts == "both":
# Select all ROIs that individually comprise a certain
# (user-defined) part of a free-floating cluster of the input map
# image. This can be helpful if the map contains small clusters that
# are of interest, but which are not big enough to fill an entire
# ROI for the user-defined threshold percentage.
# ** Announce
print("PART 2: Selecting ROIs that contain a sufficiently large "
"portion of the input image...")
# ** Convert the input map into individual clusters
# A cluster is defined as a cluster of ones surrounded by zeros in
# the user input map, after binarization based on the user set
# threshold.
class map_cluster:
def __init__(self, maptr, outdir):
# *** Output file name
self.outfile = outdir + '/map_tc.nii.gz'
# *** Extract clustes
self.cl = fsl.Cluster()
self.cl.inputs.threshold = 1
self.cl.inputs.in_file = maptr
self.cl.inputs.out_index_file = self.outfile
self.cl.terminal_output = 'none'
self.results = self.cl.run()
# *** List the number of clusters
self.imgc = nb.load(self.outfile)
self.cnums = np.unique(self.imgc.get_fdata())
# Remove zero, because that is not a label
self.cnums = self.cnums[self.cnums != 0]
# Call: Convert the input map to individual clusters
self.map_cluster = map_cluster(
self.map_data.outfile, self.args.outdir
)
# ** Function to test if a cluster fits inside an ROI above threshld
def test_cluster(mapc, c, atld, i, thr):
# *** Extract cluster
clubin = np.where(mapc == c, 1, 0)
# *** Size of the cluster in voxels
clusize = np.sum(clubin)
# Number of voxels at minimal overlap
clusize_t = (clusize / 100) * thr
# *** Test if it is even possible for the cluster to fit the ROI
# If the number of voxels of the cluster at the percentage
# threshold is bigger than the ROI, it will never fit for that
# minimum percentage inside the ROI.
# E.g., if the cluster=10 voxels, and the ROI=2 voxels, the
# cluster (even at a trheshold of 50%) would not fit inside the
# ROI. We can quickly test this, because we have the size of the
# cluster and the size of the ROI in our output matrix.
# *** Look up ROI size
roisize = self.output1[self.output1[:, 0] == i][0, 2]
# *** Only continue if the ROI size is bigger than the cluster
if roisize > clusize_t:
# *** Extract ROI (binary)
roibin = np.where(atld == i, 1, 0)
# Use the ROI as a mask for the cluster map.
map_masked = np.multiply(clubin, roibin)
# Count the number of voxels in this masked image
map_masked_size = np.sum(map_masked)
# Calculate what % this is of the original cluster size
map_masked_perc = (map_masked_size / clusize) * 100
# If this % passes the user threshold select this ROI
if map_masked_perc >= thr:
select_roi = 1
else:
select_roi = 0
# ...in all other cases, don't select this ROI
else:
select_roi = 0
map_masked_size = 0
map_masked_perc = 0
# Return output
return([select_roi, roisize, clusize, clusize_t,
map_masked_size, map_masked_perc])
# Add function to main the object
self.test_cluster = test_cluster
# ** Output array to store which ROIs were selected
self.output2 = np.empty((
np.size(
self.atlas_data.roinums
) * np.size(
self.map_cluster.cnums
),
8
))
# ** Function to run the overlap function for all clusters and ROIs
def run_test_clu(clunums, roinums, mapc, atld, thr):
# *** Start row index
outrow = 0
# *** Loop over clusters
for c in clunums:
# **** Loop over ROIs
for i in roinums:
# ***** Store the Cluster and ROI number in the matrix
self.output2[outrow, 0] = c
self.output2[outrow, 1] = i
# ***** Test for overlap
select_roi = self.test_cluster(mapc, c, atld, i, thr)
# ***** Store output in the output matrix
self.output2[outrow, 2:9] = select_roi
# ***** Continue to the next row on the output matrix
outrow = outrow + 1
# Add function to the main object
self.run_test_clu = run_test_clu
# ** Run the funtion to do all overlap testing
self.run_test_clu(
self.map_cluster.cnums,
self.atlas_data.roinums,
self.map_cluster.imgc.get_fdata(),
self.atlas_data.imgrdata.get_fdata(),
self.args.othr2
)
# ** Save output matrix to output file in output folder
ofile = self.args.outdir + '/ROI_selectionC.csv'
np.savetxt(
ofile, self.output2, delimiter=',',
fmt='%i,%i,%i,%i,%i,%i,%i,%3.2f'
)
# ** Add header
# I don't want to use Pandas to include a header, so I will just add
# a header to the text file.
with open(ofile) as fp:
data = fp.read()
header = 'Cluster,ROI value,Select R,ROI size (v),' \
'Cluster size (v), Cluster size thr (v),Overlap (v),' \
'Overlap (%)'
with open(ofile, 'w') as fp:
fp.write(header + "\n" + data)
# ** -------------------------------------------------------------------
# ** PART 3: Create an output image with all clusters
# ** -------------------------------------------------------------------
# ** Announce
print("Creating an output nifti image with all selected clusters...")
print("\nSelected ROIs:")
# ** Function to create output image
# That is, all selected ROIs with their original values
def build_outimg(omatrix, atld):
# ** Create empty output image
outimg = np.zeros(self.map_data.binimg.shape)
# ** Grab list of seleced ROIs
selection1 = np.array([])
selection2 = np.array([])
# *** Selected ROIs for Part 1
if args.parts == "p1" or args.parts == "both":
selection1 = self.output1[self.output1[:, 1] == 1]
selection1 = selection1[:, 0]
print("Part 1: [n=" + str(len(selection1)) + "]: " + " ".join(
map(str, selection1)).replace(".0", " "))
# *** Write out final selection if only Part 1 was selected
if args.parts == "p1":
data = ",".join(map(str, selection1)).replace(".0", "")
ofile = self.args.outdir + '/ROI_selection_list.csv'
with open(ofile, 'w') as fp:
fp.write(data)
# *** Selected ROIs for Part 2
if args.parts == "both":
selection2 = self.output2[self.output2[:, 2] == 1]
selection2 = selection2[:, 1]
print("Part 2: [n=" + str(len(selection2)) + "]: " + " ".join(
map(str, selection2)).replace(".0", " "))
# *** Combine selections
selection = np.concatenate((selection1, selection2))
selection = np.unique(selection)
if args.parts == "both":
print("Total: [n=" + str(len(selection)) + "]: " + " ".join(
map(str, selection)).replace(".0", " "))
# *** Write out final selection if 'Both' was selected
if args.parts == "both":
data = ",".join(map(str, selection)).replace(".0", "")
ofile = self.args.outdir + '/ROI_selection_list.csv'
with open(ofile, 'w') as fp:
fp.write(data)
# ** Loop over selected ROIs and add them together
for ROI in selection:
# *** Extract ROI (original value)
roiimg = np.where(atld == ROI, atld, 0)
# *** Add up
outimg = outimg + roiimg
return(outimg)
# Add function to the main object
self.build_outimg = build_outimg
# ** Save output image to output file in output folder
ofile = self.args.outdir + '/ROI_selection.nii.gz'
odata = nb.Nifti1Image(
self.build_outimg(
self.output1,
self.atlas_data.imgrdata.get_fdata()
),
self.map_data.img.affine,
self.map_data.img.header
)
nb.save(odata, ofile)
# * Run code
m2a = map2atlas()