def _list_outputs(self): outputs = self.output_spec().get() if isdefined(self.inputs.out_file): outputs['out_file'] = os.path.abspath(self.inputs.out_file) else: outputs['out_file'] = os.path.abspath( self._filename_from_source('out_file')) return outputs
def _list_outputs(self): outputs = self.output_spec().get() if isdefined(self.inputs.out_file): outputs['out_file'] = os.path.abspath(self.inputs.out_file) else: outputs['out_file'] = os.path.abspath( fname_presuffix(os.path.basename(self.inputs.in_file), suffix='_histo_mask')) return outputs
def _run_interface(self, runtime): runtime = super(MathMorphoMask, self)._run_interface(runtime) if runtime is not None: if isdefined(self.inputs.out_file): tmp_file = os.path.abspath(self.inputs.out_file) else: tmp_file = os.path.abspath( self._filename_from_source('out_file')) # Ensure the affine is the same mask_data = nibabel.load(tmp_file).get_data() mask_img = image.new_img_like(self.inputs.in_file, mask_data) mask_img.to_filename(tmp_file) return runtime
def _run_interface(self, runtime): if isdefined(self.inputs.intensity_threshold): threshold = self.inputs.intensity_threshold img = image.math_img('img>{0}'.format(threshold), img=self.inputs.in_file) else: img = nibabel.load(self.inputs.in_file) lower_cutoff = self.inputs.upper_cutoff - 0.05 mask_img = masking.compute_epi_mask( img, lower_cutoff=lower_cutoff, upper_cutoff=self.inputs.upper_cutoff, connected=self.inputs.connected, opening=self.inputs.opening) mask_data = mask_img.get_data() n_voxels_mask = np.sum(mask_data > 0) # Find the optimal lower cutoff affine_det = np.abs(np.linalg.det(mask_img.affine[:3, :3])) n_voxels_min = int(self.inputs.volume_threshold * .9 / affine_det) while (n_voxels_mask < n_voxels_min) and (lower_cutoff > self.inputs.lower_cutoff): lower_cutoff -= .05 mask_img = masking.compute_epi_mask( img, lower_cutoff=lower_cutoff, upper_cutoff=self.inputs.upper_cutoff, connected=self.inputs.connected, opening=self.inputs.opening) mask_data = mask_img.get_data() n_voxels_mask = np.sum(mask_data > 0) if self.inputs.verbose: print('volume {0}, lower_cutoff {1}'.format( n_voxels_mask * affine_det, lower_cutoff)) n_voxels_max = int(self.inputs.volume_threshold * 1.1 / affine_det) previous_n_voxels_mask = copy.copy(lower_cutoff) while (n_voxels_mask > n_voxels_max) and ( previous_n_voxels_mask >= n_voxels_mask) and ( lower_cutoff + 0.01 < self.inputs.upper_cutoff): lower_cutoff += .01 mask_img = masking.compute_epi_mask( img, lower_cutoff=lower_cutoff, upper_cutoff=self.inputs.upper_cutoff, connected=self.inputs.connected, opening=self.inputs.opening) mask_data = mask_img.get_data() n_voxels_mask = np.sum(mask_data > 0) if self.inputs.verbose: print('volume {0}, lower_cutoff {1}'.format( n_voxels_mask * affine_det, lower_cutoff)) else: if n_voxels_mask < n_voxels_min: lower_cutoff -= .01 mask_img = masking.compute_epi_mask( img, lower_cutoff=lower_cutoff, upper_cutoff=self.inputs.upper_cutoff, connected=self.inputs.connected, opening=self.inputs.opening) mask_data = mask_img.get_data() n_voxels_mask = np.sum(mask_data > 0) if self.inputs.verbose: print('volume {0}, lower_cutoff {1}'.format( n_voxels_mask * affine_det, lower_cutoff)) # Find the optimal opening n_voxels_max = int(self.inputs.volume_threshold * 1.5 / affine_det) opening = 0 while n_voxels_mask > n_voxels_max and opening < self.inputs.opening: opening += 1 mask_img = masking.compute_epi_mask( img, lower_cutoff=lower_cutoff, upper_cutoff=self.inputs.upper_cutoff, connected=self.inputs.connected, opening=opening) mask_data = mask_img.get_data() n_voxels_mask = np.sum(mask_data > 0) if self.inputs.verbose: print('volume {0}, lower_cutoff {1}, opening {2}'.format( n_voxels_mask * affine_det, lower_cutoff, opening)) # Find the optimal closing iterations = 0 n_voxels_min = int(self.inputs.volume_threshold * .8 / affine_det) while (n_voxels_mask < n_voxels_min) and (iterations < self.inputs.closing): iterations += 1 for structure_size in range(1, 4): structure = generate_binary_structure(3, structure_size) mask_data = binary_closing(mask_data, structure=structure, iterations=iterations) n_voxels_mask = np.sum(mask_data > 0) if self.inputs.verbose: print('volume {0}, lower_cutoff {1}, opening {2}, closing ' '{3}'.format(n_voxels_mask * affine_det, lower_cutoff, opening, iterations)) if n_voxels_mask > n_voxels_min: break if self.inputs.verbose: print('volume {0}, lower_cutoff {1}, opening {2}, closing ' '{3}'.format(n_voxels_mask * affine_det, lower_cutoff, opening, iterations)) # Fill holes n_voxels_min = int(self.inputs.volume_threshold / affine_det) if n_voxels_mask < n_voxels_min: for structure_size in range(1, 4): structure = generate_binary_structure(3, structure_size) mask_data = binary_fill_holes(mask_data, structure=structure) n_voxels_mask = np.sum(mask_data > 0) if self.inputs.verbose: print('volume {0}, structure_size {1}'.format( n_voxels_mask * affine_det, structure_size)) if n_voxels_mask > n_voxels_min: break # Dilation if needed size = self.inputs.dilation_size for n in range(3): if n_voxels_mask < n_voxels_min * .9: previous_n_voxels_mask = copy.copy(n_voxels_mask) mask_data = grey_dilation(mask_data, size=size) n_voxels_mask = np.sum(mask_data > 0) if n_voxels_mask == previous_n_voxels_mask: size2 = (size[0] + 1, size[1] + 1, size[2] + 1) mask_data = grey_dilation(mask_data, size=size2) n_voxels_mask = np.sum(mask_data > 0) if self.inputs.verbose: print('volume {0}, grey dilation {1}'.format( n_voxels_mask * affine_det, n)) else: break # Fill holes for structure_size in range(1, 4): structure = generate_binary_structure(3, structure_size) mask_data = binary_fill_holes(mask_data, structure=structure) n_voxels_mask = np.sum(mask_data > 0) if self.inputs.verbose: print('final volume {0}'.format(n_voxels_mask * affine_det)) mask_img = image.new_img_like(mask_img, mask_data, mask_img.affine) if isdefined(self.inputs.out_file): mask_img.to_filename(os.path.abspath(self.inputs.out_file)) else: mask_img.to_filename( os.path.abspath( fname_presuffix(os.path.basename(self.inputs.in_file), suffix='_histo_mask'))) return runtime