44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
''' Utils for evaluation '''
|
|
|
|
import numpy as np
|
|
|
|
|
|
def cal_dtw(shortest_distances, prediction, reference, success=None, threshold=3.0):
|
|
dtw_matrix = np.inf * np.ones((len(prediction) + 1, len(reference) + 1))
|
|
dtw_matrix[0][0] = 0
|
|
for i in range(1, len(prediction)+1):
|
|
for j in range(1, len(reference)+1):
|
|
best_previous_cost = min(
|
|
dtw_matrix[i-1][j], dtw_matrix[i][j-1], dtw_matrix[i-1][j-1])
|
|
cost = shortest_distances[prediction[i-1]][reference[j-1]]
|
|
dtw_matrix[i][j] = cost + best_previous_cost
|
|
|
|
dtw = dtw_matrix[len(prediction)][len(reference)]
|
|
ndtw = np.exp(-dtw/(threshold * len(reference)))
|
|
if success is None:
|
|
success = float(shortest_distances[prediction[-1]][reference[-1]] < threshold)
|
|
sdtw = success * ndtw
|
|
|
|
return {
|
|
'DTW': dtw,
|
|
'nDTW': ndtw,
|
|
'SDTW': sdtw
|
|
}
|
|
|
|
def cal_cls(shortest_distances, prediction, reference, threshold=3.0):
|
|
def length(nodes):
|
|
return np.sum([
|
|
shortest_distances[a][b]
|
|
for a, b in zip(nodes[:-1], nodes[1:])
|
|
])
|
|
|
|
coverage = np.mean([
|
|
np.exp(-np.min([ # pylint: disable=g-complex-comprehension
|
|
shortest_distances[u][v] for v in prediction
|
|
]) / threshold) for u in reference
|
|
])
|
|
expected = coverage * length(reference)
|
|
score = expected / (expected + np.abs(expected - length(prediction)))
|
|
return coverage * score
|
|
|