From 6491b807466cbb4f17379b53c6adef920e27ef10 Mon Sep 17 00:00:00 2001 From: elitap Date: Tue, 21 Nov 2023 09:46:36 +0000 Subject: [PATCH 1/9] add class label option to write metric report to improve readability of the report Signed-off-by: elitap --- monai/handlers/utils.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/monai/handlers/utils.py b/monai/handlers/utils.py index 58a3fd36f3..565a59daa4 100644 --- a/monai/handlers/utils.py +++ b/monai/handlers/utils.py @@ -59,6 +59,7 @@ def write_metrics_reports( metrics: dict[str, torch.Tensor | np.ndarray] | None, metric_details: dict[str, torch.Tensor | np.ndarray] | None, summary_ops: str | Sequence[str] | None, + class_labels: Sequence[str] | None = None, deli: str = ",", output_type: str = "csv", ) -> None: @@ -91,6 +92,8 @@ class mean median max 5percentile 95percentile notnans class1 6.0000 6.0000 6.0000 6.0000 6.0000 1.0000 mean 6.2500 6.2500 7.0000 5.5750 6.9250 2.0000 + class_labels: list of class names used to name the classes in the output report, if None, + "class0", ..., "classn" are used, default to None. deli: the delimiter character in the saved file, default to "," as the default output type is `csv`. to be consistent with: https://docs.python.org/3/library/csv.html#csv.Dialect.delimiter. output_type: expected output file type, supported types: ["csv"], default to "csv". @@ -118,7 +121,9 @@ class mean median max 5percentile 95percentile notnans v = v.reshape((-1, 1)) # add the average value of all classes to v - class_labels = ["class" + str(i) for i in range(v.shape[1])] + ["mean"] + if class_labels is None: + class_labels = ["class" + str(i) for i in range(v.shape[1])] + class_labels += ["mean"] v = np.concatenate([v, np.nanmean(v, axis=1, keepdims=True)], axis=1) with open(os.path.join(save_dir, f"{k}_raw.csv"), "w") as f: From 6c219a47a37632460d422546039358427ed9f47d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 21 Nov 2023 09:50:30 +0000 Subject: [PATCH 2/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/handlers/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/handlers/utils.py b/monai/handlers/utils.py index 565a59daa4..ee3a9640ad 100644 --- a/monai/handlers/utils.py +++ b/monai/handlers/utils.py @@ -92,7 +92,7 @@ class mean median max 5percentile 95percentile notnans class1 6.0000 6.0000 6.0000 6.0000 6.0000 1.0000 mean 6.2500 6.2500 7.0000 5.5750 6.9250 2.0000 - class_labels: list of class names used to name the classes in the output report, if None, + class_labels: list of class names used to name the classes in the output report, if None, "class0", ..., "classn" are used, default to None. deli: the delimiter character in the saved file, default to "," as the default output type is `csv`. to be consistent with: https://docs.python.org/3/library/csv.html#csv.Dialect.delimiter. From 1b38f868ba92f4e95902437ed0a191a6357950d7 Mon Sep 17 00:00:00 2001 From: elitap Date: Tue, 21 Nov 2023 10:32:30 +0000 Subject: [PATCH 3/9] address mypy error Signed-off-by: elitap --- monai/handlers/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/handlers/utils.py b/monai/handlers/utils.py index 565a59daa4..06d98f93ab 100644 --- a/monai/handlers/utils.py +++ b/monai/handlers/utils.py @@ -123,7 +123,7 @@ class mean median max 5percentile 95percentile notnans # add the average value of all classes to v if class_labels is None: class_labels = ["class" + str(i) for i in range(v.shape[1])] - class_labels += ["mean"] + class_labels.append(["mean"]) v = np.concatenate([v, np.nanmean(v, axis=1, keepdims=True)], axis=1) with open(os.path.join(save_dir, f"{k}_raw.csv"), "w") as f: From 3214c89ff4e02f2ead8c75f2fbda5615f65a061a Mon Sep 17 00:00:00 2001 From: elitap Date: Tue, 21 Nov 2023 11:10:36 +0000 Subject: [PATCH 4/9] fix mypy 2.0 Signed-off-by: elitap --- monai/handlers/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/monai/handlers/utils.py b/monai/handlers/utils.py index 36f40cce46..186b90c4ec 100644 --- a/monai/handlers/utils.py +++ b/monai/handlers/utils.py @@ -123,7 +123,7 @@ class mean median max 5percentile 95percentile notnans # add the average value of all classes to v if class_labels is None: class_labels = ["class" + str(i) for i in range(v.shape[1])] - class_labels.append(["mean"]) + class_labels = class_labels + ["mean"] v = np.concatenate([v, np.nanmean(v, axis=1, keepdims=True)], axis=1) with open(os.path.join(save_dir, f"{k}_raw.csv"), "w") as f: From ebf91de98946bffde307b4bf6ffe443b19472328 Mon Sep 17 00:00:00 2001 From: elitap Date: Tue, 21 Nov 2023 11:33:38 +0000 Subject: [PATCH 5/9] fix mypy 3.0 list it isgit add -u! Signed-off-by: elitap --- monai/handlers/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monai/handlers/utils.py b/monai/handlers/utils.py index 186b90c4ec..b24bd21ea9 100644 --- a/monai/handlers/utils.py +++ b/monai/handlers/utils.py @@ -59,7 +59,7 @@ def write_metrics_reports( metrics: dict[str, torch.Tensor | np.ndarray] | None, metric_details: dict[str, torch.Tensor | np.ndarray] | None, summary_ops: str | Sequence[str] | None, - class_labels: Sequence[str] | None = None, + class_labels: list[str] | None = None, deli: str = ",", output_type: str = "csv", ) -> None: @@ -123,7 +123,7 @@ class mean median max 5percentile 95percentile notnans # add the average value of all classes to v if class_labels is None: class_labels = ["class" + str(i) for i in range(v.shape[1])] - class_labels = class_labels + ["mean"] + class_labels += ["mean"] v = np.concatenate([v, np.nanmean(v, axis=1, keepdims=True)], axis=1) with open(os.path.join(save_dir, f"{k}_raw.csv"), "w") as f: From 89c62a836a00ff06d13d0f65da196a8a312ed89a Mon Sep 17 00:00:00 2001 From: elitap Date: Tue, 21 Nov 2023 13:02:54 +0000 Subject: [PATCH 6/9] move new param to last pos, ensure list of str Signed-off-by: elitap --- monai/handlers/utils.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/monai/handlers/utils.py b/monai/handlers/utils.py index b24bd21ea9..be9b48a812 100644 --- a/monai/handlers/utils.py +++ b/monai/handlers/utils.py @@ -59,9 +59,9 @@ def write_metrics_reports( metrics: dict[str, torch.Tensor | np.ndarray] | None, metric_details: dict[str, torch.Tensor | np.ndarray] | None, summary_ops: str | Sequence[str] | None, - class_labels: list[str] | None = None, deli: str = ",", output_type: str = "csv", + class_labels: list[str] | None = None, ) -> None: """ Utility function to write the metrics into files, contains 3 parts: @@ -92,11 +92,11 @@ class mean median max 5percentile 95percentile notnans class1 6.0000 6.0000 6.0000 6.0000 6.0000 1.0000 mean 6.2500 6.2500 7.0000 5.5750 6.9250 2.0000 - class_labels: list of class names used to name the classes in the output report, if None, - "class0", ..., "classn" are used, default to None. deli: the delimiter character in the saved file, default to "," as the default output type is `csv`. to be consistent with: https://docs.python.org/3/library/csv.html#csv.Dialect.delimiter. output_type: expected output file type, supported types: ["csv"], default to "csv". + class_labels: list of class names used to name the classes in the output report, if None, + "class0", ..., "classn" are used, default to None. """ if output_type.lower() != "csv": @@ -123,6 +123,9 @@ class mean median max 5percentile 95percentile notnans # add the average value of all classes to v if class_labels is None: class_labels = ["class" + str(i) for i in range(v.shape[1])] + else: + class_labels=[str(i) for i in class_labels] # ensure to have a list of str + class_labels += ["mean"] v = np.concatenate([v, np.nanmean(v, axis=1, keepdims=True)], axis=1) From 3cf19d042fee021ab02e8eac4ac81ac8c24b7922 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 21 Nov 2023 13:03:31 +0000 Subject: [PATCH 7/9] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- monai/handlers/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monai/handlers/utils.py b/monai/handlers/utils.py index be9b48a812..6e177d5942 100644 --- a/monai/handlers/utils.py +++ b/monai/handlers/utils.py @@ -124,8 +124,8 @@ class mean median max 5percentile 95percentile notnans if class_labels is None: class_labels = ["class" + str(i) for i in range(v.shape[1])] else: - class_labels=[str(i) for i in class_labels] # ensure to have a list of str - + class_labels=[str(i) for i in class_labels] # ensure to have a list of str + class_labels += ["mean"] v = np.concatenate([v, np.nanmean(v, axis=1, keepdims=True)], axis=1) From 708fcbb9b4fdef4d23bc6893ec0bc37ec69275b1 Mon Sep 17 00:00:00 2001 From: elitap Date: Tue, 21 Nov 2023 13:53:13 +0000 Subject: [PATCH 8/9] fix formatting Signed-off-by: elitap --- monai/handlers/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/monai/handlers/utils.py b/monai/handlers/utils.py index be9b48a812..0cd31b89c2 100644 --- a/monai/handlers/utils.py +++ b/monai/handlers/utils.py @@ -124,8 +124,8 @@ class mean median max 5percentile 95percentile notnans if class_labels is None: class_labels = ["class" + str(i) for i in range(v.shape[1])] else: - class_labels=[str(i) for i in class_labels] # ensure to have a list of str - + class_labels = [str(i) for i in class_labels] # ensure to have a list of str + class_labels += ["mean"] v = np.concatenate([v, np.nanmean(v, axis=1, keepdims=True)], axis=1) From dbdbf80c4d6455d97741ff7cd1fcb58e1bcf45a8 Mon Sep 17 00:00:00 2001 From: elitap Date: Tue, 21 Nov 2023 13:56:52 +0000 Subject: [PATCH 9/9] formating Signed-off-by: elitap --- monai/handlers/utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/monai/handlers/utils.py b/monai/handlers/utils.py index 481d5a1611..0cd31b89c2 100644 --- a/monai/handlers/utils.py +++ b/monai/handlers/utils.py @@ -126,7 +126,6 @@ class mean median max 5percentile 95percentile notnans else: class_labels = [str(i) for i in class_labels] # ensure to have a list of str - class_labels += ["mean"] v = np.concatenate([v, np.nanmean(v, axis=1, keepdims=True)], axis=1)