はじめに
Pythonライブラリ"DoWhy"を用いてGoogle Colab上にDAGを描画する際に出るエラーメッセージとその対処法についてのメモ書き。
WARNINGおよびERRORの発生
DoWhyだけをインストールして、DAGを描画しようとするとWARNINGが発生する。
# 必要なライブラリのインストールおよびインポート
!pip install dowhy
from dowhy import CausalModel
import dowhy.datasets
from IPython.display import Image, display
# データの準備
data = dowhy.datasets.linear_dataset(beta=10,
num_common_causes=5,
num_instruments=2,
num_effect_modifiers=1,
num_samples=5000,
treatment_is_binary=1,
stddev_treatment_noise=10,
num_discrete_common_causes=1)
df = data["df"]
# グラフを描画
model = CausalModel(
data = df,
treatment = data["treatment_name"],
outcome = data["outcome_name"],
graph = data["gml_graph"]
)
model.view_model()
display(Image(filename="causal_model.png"))
(出力結果)
WARNING:dowhy.causal_graph:Warning: Pygraphviz cannot be loaded. Check that graphviz and pygraphviz are installed.
Pygraphvizがロードできないとのこと。pipを利用してPygraphvizをインストールしてみる。
!pip install pygraphviz
(出力結果)
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Collecting pygraphviz
Using cached pygraphviz-1.10.zip (120 kB)
Preparing metadata (setup.py) ... done
Building wheels for collected packages: pygraphviz
error: subprocess-exited-with-error
× python setup.py bdist_wheel did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
Building wheel for pygraphviz (setup.py) ... error
ERROR: Failed building wheel for pygraphviz
Running setup.py clean for pygraphviz
Failed to build pygraphviz
Installing collected packages: pygraphviz
error: subprocess-exited-with-error
× Running setup.py install for pygraphviz did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
Running setup.py install for pygraphviz ... error
error: legacy-install-failure
× Encountered error while trying to install package.
╰─> pygraphviz
note: This is an issue with the package mentioned above, not pip.
hint: See above for output from the failure.
エラーが発生。
解決策
aptにてlibgraphviz-devをインストールしてから、Pygraphvizをインストールするとうまくいく。
!apt install libgraphviz-dev
!pip install pygraphviz
(出力結果)
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
libnvidia-common-510
Use 'apt autoremove' to remove it.
The following additional packages will be installed:
libgail-common libgail18 libgtk2.0-0 libgtk2.0-bin libgtk2.0-common
libgvc6-plugins-gtk libxdot4
Suggested packages:
gvfs
The following NEW packages will be installed:
libgail-common libgail18 libgraphviz-dev libgtk2.0-0 libgtk2.0-bin
libgtk2.0-common libgvc6-plugins-gtk libxdot4
0 upgraded, 8 newly installed, 0 to remove and 21 not upgraded.
Need to get 2,148 kB of archives.
After this operation, 7,427 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu focal/main amd64 libgtk2.0-common all 2.24.32-4ubuntu4 [126 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal/main amd64 libgtk2.0-0 amd64 2.24.32-4ubuntu4 [1,791 kB]
Get:3 http://archive.ubuntu.com/ubuntu focal/main amd64 libgail18 amd64 2.24.32-4ubuntu4 [14.7 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal/main amd64 libgail-common amd64 2.24.32-4ubuntu4 [116 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal/universe amd64 libxdot4 amd64 2.42.2-3build2 [15.4 kB]
Get:6 http://archive.ubuntu.com/ubuntu focal/universe amd64 libgvc6-plugins-gtk amd64 2.42.2-3build2 [20.6 kB]
Get:7 http://archive.ubuntu.com/ubuntu focal/universe amd64 libgraphviz-dev amd64 2.42.2-3build2 [57.2 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal/main amd64 libgtk2.0-bin amd64 2.24.32-4ubuntu4 [7,728 B]
Fetched 2,148 kB in 1s (2,299 kB/s)
Selecting previously unselected package libgtk2.0-common.
(Reading database ... 128097 files and directories currently installed.)
Preparing to unpack .../0-libgtk2.0-common_2.24.32-4ubuntu4_all.deb ...
Unpacking libgtk2.0-common (2.24.32-4ubuntu4) ...
Selecting previously unselected package libgtk2.0-0:amd64.
Preparing to unpack .../1-libgtk2.0-0_2.24.32-4ubuntu4_amd64.deb ...
Unpacking libgtk2.0-0:amd64 (2.24.32-4ubuntu4) ...
Selecting previously unselected package libgail18:amd64.
Preparing to unpack .../2-libgail18_2.24.32-4ubuntu4_amd64.deb ...
Unpacking libgail18:amd64 (2.24.32-4ubuntu4) ...
Selecting previously unselected package libgail-common:amd64.
Preparing to unpack .../3-libgail-common_2.24.32-4ubuntu4_amd64.deb ...
Unpacking libgail-common:amd64 (2.24.32-4ubuntu4) ...
Selecting previously unselected package libxdot4:amd64.
Preparing to unpack .../4-libxdot4_2.42.2-3build2_amd64.deb ...
Unpacking libxdot4:amd64 (2.42.2-3build2) ...
Selecting previously unselected package libgvc6-plugins-gtk.
Preparing to unpack .../5-libgvc6-plugins-gtk_2.42.2-3build2_amd64.deb ...
Unpacking libgvc6-plugins-gtk (2.42.2-3build2) ...
Selecting previously unselected package libgraphviz-dev:amd64.
Preparing to unpack .../6-libgraphviz-dev_2.42.2-3build2_amd64.deb ...
Unpacking libgraphviz-dev:amd64 (2.42.2-3build2) ...
Selecting previously unselected package libgtk2.0-bin.
Preparing to unpack .../7-libgtk2.0-bin_2.24.32-4ubuntu4_amd64.deb ...
Unpacking libgtk2.0-bin (2.24.32-4ubuntu4) ...
Setting up libxdot4:amd64 (2.42.2-3build2) ...
Setting up libgtk2.0-common (2.24.32-4ubuntu4) ...
Setting up libgtk2.0-0:amd64 (2.24.32-4ubuntu4) ...
Setting up libgvc6-plugins-gtk (2.42.2-3build2) ...
Setting up libgail18:amd64 (2.24.32-4ubuntu4) ...
Setting up libgtk2.0-bin (2.24.32-4ubuntu4) ...
Setting up libgail-common:amd64 (2.24.32-4ubuntu4) ...
Setting up libgraphviz-dev:amd64 (2.42.2-3build2) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.9) ...
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Collecting pygraphviz
Using cached pygraphviz-1.10.zip (120 kB)
Preparing metadata (setup.py) ... done
Building wheels for collected packages: pygraphviz
Building wheel for pygraphviz (setup.py) ... done
Created wheel for pygraphviz: filename=pygraphviz-1.10-cp38-cp38-linux_x86_64.whl size=184566 sha256=718abe52f52ab9edab73d4b71a54be757f089595af28c11170171f62e6c6dc17
Stored in directory: /root/.cache/pip/wheels/ac/bd/46/118c2a336c6fd8d47026854302f8fad63bb7e431e52008ea06
Successfully built pygraphviz
Installing collected packages: pygraphviz
Successfully installed pygraphviz-1.10
再度、DAGを描画してみると
# データの準備
data = dowhy.datasets.linear_dataset(beta=10,
num_common_causes=5,
num_instruments=2,
num_effect_modifiers=1,
num_samples=5000,
treatment_is_binary=1,
stddev_treatment_noise=10,
num_discrete_common_causes=1)
df = data["df"]
# グラフを描画
model = CausalModel(
data = df,
treatment = data["treatment_name"],
outcome = data["outcome_name"],
graph = data["gml_graph"]
)
model.view_model()
display(Image(filename="causal_model.png"))
無事、DAGが出力される。
おわりに
最後まで読んでいただきありがとうございました。
zennにて「Python×データ分析」をメインテーマに記事を執筆しているので、ご一読いただけますと幸いです。
また、過去にLTや勉強会で発表した資料が下記リンクにてまとめてありますので、こちらもぜひご一読くださいませ。