Skip to content

Commit 7412ba9

Browse files
JulianKlugeffigies
andauthored
Apply suggestions from code review: Use header.get_zooms instead of header.pixdim
Co-authored-by: Chris Markiewicz <[email protected]>
1 parent e15bb8d commit 7412ba9

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

nibabel/imagestats.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""
1212

1313
import numpy as np
14+
from nibabel.imageclasses import spatial_axes_first
1415

1516

1617
def mask_volume(img, units='mm3'):
@@ -43,11 +44,10 @@ def mask_volume(img, units='mm3'):
4344
>>> mask_volume(img)
4445
50.3021
4546
"""
46-
header = img.header
47-
_, vx, vy, vz, _, _, _, _ = header['pixdim']
48-
voxel_volume_mm3 = vx * vy * vz
49-
mask = img.get_fdata()
50-
mask_volume_vx = np.sum(mask)
47+
if not spatial_axes_first(img):
48+
raise ValueError("Cannot calculate voxel volume for image with unknown spatial axes")
49+
voxel_volume_mm3 = np.prod(img.header.get_zooms()[:3])
50+
mask_volume_vx = np.count_nonzero(img.dataobj)
5151
mask_volume_mm3 = mask_volume_vx * voxel_volume_mm3
5252

5353
if units == 'vox':

0 commit comments

Comments
 (0)