@@ -16,23 +16,42 @@ def __init__(self, preprocessor=None):
16
16
super ().__init__ (preprocessor = preprocessor )
17
17
18
18
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
+ """
19
23
X , y = self ._prepare_inputs (X , y , ensure_min_samples = 2 )
20
24
self .d = np .shape (X [0 ])[- 1 ]
21
25
self .components_ = np .identity (self .d )
22
26
return self
23
27
24
28
def random_M (self ):
29
+ """
30
+ Changes the matrix M for a random one of shape (d,d)
31
+ """
25
32
self .components_ = np .random .rand (self .d , self .d )
26
33
27
34
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
+ """
29
40
d = 100
30
41
u = np .random .rand (d )
31
42
v = np .random .rand (d )
32
43
mixin = IdentityBilinearMixin ()
33
44
mixin .fit ([u , v ], [0 , 0 ])
34
- mixin .random_M () # Dummy fit
45
+ return u , v , mixin
46
+
35
47
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
36
55
# The distances must match, whether calc with get_metric() or score_pairs()
37
56
dist1 = mixin .score_pairs ([[u , v ], [v , u ]])
38
57
dist2 = [mixin .get_metric ()(u , v ), mixin .get_metric ()(v , u )]
@@ -41,11 +60,13 @@ def test_same_similarity_with_two_methods():
41
60
42
61
43
62
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
+ """
44
68
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 )
49
70
dist1 = mixin .score_pairs ([[u , v ], [v , u ]])
50
71
dist2 = [mixin .get_metric ()(u , v ), mixin .get_metric ()(v , u )]
51
72
@@ -57,6 +78,10 @@ def test_check_correctness_similarity():
57
78
58
79
59
80
def test_check_handmade_example ():
81
+ """
82
+ Checks that score_pairs() result is correct comparing it with a
83
+ handmade example.
84
+ """
60
85
u = np .array ([0 , 1 , 2 ])
61
86
v = np .array ([3 , 4 , 5 ])
62
87
mixin = IdentityBilinearMixin ()
@@ -68,6 +93,11 @@ def test_check_handmade_example():
68
93
69
94
70
95
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
+ """
71
101
u = np .array ([0 , 1 , 2 ])
72
102
v = np .array ([3 , 4 , 5 ])
73
103
mixin = IdentityBilinearMixin ()
@@ -77,11 +107,13 @@ def test_check_handmade_symmetric_example():
77
107
78
108
79
109
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
+ """
80
115
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 )
85
117
mixin .random_M () # Dummy fit
86
118
n = 100
87
119
X = np .array ([np .random .rand (d ) for i in range (n )])
@@ -90,14 +122,13 @@ def test_score_pairs_finite():
90
122
91
123
92
124
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
+ """
96
130
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 ()
101
132
mixin .random_M () # Dummy fit
102
133
n = 100
103
134
X = np .array ([np .random .rand (d ) for i in range (n )])
@@ -113,11 +144,10 @@ def test_score_pairs_dim():
113
144
114
145
115
146
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"""
116
149
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 )
121
151
mixin .random_M () # Dummy fit
122
152
123
153
n = 100
0 commit comments