Skip to content
Snippets Groups Projects
Commit d4ce30ae authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

Various python 2/3 fixes in dcmstack

parent 07fe08cf
No related branches found
No related tags found
No related merge requests found
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
Stack DICOM datasets into volumes. The contents of this module are imported Stack DICOM datasets into volumes. The contents of this module are imported
into the package namespace. into the package namespace.
""" """
from __future__ import division
import warnings, re, dicom import warnings, re, dicom
from copy import deepcopy from copy import deepcopy
from collections import MutableSequence from collections import MutableSequence
...@@ -611,11 +612,11 @@ class DicomStack(object): ...@@ -611,11 +612,11 @@ class DicomStack(object):
key=lambda x: x[1][-1]) key=lambda x: x[1][-1])
#Do a thorough check for correctness #Do a thorough check for correctness
for vec_idx in xrange(num_vec_comps): for vec_idx in range(num_vec_comps):
file_idx = vec_idx*num_time_points*files_per_vol file_idx = vec_idx*num_time_points*files_per_vol
curr_vec_val = self._files_info[file_idx][1][0] curr_vec_val = self._files_info[file_idx][1][0]
for time_idx in xrange(num_time_points): for time_idx in range(num_time_points):
for slice_idx in xrange(files_per_vol): for slice_idx in range(files_per_vol):
file_idx = (vec_idx*num_time_points*files_per_vol + file_idx = (vec_idx*num_time_points*files_per_vol +
time_idx*files_per_vol + slice_idx) time_idx*files_per_vol + slice_idx)
file_info = self._files_info[file_idx] file_info = self._files_info[file_idx]
...@@ -664,7 +665,7 @@ class DicomStack(object): ...@@ -664,7 +665,7 @@ class DicomStack(object):
#If more than one file per volume, check that slice spacing is equal #If more than one file per volume, check that slice spacing is equal
if files_per_vol > 1: if files_per_vol > 1:
spacings = [] spacings = []
for idx in xrange(files_per_vol - 1): for idx in range(files_per_vol - 1):
spacings.append(slice_positions[idx+1] - slice_positions[idx]) spacings.append(slice_positions[idx+1] - slice_positions[idx])
spacings = np.array(spacings) spacings = np.array(spacings)
avg_spacing = np.mean(spacings) avg_spacing = np.mean(spacings)
...@@ -675,7 +676,7 @@ class DicomStack(object): ...@@ -675,7 +676,7 @@ class DicomStack(object):
if len(self._files_info) % files_per_vol != 0: if len(self._files_info) % files_per_vol != 0:
raise InvalidStackError("Number of files is not an even multiple " raise InvalidStackError("Number of files is not an even multiple "
"of the number of unique slice positions.") "of the number of unique slice positions.")
num_volumes = len(self._files_info) / files_per_vol num_volumes = len(self._files_info) // files_per_vol
#Figure out the number of vector components and time points #Figure out the number of vector components and time points
num_vec_comps = len(self._vector_vals) num_vec_comps = len(self._vector_vals)
...@@ -684,7 +685,7 @@ class DicomStack(object): ...@@ -684,7 +685,7 @@ class DicomStack(object):
if num_volumes % num_vec_comps != 0: if num_volumes % num_vec_comps != 0:
raise InvalidStackError("Number of volumes not an even multiple " raise InvalidStackError("Number of volumes not an even multiple "
"of the number of vector components.") "of the number of vector components.")
num_time_points = num_volumes / num_vec_comps num_time_points = num_volumes // num_vec_comps
#If both sort keys are None try to guess #If both sort keys are None try to guess
if (num_volumes > 1 and self._time_order is None and if (num_volumes > 1 and self._time_order is None and
...@@ -704,7 +705,7 @@ class DicomStack(object): ...@@ -704,7 +705,7 @@ class DicomStack(object):
#Try out each possible sort order #Try out each possible sort order
for time_order in possible_orders: for time_order in possible_orders:
#Update sorting tuples #Update sorting tuples
for idx in xrange(len(self._files_info)): for idx in range(len(self._files_info)):
nii_wrp, curr_tuple = self._files_info[idx] nii_wrp, curr_tuple = self._files_info[idx]
self._files_info[idx] = (nii_wrp, self._files_info[idx] = (nii_wrp,
(curr_tuple[0], (curr_tuple[0],
...@@ -778,7 +779,7 @@ class DicomStack(object): ...@@ -778,7 +779,7 @@ class DicomStack(object):
n_vols *= stack_shape[3] n_vols *= stack_shape[3]
if len(stack_shape) > 4: if len(stack_shape) > 4:
n_vols *= stack_shape[4] n_vols *= stack_shape[4]
files_per_vol = len(self._files_info) / n_vols files_per_vol = len(self._files_info) // n_vols
file_shape = self._files_info[0][0].nii_img.get_shape() file_shape = self._files_info[0][0].nii_img.get_shape()
for vec_idx in range(stack_shape[4]): for vec_idx in range(stack_shape[4]):
for time_idx in range(stack_shape[3]): for time_idx in range(stack_shape[3]):
...@@ -866,7 +867,7 @@ class DicomStack(object): ...@@ -866,7 +867,7 @@ class DicomStack(object):
if len(data.shape) > 4: if len(data.shape) > 4:
n_vols *= data.shape[4] n_vols *= data.shape[4]
files_per_vol = len(self._files_info) / n_vols files_per_vol = len(self._files_info) // n_vols
#Reorder the voxel data if requested #Reorder the voxel data if requested
permutation = [0, 1, 2] permutation = [0, 1, 2]
...@@ -884,11 +885,11 @@ class DicomStack(object): ...@@ -884,11 +885,11 @@ class DicomStack(object):
#This will keep the slice times and meta data order correct #This will keep the slice times and meta data order correct
if files_per_vol > 1 and flips[slice_dim] == -1: if files_per_vol > 1 and flips[slice_dim] == -1:
self._shape_dirty = True self._shape_dirty = True
for vol_idx in xrange(n_vols): for vol_idx in range(n_vols):
start = vol_idx * files_per_vol start = vol_idx * files_per_vol
stop = start + files_per_vol stop = start + files_per_vol
self._files_info[start:stop] = [self._files_info[idx] self._files_info[start:stop] = [self._files_info[idx]
for idx in xrange(stop - 1, for idx in range(stop - 1,
start - 1, start - 1,
-1) -1)
] ]
...@@ -928,7 +929,7 @@ class DicomStack(object): ...@@ -928,7 +929,7 @@ class DicomStack(object):
#If there is more than one volume, check if times are consistent #If there is more than one volume, check if times are consistent
is_consistent = True is_consistent = True
for vol_idx in xrange(1, n_vols): for vol_idx in range(1, n_vols):
start_slice = vol_idx * n_slices start_slice = vol_idx * n_slices
end_slice = start_slice + n_slices end_slice = start_slice + n_slices
slices_info = self._files_info[start_slice:end_slice] slices_info = self._files_info[start_slice:end_slice]
...@@ -954,7 +955,7 @@ class DicomStack(object): ...@@ -954,7 +955,7 @@ class DicomStack(object):
#Build meta data for each volume if needed #Build meta data for each volume if needed
vol_meta = [] vol_meta = []
if files_per_vol > 1: if files_per_vol > 1:
for vol_idx in xrange(n_vols): for vol_idx in range(n_vols):
start_slice = vol_idx * n_slices start_slice = vol_idx * n_slices
end_slice = start_slice + n_slices end_slice = start_slice + n_slices
exts = [file_info[0].meta_ext exts = [file_info[0].meta_ext
...@@ -969,7 +970,7 @@ class DicomStack(object): ...@@ -969,7 +970,7 @@ class DicomStack(object):
if len(data.shape) == 5: if len(data.shape) == 5:
if data.shape[3] != 1: if data.shape[3] != 1:
vec_meta = [] vec_meta = []
for vec_idx in xrange(data.shape[4]): for vec_idx in range(data.shape[4]):
start_idx = vec_idx * data.shape[3] start_idx = vec_idx * data.shape[3]
end_idx = start_idx + data.shape[3] end_idx = start_idx + data.shape[3]
meta = DcmMetaExtension.from_sequence(\ meta = DcmMetaExtension.from_sequence(\
...@@ -1169,7 +1170,7 @@ def parse_and_stack(src_paths, group_by=default_group_keys, extractor=None, ...@@ -1169,7 +1170,7 @@ def parse_and_stack(src_paths, group_by=default_group_keys, extractor=None,
force, force,
warn_on_except) warn_on_except)
for key, group in results.iteritems(): for key, group in results.items():
results[key] = stack_group(group, warn_on_except, **stack_args) results[key] = stack_group(group, warn_on_except, **stack_args)
return results return results
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment