|
| 1 | +import glob |
| 2 | +import json |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +import tempfile |
| 8 | +import unittest |
| 9 | + |
| 10 | +import pandas as pd |
| 11 | +import yaml |
| 12 | + |
| 13 | + |
| 14 | +class TestRPDInference(unittest.TestCase): |
| 15 | + def setUp(self): |
| 16 | + print(os.getcwd()) |
| 17 | + # set the bundle root to the directory the test is being run from. |
| 18 | + self.bundle_root = os.path.abspath( |
| 19 | + os.path.join(os.path.dirname(__file__), "..", "..", "models", "retinalOCT_RPD_segmentation") |
| 20 | + ) |
| 21 | + # Change the current working directory to bundle_root |
| 22 | + os.chdir(self.bundle_root) |
| 23 | + |
| 24 | + # Create a temporary directory for test data |
| 25 | + self.test_data_dir = tempfile.mkdtemp() |
| 26 | + self.extracted_dir = os.path.join(self.bundle_root, "sample_data") |
| 27 | + |
| 28 | + # create a dummy metadata.json file. |
| 29 | + metadata_file = os.path.join(self.test_data_dir, "metadata.json") |
| 30 | + metadata = { |
| 31 | + "version": "0.0.1", |
| 32 | + "schema": "https://github.com/Project-MONAI/MONAI-extra-test-data/releases/download/0.8.1/meta_schema_20220324.json", |
| 33 | + } |
| 34 | + with open(metadata_file, "w") as f: |
| 35 | + json.dump(metadata, f) |
| 36 | + |
| 37 | + # create output directory. |
| 38 | + self.output_dir = os.path.join(self.test_data_dir, "output") |
| 39 | + os.makedirs(self.output_dir) |
| 40 | + |
| 41 | + def tearDown(self): |
| 42 | + # Clean up the temporary directory |
| 43 | + shutil.rmtree(self.test_data_dir) |
| 44 | + |
| 45 | + def test_inference_run(self): |
| 46 | + # Override configuration parameters |
| 47 | + override = { |
| 48 | + "args": { |
| 49 | + "extracted_dir": self.extracted_dir, |
| 50 | + "output_dir": self.output_dir, |
| 51 | + "run_extract": False, |
| 52 | + "create_dataset": True, |
| 53 | + "run_inference": True, |
| 54 | + "binary_mask": True, |
| 55 | + "binary_mask_overlay": True, |
| 56 | + "instance_mask_overlay": True, |
| 57 | + "dataset_name": "testDataset", |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + # Load the original inference.yaml |
| 62 | + inference_yaml_path = "configs/inference.yaml" |
| 63 | + with open(inference_yaml_path, "r") as f: |
| 64 | + inference_yaml = yaml.safe_load(f) |
| 65 | + |
| 66 | + # Modify inference.yaml with override parameters. |
| 67 | + inference_yaml["args"].update(override["args"]) |
| 68 | + |
| 69 | + # Create a new inference.yaml in the test_data_dir |
| 70 | + test_inference_yaml_path = os.path.join(self.test_data_dir, "inference.yaml") |
| 71 | + with open(test_inference_yaml_path, "w") as f: |
| 72 | + yaml.dump(inference_yaml, f) |
| 73 | + |
| 74 | + # Run the inference command using subprocess |
| 75 | + cmd = [ |
| 76 | + sys.executable, # Use the same Python interpreter |
| 77 | + "-m", |
| 78 | + "monai.bundle", |
| 79 | + "run", |
| 80 | + "inference", |
| 81 | + "--bundle_root", |
| 82 | + self.bundle_root, |
| 83 | + "--config_file", |
| 84 | + test_inference_yaml_path, # Use the new file |
| 85 | + "--meta_file", |
| 86 | + os.path.join(self.test_data_dir, "metadata.json"), |
| 87 | + ] |
| 88 | + |
| 89 | + try: |
| 90 | + subprocess.run(cmd, check=True) |
| 91 | + except subprocess.CalledProcessError as e: |
| 92 | + self.fail(f"Inference command failed: {e}") |
| 93 | + |
| 94 | + # Add assertions to check the output |
| 95 | + # Check if output files were created |
| 96 | + output_files = os.listdir(self.output_dir) |
| 97 | + self.assertTrue(len(output_files) > 0) |
| 98 | + |
| 99 | + # Check for the COCO JSON file |
| 100 | + coco_file_found = glob.glob(os.path.join(self.output_dir, "**", "coco_instances_results.json"), recursive=True) |
| 101 | + print(coco_file_found) |
| 102 | + self.assertTrue(len(coco_file_found) == 6) |
| 103 | + |
| 104 | + # Check for the TIFF files. |
| 105 | + tiff_files_found = glob.glob(os.path.join(self.output_dir, "**", "*.tiff"), recursive=True) |
| 106 | + self.assertTrue(len(tiff_files_found) == 6) |
| 107 | + |
| 108 | + # Check for the html files. |
| 109 | + html_files_found = glob.glob(os.path.join(self.output_dir, "*.html")) |
| 110 | + self.assertTrue(len(html_files_found) == 2) |
| 111 | + |
| 112 | + # At least 10 RPD present in sample data |
| 113 | + dfvol = pd.read_html(os.path.join(self.output_dir, "dfvol_testDataset.html"))[0] |
| 114 | + self.assertTrue(dfvol["dt_instances"].sum().iloc[0] > 10) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + unittest.main() |
0 commit comments