Repository: Project-MONAI/MONAI
File: monai/handlers/clearml_handlers.py (Python)

Code before:
                                    Default set to None.
        reuse_last_task_id:         Force a new Task (experiment) with a previously used Task ID,
                                    and the same project and Task name. Default set to 'True'.
        continue_last_task:         Continue the execution of a previously executed Task (experiment).
                                    Default set to 'False'.
        auto_connect_frameworks:    Automatically connect frameworks This includes patching MatplotLib,
                                    XGBoost, scikit-learn, Keras callbacks, and TensorBoard/X to
                                    serialize plots, graphs, and the model location
                                    to the ClearML Server (backend), in addition to original output destination.

    For more details of ClearML usage, please refer to: https://clear.ml/docs/latest/docs/references/sdk/task

    """

    def __init__(
        self,
        project_name: str,
        task_name: str,
        output_uri: str,
        tags,
        reuse_last_task_id: bool,
        continue_last_task: bool,
        auto_connect_frameworks,
    ):
        try:
            from clearml import Task
        except ModuleNotFoundError as exc:
            raise ModuleNotFoundError("Please install ClearML with 'pip install clearml' before using it.") from exc

        if Task.current_task():
            self.clearml_task = Task.current_task()
        else:
            self.clearml_task = Task.init(
                project_name=project_name,
                task_name=task_name,
                output_uri=output_uri,
                tags=tags,
                reuse_last_task_id=reuse_last_task_id,
                continue_last_task=continue_last_task,
                auto_connect_frameworks=auto_connect_frameworks,
            )


class ClearMLStatsHandler(ClearMLHandler, TensorBoardStatsHandler):
    """

    Class to write tensorboard stats by inheriting TensorBoardStatsHandler class.
    Everything from Tensorboard is logged automatically to ClearML WebUI.

    """


Review comment (suggestion):
Please use MONAI `optional_import` for 3rd party packages, refer to:
https://github.com/Project-MONAI/MONAI/blob/dev/monai/handlers/tensorboard_handlers.py#L35

Thanks.


Code after:
        task_name: str | None,
        output_uri: str | bool,
        tags: Sequence[str] | None,
        reuse_last_task_id: bool,
        continue_last_task: bool,
        auto_connect_frameworks: bool | Mapping[str, bool | str | list],
        auto_connect_arg_parser: bool | Mapping[str, bool],
    ) -> None:
        """
        Args:
            project_name: ClearML project name, default to 'MONAI'.
            task_name: ClearML task name, default to 'monai_experiment'.
            output_uri: The default location for output models and other artifacts, default to 'True'.
            tags: A list of tags (str) to the created Task, default to 'None'.
            reuse_last_task_id: Force a new Task (experiment) with a previously used Task ID, default to 'True'.
            continue_last_task: Continue the execution of a previously executed Task (experiment), default to 'False'.
            auto_connect_frameworks: Automatically connect frameworks, default to 'True'.
            auto_connect_arg_parser: Automatically connect an argparse object to the Task, default to 'True'.

        """

        if TYPE_CHECKING:
            import clearml
        else:
            clearml, _ = optional_import("clearml")

        # Always check if the user didn't already add a `task.init`` in before
        # if so, use that task, otherwise create a new one.
        if clearml.Task.current_task():
            self.clearml_task = clearml.Task.current_task()
        else:
            self.clearml_task = clearml.Task.init(
                project_name=project_name,
                task_name=task_name,
                output_uri=output_uri,
                tags=tags,
                reuse_last_task_id=reuse_last_task_id,
                continue_last_task=continue_last_task,
                auto_connect_frameworks=auto_connect_frameworks,
                auto_connect_arg_parser=auto_connect_arg_parser,
            )


class ClearMLStatsHandler(ClearMLHandler, TensorBoardStatsHandler):
    """

    Class to write tensorboard stats by inheriting TensorBoardStatsHandler class.
    Everything from Tensorboard is logged automatically to ClearML.

    Usage example is available in the tutorial:
    https://github.com/Project-MONAI/tutorials/blob/master/3d_segmentation/unet_segmentation_3d_ignite.ipynb.