Repository: Project-MONAI/MONAI
File: tests/test_handler_clearml_image.py (Python)

Code before:
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import unittest

from monai.handlers import ClearMLImageHandler
from monai.utils import optional_import

Task, has_clearml = optional_import("clearml", name="Task")
_, has_tb = optional_import("torch.utils.tensorboard", name="SummaryWriter")


@unittest.skipUnless(has_clearml, "Requires 'clearml' installation")
@unittest.skipUnless(has_tb, "Requires SummaryWriter installation")
@unittest.skip("temp mute clearml tests https://github.com/Project-MONAI/MONAI/issues/6148")
class TestHandlerClearMLImageHandler(unittest.TestCase):
    def test_task_init(self):
        Task.set_offline(offline_mode=True)
        try:
            ClearMLImageHandler(
                project_name="MONAI",
                task_name="monai_experiment",
                output_uri=True,
                tags=None,
                reuse_last_task_id=True,
                continue_last_task=False,
                auto_connect_frameworks=True,
                auto_connect_arg_parser=False,
            )
        except Exception as exc:
            self.fail(exc)
        self.assertEqual(Task.current_task().name, "monai_experiment")
        self.assertEqual(Task.current_task()._project_name[1], "MONAI")
        # Close ClearML Task
        Task.current_task().close()


if __name__ == "__main__":
    unittest.main()


Review comment (bug):
Hi @wyli ,

I think we should remove this comment line? no need to describe the fixed bug in the code.

Thanks.



Code after:
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import annotations

import tempfile
import unittest
from os import environ

from monai.handlers import ClearMLImageHandler
from monai.utils import optional_import

Task, has_clearml = optional_import("clearml", name="Task")
get_active_config_file, has_get_active_config_file = optional_import(
    "clearml.backend_config.defs", name="get_active_config_file"
)
_, has_tb = optional_import("torch.utils.tensorboard", name="SummaryWriter")


@unittest.skipUnless(has_clearml, "Requires 'clearml' installation")
@unittest.skipUnless(has_tb, "Requires SummaryWriter installation")
@unittest.skipIf(not has_get_active_config_file, "ClearML 'get_active_config_file' not found")
# @unittest.skip("temp mute clearml tests https://github.com/Project-MONAI/MONAI/issues/6148")
class TestHandlerClearMLImageHandler(unittest.TestCase):
    def test_task_init(self):
        handle, path = tempfile.mkstemp()
        with open(handle, "w") as new_config:
            if get_active_config_file():
                with open(get_active_config_file()) as old_config:
                    new_config.write(old_config.read())
            new_config.write(
                "\nsdk.development.vcs_repo_detect_async: false\nsdk.development.report_use_subprocess: false\n"
            )
        environ["CLEARML_CONFIG_FILE"] = path
        try:
            Task.force_store_standalone_script(True)
            Task.set_offline(offline_mode=True)
            ClearMLImageHandler(
                project_name="MONAI",
                task_name="monai_experiment",
                output_uri=True,
                tags=None,
                reuse_last_task_id=True,
                continue_last_task=False,
                auto_connect_frameworks=True,
                auto_connect_arg_parser=False,
            )
        except Exception as exc: