Repository navigation
Add check for empty graphs in flow_hierarchy - #7393
Conversation
Schefflera-Arboricola
left a comment
There was a problem hiding this comment.
Thanks @mohamedrezk122 !
I've left a few comments/suggestions below.
And I was curious about what we do with the self-loops here(we consider them). But, I took a quick glance at the paper in the references and I think those are not allowed(but, I might be wrong; I haven't gone through it very deeply). From pdf's page 6("RESULTS AND DISCUSSIONS" section)
No multiple uni-directed links between a chosen pair and
no self-links are allowed.
| http://web.mit.edu/~cmagee/www/documents/28-DetectingEvolvingPatterns_FlowHierarchy.pdf | ||
| """ | ||
| # corner case: G has no edges | ||
| if G.size() == 0: |
There was a problem hiding this comment.
| if G.size() == 0: | |
| if nx.is_empty(G): |
here, using G.is_empty might be more efficient because we won't be iterating over all the edges.
There was a problem hiding this comment.
Nice -- I bet this idiom could be used in other places in the codebase too.
|
Thanks @Schefflera-Arboricola for the review, I have pushed the changes. |
rossbar
left a comment
There was a problem hiding this comment.
Thanks @mohamedrezk122 - I went ahead and made some suggestions to reinforce @Schefflera-Arboricola 's comments from review (which were spot on, thanks!)
flow_hierarchyflow_hierarchy
rossbar
left a comment
There was a problem hiding this comment.
LGTM, thanks @mohamedrezk122 and @Schefflera-Arboricola & @dschult for the reviews!
Schefflera-Arboricola
left a comment
There was a problem hiding this comment.
Thanks @mohamedrezk122 !
* Add match= to pytest.raises to ensure right code path is being hit * Parametrize on number of nodes for the empty graph.
| G = nx.DiGraph() | ||
| G.add_node(1) | ||
| pytest.raises(nx.NetworkXError, nx.flow_hierarchy, G) | ||
| @pytest.mark.parametrize("n", (0, 1, 3)) |
There was a problem hiding this comment.
@rossbar why did you decide to test on 3 empty graphs here?
We just check if the graph is edgeless or non-edgeless. so then why do we need different cases of edgeless graphs(i.e. 0 nodes 0 edges, 1 node 0 edges, n nodes 0 edges) when they are all the same for this algorithm? "Same" in the sense, that they all lie in the "empty graph" corner-case space.
There was a problem hiding this comment.
Although these graphs have the same property in that they contain no edges, there's still enough distinction between these cases that it doesn't hurt to test them! In other words, I think that each one of these cases is "special" enough to be worth testing. For example, there are plenty of places in NX where there are checks on the number of nodes; with the numbers zero (null graph) and one (a singleton node) being special.
In the future, someone might decide to completely re-implement flow_hierarchy - during their rewriting, they may replace the is_empty check with something like G.number_of_nodes() < 1 (or <=). In those cases, the test suite would've passed for some, but missed the case where there are multiple nodes with no edges. One of the primary uses of the test suite is to ensure that changes to code don't unintentionally modify behavior - so tests that cover potential behavior changes are valuable!
All in all the above hypothetical is not a very likely scenario, but IMO it's a principle of good test design to think about the semantics you are trying to enforce, and make sure you probe them as completely as possible. Of course, there's always a tradeoff with test runtime, but in this case adding an extra check is extremely cheap so that's not a concern.
* Add check for null graphs in flow_hierarchy Co-authored-by: Ross Barnowski <[email protected]>
The following snippet produces a division by zero error due to the lack of null graph catching condition, I just added it and updated the tests :)