如何将 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
../_images/b1c2574b892012844e2f2728e2653e513ce1875704f574d86808fcc4310c1960.svg
graph.add_edge(
    dict1_uuid, calc1_uuid,
    link_pair=LinkPair(LinkType.INPUT_CALC, "input1"))
graph.graphviz
../_images/87b1e5f982744541a339b3671a0688fb778f8ff99c9f851b76ac9f55743c47ee.svg
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(
../_images/4626823ddbb2b59748124e168e8e687447637ac7d681a5658b832cd7a17acdcb.svg

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(
../_images/5ab23738c88ac64824ca6342c72fd1fba10c4b514d10df06d466de3859537cea.svg

此外,还可以将函数解析为 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(
../_images/8ff106e25a4cf7ddb18556be5293958fa869108c91196bd622198aa7b9eea4ad.svg

边可以用边标签和链接类型中的一个或两个来标注。

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(
../_images/1637b8b130ff81a696054e9353e76c20aca25fd0d1d828bdf014fbbbfa20cad2.svg

可以使用 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(
../_images/104b018fb82d1e47813bf4a55654998743bf41e3836ace7a12a997fc4b30e2ab.svg

还可以过滤链接类型,只查看 数据逻辑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(
../_images/a77ac1bb1f19e8da19650580b78c2c8d9d91aeb6837e4d144f1a11a0981c11dd.svg
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(
../_images/c5f33298d6b269972662460e53be3549ff74430fae1a86bb8bdd33557e65fe6c.svg

如果希望突出显示特定的 node 类别,则可以使用 highlight_classes 选项只为指定的 node 染色:

graph = Graph(graph_attr={"rankdir": "LR"})
graph.recurse_descendants(
    dict1_uuid,
    highlight_classes=['Dict']
)
graph.graphviz
../_images/6809472f34f47b889c191717825bbb180537fcaaf168933b5e49cce6777f2478.svg