-
Notifications
You must be signed in to change notification settings - Fork 258
feat: Graph.Nearest_Neighbor_Graph() #851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces a new graph generator function Graph.Nearest_Neighbor_Graph()
to python-igraph, which creates k-nearest neighbor graphs from spatial point data. The implementation serves as an example for exposing additional graph generators, specifically to guide the future implementation of a Delaunay generator.
Key changes:
- Adds C implementation of the nearest neighbor graph generator with support for different metrics (euclidean, manhattan) and configurable parameters
- Includes Python binding and method registration for the new graph generator
- Adds basic test coverage for the new functionality
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
File | Description |
---|---|
src/_igraph/graphobject.c | Implements the C function and registers the Python method for nearest neighbor graph generation |
src/_igraph/convert.h | Declares the metric conversion function for parameter parsing |
src/_igraph/convert.c | Implements conversion from Python objects to igraph metric types |
tests/test_generators.py | Adds basic test case for the new graph generator |
Comments suppressed due to low confidence (2)
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
9ea8ead
to
4b09056
Compare
4b09056
to
fb6f407
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer Graph.Nearest_Neighbor()
so I'll do the rename.
Nope, I changed my mind ;-)
* Spatial graphs * | ||
**********************************************************************/ | ||
|
||
PyObject *igraphmodule_Graph_Nearest_Neighbor_Graph(PyTypeObject *type, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PyObject *igraphmodule_Graph_Nearest_Neighbor_Graph(PyTypeObject *type, | |
PyObject *igraphmodule_Graph_Nearest_Neighbor(PyTypeObject *type, |
def testNearestNeighborGraph(self): | ||
points = [[0,0], [1,2], [-3, -3]] | ||
|
||
g = Graph.Nearest_Neighbor_Graph(points) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
g = Graph.Nearest_Neighbor_Graph(points) | |
g = Graph.Nearest_Neighbor(points) |
self.assertEqual(g.vcount(), 3) | ||
self.assertEqual(g.ecount(), 2) | ||
|
||
g = Graph.Nearest_Neighbor_Graph(points, directed=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
g = Graph.Nearest_Neighbor_Graph(points, directed=True) | |
g = Graph.Nearest_Neighbor(points, directed=True) |
self.assertEqual(g.ecount(), 3) | ||
|
||
# expecting a complete graph | ||
g = Graph.Nearest_Neighbor_Graph(points, k=2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
g = Graph.Nearest_Neighbor_Graph(points, k=2) | |
g = Graph.Nearest_Neighbor(points, k=2) |
@Zepeacedust, this is an example of exposing a new graph generator in python-igraph, that you can later use as a model to expose the Delaunay generator.
This is not my comfort zone so let's wait for comments from @ntamas.
@ntamas, do you prefer
Nearest_Neighbor_Graph
orNearest_Neighbor
here? Since this term is used in other contexts as well, I vote for keeping the Graph suffix.