matheusfacure / causal-inference-in-python-code

Code for the Book Causal Inference in Python
MIT License
262 stars 116 forks source link

Chapter 3 networkx code note working #5

Open hanowell opened 1 month ago

hanowell commented 1 month ago

I have installed networkx and imported it as nx in command [5]. But command [6] returns the following error:

{
    "name": "NodeNotFound",
    "message": "One of x, y, or z is not a node or a set of nodes in G",
    "stack": "---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~\\.pyenv\\pyenv-win\\versions\\3.12.0\\Lib\\site-packages\
etworkx\\algorithms\\d_separation.py:283, in is_d_separator(G, x, y, z)
    281 z = {z} if z in G else z
--> 283 intersection = x & y or x & z or y & z
    284 if intersection:

TypeError: unsupported operand type(s) for &: 'set' and 'dict'

During handling of the above exception, another exception occurred:

NodeNotFound                              Traceback (most recent call last)
Cell In[7], line 2
      1 print(\"Are D and C dependent?\")
----> 2 print(not(nx.d_separated(model, {\"D\"}, {\"C\"}, {})))
      4 print(\"Are D and C dependent given A?\")
      5 print(not(nx.d_separated(model, {\"D\"}, {\"C\"}, {\"A\"})))

File ~\\.pyenv\\pyenv-win\\versions\\3.12.0\\Lib\\site-packages\
etworkx\\algorithms\\d_separation.py:700, in d_separated(G, x, y, z)
    692 import warnings
    694 warnings.warn(
    695     \"d_separated is deprecated and will be removed in NetworkX v3.5.\"
    696     \"Please use `is_d_separator(G, x, y, z)`.\",
    697     category=DeprecationWarning,
    698     stacklevel=2,
    699 )
--> 700 return nx.is_d_separator(G, x, y, z)

File ~\\.pyenv\\pyenv-win\\versions\\3.12.0\\Lib\\site-packages\
etworkx\\utils\\decorators.py:789, in argmap.__call__.<locals>.func(_argmap__wrapper, *args, **kwargs)
    788 def func(*args, __wrapper=None, **kwargs):
--> 789     return argmap._lazy_compile(__wrapper)(*args, **kwargs)

File <class 'networkx.utils.decorators.argmap'> compilation 17:4, in argmap_is_d_separator_13(G, x, y, z, backend, **backend_kwargs)
      2 import collections
      3 import gzip
----> 4 import inspect
      5 import itertools
      6 import re

File ~\\.pyenv\\pyenv-win\\versions\\3.12.0\\Lib\\site-packages\
etworkx\\utils\\backends.py:633, in _dispatchable.__call__(self, backend, *args, **kwargs)
    628 \"\"\"Returns the result of the original function, or the backend function if
    629 the backend is specified and that backend implements `func`.\"\"\"
    631 if not backends:
    632     # Fast path if no backends are installed
--> 633     return self.orig_func(*args, **kwargs)
    635 # Use `backend_name` in this function instead of `backend`
    636 backend_name = backend

File ~\\.pyenv\\pyenv-win\\versions\\3.12.0\\Lib\\site-packages\
etworkx\\algorithms\\d_separation.py:293, in is_d_separator(G, x, y, z)
    291         raise nx.NodeNotFound(f\"The node(s) {set_v - G.nodes} are not found in G\")
    292 except TypeError:
--> 293     raise nx.NodeNotFound(\"One of x, y, or z is not a node or a set of nodes in G\")
    295 if not nx.is_directed_acyclic_graph(G):
    296     raise nx.NetworkXError(\"graph should be directed acyclic\")

NodeNotFound: One of x, y, or z is not a node or a set of nodes in G"
}
hanowell commented 1 month ago

This seems to be fixed by changing the second line to:

print(not(nx.d_separated(model, {"D"}, {"C"}, {*{}})))

Indeed, perhaps all instances of {} in the d_separator function calls should be changed to {*{}}