Skip to content

Commit b298600

Browse files
committed
use cyflann instead of pyflann (radius search not working)
1 parent 96b628e commit b298600

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

pygsp/graphs/nngraphs/nngraph.py

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727

2828
}
2929

30-
def _import_pfl():
30+
def _import_cfl():
3131
try:
32-
import pyflann as pfl
32+
import cyflann as cfl
3333
except Exception:
34-
raise ImportError('Cannot import pyflann. Choose another nearest '
34+
raise ImportError('Cannot import cyflann. Choose another nearest '
3535
'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
3838

3939

4040

@@ -50,15 +50,15 @@ def _knn_flann(X, num_neighbors, dist_type, order):
5050
if dist_type == 'max_dist':
5151
raise ValueError('FLANN and max_dist is not supported')
5252

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)
5757
# Default FLANN parameters (I tried changing the algorithm and
5858
# testing performance on huge matrices, but the default one
5959
# 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()
6262
if dist_type == 'euclidean': # flann returns squared distances
6363
return NN, np.sqrt(D)
6464
return NN, D
@@ -102,21 +102,20 @@ def _radius_flann(X, epsilon, dist_type, order=0):
102102
# do not allow it
103103
if dist_type == 'max_dist':
104104
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)
110105

106+
cfl = _import_cfl()
107+
cfl.set_distance_type(dist_type, order=order)
108+
c = cfl.FLANNIndex(algorithm='kdtree')
109+
c.build_index(X)
111110
D = []
112111
NN = []
113112
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)
115114
D.append(d)
116115
NN.append(nn)
117-
flann.delete_index()
116+
c.free_index()
118117
if dist_type == 'euclidean': # flann returns squared distances
119-
return NN, np.sqrt(D)
118+
return NN, list(map(np.sqrt, D))
120119
return NN, D
121120

122121
def center_input(X, N):
@@ -228,7 +227,13 @@ def __init__(self, Xin, NNtype='knn', backend='scipy-kdtree', center=True,
228227
Xout = rescale_input(Xout, N, d)
229228

230229

230+
if self._nn_functions.get(NNtype) == None:
231+
raise ValueError('Invalid NNtype {}'.format(self.NNtype))
231232

233+
if self._nn_functions[NNtype].get(backend) == None:
234+
raise ValueError('Invalid backend {} for type {}'.format(backend,
235+
self.NNtype))
236+
232237
if self.NNtype == 'knn':
233238
spi = np.zeros((N * k))
234239
spj = np.zeros((N * k))
@@ -262,8 +267,6 @@ def __init__(self, Xin, NNtype='knn', backend='scipy-kdtree', center=True,
262267
float(self.sigma))
263268
start = start + leng
264269

265-
else:
266-
raise ValueError('Unknown NNtype {}'.format(self.NNtype))
267270

268271
W = sparse.csc_matrix((spv, (spi, spj)), shape=(N, N))
269272

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
# Construct patch graphs from images.
3131
'scikit-image',
3232
# Approximate nearest neighbors for kNN graphs.
33-
'pyflann; python_version == "2.*"',
34-
'pyflann3; python_version == "3.*"',
33+
'flann',
34+
'cyflann',
3535
# Convex optimization on graph.
3636
'pyunlocbox',
3737
# Plot graphs, signals, and filters.

0 commit comments

Comments
 (0)