def _test_load_images_from_channels( metadata: Any, image_channel: Any, ground_truth_channel: Any, mask_channel: Any) -> None: """ Test if images are loaded as expected from channels """ sample = io_util.load_images_from_dataset_source( PatientDatasetSource( metadata=metadata, image_channels=[image_channel] * 2, ground_truth_channels=[ground_truth_channel] * 4, mask_channel=mask_channel ) ) if image_channel: image_with_header = io_util.load_image(image_channel) assert list(sample.image.shape) == [2] + list(image_with_header.image.shape) assert all([np.array_equal(x, image_with_header.image) for x in sample.image]) # type: ignore if mask_channel: assert np.array_equal(sample.mask, image_with_header.image) if ground_truth_channel: assert list(sample.labels.shape) == [5] + list(image_with_header.image.shape) assert np.all(sample.labels[0] == 0) and np.all(sample.labels[1:] == 1)
def load_train_and_test_data_channels( patient_ids: List[int], normalization_fn: PhotometricNormalization) -> List[Sample]: if np.any(np.asarray(patient_ids) <= 0): raise ValueError("data_items must be >= 0") file_name = lambda k, y: full_ml_test_data_path("train_and_test_data" ) / f"id{k}_{y}.nii.gz" get_sample = lambda z: io_util.load_images_from_dataset_source( dataset_source=PatientDatasetSource( metadata=PatientMetadata(patient_id=z), image_channels=[file_name(z, c) for c in TEST_CHANNEL_IDS], mask_channel=file_name(z, TEST_MASK_ID), ground_truth_channels=[file_name(z, TEST_GT_ID)])) samples = [] for x in patient_ids: sample = get_sample(x) sample = Sample(image=normalization_fn.transform( sample.image, sample.mask), mask=sample.mask, labels=sample.labels, metadata=sample.metadata) samples.append(sample) return samples
def get_samples_at_index(self, index: int) -> List[Sample]: # load the channels into memory ds = self.dataset_sources[self.dataset_indices[index]] samples = [io_util.load_images_from_dataset_source(dataset_source=ds) ] # type: ignore return [ Compose3D.apply(self.full_image_sample_transforms, x) for x in samples ]
def main(yaml_file_path: Path) -> None: """ Invoke either by * specifying a model, '--model Lung' * or specifying dataset and normalization parameters separately: --azure_dataset_id=foo --norm_method=None In addition, the arguments '--image_channel' and '--gt_channel' must be specified (see below). """ config, runner_config, args = get_configs( SegmentationModelBase(should_validate=False), yaml_file_path) dataset_config = DatasetConfig(name=config.azure_dataset_id, local_folder=config.local_dataset, use_mounting=True) local_dataset, mount_context = dataset_config.to_input_dataset_local( workspace=runner_config.get_workspace()) dataframe = pd.read_csv(local_dataset / DATASET_CSV_FILE_NAME) normalizer_config = NormalizeAndVisualizeConfig(**args) actual_mask_channel = None if normalizer_config.ignore_mask else config.mask_id image_channel = normalizer_config.image_channel or config.image_channels[0] if not image_channel: raise ValueError( "No image channel selected. Specify a model by name, or use the image_channel argument." ) gt_channel = normalizer_config.gt_channel or config.ground_truth_ids[0] if not gt_channel: raise ValueError( "No GT channel selected. Specify a model by name, or use the gt_channel argument." ) dataset_sources = load_dataset_sources( dataframe, local_dataset_root_folder=local_dataset, image_channels=[image_channel], ground_truth_channels=[gt_channel], mask_channel=actual_mask_channel) result_folder = local_dataset if normalizer_config.result_folder is not None: result_folder = result_folder / normalizer_config.result_folder if not result_folder.is_dir(): result_folder.mkdir() all_patient_ids = [*dataset_sources.keys()] if normalizer_config.only_first == 0: patient_ids_to_process = all_patient_ids else: patient_ids_to_process = all_patient_ids[:normalizer_config.only_first] args_file = result_folder / ARGS_TXT args_file.write_text(" ".join(sys.argv[1:])) config_file = result_folder / "config.txt" config_file.write_text(str(config)) normalizer = PhotometricNormalization(config) for patient_id in patient_ids_to_process: print(f"Starting to process patient {patient_id}") images = load_images_from_dataset_source(dataset_sources[patient_id]) plotting.plot_normalization_result(images, normalizer, result_folder, result_prefix=image_channel)
def get_samples_at_index(self, index: int) -> List[Sample]: # load the channels into memory if not self._is_nifti_dataset(): raise ValueError( "Unknown file extension. Files should be Nifti or HDF5 format but found " + self.file_extension) ds = self.dataset_sources[self.dataset_indices[index]] samples = [io_util.load_images_from_dataset_source(dataset_source=ds) ] # type: ignore return [ Compose3D.apply(self.full_image_sample_transforms, x) for x in samples ]