機械学習実装練習中にJupyterLab上でdotファイルをpngファイルに変換して表示したかったので全手順をメモしておきます。
手順
- pipのインストール
- pydotplus、graphvizのインストール
- JupyterLabにバインド
- dotファイルの変換と表示をする関数を作成
- 表示処理の実施
pipのインストール
この辺を参考に。。。
pydotplus、graphbizのインストール
以下のコマンドを実行
$ pip install pydotplus
$ pip install graphbiz
JupyterLabにバインド
grahvizをJupyterLabで利用できるようにバインドする
$ conda install -c conda-forge python-graphviz
dotファイルの変換と表示をする関数を作成
JupyterLab上で以下を記述
import os
import pydotplus
from IPython.display import Image,display_png
PROJECT_ROOT_DIR = "."
def image_path(fig_id):
return os.path.join(PROJECT_ROOT_DIR, "images", fig_id)
def view_dot(fig_id):
file_full_path=PROJECT_ROOT_DIR+"/images"
graph = pydotplus.graphviz.graph_from_dot_file(file_full_path+"/"+fig_id)
graph.write_png(file_full_path+"/"+fig_id+'.png')
display_png(Image(graph.create_png()))
表示処理の実施
JupyterLab上で対象のdotファイルを表示させます。
view_dot("ファイル名.dot")
まとめ
バインド処理がそこそこ時間がかかりますのでコーヒーでもいただきながら待ちましょう。
参考