|
| 1 | +import os |
| 2 | +import shutil |
| 3 | +import configparser |
| 4 | +import logging |
| 5 | +import sys |
| 6 | +import subprocess |
| 7 | +import traceback |
| 8 | +import zipfile |
| 9 | + |
| 10 | +try: |
| 11 | + import requests |
| 12 | +except: |
| 13 | + subprocess.check_call([sys.executable, "-m", "pip", "install", 'requests']) |
| 14 | + import requests |
| 15 | + |
| 16 | + |
| 17 | +def inference_test_docker(): |
| 18 | + """ |
| 19 | + Testing the CLI within a Docker container for the validation unit test, running on CPU. |
| 20 | + The latest Docker image is being hosted at: dbouget/raidionics-val:v1.0-py38-cpu |
| 21 | +
|
| 22 | + Returns |
| 23 | + ------- |
| 24 | +
|
| 25 | + """ |
| 26 | + logging.basicConfig() |
| 27 | + logging.getLogger().setLevel(logging.DEBUG) |
| 28 | + logging.info("Running validation unit test in Docker container.\n") |
| 29 | + logging.info("Downloading unit test resources.\n") |
| 30 | + test_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'unit_tests_results_dir') |
| 31 | + if os.path.exists(test_dir): |
| 32 | + shutil.rmtree(test_dir) |
| 33 | + os.makedirs(test_dir) |
| 34 | + |
| 35 | + try: |
| 36 | + resources_url = 'https://github.com/raidionics/Raidionics-models/releases/download/1.2.0/Samples-RaidionicsValLib_UnitTest1.zip' |
| 37 | + |
| 38 | + archive_dl_dest = os.path.join(test_dir, 'resources.zip') |
| 39 | + headers = {} |
| 40 | + response = requests.get(resources_url, headers=headers, stream=True) |
| 41 | + response.raise_for_status() |
| 42 | + if response.status_code == requests.codes.ok: |
| 43 | + with open(archive_dl_dest, "wb") as f: |
| 44 | + for chunk in response.iter_content(chunk_size=1048576): |
| 45 | + f.write(chunk) |
| 46 | + with zipfile.ZipFile(archive_dl_dest, 'r') as zip_ref: |
| 47 | + zip_ref.extractall(test_dir) |
| 48 | + |
| 49 | + if not os.path.exists(os.path.join(test_dir, 'Input_dataset')): |
| 50 | + raise ValueError('Resources download or extraction failed, content not available on disk.') |
| 51 | + except Exception as e: |
| 52 | + logging.error("Error during resources download with: \n {}.\n".format(traceback.format_exc())) |
| 53 | + if os.path.exists(test_dir): |
| 54 | + shutil.rmtree(test_dir) |
| 55 | + raise ValueError("Error during resources download.\n") |
| 56 | + |
| 57 | + logging.info("Preparing configuration file.\n") |
| 58 | + try: |
| 59 | + val_config = configparser.ConfigParser() |
| 60 | + val_config.add_section('Default') |
| 61 | + val_config.set('Default', 'task', "validation") |
| 62 | + val_config.set('Default', 'data_root', '/workspace/resources/Input_dataset') |
| 63 | + val_config.set('Default', 'number_processes', '1') |
| 64 | + val_config.add_section('Validation') |
| 65 | + val_config.set('Validation', 'input_folder', '/workspace/resources/StudyResults') |
| 66 | + val_config.set('Validation', 'output_folder', '/workspace/resources/StudyResults') |
| 67 | + val_config.set('Validation', 'gt_files_suffix', 'label_tumor.nii.gz') |
| 68 | + val_config.set('Validation', 'prediction_files_suffix', 'pred_tumor.nii.gz') |
| 69 | + val_config.set('Validation', 'use_index_naming_convention', 'false') |
| 70 | + val_config.set('Validation', 'nb_folds', '1') |
| 71 | + val_config.set('Validation', 'split_way', 'three-way') |
| 72 | + val_config.set('Validation', 'detection_overlap_thresholds', '0.') |
| 73 | + val_config.set('Validation', 'metrics_space', 'pixelwise, objectwise') |
| 74 | + val_config.set('Validation', 'class_names', 'tumor') |
| 75 | + val_config.set('Validation', 'extra_metrics', 'IOU') |
| 76 | + val_config.set('Validation', 'tiny_objects_removal_threshold', '25') |
| 77 | + val_config.set('Validation', 'true_positive_volume_thresholds', '0.1') |
| 78 | + val_config.set('Validation', 'use_brats_data', 'false') |
| 79 | + val_config_filename = os.path.join(test_dir, 'test_val_config.ini') |
| 80 | + with open(val_config_filename, 'w') as outfile: |
| 81 | + val_config.write(outfile) |
| 82 | + |
| 83 | + logging.info("Running validation unit test in Docker container.\n") |
| 84 | + try: |
| 85 | + import platform |
| 86 | + cmd_docker = ['docker', 'run', '-v', '{}:/workspace/resources'.format(test_dir), |
| 87 | + '--network=host', '--ipc=host', '--user', str(os.geteuid()), |
| 88 | + 'dbouget/raidionics-val:v1.0-py38-cpu', |
| 89 | + '-c', '/workspace/resources/test_val_config.ini', '-v', 'debug'] |
| 90 | + logging.info("Executing the following Docker call: {}".format(cmd_docker)) |
| 91 | + if platform.system() == 'Windows': |
| 92 | + subprocess.check_call(cmd_docker, shell=True) |
| 93 | + else: |
| 94 | + subprocess.check_call(cmd_docker) |
| 95 | + except Exception as e: |
| 96 | + logging.error("Error during validation test in Docker container with: \n {}.\n".format(traceback.format_exc())) |
| 97 | + if os.path.exists(test_dir): |
| 98 | + shutil.rmtree(test_dir) |
| 99 | + raise ValueError("Error during validation test in Docker container.\n") |
| 100 | + |
| 101 | + logging.info("Collecting and comparing results.\n") |
| 102 | + scores_segmentation_filename = os.path.join(test_dir, 'StudyResults', 'Validation', 'all_dice_scores.csv') |
| 103 | + if not os.path.exists(scores_segmentation_filename): |
| 104 | + logging.error("Validation in Docker container failed, no dice scores were generated.\n") |
| 105 | + if os.path.exists(test_dir): |
| 106 | + shutil.rmtree(test_dir) |
| 107 | + raise ValueError("Validation in Docker container failed, no dice scores were generated.\n") |
| 108 | + logging.info("Validation unit test in Docker container succeeded.\n") |
| 109 | + except Exception as e: |
| 110 | + logging.error("Error during validation in Docker container with: \n {}.\n".format(traceback.format_exc())) |
| 111 | + if os.path.exists(test_dir): |
| 112 | + shutil.rmtree(test_dir) |
| 113 | + raise ValueError("Error during validation in Docker container with.\n") |
| 114 | + |
| 115 | + logging.info("Validation unit test in Docker container succeeded.\n") |
| 116 | + if os.path.exists(test_dir): |
| 117 | + shutil.rmtree(test_dir) |
| 118 | + |
| 119 | + |
| 120 | +inference_test_docker() |
0 commit comments