Skip to content

feat: expose voronoi to python #833

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

Draft
wants to merge 10 commits into
base: develop
Choose a base branch
from

Conversation

BeaMarton13
Copy link
Contributor

I used Cody for the functions.

  • By submitting this pull request, I assign the copyright of my contribution to The igraph development team.

@BeaMarton13 BeaMarton13 changed the title feat: expose voronoi python feat: expose voronoi to python Jun 16, 2025
@ntamas
Copy link
Member

ntamas commented Jun 16, 2025

Try to avoid re-formatting the .py files even if your IDE insists on reformatting. It makes it much harder to go through the PR and separate pure formatting-only modifications from the "real" part of the PR.

Copy link
Member

@szhorvat szhorvat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only a few comments, not an exhaustive review.

igraph_vector_t *lengths_v = 0;
igraph_vector_t *weights_v = 0;
igraph_vector_int_t membership_v, generators_v;
igraph_neimode_t mode = IGRAPH_ALL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaning towards making IGRAPH_OUT the default. This is explicitly designed to handle directed graphs properly. Opinions @ntamas ?

If this is changed, the documentation should also be changed from "all" to "out".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There isn't really any consistency in the Python interface w.r.t. the defaults of mode arguments. Let's see what functions.yaml says in the upstream repo and use that default - this will be used in the new Python interface as well.

Comment on lines 13912 to 13913
igraph_vector_t *lengths_v = 0;
igraph_vector_t *weights_v = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that this codebase frequently uses 0 for null pointers, but let's aim to be clear with newly added code: NULL should be used for pointers, 0 for numbers, and false for Booleans.

Copy link
Member

@ntamas ntamas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a full review yet, I haven't checked the unit test and the Python side in detail. From a bird's eye view the Python side looks good.

}

/* Handle radius parameter */
if (radius_o != Py_None) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useigraphmodule_PyObject_to_real_t here, this simplifies the entire logic that you have coded here. I think it does not handle Py_None so you need to handle that separately.

@szhorvat szhorvat requested a review from Copilot June 22, 2025 13:05
Copy link

@Copilot Copilot AI left a 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 pull request exposes a new Voronoi community detection algorithm to Python.

  • Adds new test cases in tests/test_decomposition.py to validate the Voronoi clustering implementation.
  • Implements the _community_voronoi function in src/igraph/community.py with detailed documentation.
  • Updates the Python API and C extension bindings in src/igraph/init.py and src/_igraph/graphobject.c respectively.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/test_decomposition.py Introduces tests for the community_voronoi method using multiple graph scenarios.
src/igraph/community.py Implements the Voronoi clustering algorithm with comprehensive documentation.
src/igraph/init.py Exposes the new community_voronoi function through the public API.
src/_igraph/graphobject.c Adds the C extension binding for community_voronoi, handling Python–C argument conversion and result packaging.

Comment on lines 502 to 503
self.assertTrue(
communities == expected_communities or communities == expected_communities[::-1] # Order might be swapped
Copy link
Preview

Copilot AI Jun 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider comparing the communities without relying on order (e.g. by converting both collections to a set of frozensets) to make the test more robust.

Suggested change
self.assertTrue(
communities == expected_communities or communities == expected_communities[::-1] # Order might be swapped
self.assertEqual(
set(frozenset(c) for c in communities),
set(frozenset(c) for c in expected_communities),

Copilot uses AI. Check for mistakes.


/* Handle weights parameter */
if (igraphmodule_attrib_to_vector_t(weights_o, self, &weights_v, ATTRIBUTE_TYPE_EDGE)) {
if (lengths_v != 0) {
Copy link
Preview

Copilot AI Jun 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, consider checking against NULL (e.g., 'if (lengths_v != NULL)') when working with pointer values.

Suggested change
if (lengths_v != 0) {
if (lengths_v != NULL) {

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, this would be more readable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants