def initialize(self, data_collection): super(SkullStrip, self).initialize(data_collection) for label, data_group in data_collection.data_groups.iteritems(): reference_filename = data_group.data[data_collection.current_case][ self.reference_channel] self.mask_filename = self.generate_output_filename( reference_filename, self.mask_string) if type(data_group.preprocessed_case) is list: input_file = data_group.preprocessed_case[ self.reference_channel] else: # What to do about affines here... Also, reroute this file to a temporary directory. input_file = save_numpy_2_nifti( data_group.preprocessed_case[..., self.reference_channel], data_group.preprocessed_affine, self.generate_output_filename(reference_filename)) specific_command = self.command + [ quotes(input_file), quotes(self.mask_filename), '-f', str(self.bet2_f), '-g', str(self.bet2_g), '-m' ] subprocess.call(' '.join(specific_command), shell=True) os.rename(self.mask_filename + '_mask.nii.gz', self.mask_filename) self.mask_numpy = read_image_files(self.mask_filename, return_affine=False)
def preprocess(self, data_group): for file_idx, filename in enumerate(data_group.preprocessed_case): if self.reference_channel is None: specific_command = self.command + [ 'ResampleScalarVolume', '-i', self.interpolation, '-s', self.dimensions, quotes(filename), quotes(self.output_filenames[file_idx]) ] else: specific_command = self.command + [ 'ResampleScalarVectorDWIVolume', '-R', self.reference_channel, '--interpolation', self.interpolation_dict[self.interpolation], quotes(self.base_file), quotes(self.output_filename) ] subprocess.call(' '.join(specific_command), shell=True, stdout=FNULL, stderr=subprocess.STDOUT) self.output_data = self.output_filenames data_group.preprocessed_case = self.output_filenames
def preprocess(self, data_group): for file_idx, filename in enumerate(data_group.preprocessed_case): transform_filename = self.generate_output_filename(filename, self.transform_string, file_extension='.tfm') if self.reference_channel is not None: if self.reference_channel == file_idx: self.output_filenames[file_idx] = filename self.output_transforms += [None] continue else: specific_command = self.command + ['--fixedVolume', quotes(data_group.preprocessed_case[self.reference_channel]), '--transformType', self.transform_type, '--initializeTransformMode', self.transform_initialization, '--interpolationMode', self.interpolation, '--samplingPercentage', str(self.sampling_percentage), '--movingVolume', quotes(filename), '--outputVolume', quotes(self.output_filenames[file_idx]), '--outputTransform', quotes(transform_filename)] else: specific_command = self.command + ['--fixedVolume', quotes(self.reference_file), '--transformType', self.transform_type, '--initializeTransformMode', self.transform_initialization, '--interpolationMode', self.interpolation, '--samplingPercentage', str(self.sampling_percentage), '--movingVolume', quotes(self.base_file), '--outputVolume', quotes(self.output_filename), '--outputTransform', quotes(transform_filename)] subprocess.call(' '.join(specific_command), shell=True, stdout=FNULL, stderr=subprocess.STDOUT) # self.output_transforms += [read_image_files(transform_filename)] if not self.save_transforms: os.remove(transform_filename) self.output_data = self.output_filenames data_group.preprocessed_case = self.output_filenames
def save_input_2_dso(input_data, reference_dicom_filepath, dso_metadata, reference_nifti_filepath=None, output_filepath=None, command='/home/abeers/Software/dcmqi-1.0.9-linux/bin/itkimage2segimage', verbose=True): """ Given an DICOM directory to reference and a list of files (and in the future Nifti files), save these files to a DSO object. """ reference_dicom_filepath = os.path.abspath(reference_dicom_filepath) output_filepath = os.path.abspath(output_filepath) dso_metadata = os.path.abspath(dso_metadata) base_command = [command, '--inputDICOMDirectory', quotes(reference_dicom_filepath), '--outputDICOM', output_filepath, '--inputMetadata', dso_metadata, '--inputImageList'] if type(input_data) is not list: input_data = [input_data] input_data_string = '' for data_object in input_data: if input_data_string != '': input_data_string += ',' # Check this typing syntax for Python 3 if type(data_object) is str: data_object = os.path.abspath(data_object) input_data_string += data_object else: pass # TODO: Put in utility for converting numpy to DSO. base_command += [input_data_string] print((' '.join(base_command))) subprocess.call(' '.join(base_command), shell=True) return output_filepath
def preprocess(self, data_group): for file_idx, filename in enumerate(data_group.preprocessed_case): specific_command = self.command + ['N4ITKBiasFieldCorrection', quotes(filename), quotes(self.output_filenames[file_idx])] subprocess.call(' '.join(specific_command), shell=True, stdout=FNULL, stderr=subprocess.STDOUT) self.output_data = self.output_filenames data_group.preprocessed_case = self.output_filenames
def singularity_wrapper(command, cli_args=None, filename_args=None, interactive=False, container='deepneuro'): raise NotImplementedError if filename_args is not None: filename_args = [ arg for arg in filename_args if cli_args[arg] is not None ] mounted_dir = os.path.abspath( os.path.dirname( os.path.commonprefix([cli_args[arg] for arg in filename_args]))) for arg in filename_args: after_mounted = os.path.abspath(cli_args[arg]).split( mounted_dir, 1)[1].lstrip(os.sep) cli_args[arg] = quotes(os.path.join('/INPUT_DATA', after_mounted)) else: pass # TODO: Default behavior when mounted directory not needed. if interactive: docker_command = [ 'nvidia-docker', 'run', '-it', '-v', mounted_dir + ':/INPUT_DATA', container, 'bash' ] else: docker_command = [ 'nvidia-docker', 'run', '--rm', '-v', mounted_dir + ':/INPUT_DATA', container ] + command # This presumes everything is an optional arg, which is wrong. # Rewrite this to accept command line strings, instead of Python variables. # Would solve problem.. for arg in cli_args: if cli_args[arg] is True: docker_command += ['-' + str(arg)] elif not cli_args[arg] or cli_args[arg] is None: continue else: docker_command += ['-' + str(arg) + ' ' + cli_args[arg]] call(' '.join(docker_command), shell=True)