From 302f3e6dc4c66dfe1804ba8242578ec8ec886483 Mon Sep 17 00:00:00 2001 From: Behrooz <3968947+drbeh@users.noreply.github.com> Date: Fri, 27 Oct 2023 09:19:52 -0400 Subject: [PATCH 1/3] set autorunner methods to return self Signed-off-by: Behrooz <3968947+drbeh@users.noreply.github.com> --- monai/apps/auto3dseg/auto_runner.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/monai/apps/auto3dseg/auto_runner.py b/monai/apps/auto3dseg/auto_runner.py index 2105f051bd..b37a45abf9 100644 --- a/monai/apps/auto3dseg/auto_runner.py +++ b/monai/apps/auto3dseg/auto_runner.py @@ -442,6 +442,8 @@ def set_gpu_customization( if gpu_customization_specs is not None: self.gpu_customization_specs = gpu_customization_specs + return self + def set_num_fold(self, num_fold: int = 5) -> None: """ Set the number of cross validation folds for all algos. @@ -454,6 +456,8 @@ def set_num_fold(self, num_fold: int = 5) -> None: raise ValueError(f"num_fold is expected to be an integer greater than zero. Now it gets {num_fold}") self.num_fold = num_fold + return self + def set_training_params(self, params: dict[str, Any] | None = None) -> None: """ Set the training params for all algos. @@ -474,6 +478,8 @@ def set_training_params(self, params: dict[str, Any] | None = None) -> None: DeprecationWarning, ) + return self + def set_device_info( self, cuda_visible_devices: list[int] | str | None = None, @@ -531,6 +537,8 @@ def set_device_info( if cmd_prefix is not None: logger.info(f"Using user defined command running prefix {cmd_prefix}, will override other settings") + return self + def set_ensemble_method(self, ensemble_method_name: str = "AlgoEnsembleBestByFold", **kwargs: Any) -> None: """ Set the bundle ensemble method name and parameters for save image transform parameters. @@ -546,6 +554,8 @@ def set_ensemble_method(self, ensemble_method_name: str = "AlgoEnsembleBestByFol ) self.kwargs.update(kwargs) + return self + def set_image_save_transform(self, **kwargs: Any) -> None: """ Set the ensemble output transform. @@ -565,6 +575,8 @@ def set_image_save_transform(self, **kwargs: Any) -> None: "Check https://docs.monai.io/en/stable/transforms.html#saveimage for more information." ) + return self + def set_prediction_params(self, params: dict[str, Any] | None = None) -> None: """ Set the prediction params for all algos. @@ -581,6 +593,8 @@ def set_prediction_params(self, params: dict[str, Any] | None = None) -> None: """ self.pred_params = deepcopy(params) if params is not None else {} + return self + def set_analyze_params(self, params: dict[str, Any] | None = None) -> None: """ Set the data analysis extra params. @@ -595,6 +609,8 @@ def set_analyze_params(self, params: dict[str, Any] | None = None) -> None: else: self.analyze_params = deepcopy(params) + return self + def set_hpo_params(self, params: dict[str, Any] | None = None) -> None: """ Set parameters for the HPO module and the algos before the training. It will attempt to (1) override bundle @@ -621,6 +637,8 @@ def set_hpo_params(self, params: dict[str, Any] | None = None) -> None: """ self.hpo_params = self.train_params if params is None else params + return self + def set_nni_search_space(self, search_space): """ Set the search space for NNI parameter search. @@ -638,6 +656,8 @@ def set_nni_search_space(self, search_space): self.search_space = search_space self.hpo_tasks = value_combinations + return self + def _train_algo_in_sequence(self, history: list[dict[str, Any]]) -> None: """ Train the Algos in a sequential scheme. The order of training is randomized. From cbe8b52f1f042ec7f4f5e1f684da298d5404be77 Mon Sep 17 00:00:00 2001 From: Behrooz <3968947+drbeh@users.noreply.github.com> Date: Fri, 27 Oct 2023 09:47:11 -0400 Subject: [PATCH 2/3] fix return typehint Signed-off-by: Behrooz <3968947+drbeh@users.noreply.github.com> --- monai/apps/auto3dseg/auto_runner.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/monai/apps/auto3dseg/auto_runner.py b/monai/apps/auto3dseg/auto_runner.py index b37a45abf9..d8cd513cf7 100644 --- a/monai/apps/auto3dseg/auto_runner.py +++ b/monai/apps/auto3dseg/auto_runner.py @@ -407,7 +407,7 @@ def inspect_datalist_folds(self, datalist_filename: str) -> int: def set_gpu_customization( self, gpu_customization: bool = False, gpu_customization_specs: dict[str, Any] | None = None - ) -> None: + ) -> AutoRunner: """ Set options for GPU-based parameter customization/optimization. @@ -444,7 +444,7 @@ def set_gpu_customization( return self - def set_num_fold(self, num_fold: int = 5) -> None: + def set_num_fold(self, num_fold: int = 5) -> AutoRunner: """ Set the number of cross validation folds for all algos. @@ -458,7 +458,7 @@ def set_num_fold(self, num_fold: int = 5) -> None: return self - def set_training_params(self, params: dict[str, Any] | None = None) -> None: + def set_training_params(self, params: dict[str, Any] | None = None) -> AutoRunner: """ Set the training params for all algos. @@ -486,7 +486,7 @@ def set_device_info( num_nodes: int | None = None, mn_start_method: str | None = None, cmd_prefix: str | None = None, - ) -> None: + ) -> AutoRunner: """ Set the device related info @@ -539,7 +539,7 @@ def set_device_info( return self - def set_ensemble_method(self, ensemble_method_name: str = "AlgoEnsembleBestByFold", **kwargs: Any) -> None: + def set_ensemble_method(self, ensemble_method_name: str = "AlgoEnsembleBestByFold", **kwargs: Any) -> AutoRunner: """ Set the bundle ensemble method name and parameters for save image transform parameters. @@ -556,7 +556,7 @@ def set_ensemble_method(self, ensemble_method_name: str = "AlgoEnsembleBestByFol return self - def set_image_save_transform(self, **kwargs: Any) -> None: + def set_image_save_transform(self, **kwargs: Any) -> AutoRunner: """ Set the ensemble output transform. @@ -577,7 +577,7 @@ def set_image_save_transform(self, **kwargs: Any) -> None: return self - def set_prediction_params(self, params: dict[str, Any] | None = None) -> None: + def set_prediction_params(self, params: dict[str, Any] | None = None) -> AutoRunner: """ Set the prediction params for all algos. @@ -595,7 +595,7 @@ def set_prediction_params(self, params: dict[str, Any] | None = None) -> None: return self - def set_analyze_params(self, params: dict[str, Any] | None = None) -> None: + def set_analyze_params(self, params: dict[str, Any] | None = None) -> AutoRunner: """ Set the data analysis extra params. @@ -611,7 +611,7 @@ def set_analyze_params(self, params: dict[str, Any] | None = None) -> None: return self - def set_hpo_params(self, params: dict[str, Any] | None = None) -> None: + def set_hpo_params(self, params: dict[str, Any] | None = None) -> AutoRunner: """ Set parameters for the HPO module and the algos before the training. It will attempt to (1) override bundle templates with the key-value pairs in ``params`` (2) change the config of the HPO module (e.g. NNI) if the @@ -639,7 +639,7 @@ def set_hpo_params(self, params: dict[str, Any] | None = None) -> None: return self - def set_nni_search_space(self, search_space): + def set_nni_search_space(self, search_space) -> AutoRunner: """ Set the search space for NNI parameter search. From 668638f04fd6f8aa8f8c196e5c5a8c2d19af8604 Mon Sep 17 00:00:00 2001 From: Behrooz <3968947+drbeh@users.noreply.github.com> Date: Fri, 27 Oct 2023 10:11:49 -0400 Subject: [PATCH 3/3] add typehint for nni search space Signed-off-by: Behrooz <3968947+drbeh@users.noreply.github.com> --- monai/apps/auto3dseg/auto_runner.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/apps/auto3dseg/auto_runner.py b/monai/apps/auto3dseg/auto_runner.py index d8cd513cf7..23fb3eb250 100644 --- a/monai/apps/auto3dseg/auto_runner.py +++ b/monai/apps/auto3dseg/auto_runner.py @@ -639,7 +639,7 @@ def set_hpo_params(self, params: dict[str, Any] | None = None) -> AutoRunner: return self - def set_nni_search_space(self, search_space) -> AutoRunner: + def set_nni_search_space(self, search_space: dict[str, Any]) -> AutoRunner: """ Set the search space for NNI parameter search.