def setUp(self): hash_type = testing_data_config("images", FILE_KEY, "hash_type") hash_val = testing_data_config("images", FILE_KEY, "hash_val") download_url_or_skip_test(FILE_URL, FILE_PATH, hash_type=hash_type, hash_val=hash_val)
def setUpModule(): # noqa: N802 hash_type = testing_data_config("images", FILE_KEY, "hash_type") hash_val = testing_data_config("images", FILE_KEY, "hash_val") download_url_or_skip_test(FILE_URL, FILE_PATH, hash_type=hash_type, hash_val=hash_val)
def setUpModule(): for info in [(ARRAY_SMALL_0, FILE_PATH_SMALL_0), (ARRAY_SMALL_1, FILE_PATH_SMALL_1)]: array = info[0].transpose([1, 2, 0]) imwrite(info[1], array, shape=array.shape, photometric="rgb") hash_type = testing_data_config("images", FILE_KEY, "hash_type") hash_val = testing_data_config("images", FILE_KEY, "hash_val") download_url_or_skip_test(FILE_URL, FILE_PATH, hash_type=hash_type, hash_val=hash_val)
def setUp(self): self.fnames = [] for key in ("0000_t2_tse_tra_4", "0000_ep2d_diff_tra_7"): fname = os.path.join(os.path.dirname(__file__), "testing_data", f"test_{key}.nii.gz") url = testing_data_config("images", key, "url") hash_type = testing_data_config("images", key, "hash_type") hash_val = testing_data_config("images", key, "hash_val") download_url_or_skip_test(url=url, filepath=fname, hash_type=hash_type, hash_val=hash_val) self.fnames.append(fname)
def setUpClass(cls): super(__class__, cls).setUpClass() cls.fnames = [] cls.tmpdir = tempfile.mkdtemp() for key in ("0000_t2_tse_tra_4", "0000_ep2d_diff_tra_7"): fname = os.path.join(cls.tmpdir, f"test_{key}.nii.gz") url = testing_data_config("images", key, "url") hash_type = testing_data_config("images", key, "hash_type") hash_val = testing_data_config("images", key, "hash_val") download_url_or_skip_test(url=url, filepath=fname, hash_type=hash_type, hash_val=hash_val) cls.fnames.append(fname)
def test_actions(self): testing_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testing_data") config_dict = testing_data_config("images", "mednist") url = config_dict["url"] filepath = Path(testing_dir) / "MedNIST.tar.gz" output_dir = Path(testing_dir) hash_val, hash_type = config_dict["hash_val"], config_dict["hash_type"] with skip_if_downloading_fails(): download_and_extract(url, filepath, output_dir, hash_val=hash_val, hash_type=hash_type) download_and_extract(url, filepath, output_dir, hash_val=hash_val, hash_type=hash_type) wrong_md5 = "0" with self.assertLogs(logger="monai.apps", level="ERROR"): try: download_url(url, filepath, wrong_md5) except (ContentTooShortError, HTTPError, RuntimeError) as e: if isinstance(e, RuntimeError): # FIXME: skip MD5 check as current downloading method may fail self.assertTrue(str(e).startswith("md5 check")) return # skipping this test due the network connection errors try: extractall(filepath, output_dir, wrong_md5) except RuntimeError as e: self.assertTrue(str(e).startswith("md5 check"))
def setUp(self): self.config = testing_data_config("configs", "test_meta_file") download_url_or_skip_test( url=self.config["url"], filepath=SCHEMA_FILE, hash_val=self.config.get("hash_val"), hash_type=self.config.get("hash_type", "sha256"), )
def setUp(self): config = testing_data_config("images", "Prostate_T2W_AX_1") download_url_or_skip_test( url=config["url"], filepath=FILE_PATH, hash_val=config.get("hash_val"), hash_type=config.get("hash_type", "sha256"), )
def setUpClass(cls): super().setUpClass() for k, n in ((key, FILE_PATH), (key_1, FILE_PATH_1)): config = testing_data_config("images", f"{k}") download_url_or_skip_test(filepath=n, **config) cls.files = [{ keys[0]: x, keys[1]: y } for (x, y) in [[FILE_PATH, FILE_PATH_1]] * 4]
def test_default(self, key, file_type): with tempfile.TemporaryDirectory() as tmp_dir: with skip_if_downloading_fails(): img_spec = testing_data_config("images", key) download_and_extract( img_spec["url"], output_dir=tmp_dir, hash_val=img_spec["hash_val"], hash_type=img_spec["hash_type"], file_type=file_type, )
def setUp(self): set_determinism(seed=0) self.data_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), "testing_data") data_dir = os.path.join(self.data_dir, "MedNIST") dataset_file = os.path.join(self.data_dir, "MedNIST.tar.gz") if not os.path.exists(data_dir): with skip_if_downloading_fails(): data_spec = testing_data_config("images", "mednist") download_and_extract( data_spec["url"], dataset_file, self.data_dir, hash_val=data_spec["hash_val"], hash_type=data_spec["hash_type"], ) assert os.path.exists(data_dir) class_names = sorted(x for x in os.listdir(data_dir) if os.path.isdir(os.path.join(data_dir, x))) image_files = [[ os.path.join(data_dir, class_name, x) for x in sorted(os.listdir(os.path.join(data_dir, class_name))) ] for class_name in class_names] image_file_list, image_classes = [], [] for i, _ in enumerate(class_names): image_file_list.extend(image_files[i]) image_classes.extend([i] * len(image_files[i])) # split train, val, test valid_frac, test_frac = 0.1, 0.1 self.train_x, self.train_y = [], [] self.val_x, self.val_y = [], [] self.test_x, self.test_y = [], [] for i in range(len(image_classes)): rann = np.random.random() if rann < valid_frac: self.val_x.append(image_file_list[i]) self.val_y.append(image_classes[i]) elif rann < test_frac + valid_frac: self.test_x.append(image_file_list[i]) self.test_y.append(image_classes[i]) else: self.train_x.append(image_file_list[i]) self.train_y.append(image_classes[i]) self.device = "cuda:0" if torch.cuda.is_available() else "cpu:0"
def setUp(self): self.original_urls = se_mod.SE_NET_MODELS.copy() replace_url = test_is_quick() if not replace_url: try: SEResNet50(pretrained=True, spatial_dims=2, in_channels=3, num_classes=2) except OSError as rt_e: print(rt_e) if "certificate" in str(rt_e): # [SSL: CERTIFICATE_VERIFY_FAILED] replace_url = True if replace_url: testing_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testing_data") testing_data_urls = { "senet154": { "url": testing_data_config("models", "senet154-c7b49a05", "url"), "filename": "senet154-c7b49a05.pth", }, "se_resnet50": { "url": testing_data_config("models", "se_resnet50-ce0d4300", "url"), "filename": "se_resnet50-ce0d4300.pth", }, "se_resnet101": { "url": testing_data_config("models", "se_resnet101-7e38fcc6", "url"), "filename": "se_resnet101-7e38fcc6.pth", }, "se_resnet152": { "url": testing_data_config("models", "se_resnet152-d17c99b7", "url"), "filename": "se_resnet152-d17c99b7.pth", }, "se_resnext50_32x4d": { "url": testing_data_config("models", "se_resnext50_32x4d-a260b3a4", "url"), "filename": "se_resnext50_32x4d-a260b3a4.pth", }, "se_resnext101_32x4d": { "url": testing_data_config("models", "se_resnext101_32x4d-3b2fe3d8", "url"), "filename": "se_resnext101_32x4d-3b2fe3d8.pth", }, } for item in testing_data_urls: testing_data_urls[item]["filename"] = os.path.join(testing_dir, testing_data_urls[item]["filename"]) se_mod.SE_NET_MODELS = testing_data_urls
import unittest from unittest import skipUnless import numpy as np from numpy.testing import assert_array_equal from parameterized import parameterized from monai.apps.pathology.data import MaskedInferenceWSIDataset from monai.utils import optional_import from tests.utils import download_url_or_skip_test, skip_if_quick, testing_data_config _, has_cim = optional_import("cucim", name="CuImage") _, has_osl = optional_import("openslide") FILE_KEY = "wsi_img" FILE_URL = testing_data_config("images", FILE_KEY, "url") base_name, extension = os.path.basename(f"{FILE_URL}"), ".tiff" FILE_NAME = f"temp_{base_name}" FILE_PATH = os.path.join(os.path.dirname(__file__), "testing_data", FILE_NAME + extension) MASK1 = os.path.join(os.path.dirname(__file__), "testing_data", "temp_tissue_mask1.npy") MASK2 = os.path.join(os.path.dirname(__file__), "testing_data", "temp_tissue_mask2.npy") MASK4 = os.path.join(os.path.dirname(__file__), "testing_data", "temp_tissue_mask4.npy") HEIGHT = 32914 WIDTH = 46000
def setUpClass(cls): super().setUpClass() for k, n in ((key, FILE_PATH), (key_1, FILE_PATH_1)): config = testing_data_config("images", f"{k}") download_url_or_skip_test(filepath=n, **config)