def _combine_volumes_write_out(info_pair): """Write out the given information to a nifti volume. Needs to be used by ModelProcessingWorker._combine_volumes """ map_name, info_list = info_pair chunks_dir, output_dir, volume_header, write_gzipped = info_list data = np.load(os.path.join(chunks_dir, map_name + '.npy'), mmap_mode='r') write_all_as_nifti({map_name: data}, output_dir, volume_header, gzip=write_gzipped) del data
def write_volume_maps(maps, directory, header, overwrite_volumes=True): """Write a dictionary with maps to the given directory using the given header. Args: maps (dict): The maps with as keys the map names and as values 3d or 4d maps directory (str): The dir to write to header: The Nibabel Image Header overwrite_volumes (boolean): If we want to overwrite the volumes if they are present. """ from mdt.nifti import write_all_as_nifti write_all_as_nifti(maps, directory, header, overwrite_volumes=overwrite_volumes)
def _combine_volumes(self, output_dir, tmp_storage_dir, nifti_header, maps_subdir=''): """Combine volumes found in subdirectories to a final volume. Args: output_dir (str): the location for the output files tmp_storage_dir (str): the directory with the temporary results maps_subdir (str): the subdirectory for both the output directory as the tmp storage directory. If this is set we will load the results from a subdirectory (with this name) from the tmp_storage_dir and write the results to a subdirectory (with this name) in the output dir. Returns: dict: the dictionary with the ROIs for every volume, by parameter name """ full_output_dir = os.path.join(output_dir, maps_subdir) if not os.path.exists(full_output_dir): os.makedirs(full_output_dir) for fname in os.listdir(full_output_dir): if fname.endswith('.nii.gz'): os.remove(os.path.join(full_output_dir, fname)) map_names = list( map(lambda p: os.path.splitext(os.path.basename(p))[0], glob.glob(os.path.join(tmp_storage_dir, maps_subdir, '*.npy')))) chunks_dir = os.path.join(tmp_storage_dir, maps_subdir) for map_name in map_names: data = np.load(os.path.join(chunks_dir, map_name + '.npy'), mmap_mode='r') write_all_as_nifti({map_name: data}, full_output_dir, nifti_header=nifti_header, gzip=self._write_volumes_gzipped)