py-why / dowhy

DoWhy is a Python library for causal inference that supports explicit modeling and testing of causal assumptions. DoWhy is based on a unified language for causal inference, combining causal graphical models and potential outcomes frameworks.

Home Page:https://www.pywhy.org/dowhy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CausalModel's `view_model()` fails when dot is not installed

kbattocchi opened this issue · comments

Describe the bug

In dowhy 0.10.1, calling view_model() on a CausalModel throws an exception when dot isn't available. In dowhy 0.9 this worked fine even when dot was not available.

Steps to reproduce the behavior

Install dowhy 0.10.1 on a computer without graphviz/dot installed. Then run:

import dowhy.datasets
from dowhy import CausalModel

# generate some data
data = dowhy.datasets.linear_dataset(beta=10, num_common_causes=5, num_samples=5000)

model=CausalModel(data = data["df"], treatment=data["treatment_name"], outcome=data["outcome_name"], graph=data["gml_graph"])

model.view_model()

Actual behavior

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\dowhy\utils\plotting.py:48, in plot(causal_graph, layout_prog, causal_strengths, colors, filename, display_plot, figure_size, **kwargs)
     47 try:
---> 48     from dowhy.utils.graphviz_plotting import plot_causal_graph_graphviz
     50     try:

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\dowhy\utils\graphviz_plotting.py:7
      6 import numpy as np
----> 7 import pygraphviz
     10 def plot_causal_graph_graphviz(
     11     causal_graph: nx.Graph,
     12     layout_prog: Optional[str] = None,
   (...)
     18     figure_size: Optional[Tuple[int, int]] = None,
     19 ) -> None:

ModuleNotFoundError: No module named 'pygraphviz'

During handling of the above exception, another exception occurred:

FileNotFoundError                         Traceback (most recent call last)
File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\pydot.py:1923, in Dot.create(self, prog, format, encoding)
   1922 try:
-> 1923     stdout_data, stderr_data, process = call_graphviz(
...
-> 1933     raise OSError(*args)
   1934 else:
   1935     raise

FileNotFoundError: [WinError 2] "dot" not found in path.

Expected behavior
The model is displayed even though dot is not available.

Version information:

  • DoWhy version 0.10.1

Additional context

One workaround is to explicitly set layout=None when calling view_model. However, it would be nice if the view_model method worked by default, as it used to - one possible fix might be to just set None as the default for layout, instead of the current value of "dot".

Fixed by #1042