def test_seg_no_basedir(self): tempdir = tempfile.mkdtemp() test_data = { "name": "Spleen", "description": "Spleen Segmentation", "labels": { "0": "background", "1": "spleen" }, "training": [ { "image": os.path.join(tempdir, "spleen_19.nii.gz"), "label": os.path.join(tempdir, "spleen_19.nii.gz"), }, { "image": os.path.join(tempdir, "spleen_31.nii.gz"), "label": os.path.join(tempdir, "spleen_31.nii.gz"), }, ], "test": [ os.path.join(tempdir, "spleen_15.nii.gz"), os.path.join(tempdir, "spleen_23.nii.gz") ], } json_str = json.dumps(test_data) file_path = os.path.join(tempdir, "test_data.json") with open(file_path, "w") as json_file: json_file.write(json_str) result = load_decathalon_datalist(file_path, True, "training", None) self.assertEqual(result[0]["image"], os.path.join(tempdir, "spleen_19.nii.gz")) self.assertEqual(result[0]["label"], os.path.join(tempdir, "spleen_19.nii.gz"))
def test_cls_values(self): tempdir = tempfile.mkdtemp() test_data = { "name": "ChestXRay", "description": "Chest X-ray classification", "labels": { "0": "background", "1": "chest" }, "training": [{ "image": "chest_19.nii.gz", "label": 0 }, { "image": "chest_31.nii.gz", "label": 1 }], "test": ["chest_15.nii.gz", "chest_23.nii.gz"], } json_str = json.dumps(test_data) file_path = os.path.join(tempdir, "test_data.json") with open(file_path, "w") as json_file: json_file.write(json_str) result = load_decathalon_datalist(file_path, False, "training", tempdir) self.assertEqual(result[0]["image"], os.path.join(tempdir, "chest_19.nii.gz")) self.assertEqual(result[0]["label"], 0) shutil.rmtree(tempdir)
def test_seg_values(self): with tempfile.TemporaryDirectory() as tempdir: test_data = { "name": "Spleen", "description": "Spleen Segmentation", "labels": { "0": "background", "1": "spleen" }, "training": [ { "image": "spleen_19.nii.gz", "label": "spleen_19.nii.gz" }, { "image": "spleen_31.nii.gz", "label": "spleen_31.nii.gz" }, ], "test": ["spleen_15.nii.gz", "spleen_23.nii.gz"], } json_str = json.dumps(test_data) file_path = os.path.join(tempdir, "test_data.json") with open(file_path, "w") as json_file: json_file.write(json_str) result = load_decathalon_datalist(file_path, True, "training", tempdir) self.assertEqual(result[0]["image"], os.path.join(tempdir, "spleen_19.nii.gz")) self.assertEqual(result[0]["label"], os.path.join(tempdir, "spleen_19.nii.gz"))
def _generate_data_list(self, dataset_dir: str) -> List[Dict]: section = "training" if self.section in ["training", "validation"] else "test" datalist = load_decathalon_datalist(os.path.join(dataset_dir, "dataset.json"), True, section) if section == "test": return datalist else: data = list() for i in datalist: self.randomize() if self.section == "training": if self.rann < self.val_frac: continue else: if self.rann >= self.val_frac: continue data.append(i) return data
def test_seg_no_labels(self): tempdir = tempfile.mkdtemp() test_data = { "name": "Spleen", "description": "Spleen Segmentation", "labels": { "0": "background", "1": "spleen" }, "test": ["spleen_15.nii.gz", "spleen_23.nii.gz"], } json_str = json.dumps(test_data) file_path = os.path.join(tempdir, "test_data.json") with open(file_path, "w") as json_file: json_file.write(json_str) result = load_decathalon_datalist(file_path, True, "test", tempdir) self.assertEqual(result[0]["image"], os.path.join(tempdir, "spleen_15.nii.gz")) shutil.rmtree(tempdir)