如何将 provenance 可视化#
备注
本教程可作为 Jupyter Notebook 下载和运行:visualising_graphs.ipynb
数据库的 provenance graph 可以通过 via graphviz,使用 python API 和命令行界面进行可视化检查。
参见
verdi graph generate -h
我们首先加载一个包含 provenance graph 的配置文件(在这种情况下,我们加载一个档案作为配置文件)。
from aiida import load_profile
from aiida.common import LinkType
from aiida.orm import LinkPair
from aiida.storage.sqlite_zip import SqliteZipBackend
from aiida.tools.visualization import Graph, pstate_node_styles
profile = load_profile(SqliteZipBackend.create_profile('include/graph1.aiida'))
dict1_uuid = '0ea79a16-501f-408a-8c84-a2704a778e4b'
calc1_uuid = 'b23e692e-4e01-48dd-b515-4c63877d73a4'
Graph
类用于存储node和边的可视化表示,可通过图形遍历方法之一单独或累积添加。graphviz
属性返回一个graphviz.Digraph实例,该实例将在笔记本中自动呈现图形,也可用于将图形保存到文件中。
graph = Graph()
graph.add_node(dict1_uuid)
graph.add_node(calc1_uuid)
graph.graphviz
graph.add_edge(
dict1_uuid, calc1_uuid,
link_pair=LinkPair(LinkType.INPUT_CALC, "input1"))
graph.graphviz
graph.add_incoming(calc1_uuid)
graph.add_outgoing(calc1_uuid)
graph.graphviz
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:598: AiidaDeprecationWarning: `Code.get_execname` method is deprecated, use `get_executable` instead. (this will be removed in v3)
warn_deprecation('`Code.get_execname` method is deprecated, use `get_executable` instead.', version=3)
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:569: AiidaDeprecationWarning: `Code.is_local` method is deprecated, use a `PortableCode` instance and check the type. (this will be removed in v3)
warn_deprecation(
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:520: AiidaDeprecationWarning: `Code.get_remote_exec_path` method is deprecated, use `InstalledCode.filepath_executable` instead. (this will be removed in v3)
warn_deprecation(
如 graphviz 属性表 所述,Graph
也可以使用全局样式属性进行初始化。
graph = Graph(node_id_type="uuid",
global_node_style={"penwidth": 1},
global_edge_style={"color": "blue"},
graph_attr={"rankdir": "LR"})
graph.add_incoming(calc1_uuid)
graph.add_outgoing(calc1_uuid)
graph.graphviz
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:598: AiidaDeprecationWarning: `Code.get_execname` method is deprecated, use `get_executable` instead. (this will be removed in v3)
warn_deprecation('`Code.get_execname` method is deprecated, use `get_executable` instead.', version=3)
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:569: AiidaDeprecationWarning: `Code.is_local` method is deprecated, use a `PortableCode` instance and check the type. (this will be removed in v3)
warn_deprecation(
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:520: AiidaDeprecationWarning: `Code.get_remote_exec_path` method is deprecated, use `InstalledCode.filepath_executable` instead. (this will be removed in v3)
warn_deprecation(
此外,还可以将函数解析为 Graph
初始化器,以精确指定每个 node 的表示方式。例如,pstate_node_styles()
函数会根据 node 的进程状态为其着色。
def link_style(link_pair, **kwargs):
return {"color": "blue"}
graph = Graph(node_style_fn=pstate_node_styles,
link_style_fn=link_style,
graph_attr={"rankdir": "LR"})
graph.add_incoming(calc1_uuid)
graph.add_outgoing(calc1_uuid)
graph.graphviz
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:598: AiidaDeprecationWarning: `Code.get_execname` method is deprecated, use `get_executable` instead. (this will be removed in v3)
warn_deprecation('`Code.get_execname` method is deprecated, use `get_executable` instead.', version=3)
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:569: AiidaDeprecationWarning: `Code.is_local` method is deprecated, use a `PortableCode` instance and check the type. (this will be removed in v3)
warn_deprecation(
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:520: AiidaDeprecationWarning: `Code.get_remote_exec_path` method is deprecated, use `InstalledCode.filepath_executable` instead. (this will be removed in v3)
warn_deprecation(
边可以用边标签和链接类型中的一个或两个来标注。
graph = Graph(graph_attr={"rankdir": "LR"})
graph.add_incoming(calc1_uuid,
annotate_links="both")
graph.add_outgoing(calc1_uuid,
annotate_links="both")
graph.graphviz
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:598: AiidaDeprecationWarning: `Code.get_execname` method is deprecated, use `get_executable` instead. (this will be removed in v3)
warn_deprecation('`Code.get_execname` method is deprecated, use `get_executable` instead.', version=3)
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:569: AiidaDeprecationWarning: `Code.is_local` method is deprecated, use a `PortableCode` instance and check the type. (this will be removed in v3)
warn_deprecation(
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:520: AiidaDeprecationWarning: `Code.get_remote_exec_path` method is deprecated, use `InstalledCode.filepath_executable` instead. (this will be removed in v3)
warn_deprecation(
可以使用 recurse_descendants()
和 recurse_ancestors()
方法来构建完整的 provenance graph。
graph = Graph(graph_attr={"rankdir": "LR"})
graph.recurse_descendants(
dict1_uuid,
origin_style=None,
include_process_inputs=True,
annotate_links="both"
)
graph.graphviz
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:598: AiidaDeprecationWarning: `Code.get_execname` method is deprecated, use `get_executable` instead. (this will be removed in v3)
warn_deprecation('`Code.get_execname` method is deprecated, use `get_executable` instead.', version=3)
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:569: AiidaDeprecationWarning: `Code.is_local` method is deprecated, use a `PortableCode` instance and check the type. (this will be removed in v3)
warn_deprecation(
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:520: AiidaDeprecationWarning: `Code.get_remote_exec_path` method is deprecated, use `InstalledCode.filepath_executable` instead. (this will be removed in v3)
warn_deprecation(
还可以过滤链接类型,只查看 数据
或 逻辑
provenance。
graph = Graph(graph_attr={"rankdir": "LR"})
graph.recurse_descendants(
dict1_uuid,
origin_style=None,
include_process_inputs=True,
annotate_links="both",
link_types=("input_calc", "create")
)
graph.graphviz
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:598: AiidaDeprecationWarning: `Code.get_execname` method is deprecated, use `get_executable` instead. (this will be removed in v3)
warn_deprecation('`Code.get_execname` method is deprecated, use `get_executable` instead.', version=3)
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:569: AiidaDeprecationWarning: `Code.is_local` method is deprecated, use a `PortableCode` instance and check the type. (this will be removed in v3)
warn_deprecation(
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:520: AiidaDeprecationWarning: `Code.get_remote_exec_path` method is deprecated, use `InstalledCode.filepath_executable` instead. (this will be removed in v3)
warn_deprecation(
graph = Graph(graph_attr={"rankdir": "LR"})
graph.recurse_descendants(
dict1_uuid,
origin_style=None,
include_process_inputs=True,
annotate_links="both",
link_types=("input_work", "return")
)
graph.graphviz
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:598: AiidaDeprecationWarning: `Code.get_execname` method is deprecated, use `get_executable` instead. (this will be removed in v3)
warn_deprecation('`Code.get_execname` method is deprecated, use `get_executable` instead.', version=3)
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:569: AiidaDeprecationWarning: `Code.is_local` method is deprecated, use a `PortableCode` instance and check the type. (this will be removed in v3)
warn_deprecation(
/home/docs/checkouts/readthedocs.org/user_builds/aiida-core-zh-cn/envs/latest/lib/python3.11/site-packages/aiida/orm/nodes/data/code/legacy.py:520: AiidaDeprecationWarning: `Code.get_remote_exec_path` method is deprecated, use `InstalledCode.filepath_executable` instead. (this will be removed in v3)
warn_deprecation(
如果希望突出显示特定的 node 类别,则可以使用 highlight_classes
选项只为指定的 node 染色:
graph = Graph(graph_attr={"rankdir": "LR"})
graph.recurse_descendants(
dict1_uuid,
highlight_classes=['Dict']
)
graph.graphviz