-
Notifications
You must be signed in to change notification settings - Fork 9
/
pi0_metrics.py
58 lines (43 loc) · 1.58 KB
/
pi0_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
import numpy as np
def normalize_vector(vec):
"""
Given n-dimensional vector, return normalized unit vector.
Inputs:
- vec (np.array): N x d array, where N is the number of vectors.
Returns:
- normed_vec: N x d normalized vector array.
"""
v = np.atleast_2d(vec)
v = v / np.linalg.norm(v, axis=1).reshape(v.shape[0], 1)
return v
def cosine_similarity(vec1, vec2):
"""
NOTE: Input vectors need not be normalized to unity.
Inputs:
- vec1: (N, 3) np.array, where each row contains a
predicted/truth direction vector.
- vec2: (N, 3) np.array, where each row contains a
truth/predicted direction vector.
Returns:
- similarity: (N, ) np.array, where each row is the
cosine similarity metric.
"""
assert vec1.shape == vec2.shape
# Normalize to unit vectors
vec1 = vec1 / np.linalg.norm(vec1, axis=1).reshape(vec1.shape[0], 1)
vec2 = vec2 / np.linalg.norm(vec2, axis=1).reshape(vec2.shape[0], 1)
return np.diag(np.dot(vec1, vec2.T))
def angular_similarity(vec1, vec2, weight=1.0):
csim = cosine_similarity(vec1, vec2)
return 1 - np.arccos(csim) / (np.pi / weight)
def f_score(purity, efficiency):
"""
Computes the F1-score for clustering accuracy, defined as the
harmonic mean of purity and efficiency.
Inputs:
- purity (float): purity score for given event
- efficiency (float): efficiency score for given event
Returns:
- fscore: self-expanatory.
"""
return 2 * (purity * efficiency) / (purity + efficiency)