Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
148 lines (134 loc) · 4.37 KB
/
Copy path__init__.py
File metadata and controls
148 lines (134 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
All the metrics are to be called from here
"""
from warnings import warn
from typing import Union
from GANDLF.losses.regression import MSE_loss, CEL
from .segmentation import (
multi_class_dice,
multi_class_dice_per_label,
hd100,
hd100_per_label,
hd95,
hd95_per_label,
nsd,
nsd_per_label,
sensitivity,
sensitivity_per_label,
specificity_segmentation,
specificity_segmentation_per_label,
jaccard,
jaccard_per_label,
)
from .regression import classification_accuracy, balanced_acc_score, per_label_accuracy
from .generic import (
recall_score,
precision_score,
iou_score,
f1_score,
accuracy,
specificity_score,
)
from .synthesis import (
structural_similarity_index,
mean_squared_error,
root_mean_squared_error,
peak_signal_noise_ratio,
mean_squared_log_error,
mean_absolute_error,
ncc_metrics,
)
import GANDLF.metrics.classification as classification
import GANDLF.metrics.regression as regression
from .segmentation_panoptica import generate_instance_segmentation
# global defines for the metrics
global_metrics_dict = {
"dice": multi_class_dice,
"dice_per_label": multi_class_dice_per_label,
"accuracy": accuracy,
"mse": MSE_loss,
"hd95": hd95,
"hd95_per_label": hd95_per_label,
"hausdorff95_per_label": hd95_per_label,
"hausdorff95": hd95,
"hd100": hd100,
"hd100_per_label": hd100_per_label,
"hausdorff": hd100,
"hausdorff100": hd100,
"nsd": nsd,
"nsd_per_label": nsd_per_label,
"normalized_surface_dice": nsd,
"normalized_surface_dice_per_label": nsd_per_label,
"normalized_sd": nsd,
"normalized_sd_per_label": nsd_per_label,
"cel": CEL,
"f1_score": f1_score,
"f1": f1_score,
"classification_accuracy": classification_accuracy,
"precision": precision_score,
"recall": recall_score,
"specificity": specificity_score,
"iou": iou_score,
"balanced_accuracy": balanced_acc_score,
"per_label_one_hot_accuracy": per_label_accuracy,
"sensitivity": sensitivity,
"sensitivity_per_label": sensitivity_per_label,
"specificity_segmentation": specificity_segmentation,
"specificity_segmentation_per_label": specificity_segmentation_per_label,
"jaccard": jaccard,
"jaccard_per_label": jaccard_per_label,
}
# global define for the metrics that use surface distances, and hence require "connectivity" to be defined
surface_distance_ids = [
"hd95",
"hd95_per_label",
"hausdorff95_per_label",
"hausdorff95",
"hd100",
"hd100_per_label",
"hausdorff",
"hausdorff100",
"nsd",
"nsd_per_label",
"normalized_surface_dice",
"normalized_surface_dice_per_label",
"normalized_sd",
"normalized_sd_per_label",
]
def get_metrics(params: dict) -> dict:
"""
Returns an dictionary of containing calculators of the specified metric functions
Args:
params (dict): A dictionary containing the overall training parameters.
Returns:
metric_calculators (dict): A dictionary containing the calculators of the specified metric functions.
"""
metric_calculators = {}
for metric_name in params["metrics"]:
metric_name = metric_name.lower()
if metric_name not in global_metrics_dict:
warn(
f"Metric {metric_name} not found in global metrics dictionary, it will not be used.",
UserWarning,
)
continue
else:
metric_calculators[metric_name] = global_metrics_dict[metric_name]
return metric_calculators
def overall_stats(predictions, ground_truth, params) -> dict[str, Union[float, list]]:
"""
Generates a dictionary of metrics calculated on the overall predictions and ground truths.
Args:
predictions (torch.Tensor): The output of the model.
ground_truth (torch.Tensor): The ground truth labels.
params (dict): The parameter dictionary containing training and data information.
Returns:
dict: A dictionary of metrics.
"""
assert len(predictions) == len(
ground_truth
), "Predictions and ground truth must be of same length"
if params["problem_type"] == "classification":
return classification.overall_stats(predictions, ground_truth, params)
elif params["problem_type"] == "regression":
return regression.overall_stats(predictions, ground_truth, params)