Metrics Reference
This page documents the metrics sub-package.
plots
medpipe.metrics.plots
Plot functions module.
This module provides functions to plot results.
Functions: - plot_metrics_CI: Plots the metrics with confidence intrevals for each fold. - plot_prediction_distribution: Plots the prediction probabilities. - plot_reliability_diagrams: Plots the reliability diagrams.
plot_metrics_CI(ci_dict, label_list, save_path='', extension='.png', show_fig=True, **kwargs)
Plots the metrics with confidence intrevals for each fold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ci_dict
|
dict[str, list[tuple(float, float, float)]]
|
Dictionary containing the metric value and confidence intervals. The keys are the name of the metrics and the values are a list of tuple with first element the metric value, second the lower bound, and third the upper bound. One list elements per model. One list elements per model |
required |
label_list
|
list[str]
|
List of labels for the legend. |
required |
save_path
|
str
|
Path to the save file. |
[]
|
extension
|
str
|
Extension to save figure in. |
".png"
|
show_fig
|
bool
|
Flag to show the figure. |
True
|
**kwargs
|
Extra arguments for the figure or axes objects. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in src/medpipe/metrics/plots.py
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 | |
plot_prediction_distribution(dist_list, label_list=[], n_bins=10, save_path='', extension='.png', show_fig=True, **kwargs)
Plots the prediction probabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dist_list
|
list[array]
|
List of the predicted probability distributions. |
required |
label_list
|
list[str]
|
List of labels for the legend. |
[]
|
n_bins
|
int
|
Number of bins for the histogram. |
10
|
save_path
|
str
|
Path to the save file. |
[]
|
extension
|
str
|
Extension to save figure in. |
".png"
|
show_fig
|
bool
|
Flag to show the figure. |
True
|
**kwargs
|
Extra arguments for the figure or axes objects. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in src/medpipe/metrics/plots.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | |
plot_reliability_diagrams(y_test, proba_list, label_list=[], save_path='', extension='.png', show_fig=True, display_kwargs={}, **kwargs)
Plots the reliability diagrams for the given probabilities.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_test
|
array-like of shape (n_samples, n_classes)
|
Ground truth labels. |
required |
proba_list
|
list[array]
|
List of predicted probabilities. |
required |
label_list
|
list[str]
|
|
[]
|
save_path
|
str
|
Path to the save file. |
[]
|
extension
|
str
|
Extension to save figure in. |
".png"
|
show_fig
|
bool
|
Flag to show the figure. |
True
|
display_kwargs
|
dict[str, value]
|
Extra arguments for the CalibrationDisplay. |
{}
|
**kwargs
|
Extra arguments for the figure or axes objects. |
{}
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in src/medpipe/metrics/plots.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
core
medpipe.metrics.core
Core metric functions module.
This module provides functions to compute and print metrics.
Functions: - print_metrics: prints the numerical metrics. - print_metrics_CI: prints numerical metrics with their confidence intervals. - compute_all_CI: computes the confidence interval for all metrics. - compute_CI: computes the confidence interval. - extract_metric : extracts a metric for each fold. - compute_pred_metrics : computes the metrics that require the prediction labels. - compute_score_metrics : computes the metrics that require the score.
compute_CI(data)
Computes the confidence interval of the data.
The CI is calculated using the Student's t-distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
array-like of shape (n_samples, n_sets)
|
Data on which to compute the confidence interval. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
mean_arr |
np.array(float) of shape (n_sets,)
|
Mean values. |
lower_b_arr |
np.array(float) of shape (n_sets,)
|
Lower bound of the confidence intervals. |
upper_b_arr |
np.array(float) of shape (n_sets,)
|
Upper bound of the confidence intervals. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If data is not array-like |
Source code in src/medpipe/metrics/core.py
compute_all_CI(model_metrics, metric_list=[], **kwargs)
Computes the confidence intervals for all metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_metrics
|
dict[int, dict[str, float or tuple(array - like)]]
|
Model metrics for different folds. |
required |
metric_list
|
list[str]
|
List of metrics to calculate confidence interval. |
[]
|
**kwargs
|
Extra arguments for the compute_CI function. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
ci_dict |
dict[str, tuple(float, float, float)]
|
Dictionary containing the metric value and confidence intervals. The keys are the name of the metrics and the values are a tuple with first element the metric value, second the lower bound, and third the upper bound. |
Source code in src/medpipe/metrics/core.py
compute_pred_metrics(metric_list, y_true, y_pred)
Computes the metrics that require the prediction labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_list
|
list[str]
|
List of metrics. Possible values: - accuracy - f1 - precision - recall |
required |
y_true
|
array-like of shape (n_samples, n_classes)
|
Ground truth labels. |
required |
y_pred
|
array-like of shape (n_samples, n_classes)
|
Predicted labels. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
metric_dict |
dict[str, list[float]]
|
Dictionary of the metrics. The keys are the name of the metric and the values are the computed metric value. If multilabel then the list contains the value for each class and the last value is the average value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the metric is not recognised. |
Source code in src/medpipe/metrics/core.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | |
compute_score_metrics(metric_list, y_true, y_pred_proba)
Computes the metrics that require the score.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_list
|
list[str]
|
List of metrics. Possible values: - roc - auroc (area under the curve) - prc (precision-recall curve) - ap (average precision) - log_loss |
required |
y_true
|
array-like of shape (n_samples, n_classes)
|
Ground truth labels. |
required |
y_pred_proba
|
array or list[array]
|
Predicted scores. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
metric_dict |
dict[str, list[float or tuple]]
|
Dictionary of the metrics. The keys are the name of the metric and the values are the computed metric values. If multilabel then the list contains the value for each class. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the metric is not recognised. |
Source code in src/medpipe/metrics/core.py
extract_metric(model_metrics, metric_name)
Extracts the desired metric from each fold in the metric dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_metrics
|
dict[int, dict[str, float or tuple(array - like)]]
|
Model metrics for different folds. |
required |
metric_name
|
str
|
Name of the metric to extract. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
metric_list |
list[float]
|
List containing the metric values for each fold. |
Source code in src/medpipe/metrics/core.py
print_metrics(metric_dict, label_list, logger=None)
Prints the metrics on the terminal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_dict
|
dict[str, float or tuple(array - like)]
|
Dictionary of the model performance for one fold. Keys are the metric name and values are the metric value. The test metrics used are: - accuracy - f1 - precision - recall - log_loss - auroc (Area Under Receiver Operator Characteristic) - ap (Average Precision) |
required |
label_list
|
list[str]
|
List of predicted labels. |
required |
logger
|
Logger
|
Logger object to log prints. If None print to terminal. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |
Source code in src/medpipe/metrics/core.py
print_metrics_CI(ci_dict, label_list, logger=None)
Prints the metrics with their confidence intervals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ci_dict
|
dict[str, tuple(float, float, float)]
|
Dictionary containing the metric value and confidence intervals. The keys are the name of the metrics and the values are a tuple with first element the metric value, second the lower bound, and third the upper bound. |
required |
logger
|
Logger
|
Logger object to log prints. If None print to terminal. |
None
|
Returns:
| Type | Description |
|---|---|
None
|
Nothing is returned. |