def test_verify(self, meta_file, config_file): with tempfile.TemporaryDirectory() as tempdir: def_args = { "meta_file": "will be replaced by `meta_file` arg", "p": 2 } def_args_file = os.path.join(tempdir, "def_args.json") ConfigParser.export_config_file(config=def_args, filepath=def_args_file) cmd = [ "coverage", "run", "-m", "monai.bundle", "verify_net_in_out", "network_def", "--meta_file" ] cmd += [ meta_file, "--config_file", config_file, "-n", "4", "--any", "16", "--args_file", def_args_file ] cmd += [ "--_meta_#network_data_format#inputs#image#spatial_shape", "[16,'*','2**p*n']" ] test_env = os.environ.copy() print(f"CUDA_VISIBLE_DEVICES in {__file__}", test_env.get("CUDA_VISIBLE_DEVICES")) subprocess.check_call(cmd, env=test_env)
def test_export(self, key_in_ckpt): meta_file = os.path.join(os.path.dirname(__file__), "testing_data", "metadata.json") config_file = os.path.join(os.path.dirname(__file__), "testing_data", "inference.json") with tempfile.TemporaryDirectory() as tempdir: def_args = {"meta_file": "will be replaced by `meta_file` arg"} def_args_file = os.path.join(tempdir, "def_args.json") ckpt_file = os.path.join(tempdir, "model.pt") ts_file = os.path.join(tempdir, "model.ts") parser = ConfigParser() parser.export_config_file(config=def_args, filepath=def_args_file) parser.read_config(config_file) net = parser.get_parsed_content("network_def") save_state(src=net if key_in_ckpt == "" else {key_in_ckpt: net}, path=ckpt_file) cmd = [ "coverage", "run", "-m", "monai.bundle", "ckpt_export", "network_def", "--filepath", ts_file ] cmd += [ "--meta_file", meta_file, "--config_file", config_file, "--ckpt_file", ckpt_file ] cmd += ["--key_in_ckpt", key_in_ckpt, "--args_file", def_args_file] subprocess.check_call(cmd) self.assertTrue(os.path.exists(ts_file))
def test_url_download_bundle(self, bundle_files, bundle_name, url, hash_val): with skip_if_downloading_fails(): # download a single file from url, also use `args_file` with tempfile.TemporaryDirectory() as tempdir: def_args = { "name": bundle_name, "bundle_dir": tempdir, "url": "" } def_args_file = os.path.join(tempdir, "def_args.json") parser = ConfigParser() parser.export_config_file(config=def_args, filepath=def_args_file) cmd = [ "coverage", "run", "-m", "monai.bundle", "download", "--args_file", def_args_file ] cmd += ["--url", url] subprocess.check_call(cmd) for file in bundle_files: file_path = os.path.join(tempdir, bundle_name, file) self.assertTrue(os.path.exists(file_path)) if file == "network.json": self.assertTrue( check_hash(filepath=file_path, val=hash_val))
def test_verify(self, meta_file, schema_file): with tempfile.TemporaryDirectory() as tempdir: def_args = {"meta_file": "will be replaced by `meta_file` arg"} def_args_file = os.path.join(tempdir, "def_args.json") ConfigParser.export_config_file(config=def_args, filepath=def_args_file) cmd = ["coverage", "run", "-m", "monai.bundle", "verify_metadata", "--meta_file", meta_file] cmd += ["--filepath", schema_file, "--hash_val", self.config["hash_val"], "--args_file", def_args_file] subprocess.check_call(cmd)
def test_macro_replace(self): with tempfile.TemporaryDirectory() as tempdir: another_file = os.path.join(tempdir, "another.json") ConfigParser.export_config_file(config={"E": 4}, filepath=another_file) # test macro with id, relative id, and macro in another file config = {"A": {"B": 1, "C": 2}, "D": [3, "%A#B", "%#0", f"%{another_file}#E"]} parser = ConfigParser(config=config) parser.resolve_macro_and_relative_ids() self.assertEqual(str(parser.get()), str({"A": {"B": 1, "C": 2}, "D": [3, 1, 3, 4]}))
def test_shape(self, config_file, expected_shape): test_image = np.random.rand(*expected_shape) tempdir = self.data_dir filename = os.path.join(tempdir, "image.nii") nib.save(nib.Nifti1Image(test_image, np.eye(4)), filename) # generate default args in a JSON file logging_conf = os.path.join(os.path.dirname(__file__), "testing_data", "logging.conf") def_args = {"config_file": "will be replaced by `config_file` arg", "logging_file": logging_conf} def_args_file = os.path.join(tempdir, "def_args.json") ConfigParser.export_config_file(config=def_args, filepath=def_args_file) meta = {"datalist": [{"image": filename}], "output_dir": tempdir, "window": (96, 96, 96)} # test YAML file meta_file = os.path.join(tempdir, "meta.yaml") ConfigParser.export_config_file(config=meta, filepath=meta_file, fmt="yaml") # test override with file, up case postfix overridefile1 = os.path.join(tempdir, "override1.JSON") with open(overridefile1, "w") as f: # test override with part of the overriding file json.dump({"move_net": "$@network_def.to(@device)"}, f) os.makedirs(os.path.join(tempdir, "jsons"), exist_ok=True) overridefile2 = os.path.join(tempdir, "jsons/override2.JSON") with open(overridefile2, "w") as f: # test override with the whole overriding file json.dump("Dataset", f) saver = LoadImage(image_only=True) if sys.platform == "win32": override = "--network $@network_def.to(@device) --dataset#_target_ Dataset" else: override = f"--network %{overridefile1}#move_net --dataset#_target_ %{overridefile2}" # test with `monai.bundle` as CLI entry directly cmd = f"-m monai.bundle run evaluator --postprocessing#transforms#2#output_postfix seg {override}" la = ["coverage", "run"] + cmd.split(" ") + ["--meta_file", meta_file] + ["--config_file", config_file] test_env = os.environ.copy() print(f"CUDA_VISIBLE_DEVICES in {__file__}", test_env.get("CUDA_VISIBLE_DEVICES")) subprocess.check_call(la + ["--args_file", def_args_file], env=test_env) self.assertTupleEqual(saver(os.path.join(tempdir, "image", "image_seg.nii.gz")).shape, expected_shape) # here test the script with `google fire` tool as CLI cmd = "-m fire monai.bundle.scripts run --runner_id evaluator" cmd += f" --evaluator#amp False {override}" la = ["coverage", "run"] + cmd.split(" ") + ["--meta_file", meta_file] + ["--config_file", config_file] subprocess.check_call(la, env=test_env) self.assertTupleEqual(saver(os.path.join(tempdir, "image", "image_trans.nii.gz")).shape, expected_shape)