Repository navigation
[ENH, BUG]: added colliders and v_structures and deprecated compute_v_structures in dag.py - #7398
Conversation
compute_colliders and fixed compute_v_structurescompute_colliders and fixed compute_v_structures
|
Needed to sort parents because doctests were failing. Doctest logs |
dschult
left a comment
There was a problem hiding this comment.
If we're going to change this much, maybe we should consider changing the name so it doesn't include compute. We don't usually include words like that in the function names. What do you think about v_structures(G) and colliders(G)?
I'd love to have a better name that v_structures :)
dschult
left a comment
There was a problem hiding this comment.
I wonder if these new names describe what is going on well.
Should we use causal_colliders to refer to the literature? Or is colliders better/shorter/faster (searching for "collider" doesn't lead to results about causal graphs colliders. But "collider graph theory" does...
dschult
left a comment
There was a problem hiding this comment.
I took the liberty of pushing up some changes that remove both sorting and mins. It works locally so maybe will work on CI.
Removing both the sorting and the min of parents seems to work. The triplets are formed in a deterministic way -- so its the same each time it is run. So I guess this unsorted way of testing doesn't allow other correct solutions, but it does raise if the code no longer works the same way -- and it doesn't encourage users to include lots of sorting and min function calls in their own calls.
In doing this, I noticed that the examples are no longer DAGs. (which led to more investigation)
The docs say these functions should have DAGs as inputs. But it doesn't seem to be checked anywhere. That means we are returning colliders even for directed digraphs with cycles. For better or for worse, there are papers on possibly cyclic causal directed graphs.
https://www.cmu.edu/dietrich/philosophy/docs/tech-reports/68_Richardson.pdf
https://www.sciencedirect.com/science/article/pii/S0888613X19301410
https://arxiv.org/pdf/1309.6836.pdf
And they do talk about colliders in these situations.
So I think we should leave it possible to use these functions on cyclic graphs. But we should add a warning in the doc_string to check that the graph is acyclic if that is what they want.
I have changed the examples to be DAGs and added a check to the example codes to show that it is a good idea to check for a DAG if you want to restrict to that formulation of a collider. And I've added references and notes to the doc_string.
Please change any of this you want
|
Thanks for adding the points about directed cyclic graphs. And about the sort and min-max on parents, I think we should keep them so that the tests pass for all OS, because right now the dispatch test is failing. Also ref. this comment, here also the doctests in dispatch were failing and (ubuntu, 3.11)'s doctests were also failing in CI(from past commits in this PR) bcoz we didn't use sorted and min-max. LMK what u think. Thx! |
rossbar
left a comment
There was a problem hiding this comment.
Note that removing compute_v_structures in this way will be a breaking change for users (e.g. the user who reported #7385).
IMO the easiest way to deal with this would be to reintroduce a compute_v_structures function that essentially just raises a deprecation warning then calls the new colliders.
It may also be worth discussing whether these functions should indeed be exported to the main namespace. From a readthrough, I understand that they are intended to work on any acyclic graphs (not only DAGs); however, I don't know how grokkable they are in a general context. IMO nx.dag.v_structures is a lot clearer than nx.v_structures...
+1 on this.
I'm not sure by "these functions" if you meant all the functions in
|
|
With #7432 merged, this should be rebased-on/merged-with main and will hopefully pass the dispatch tests now that nx-loopback preserves edge order when converting during the tests. I don't think there will be any conflicts on the merge, but if it is messy let me know and I'll take a stab at it. ( |
dc5f336 to
073267b
Compare
|
What I think this discussion is leading toward is the following:
|
compute_colliders and fixed compute_v_structurescolliders and v_structures and deprecated compute_v_structures in dag.py
|
The newly added algos >>> import networkx as nx
>>> G = nx.DiGraph([(1, 2), (0, 4), (3, 1), (2, 4), (0, 5), (4, 5), (1, 5)])
>>> nx.v_structures(G)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'networkx' has no attribute 'v_structures'
>>> nx.dag.v_structures(G)
<generator object v_structures at 0x1079c9d50>
>>> nx.compute_v_structures(G)
<stdin>:1: DeprecationWarning:
`compute_v_structures` actually yields colliders. It will be
removed in version 3.6. Use `nx.dag.v_structures` instead.
<generator object colliders at 0x10799b140>
>>> nx.dag.colliders(G)
<generator object colliders at 0x10799b740>
>>> nx.colliders(G)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'networkx' has no attribute 'colliders'Here, I can see the warning locally on my laptop with import networkx as nx
G = nx.Graph([(1,2),(3,4)])
cn = nx.core_number(G)
G = nx.MultiGraph(G)
print(nx.k_core(G, core_number=cn))
# Output: MultiGraph with 4 nodes and 2 edgesbut I'm able to see it locally: >>> print(nx.k_core(G, core_number=cn))
<stdin>:1: DeprecationWarning:
`k_core` will not accept `MultiGraph` objects in version 3.5.
Convert it to an undirected graph instead, using::
G = nx.Graph(G)
MultiGraph with 4 nodes and 2 edges |
dschult
left a comment
There was a problem hiding this comment.
This looks good to me. I made comments below about using the pytest.raises context manager approach to testing exceptions. But those are minor changes.
I approve this PR. Thanks @Schefflera-Arboricola !
dschult
left a comment
There was a problem hiding this comment.
This PR is ready to merge as far as I can tell.
Thanks @Schefflera-Arboricola for all the discussion comments. :)
I approve this PR.
Co-authored-by: Dan Schult <[email protected]>
502fd67 to
7db3810
Compare
rossbar
left a comment
There was a problem hiding this comment.
Excellent work @Schefflera-Arboricola , thanks for scoping out and solving this one! I took the liberty of pushing up a few minor wording/rst formatting tweaks (and rebased on main to pull in your fix for the docs CI) but LGTM!
closes #7385
previously `compute_v_structures` was computing colliders. Now, I've renamed `compute_v_structures` to `compute_colliders`(and updated its implementation too) and added the correct implementation for `compute_v_structures`. Also, updated and added the relevant docs and tests.Thank you :)