13
13
M = 500
14
14
15
15
16
+ @pytest .fixture (scope = "module" )
17
+ def falkon_import ():
18
+ try :
19
+ return True
20
+ except ImportError :
21
+ return False
22
+
23
+
16
24
@pytest .fixture (scope = "module" )
17
25
def input ():
18
26
return torch .normal (0 , 1 , size = (n_samples , n_genes ))
@@ -81,41 +89,60 @@ def fit_sklearn_matern_ridge(self, sklearn_matern_KRR, input, embedding):
81
89
###
82
90
83
91
@pytest .fixture (scope = "class" )
84
- def falkon_rbf_KRR (self ):
85
- return KRRApprox (
86
- method = "falkon" , kernel = "rbf" , kernel_params = {"sigma" : np .sqrt (2 * n_genes )}, penalization = penalization , M = M
87
- )
92
+ def falkon_rbf_KRR (self , falkon_import ):
93
+ if falkon_import :
94
+ return KRRApprox (
95
+ method = "falkon" ,
96
+ kernel = "rbf" ,
97
+ kernel_params = {"sigma" : np .sqrt (2 * n_genes )},
98
+ penalization = penalization ,
99
+ M = M ,
100
+ )
101
+ else :
102
+ return None
88
103
89
104
@pytest .fixture (scope = "class" )
90
- def falkon_matern_KRR (self ):
91
- return KRRApprox (
92
- method = "falkon" ,
93
- kernel = "matern" ,
94
- kernel_params = {"sigma" : np .sqrt (2 * n_genes ), "nu" : 1.5 },
95
- penalization = penalization ,
96
- M = M ,
97
- )
105
+ def falkon_matern_KRR (self , falkon_import ):
106
+ if falkon_import :
107
+ return KRRApprox (
108
+ method = "falkon" ,
109
+ kernel = "matern" ,
110
+ kernel_params = {"sigma" : np .sqrt (2 * n_genes ), "nu" : 1.5 },
111
+ penalization = penalization ,
112
+ M = M ,
113
+ )
114
+ else :
115
+ return None
98
116
99
117
@pytest .fixture (scope = "class" )
100
- def falkon_laplacian_KRR (self ):
101
- return KRRApprox (
102
- method = "falkon" ,
103
- kernel = "laplacian" ,
104
- kernel_params = {"sigma" : np .sqrt (2 * n_genes )},
105
- penalization = penalization ,
106
- M = M ,
107
- )
118
+ def falkon_laplacian_KRR (self , falkon_import ):
119
+ if falkon_import :
120
+ return KRRApprox (
121
+ method = "falkon" ,
122
+ kernel = "laplacian" ,
123
+ kernel_params = {"sigma" : np .sqrt (2 * n_genes )},
124
+ penalization = penalization ,
125
+ M = M ,
126
+ )
127
+ else :
128
+ return None
108
129
109
130
@pytest .fixture (scope = "class" )
110
131
def fit_falkon_rbf_ridge (self , falkon_rbf_KRR , input , embedding ):
132
+ if falkon_rbf_KRR is None :
133
+ return None
111
134
return falkon_rbf_KRR .fit (input , embedding )
112
135
113
136
@pytest .fixture (scope = "class" )
114
137
def fit_falkon_laplacian_ridge (self , falkon_laplacian_KRR , input , embedding ):
138
+ if falkon_laplacian_KRR is None :
139
+ return None
115
140
return falkon_laplacian_KRR .fit (input , embedding )
116
141
117
142
@pytest .fixture (scope = "class" )
118
143
def fit_falkon_matern_ridge (self , falkon_matern_KRR , input , embedding ):
144
+ if falkon_matern_KRR is None :
145
+ return None
119
146
return falkon_matern_KRR .fit (input , embedding )
120
147
121
148
###
@@ -131,9 +158,10 @@ def test_all_sklearn_kernels(self):
131
158
KRRApprox (kernel = kernel , method = "sklearn" )
132
159
return True
133
160
134
- def test_all_falkon_kernels (self ):
135
- for kernel in KRRApprox .falkon_kernel :
136
- KRRApprox (kernel = kernel , method = "falkon" )
161
+ def test_all_falkon_kernels (self , falkon_import ):
162
+ if falkon_import :
163
+ for kernel in KRRApprox .falkon_kernel :
164
+ KRRApprox (kernel = kernel , method = "falkon" )
137
165
return True
138
166
139
167
###
@@ -160,32 +188,37 @@ def test_laplacian_sklearn_fit(self, fit_sklearn_laplacian_ridge, valid_input, v
160
188
###
161
189
162
190
def test_rbf_falkon_fit (self , fit_falkon_rbf_ridge , valid_input , valid_embedding ):
163
- pred = fit_falkon_rbf_ridge .transform (valid_input )
164
- pearson_corr = scipy .stats .pearsonr (pred .flatten (), valid_embedding .detach ().numpy ().flatten ())
165
- assert pearson_corr [0 ] > pearson_threshold
191
+ if fit_falkon_rbf_ridge is not None :
192
+ pred = fit_falkon_rbf_ridge .transform (valid_input )
193
+ pearson_corr = scipy .stats .pearsonr (pred .flatten (), valid_embedding .detach ().numpy ().flatten ())
194
+ assert pearson_corr [0 ] > pearson_threshold
166
195
167
196
def test_matern_falkon_fit (self , fit_falkon_matern_ridge , valid_input , valid_embedding ):
168
- pred = fit_falkon_matern_ridge .transform (valid_input )
169
- pearson_corr = scipy .stats .pearsonr (pred .flatten (), valid_embedding .detach ().numpy ().flatten ())
170
- assert pearson_corr [0 ] > pearson_threshold
197
+ if fit_falkon_matern_ridge is not None :
198
+ pred = fit_falkon_matern_ridge .transform (valid_input )
199
+ pearson_corr = scipy .stats .pearsonr (pred .flatten (), valid_embedding .detach ().numpy ().flatten ())
200
+ assert pearson_corr [0 ] > pearson_threshold
171
201
172
202
def test_laplacian_falkon_fit (self , fit_falkon_laplacian_ridge , valid_input , valid_embedding ):
173
- pred = fit_falkon_laplacian_ridge .transform (valid_input )
174
- pearson_corr = scipy .stats .pearsonr (pred .flatten (), valid_embedding .detach ().numpy ().flatten ())
175
- assert pearson_corr [0 ] > pearson_threshold
203
+ if fit_falkon_laplacian_ridge is not None :
204
+ pred = fit_falkon_laplacian_ridge .transform (valid_input )
205
+ pearson_corr = scipy .stats .pearsonr (pred .flatten (), valid_embedding .detach ().numpy ().flatten ())
206
+ assert pearson_corr [0 ] > pearson_threshold
176
207
177
208
def test_ridge_coef_sklearn_fit (self , fit_sklearn_laplacian_ridge , input , valid_input ):
178
- pred_reconstruct = fit_sklearn_laplacian_ridge .kernel_ (
179
- valid_input , input [fit_sklearn_laplacian_ridge .ridge_samples_idx_ , :]
180
- )
181
- pred_reconstruct = pred_reconstruct .dot (fit_sklearn_laplacian_ridge .sample_weights_ )
182
- np .testing .assert_array_almost_equal (
183
- pred_reconstruct , fit_sklearn_laplacian_ridge .transform (valid_input ), decimal = 3
184
- )
209
+ if fit_sklearn_laplacian_ridge is not None :
210
+ pred_reconstruct = fit_sklearn_laplacian_ridge .kernel_ (
211
+ valid_input , input [fit_sklearn_laplacian_ridge .ridge_samples_idx_ , :]
212
+ )
213
+ pred_reconstruct = pred_reconstruct .dot (fit_sklearn_laplacian_ridge .sample_weights_ )
214
+ np .testing .assert_array_almost_equal (
215
+ pred_reconstruct , fit_sklearn_laplacian_ridge .transform (valid_input ), decimal = 3
216
+ )
185
217
186
218
def test_ridge_coef_falkon_fit (self , fit_falkon_laplacian_ridge , input , valid_input ):
187
- pred_reconstruct = fit_falkon_laplacian_ridge .kernel_ (valid_input , fit_falkon_laplacian_ridge .anchors ())
188
- pred_reconstruct = pred_reconstruct .matmul (fit_falkon_laplacian_ridge .sample_weights_ )
189
- np .testing .assert_array_almost_equal (
190
- pred_reconstruct , fit_falkon_laplacian_ridge .transform (valid_input ), decimal = 3
191
- )
219
+ if fit_falkon_laplacian_ridge is not None :
220
+ pred_reconstruct = fit_falkon_laplacian_ridge .kernel_ (valid_input , fit_falkon_laplacian_ridge .anchors ())
221
+ pred_reconstruct = pred_reconstruct .matmul (fit_falkon_laplacian_ridge .sample_weights_ )
222
+ np .testing .assert_array_almost_equal (
223
+ pred_reconstruct , fit_falkon_laplacian_ridge .transform (valid_input ), decimal = 3
224
+ )
0 commit comments