27
27
28
28
}
29
29
30
- def _import_pfl ():
30
+ def _import_cfl ():
31
31
try :
32
- import pyflann as pfl
32
+ import cyflann as cfl
33
33
except Exception :
34
- raise ImportError ('Cannot import pyflann . Choose another nearest '
34
+ raise ImportError ('Cannot import cyflann . Choose another nearest '
35
35
'neighbors method or try to install it with '
36
- 'pip (or conda) install pyflann (or pyflann3) .' )
37
- return pfl
36
+ 'pip (or conda) install cyflann .' )
37
+ return cfl
38
38
39
39
40
40
@@ -50,15 +50,15 @@ def _knn_flann(X, num_neighbors, dist_type, order):
50
50
if dist_type == 'max_dist' :
51
51
raise ValueError ('FLANN and max_dist is not supported' )
52
52
53
- pfl = _import_pfl ()
54
- pfl .set_distance_type (dist_type , order = order )
55
- flann = pfl . FLANN ( )
56
-
53
+ cfl = _import_cfl ()
54
+ cfl .set_distance_type (dist_type , order = order )
55
+ c = cfl . FLANNIndex ( algorithm = 'kdtree' )
56
+ c . build_index ( X )
57
57
# Default FLANN parameters (I tried changing the algorithm and
58
58
# testing performance on huge matrices, but the default one
59
59
# seems to work best).
60
- NN , D = flann . nn (X , X , num_neighbors = ( num_neighbors + 1 ),
61
- algorithm = 'kdtree' )
60
+ NN , D = c . nn_index (X , num_neighbors + 1 )
61
+ c . free_index ( )
62
62
if dist_type == 'euclidean' : # flann returns squared distances
63
63
return NN , np .sqrt (D )
64
64
return NN , D
@@ -102,21 +102,20 @@ def _radius_flann(X, epsilon, dist_type, order=0):
102
102
# do not allow it
103
103
if dist_type == 'max_dist' :
104
104
raise ValueError ('FLANN and max_dist is not supported' )
105
- pfl = _import_pfl ()
106
-
107
- pfl .set_distance_type (dist_type , order = order )
108
- flann = pfl .FLANN ()
109
- flann .build_index (X )
110
105
106
+ cfl = _import_cfl ()
107
+ cfl .set_distance_type (dist_type , order = order )
108
+ c = cfl .FLANNIndex (algorithm = 'kdtree' )
109
+ c .build_index (X )
111
110
D = []
112
111
NN = []
113
112
for k in range (N ):
114
- nn , d = flann .nn_radius (X [k , :], epsilon * epsilon )
113
+ nn , d = c .nn_radius (X [k , :], epsilon * epsilon )
115
114
D .append (d )
116
115
NN .append (nn )
117
- flann . delete_index ()
116
+ c . free_index ()
118
117
if dist_type == 'euclidean' : # flann returns squared distances
119
- return NN , np .sqrt ( D )
118
+ return NN , list ( map ( np .sqrt , D ) )
120
119
return NN , D
121
120
122
121
def center_input (X , N ):
@@ -228,7 +227,13 @@ def __init__(self, Xin, NNtype='knn', backend='scipy-kdtree', center=True,
228
227
Xout = rescale_input (Xout , N , d )
229
228
230
229
230
+ if self ._nn_functions .get (NNtype ) == None :
231
+ raise ValueError ('Invalid NNtype {}' .format (self .NNtype ))
231
232
233
+ if self ._nn_functions [NNtype ].get (backend ) == None :
234
+ raise ValueError ('Invalid backend {} for type {}' .format (backend ,
235
+ self .NNtype ))
236
+
232
237
if self .NNtype == 'knn' :
233
238
spi = np .zeros ((N * k ))
234
239
spj = np .zeros ((N * k ))
@@ -262,8 +267,6 @@ def __init__(self, Xin, NNtype='knn', backend='scipy-kdtree', center=True,
262
267
float (self .sigma ))
263
268
start = start + leng
264
269
265
- else :
266
- raise ValueError ('Unknown NNtype {}' .format (self .NNtype ))
267
270
268
271
W = sparse .csc_matrix ((spv , (spi , spj )), shape = (N , N ))
269
272
0 commit comments