Repository navigation
feat(api): update non-tree check in _tree_center and move to tree subpackage - #8174
Conversation
Co-authored-by: Alejandro Candioti <[email protected]>
51c3dc4 to
ca3167d
Compare
_tree_center to tree subpackage_tree_center and move to tree subpackage
Co-authored-by: Ross Barnowski <[email protected]>
dschult
left a comment
There was a problem hiding this comment.
Nice -- and ready to merge. But I have two comments/suggestions/questions:
- what's up with the benchmark? Have you thought about what kind of trees to time this with? Why are we adding the benchmark?
- (see below) the condition for "not a tree" is cryptic, so best to make it as clear as possible. Are parens around and/or clauses good practice for readability? And simpler if we split out ideas into separate lines where possible, so avoid walrus operator unless it helps beyond reducing lines.
Not blockers -- and the benchmarks are a bigger conversation I suspect. :)
Co-authored-by: Dan Schult <[email protected]>
|
@dschult I originally added the benchmark when we were looking into making the nontree check more exhaustive, as I didn't want additional checks to negatively affect the algorithm's performance (as is the case when adding a check for self-loops, for example). Given that this is fundamentally a performance improvement, it makes sense IMO to have a benchmark for it somewhere in the repo. As for why this specific benchmark... I don't have a good answer :(. My aim was to "measure performance across a variety of different trees," and that seemed like a general enough goal to make a separate benchmark class; it could be reused e.g. to measure performance improvements for other functions we add to the |
… subpackage (networkx#8174) * api: move `tree.center` to new `tree.distance_measures` subpackage * clean up docstring * Use `NotATree` exception and improve nontree check * add tests for `nx.tree.center` * add benchmarks for `nx(.tree).center` * additional nontree check Co-authored-by: Alejandro Candioti <[email protected]> * fail on self-loops * remove pointless benchmarking imports Co-authored-by: Ross Barnowski <[email protected]> * remove walrus operator Co-authored-by: Dan Schult <[email protected]> --------- Co-authored-by: Alejandro Candioti <[email protected]> Co-authored-by: Ross Barnowski <[email protected]> Co-authored-by: Dan Schult <[email protected]>
Fixes #8098.
This PR moves the
_tree_centerfunction to thetreesubpackage in a newtree.distance_measuresmodule, allowing users to writenx.tree.center(G).Other than changing the location and making this function more visible, there are several other changes:
nx.NetworkXErrortonx.NotATree---since this was previously not exposed, and the call innx.centeronly happens after verifying the graph is a tree, this shouldn't be backward incompatible.This will also make an eventual migration of the tree centroid/barycenter function (after #8089 gets merged) easier.
Cc @amcandio who made the initial improvement, @rossbar who suggested moving to the
treesubpackage.EDIT: Comprehensive diff
Dan asked for a comprehensive overview of what changed. Here's the output of
git diff --no-index old.py new.pywhereold.pyandnew.pyare two files with the old version and new version respectively of the tree center function.