-
Notifications
You must be signed in to change notification settings - Fork 88
/
metrics.py
97 lines (77 loc) · 2.71 KB
/
metrics.py
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
"""
Metrics for evaluating performance of segmentation estimators.
Metrics are suitable for comparing predicted change point sets
against true change points and quantify the error.
"""
import numpy as np
import numpy.typing as npt
from scipy.spatial.distance import directed_hausdorff
from sklearn.utils import check_array
__maintainer__ = []
def count_error(
true_change_points: npt.ArrayLike, pred_change_points: npt.ArrayLike
) -> float:
"""
Error counting the difference in the number of change points.
Parameters
----------
true_change_points: array_like
Integer indexes (positions) of true change points
pred_change_points: array_like
Integer indexes (positions) of predicted change points
Returns
-------
count_error
"""
true_change_points = check_array(true_change_points, ensure_2d=False)
pred_change_points = check_array(pred_change_points, ensure_2d=False)
return abs(true_change_points.size - pred_change_points.size)
def hausdorff_error(
true_change_points: npt.ArrayLike,
pred_change_points: npt.ArrayLike,
symmetric: bool = True,
seed: int = 0,
) -> float:
"""
Compute the Hausdorff distance between two sets of change points.
.. seealso::
This function wraps :py:func:`scipy.spatial.distance.directed_hausdorff`
Parameters
----------
true_change_points: array_like
Integer indexes (positions) of true change points
pred_change_points: array_like
Integer indexes (positions) of predicted change points
symmetric: bool
If `True` symmetric Hausdorff distance will be used
seed: int, default=0
Local numpy.random.RandomState seed. Default is 0, a random
shuffling of u and v that guarantees reproducibility.
Returns
-------
Hausdorff error.
"""
a = np.array(true_change_points).reshape(-1, 1)
b = np.array(pred_change_points).reshape(-1, 1)
d = directed_hausdorff(a, b)[0]
if symmetric:
d = max(d, directed_hausdorff(b, a)[0])
return d
def prediction_ratio(
true_change_points: npt.ArrayLike, pred_change_points: npt.ArrayLike
) -> float:
"""
Prediction ratio is the ratio of number of predicted to true change points.
Parameters
----------
true_change_points: array_like
Integer indexes (positions) of true change points
pred_change_points: array_like
Integer indexes (positions) of predicted change points
Returns
-------
prediction_ratio
"""
true_change_points = check_array(true_change_points, ensure_2d=False)
pred_change_points = check_array(pred_change_points, ensure_2d=False)
return pred_change_points.size / true_change_points.size