|
| 1 | +# Copyright (c) MONAI Consortium |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +import os |
| 13 | +import unittest |
| 14 | + |
| 15 | +import torch |
| 16 | +from parameterized import parameterized |
| 17 | + |
| 18 | +from monailabel.config import settings |
| 19 | +from monailabel.interfaces.app import MONAILabelApp |
| 20 | +from monailabel.interfaces.tasks.batch_infer import BatchInferImageType |
| 21 | +from monailabel.interfaces.utils.app import app_instance |
| 22 | + |
| 23 | + |
| 24 | +class TestApp(unittest.TestCase): |
| 25 | + app = None |
| 26 | + base_dir = os.path.realpath(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))) |
| 27 | + data_dir = os.path.join(base_dir, "tests", "data") |
| 28 | + |
| 29 | + app_dir = os.path.join(base_dir, "sample-apps", "radiology") |
| 30 | + studies = os.path.join(data_dir, "dataset", "local", "spleen") |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def setUpClass(cls) -> None: |
| 34 | + settings.MONAI_LABEL_APP_DIR = cls.app_dir |
| 35 | + settings.MONAI_LABEL_STUDIES = cls.studies |
| 36 | + settings.MONAI_LABEL_DATASTORE_AUTO_RELOAD = False |
| 37 | + |
| 38 | + if torch.cuda.is_available(): |
| 39 | + cls.app: MONAILabelApp = app_instance( |
| 40 | + app_dir=cls.app_dir, |
| 41 | + studies=cls.studies, |
| 42 | + conf={ |
| 43 | + "preload": "true", |
| 44 | + "models": "segmentation_spleen", |
| 45 | + }, |
| 46 | + ) |
| 47 | + |
| 48 | + @classmethod |
| 49 | + def tearDownClass(cls) -> None: |
| 50 | + pass |
| 51 | + |
| 52 | + def test_app_init(self): |
| 53 | + if not self.app: |
| 54 | + return |
| 55 | + self.app.on_init_complete() |
| 56 | + |
| 57 | + def test_cleanup_sessions(self): |
| 58 | + if not self.app: |
| 59 | + return |
| 60 | + self.app.cleanup_sessions() |
| 61 | + |
| 62 | + def test_async_batch_infer(self): |
| 63 | + if not self.app: |
| 64 | + return |
| 65 | + |
| 66 | + model = "segmentation_spleen" |
| 67 | + params = {"max_workers": 2} |
| 68 | + |
| 69 | + self.app.server_mode(True) |
| 70 | + self.app.async_batch_infer(model, BatchInferImageType.IMAGES_ALL, params) |
| 71 | + |
| 72 | + try: |
| 73 | + self.app.server_mode(False) |
| 74 | + self.app.async_batch_infer(model, BatchInferImageType.IMAGES_LABELED) |
| 75 | + except: |
| 76 | + pass |
| 77 | + |
| 78 | + def test_async_train(self): |
| 79 | + if not self.app: |
| 80 | + return |
| 81 | + |
| 82 | + model = "segmentation_spleen" |
| 83 | + params = {"max_epochs": 1} |
| 84 | + |
| 85 | + self.app.server_mode(True) |
| 86 | + self.app.async_training(model, params) |
| 87 | + |
| 88 | + try: |
| 89 | + self.app.server_mode(False) |
| 90 | + self.app.async_training(model, params) |
| 91 | + except: |
| 92 | + pass |
| 93 | + |
| 94 | + @parameterized.expand(["xnat", "dsa", ""]) |
| 95 | + def test_init_datastores(self, r): |
| 96 | + if not self.app: |
| 97 | + return |
| 98 | + |
| 99 | + try: |
| 100 | + settings.MONAI_LABEL_DATASTORE = r |
| 101 | + self.app.init_remote_datastore() |
| 102 | + except: |
| 103 | + pass |
| 104 | + finally: |
| 105 | + settings.MONAI_LABEL_DATASTORE = "" |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + unittest.main() |
0 commit comments