Nearest Neighbors

class discopygal.solvers.nearest_neighbors.NearestNeighbors(metric)

Abstract class represeting the interface a nearest neighbors algorithm should comply.

Parameters:

metric (Metric) – metric to compute nearest neighbors

fit(points)

Get a list of points (in CGAL Point_2 or Point_d format) and fit some kind of data structure on them.

Parameters:

points (list<Point_2> or list<Point_d>) – list of points

get_points()

Return the list of inner points

Returns:

list of points

Return type:

list<Point_2> or list<Point_d>

k_nearest(point, k)

Given a point, return the k-nearest neighbors to that point.

Parameters:
  • point (Point_2 or Point_d) – query point

  • k (int) – number of neighbors to return (k)

Returns:

k nearest neighbors

Return type:

list<Point_2> or list<Point_d>

nearest_in_radius(point, radius)

Given a point and a radius, return all the neighbors that have distance <= radius

Parameters:
  • point (Point_2 or Point_d) – query point

  • radius (FT) – radius of neighborhood

Returns:

nearest neighbors in radius

Return type:

list<Point_2> or list<Point_d>

class discopygal.solvers.nearest_neighbors.NearestNeighborsCached(nn, max_cache=100)

Wrapper for a nearest neighbor object that also uses cache, and allows for insertion of points in real time. This allows adding points lazily, and only when cache is full then rebuilding the underlying data structure.

Example:
>>> nn = NearestNeighborsCached(NearestNeighbors_sklearn(metric=Metric_Euclidean))
>>> nn.add_point(Point_d(8, [...]))
Parameters:
  • nn (NearestNeighbors) – nearest neigbors object to wrap

  • max_cache – maximum cache size

add_point(point)

Add a point to the nearest neighbor search

Parameters:

point (Point_2 or Point_d) – point to add

fit(points)

Get a list of points (in CGAL Point_2 or Point_d format) and fit some kind of data structure on them.

Parameters:

points (list<Point_2> or list<Point_d>) – list of points

get_points()

Return the list of inner points

Returns:

list of points

Return type:

list<Point_2> or list<Point_d>

k_nearest(point, k)

Given a point, return the k-nearest neighbors to that point.

Parameters:
  • point (Point_2 or Point_d) – query point

  • k (int) – number of neighbors to return (k)

Returns:

k nearest neighbors

Return type:

list<Point_2> or list<Point_d>

nearest_in_radius(point, radius)

Given a point and a radius, return all the neighbors that have distance <= radius

Parameters:
  • point (Point_2 or Point_d) – query point

  • radius (FT) – radius of neighborhood

Returns:

nearest neighbors in radius

Return type:

list<Point_2> or list<Point_d>

class discopygal.solvers.nearest_neighbors.NearestNeighbors_CGAL(metric=<class 'discopygal.solvers.metrics.Metric_Euclidean'>)

CGAL implementation of nearest neighbors

Parameters:

metric (Metric) – metric to compute nearest neighbors

fit(points)

Get a list of points (in CGAL Point_2 or Point_d format) and fit some kind of data structure on them.

Parameters:

points (list<Point_2> or list<Point_d>) – list of points

k_nearest(point, k)

Given a point, return the k-nearest neighbors to that point.

Parameters:
  • point (Point_2 or Point_d) – query point

  • k (int) – number of neighbors to return (k)

Returns:

k nearest neighbors

Return type:

list<Point_2> or list<Point_d>

nearest_in_radius(point, radius)

Given a point and a radius, return all the neighbors that have distance <= radius

Parameters:
  • point (Point_2) – query point

  • radius (FT) – radius of neighborhood

Returns:

nearest neighbors in radius

Return type:

list<Point_2>

class discopygal.solvers.nearest_neighbors.NearestNeighbors_sklearn(metric=<class 'discopygal.solvers.metrics.Metric_Euclidean'>)

Sklearn implementation of nearest neighbors

Parameters:

metric (Metric) – metric to compute nearest neighbors

fit(points)

Get a list of points (in CGAL Point_2 or Point_d format) and fit some kind of data structure on them.

Parameters:

points (list<Point_2> or list<Point_d>) – list of points

k_nearest(point, k)

Given a point, return the k-nearest neighbors to that point.

Parameters:
  • point (Point_2 or Point_d) – query point

  • k (int) – number of neighbors to return (k)

Returns:

k nearest neighbors

Return type:

list<Point_2> or list<Point_d>

nearest_in_radius(point, radius)

Given a point and a radius, return all the neighbors that have distance <= radius

Parameters:
  • point (Point_2) – query point

  • radius (FT) – radius of neighborhood

Returns:

nearest neighbors in radius

Return type:

list<Point_2>