def test_actions(self):
        tempdir = tempfile.mkdtemp()
        url = "https://www.dropbox.com/s/5wwskxctvcxiuea/MedNIST.tar.gz?dl=1"
        filepath = os.path.join(tempdir, "MedNIST.tar.gz")
        output_dir = tempdir
        md5_value = "0bc7306e7427e00ad1c5526a6677552d"
        try:
            download_and_extract(url, filepath, output_dir, md5_value)
            download_and_extract(url, filepath, output_dir, md5_value)
        except (ContentTooShortError, HTTPError):
            pass  # ignore remote errors in this test

        wrong_md5 = "0"
        try:
            download_url(url, filepath, wrong_md5)
        except RuntimeError as e:
            self.assertTrue(str(e).startswith("MD5 check"))

        shutil.rmtree(os.path.join(tempdir, "MedNIST"))
        try:
            extractall(filepath, output_dir, wrong_md5)
        except RuntimeError as e:
            self.assertTrue(str(e).startswith("MD5 check"))

        shutil.rmtree(tempdir)
Пример #2
0
    def test_actions(self):
        testing_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                   "testing_data")
        url = "https://drive.google.com/uc?id=13MhoPsNgI6qboJfLicFf_jNvsFUbIYsd"
        filepath = os.path.join(testing_dir, "MedNIST.tar.gz")
        output_dir = testing_dir
        md5_value = "0bc7306e7427e00ad1c5526a6677552d"
        try:
            download_and_extract(url, filepath, output_dir, md5_value)
            download_and_extract(url, filepath, output_dir, md5_value)
        except (ContentTooShortError, HTTPError, RuntimeError) as e:
            print(str(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

        wrong_md5 = "0"
        try:
            download_url(url, filepath, wrong_md5)
        except (ContentTooShortError, HTTPError, RuntimeError) as e:
            print(str(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"))
Пример #3
0
    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"))
Пример #4
0
def run_main():
    dataset_file = os.path.join(TEST_DATA, "dataset.zip")
    dataset_url = "https://github.com/Project-MONAI/MONAILabel/releases/download/data/test_dataset.zip"
    if not os.path.exists(os.path.join(TEST_DATA, "dataset")):
        if not os.path.exists(dataset_file):
            download_url(url=dataset_url, filepath=dataset_file)
        extractall(filepath=dataset_file, output_dir=TEST_DATA)
Пример #5
0
def download_file(url, path, delay=1, skip_on_exists=True):
    if skip_on_exists and os.path.exists(path):
        return

    os.makedirs(os.path.dirname(path), exist_ok=True)
    logger.info(f"Downloading resource: {path} from {url}")

    download_url(url, path)
    if delay > 0:
        time.sleep(delay)
Пример #6
0
    def download(resources):
        if not resources:
            return

        for resource in resources:
            if not os.path.exists(resource[0]):
                os.makedirs(os.path.dirname(resource[0]), exist_ok=True)
                logger.info(
                    f"Downloading resource: {resource[0]} from {resource[1]}")
                download_url(resource[1], resource[0])
                time.sleep(1)
    classification_report,
    confusion_matrix,
)

print_config()

# Setup data directory
root_dir = "./"
data_dir = os.path.join(root_dir, "data")
res_dir = os.path.join(root_dir, "results")
os.makedirs(res_dir, exist_ok=True)
trained_model_path = os.path.join(res_dir, "net_key_metric=0.7314.pt")

if not os.path.exists(trained_model_path):
    resource = "https://github.com/Thibault-Pelletier/MonaiWorkshop/raw/e29cdaa46e0097db909478e95c001d303ae963ab/results/net_key_metric%3D0.7314.pt"
    download_url(url=resource, filepath=trained_model_path)

# Download data if necessary
resource = "https://drive.google.com/uc?id=1aMc9eW_fGCphGBjAKDedxu8-aJVcSczd"  # Full 2.9 GB dataset
# resource = "https://drive.google.com/uc?id=1rZwPR3CFlFmYTev2YkxTJDfmLrbCiy63"  # Small 200 MB dataset subset

compressed_file = os.path.join(root_dir, "brats2015 - data.zip")
if not os.path.exists(data_dir):
    os.makedirs(data_dir)
    download_url(url=resource, filepath=compressed_file)
    extractall(filepath=compressed_file, output_dir=data_dir)

# Set dataset path
train_dirs = [
    os.path.join(data_dir, "train", "HGG"),
    os.path.join(data_dir, "train", "LGG")