def from_filename(cls, file_path, slice_number, slice_thickness, file_path_mask=None, verbose=False, ): slice = cls() if not ph.file_exists(file_path): raise exceptions.FileNotExistent(file_path) slice._dir_input = os.path.dirname(file_path) slice._filename = os.path.basename(file_path).split(".")[0] slice._slice_number = slice_number slice._slice_thickness = slice_thickness # Append stacks as SimpleITK and ITK Image objects slice.sitk = sitkh.read_nifti_image_sitk(file_path, sitk.sitkFloat64) slice.itk = sitkh.get_itk_from_sitk_image(slice.sitk) # Append masks (if provided) if file_path_mask is None: slice.sitk_mask = slice._generate_identity_mask() if verbose: ph.print_info( "Identity mask created for '%s'." % (file_path)) else: if not ph.file_exists(file_path_mask): raise exceptions.FileNotExistent(file_path_mask) slice.sitk_mask = sitkh.read_nifti_image_sitk( file_path_mask, sitk.sitkUInt8) try: # ensure mask occupies the same physical space slice.sitk_mask.CopyInformation(slice.sitk) except RuntimeError as e: raise IOError( "Given image and its mask do not occupy the same space: %s" % e.message) slice.itk_mask = sitkh.get_itk_from_sitk_image(slice.sitk_mask) # Store current affine transform of image slice._affine_transform_sitk = sitkh.get_sitk_affine_transform_from_sitk_image( slice.sitk) # Prepare history of affine transforms, i.e. encoded spatial # position+orientation of slice, and motion estimates of slice # obtained in the course of the registration/reconstruction process slice._history_affine_transforms = [] slice._history_affine_transforms.append(slice._affine_transform_sitk) slice._history_motion_corrections = [] slice._history_motion_corrections.append(sitk.Euler3DTransform()) return slice
def from_filename(cls, file_path, slice_number, file_path_mask=None, verbose=False): slice = cls() if not ph.file_exists(file_path): raise exceptions.FileNotExistent(file_path) slice._dir_input = os.path.dirname(file_path) slice._filename = os.path.basename(file_path).split(".")[0] slice._slice_number = slice_number # Append stacks as SimpleITK and ITK Image objects slice.sitk = sitk.ReadImage(file_path, sitk.sitkFloat64) slice.itk = sitkh.get_itk_from_sitk_image(slice.sitk) # Append masks (if provided) if file_path_mask is None: slice.sitk_mask = slice._generate_identity_mask() if verbose: ph.print_info("Identity mask created for '%s'." % (file_path)) else: if not ph.file_exists(file_path_mask): raise exceptions.FileNotExistent(file_path_mask) slice.sitk_mask = sitk.ReadImage(file_path_mask, sitk.sitkUInt8) slice.itk_mask = sitkh.get_itk_from_sitk_image(slice.sitk_mask) # Store current affine transform of image slice._affine_transform_sitk = sitkh.get_sitk_affine_transform_from_sitk_image( slice.sitk) # Prepare history of affine transforms, i.e. encoded spatial # position+orientation of slice, and motion estimates of slice # obtained in the course of the registration/reconstruction process slice._history_affine_transforms = [] slice._history_affine_transforms.append(slice._affine_transform_sitk) slice._history_motion_corrections = [] slice._history_motion_corrections.append(sitk.Euler3DTransform()) return slice
def from_filename( cls, file_path, file_path_mask=None, extract_slices=True, verbose=False, slice_thickness=None, ): stack = cls() if not ph.file_exists(file_path): raise exceptions.FileNotExistent(file_path) path_to_directory = os.path.dirname(file_path) # Strip extension from filename and remove potentially included "." filename = [ re.sub("." + ext, "", os.path.basename(file_path)) for ext in ALLOWED_EXTENSIONS if file_path.endswith(ext) ][0] # filename = filename.replace(".", "p") stack._dir = os.path.dirname(file_path) stack._filename = filename # Append stacks as SimpleITK and ITK Image objects stack.sitk = sitkh.read_nifti_image_sitk(file_path, sitk.sitkFloat64) stack.itk = sitkh.get_itk_from_sitk_image(stack.sitk) # Set slice thickness of acquisition if slice_thickness is None: stack._slice_thickness = stack.sitk.GetSpacing()[-1] else: stack._slice_thickness = slice_thickness # Append masks (either provided or binary mask) if file_path_mask is None: stack.sitk_mask = stack._generate_identity_mask() if verbose: ph.print_info("Identity mask created for '%s'." % (file_path)) else: if not ph.file_exists(file_path_mask): raise exceptions.FileNotExistent(file_path_mask) stack.sitk_mask = sitkh.read_nifti_image_sitk( file_path_mask, sitk.sitkUInt8) try: # ensure masks occupy same physical space stack.sitk_mask.CopyInformation(stack.sitk) except RuntimeError as e: raise IOError( "Given image and its mask do not occupy the same space: %s" % e.message) stack._is_unity_mask = False # Append itk object stack.itk_mask = sitkh.get_itk_from_sitk_image(stack.sitk_mask) # Store current affine transform of image stack._affine_transform_sitk = sitkh.get_sitk_affine_transform_from_sitk_image( stack.sitk) # Prepare history of affine transforms, i.e. encoded spatial # position+orientation of stack, and motion estimates of stack # obtained in the course of the registration/reconstruction process stack._history_affine_transforms = [] stack._history_affine_transforms.append(stack._affine_transform_sitk) stack._history_motion_corrections = [] stack._history_motion_corrections.append(sitk.Euler3DTransform()) # Extract all slices and their masks from the stack and store them if extract_slices: dimenson = stack.sitk.GetDimension() if dimenson == 3: stack._N_slices = stack.sitk.GetSize()[-1] stack._slices = stack._extract_slices( slice_thickness=stack.get_slice_thickness()) elif dimenson == 2: stack._N_slices = 1 stack._slices = [stack.sitk[:, :]] else: stack._N_slices = 0 stack._slices = None if verbose: ph.print_info( "Stack (image + mask) associated to '%s' successfully read." % (file_path)) return stack
def from_filename(cls, file_path, file_path_mask=None, extract_slices=True, verbose=False): stack = cls() if not ph.file_exists(file_path): raise exceptions.FileNotExistent(file_path) path_to_directory = os.path.dirname(file_path) # Strip extension from filename and remove potentially included "." filename = [re.sub("." + ext, "", os.path.basename(file_path)) for ext in ALLOWED_EXTENSIONS if file_path.endswith(ext)][0] # filename = filename.replace(".", "p") stack._dir = os.path.dirname(file_path) stack._filename = filename # Append stacks as SimpleITK and ITK Image objects stack.sitk = sitk.ReadImage(file_path, sitk.sitkFloat64) stack.itk = sitkh.get_itk_from_sitk_image(stack.sitk) # Append masks (either provided or binary mask) if file_path_mask is None: stack.sitk_mask = stack._generate_identity_mask() if verbose: ph.print_info( "Identity mask created for '%s'." % (file_path)) else: if not ph.file_exists(file_path_mask): raise exceptions.FileNotExistent(file_path_mask) stack.sitk_mask = sitk.ReadImage(file_path_mask, sitk.sitkUInt8) stack._is_unity_mask = False # Append itk object stack.itk_mask = sitkh.get_itk_from_sitk_image(stack.sitk_mask) # Extract all slices and their masks from the stack and store them if extract_slices: dimenson = stack.sitk.GetDimension() if dimenson == 3: stack._N_slices = stack.sitk.GetSize()[-1] stack._slices = stack._extract_slices() elif dimenson == 2: stack._N_slices = 1 stack._slices = [stack.sitk[:, :]] else: stack._N_slices = 0 stack._slices = None if verbose: ph.print_info( "Stack (image + mask) associated to '%s' successfully read." % (file_path)) return stack