Skip to content

Commit b35205f

Browse files
author
Arno Klein
committed
Revise thickinthehead algorithm to fix issue #149
1 parent ccb6170 commit b35205f

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

mindboggle/shapes/volume_shapes.py

+24-18
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ def volume_per_brain_region(input_file, include_labels=[], exclude_labels=[],
8787

8888
# Load labeled image volumes:
8989
img = nb.load(input_file)
90-
hdr = img.get_header()
91-
volume_per_voxel = np.product(hdr.get_zooms())
90+
volume_per_voxel = np.product(img.header.get_zooms())
9291
labels = img.get_data().ravel()
9392

9493
unique_labels, counts = count_per_label(labels, include_labels,
@@ -370,28 +369,34 @@ def thickinthehead(segmented_file, labeled_file, cortex_value=2,
370369
# ------------------------------------------------------------------------
371370
if resize:
372371
rescale = 2.0
372+
maxdim = 257
373373
else:
374374
rescale = 1.0
375-
compute_real_volume = True
376-
if compute_real_volume:
377-
img = nb.load(cortex)
378-
hdr = img.get_header()
379-
vv_orig = np.prod(hdr.get_zooms())
380-
vv = np.prod([x/rescale for x in hdr.get_zooms()])
381-
cortex_data = img.get_data().ravel()
382-
else:
383-
vv = 1/rescale
384-
cortex_data = nb.load(cortex).get_data().ravel()
375+
maxdim = 514
376+
img = nb.load(cortex)
377+
dims = img.header.get_data_shape()
378+
379+
if any([x > maxdim for x in dims]) and resize:
380+
raise IOError("Image dimensions greater than " + str(maxdim) +
381+
" voxels, which may be too large for thickinthehead "
382+
"to resample.")
383+
384+
voxdims0 = img.header.get_zooms()
385+
voxdims = [x / rescale for x in voxdims0]
386+
voxvol0 = np.prod(voxdims0)
387+
voxvol = np.prod(voxdims)
388+
cortex_data = img.get_data().ravel()
385389

386390
# ------------------------------------------------------------------------
387391
# Resample cortex and noncortex files from 1x1x1 to 0.5x0.5x0.5
388392
# to better represent the contours of the boundaries of the cortex:
389393
# ------------------------------------------------------------------------
390394
if resize:
391-
dims = ' '.join([str(1/rescale), str(1/rescale), str(1/rescale)])
392-
cmd = [ants_resample, '3', cortex, cortex, dims, '0 0 1']
395+
#voxdims_str = ' '.join([str(1/rescale), str(1/rescale), str(1/rescale)])
396+
voxdims_str = ' '.join([str(x) for x in voxdims])
397+
cmd = [ants_resample, '3', cortex, cortex, voxdims_str, '0 0 1']
393398
execute(cmd, 'os')
394-
cmd = [ants_resample, '3', noncortex, noncortex, dims, '0 0 1']
399+
cmd = [ants_resample, '3', noncortex, noncortex, voxdims_str, '0 0 1']
395400
execute(cmd, 'os')
396401

397402
# ------------------------------------------------------------------------
@@ -447,12 +452,13 @@ def thickinthehead(segmented_file, labeled_file, cortex_value=2,
447452
# - Estimate the thickness of the labeled cortical region as the
448453
# volume of the labeled region divided by the surface area.
449454
# --------------------------------------------------------------------
450-
label_cortex_volume = vv_orig * len(np.where(cortex_data==label)[0])
451-
label_inner_edge_volume = vv * len(np.where(inner_edge_data==label)[0])
455+
label_cortex_volume = voxvol0 * len(np.where(cortex_data==label)[0])
456+
label_inner_edge_volume = voxvol * \
457+
len(np.where(inner_edge_data==label)[0])
452458
if label_inner_edge_volume:
453459
if use_outer_edge:
454460
label_outer_edge_volume = \
455-
vv * len(np.where(outer_edge_data==label)[0])
461+
voxvol * len(np.where(outer_edge_data==label)[0])
456462
label_area = (label_inner_edge_volume +
457463
label_outer_edge_volume) / 2.0
458464
else:

0 commit comments

Comments
 (0)