def configure(self): if isinstance(nib, UnsupportedPackage): nib.raise_error(self.__provider__) self.channels_first = self.get_value_from_config('channels_first') self.multi_infer = self.get_value_from_config('multi_infer') if not self.data_source: raise ConfigError( 'data_source parameter is required to create "{}" ' 'data reader and read data'.format(self.__provider__))
def configure(self): if isinstance(nib, UnsupportedPackage): nib.raise_error(self.__provider__) self.channels_first = self.get_value_from_config('channels_first') self.multi_infer = self.get_value_from_config('multi_infer') self.frame_axis = int(self.get_value_from_config('frame_axis')) self.frame_separator = self.get_value_from_config('frame_separator') self.multi_frame = self.get_value_from_config('multi_frame') self.to_4D = self.get_value_from_config('to_4D') if not self.data_source: if not self._postpone_data_source: raise ConfigError('data_source parameter is required to create "{}" ' 'data reader and read data'.format(self.__provider__)) else: self.data_source = get_path(self.data_source, is_directory=True)
def convert(self, check_content=False, **kwargs): if self.multi_frame and isinstance(nib, UnsupportedPackage): nib.raise_error(self.__provider__) if self.as_regression: annotation_class = NiftiRegressionAnnotation reader_config = { 'to_4D': False, 'multi_frame': self.multi_frame, 'separator': self.frame_separator, 'frame_axis': self.frame_axis } else: annotation_class = BrainTumorSegmentationAnnotation reader_config = { 'loader': GTMaskLoader.NIFTI_CHANNELS_FIRST if self.mask_channels_first else GTMaskLoader.NIFTI } mask_folder = Path(self.mask_folder) image_folder = Path(self.image_folder) image_dir = get_path(self.data_dir / image_folder, is_directory=True) mask_dir = get_path(self.data_dir / mask_folder, is_directory=True) content_check_errors = [] if check_content else None annotations = [] for file_in_dir in image_dir.iterdir(): file_name = file_in_dir.parts[-1] mask_file_name = (file_name.rsplit('.', 1)[0] + '.nii.gz' if not file_name.endswith('.nii.gz') else file_name) if not self.relaxed_names: mask = mask_dir / mask_file_name else: mask = self.find_file_by_id(mask_dir, mask_file_name) mask_file_name = mask.parts[-1] if not mask.exists(): if not check_content: warnings.warn( 'Annotation mask for {} does not exist. File will be ignored.' .format(file_name)) else: content_check_errors.append( '{}: annotation mask does not exist, please remove this file or add gt mask ({})' .format(file_in_dir, mask)) continue if self.multi_frame: nib_image = nib.load(file_in_dir) total_frames = nib_image.dataobj.shape[self.frame_axis] for frame_cnt in range(total_frames): annotation = annotation_class( "{}{}{}".format(str(image_folder / file_name), self.frame_separator, frame_cnt), "{}{}{}".format(str(mask_folder / mask_file_name), self.frame_separator, frame_cnt), **reader_config) annotations.append(annotation) else: annotation = annotation_class( str(image_folder / file_name), str(mask_folder / mask_file_name), **reader_config) annotations.append(annotation) return ConverterReturn(annotations, self.get_meta(), content_check_errors)