Skip to content

Commit edf64c5

Browse files
author
mvargas33
committed
Commented each test
1 parent 39a1c76 commit edf64c5

File tree

1 file changed

+51
-21
lines changed

1 file changed

+51
-21
lines changed

test/test_bilinear_mixin.py

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,42 @@ def __init__(self, preprocessor=None):
1616
super().__init__(preprocessor=preprocessor)
1717

1818
def fit(self, X, y):
19+
"""
20+
Checks input's format. Sets M matrix to identity of shape (d,d)
21+
where d is the dimension of the input.
22+
"""
1923
X, y = self._prepare_inputs(X, y, ensure_min_samples=2)
2024
self.d = np.shape(X[0])[-1]
2125
self.components_ = np.identity(self.d)
2226
return self
2327

2428
def random_M(self):
29+
"""
30+
Changes the matrix M for a random one of shape (d,d)
31+
"""
2532
self.components_ = np.random.rand(self.d, self.d)
2633

2734

28-
def test_same_similarity_with_two_methods():
35+
def identity_fit(d=100):
36+
"""
37+
Creates two d-dimentional arrays. Fits an IdentityBilinearMixin()
38+
and then returns the two arrays and the mixin. Testing purposes
39+
"""
2940
d = 100
3041
u = np.random.rand(d)
3142
v = np.random.rand(d)
3243
mixin = IdentityBilinearMixin()
3344
mixin.fit([u, v], [0, 0])
34-
mixin.random_M() # Dummy fit
45+
return u, v, mixin
46+
3547

48+
def test_same_similarity_with_two_methods():
49+
""""
50+
Tests that score_pairs() and get_metric() give consistent results.
51+
In both cases, the results must match for the same input.
52+
"""
53+
u, v, mixin = identity_fit()
54+
mixin.random_M() # Dummy fit
3655
# The distances must match, whether calc with get_metric() or score_pairs()
3756
dist1 = mixin.score_pairs([[u, v], [v, u]])
3857
dist2 = [mixin.get_metric()(u, v), mixin.get_metric()(v, u)]
@@ -41,11 +60,13 @@ def test_same_similarity_with_two_methods():
4160

4261

4362
def test_check_correctness_similarity():
63+
"""
64+
Tests the correctness of the results made from socre_paris() and
65+
get_metric(). Results are compared with the real bilinear similarity
66+
calculated in-place.
67+
"""
4468
d = 100
45-
u = np.random.rand(d)
46-
v = np.random.rand(d)
47-
mixin = IdentityBilinearMixin()
48-
mixin.fit([u, v], [0, 0]) # Identity fit
69+
u, v, mixin = identity_fit(d)
4970
dist1 = mixin.score_pairs([[u, v], [v, u]])
5071
dist2 = [mixin.get_metric()(u, v), mixin.get_metric()(v, u)]
5172

@@ -57,6 +78,10 @@ def test_check_correctness_similarity():
5778

5879

5980
def test_check_handmade_example():
81+
"""
82+
Checks that score_pairs() result is correct comparing it with a
83+
handmade example.
84+
"""
6085
u = np.array([0, 1, 2])
6186
v = np.array([3, 4, 5])
6287
mixin = IdentityBilinearMixin()
@@ -68,6 +93,11 @@ def test_check_handmade_example():
6893

6994

7095
def test_check_handmade_symmetric_example():
96+
"""
97+
When the Bilinear matrix is the identity. The similarity
98+
between two arrays must be equal: S(u,v) = S(v,u). Also
99+
checks the random case: when the matrix is pd and symetric.
100+
"""
71101
u = np.array([0, 1, 2])
72102
v = np.array([3, 4, 5])
73103
mixin = IdentityBilinearMixin()
@@ -77,11 +107,13 @@ def test_check_handmade_symmetric_example():
77107

78108

79109
def test_score_pairs_finite():
110+
"""
111+
Checks for 'n' score_pairs() of 'd' dimentions, that all
112+
similarities are finite numbers, not NaN, +inf or -inf.
113+
Considering a random M for bilinear similarity.
114+
"""
80115
d = 100
81-
u = np.random.rand(d)
82-
v = np.random.rand(d)
83-
mixin = IdentityBilinearMixin()
84-
mixin.fit([u, v], [0, 0])
116+
u, v, mixin = identity_fit(d)
85117
mixin.random_M() # Dummy fit
86118
n = 100
87119
X = np.array([np.random.rand(d) for i in range(n)])
@@ -90,14 +122,13 @@ def test_score_pairs_finite():
90122

91123

92124
def test_score_pairs_dim():
93-
# scoring of 3D arrays should return 1D array (several tuples),
94-
# and scoring of 2D arrays (one tuple) should return an error (like
95-
# scikit-learn's error when scoring 1D arrays)
125+
"""
126+
Scoring of 3D arrays should return 1D array (several tuples),
127+
and scoring of 2D arrays (one tuple) should return an error (like
128+
scikit-learn's error when scoring 1D arrays)
129+
"""
96130
d = 100
97-
u = np.random.rand(d)
98-
v = np.random.rand(d)
99-
mixin = IdentityBilinearMixin()
100-
mixin.fit([u, v], [0, 0])
131+
u, v, mixin = identity_fit()
101132
mixin.random_M() # Dummy fit
102133
n = 100
103134
X = np.array([np.random.rand(d) for i in range(n)])
@@ -113,11 +144,10 @@ def test_score_pairs_dim():
113144

114145

115146
def test_check_scikitlearn_compatibility():
147+
"""Check that the similarity returned by get_metric() is compatible with
148+
scikit-learn's algorithms using a custom metric, DBSCAN for instance"""
116149
d = 100
117-
u = np.random.rand(d)
118-
v = np.random.rand(d)
119-
mixin = IdentityBilinearMixin()
120-
mixin.fit([u, v], [0, 0])
150+
u, v, mixin = identity_fit(d)
121151
mixin.random_M() # Dummy fit
122152

123153
n = 100

0 commit comments

Comments
 (0)