Skip to content

Add centroid as alias to barycenter - #8622

Merged
rossbar merged 7 commits into
networkx:mainfrom
OliCheesman:add-centroid-alias-to-barycenter
Apr 21, 2026
Merged

rossbar merged 7 commits into
networkx:mainfrom
OliCheesman:add-centroid-alias-to-barycenter

Conversation

@OliCheesman

Copy link
Copy Markdown
Contributor

Solves #8491

@OliCheesman

Copy link
Copy Markdown
Contributor Author

The function 'centroid' already appears in 'trees/distance_measures.py'. Does anyone have any solutions of how to solve it?

@OliCheesman

Copy link
Copy Markdown
Contributor Author

I've used the "name" attribute to differentiate the functions. Let me know if anyone has better suggestions.

@OliCheesman

Copy link
Copy Markdown
Contributor Author

Is it possible to get another opinion on these changes please? I would like to know if they are sufficient in solving the issue

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks pretty good to me.
I think the docs also need to be changed.
doc/reference/algorithms/distance_measures.rst should have centroid listed.

@dschult

dschult commented Apr 21, 2026

Copy link
Copy Markdown
Member

The docs for nx._dispatchable describe the name= input parameter as:

         name : str, optional (default: name of `func`)
            The dispatch name for the function. It defaults to the name of `func`,
            but can be set manually to avoid conflicts in the global dispatch
            namespace. A common pattern is to prefix the function name with its
            module or submodule to make it unique. For example:

                - ``@_dispatchable(name="tournament_is_strongly_connected")``
                  resolves conflict between ``nx.tournament.is_strongly_connected``
                  and ``nx.is_strongly_connected``.
                - ``@_dispatchable(name="approximate_node_connectivity")``
                  resolves conflict between ``nx.approximation.node_connectivity``
                  and ``nx.connectivity.node_connectivity``.

So I think we do actually need to use it for the tree.centroid version. Please leave the name keyword arg out for the main nx.centroid function (i.e. don't set name="graph_centroid").

There also appear to be two "style" issues. An extra space at the end of one line. And the one-line comment for barycenter should have the closing triple-" be at the end of the first line rather than the start oft he second line.

@OliCheesman

Copy link
Copy Markdown
Contributor Author

@dschult thanks for the help. I have added the name keyword argument for just tree.centroid and fixed the style issues.

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM to me too, thanks @OliCheesman and @dschult !

@rossbar
rossbar merged commit 65becad into networkx:main Apr 21, 2026
44 checks passed
@dschult dschult added this to the 3.7 milestone Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants