Source code for aiidalab_widgets_base.nodes

"""Widgets to work with AiiDA nodes."""
import functools
from typing import Tuple

import ipywidgets as ipw
import traitlets
from aiida.cmdline.utils.ascii_vis import calc_info
from aiida.common import AttributeDict
from aiida.engine import ProcessState
from aiida.orm import (
from import _AiidaLabApp
from IPython.display import clear_output, display
from ipytree import Node as TreeNode
from ipytree import Tree

        "Geometry Optimization",
        "Geometry Optimization - typically this is the first step needed to find optimal positions of atoms in the unit cell.",
        "Geometry analysis",
        "Geometry analysis - calculate parameters describing the geometry of a material.",
        "Isotherm - compute adsorption isotherm of a small molecules in the selected material.",

        "name": "quantum-espresso",
        "calculation_type": "geo_opt",
        "notebook": "qe.ipynb",
        "parameter_name": "structure_uuid",
        "description": "Optimize atomic positions and/or unit cell employing Quantum ESPRESSO. Quantum ESPRESSO is preferable for small structures with no cell dimensions larger than 15 Å. Additionally, you can choose to compute electronic properties of the material such as band structure and density of states.",
        "name": "aiidalab-lsmo",
        "calculation_type": "geo_opt",
        "notebook": "multistage_geo_opt_ddec.ipynb",
        "parameter_name": "structure_uuid",
        "description": "Optimize atomic positions and unit cell with CP2K. CP2K is very efficient for large (any cell dimension is larger than 15 Å) and/or porous structures. Additionally, you can choose to assign point charges to the atoms using DDEC.",
        "name": "aiidalab-lsmo",
        "calculation_type": "geo_analysis",
        "notebook": "pore_analysis.ipynb",
        "parameter_name": "structure_uuid",
        "description": "Calculate descriptors for the pore geometry using the Zeo++.",
        "name": "aiidalab-lsmo",
        "calculation_type": "isotherm",
        "notebook": "compute_isotherm.ipynb",
        "parameter_name": "structure_uuid",
        "description": "Compute adsorption isotherm of the selected material using the RASPA code. Typically, one needs to optimize geometry and compute the charges of material before computing the isotherm. However, if this is already done, you can go for it.",

[docs]class AiidaNodeTreeNode(TreeNode):
[docs] def __init__(self, pk, name, **kwargs): = pk self.nodes_registry = {} super().__init__(name=name, **kwargs)
@traitlets.default("opened") def _default_openend(self): return False
[docs]class AiidaProcessNodeTreeNode(AiidaNodeTreeNode):
[docs] def __init__(self, pk, **kwargs): self.outputs_node = AiidaOutputsTreeNode(name="outputs", parent_pk=pk) super().__init__(pk=pk, **kwargs)
[docs]class WorkChainProcessTreeNode(AiidaProcessNodeTreeNode): icon = traitlets.Unicode("chain").tag(sync=True)
[docs]class CalcJobTreeNode(AiidaProcessNodeTreeNode): icon = traitlets.Unicode("gears").tag(sync=True)
[docs]class CalcFunctionTreeNode(AiidaProcessNodeTreeNode): icon = traitlets.Unicode("gear").tag(sync=True)
[docs]class AiidaOutputsTreeNode(TreeNode): icon = traitlets.Unicode("folder").tag(sync=True) disabled = traitlets.Bool(True).tag(sync=True)
[docs] def __init__(self, name, parent_pk, namespaces: Tuple[str, ...] = (), **kwargs): self.parent_pk = parent_pk self.nodes_registry = {} self.namespaces = namespaces super().__init__(name=name, **kwargs)
[docs]class UnknownTypeTreeNode(AiidaNodeTreeNode): icon = traitlets.Unicode("file").tag(sync=True)
[docs]class NodesTreeWidget(ipw.Output): """A tree widget for the structured representation of a nodes graph.""" nodes = traitlets.Tuple().tag(trait=traitlets.Instance(Node)) selected_nodes = traitlets.Tuple(read_only=True).tag(trait=traitlets.Instance(Node)) PROCESS_STATE_STYLE = { ProcessState.EXCEPTED: "danger", ProcessState.FINISHED: "success", ProcessState.KILLED: "warning", ProcessState.RUNNING: "info", ProcessState.WAITING: "info", } PROCESS_STATE_STYLE_DEFAULT = "default" NODE_TYPE = { WorkChainNode: WorkChainProcessTreeNode, CalcFunctionNode: CalcFunctionTreeNode, CalcJobNode: CalcJobTreeNode, }
[docs] def __init__(self, **kwargs): self._tree = Tree() self._tree.observe(self._observe_tree_selected_nodes, ["selected_nodes"]) super().__init__(**kwargs)
[docs] def _refresh_output(self): # There appears to be a bug in the ipytree implementation that sometimes # causes the output to not be properly cleared. We therefore refresh the # displayed tree upon change of the process trait. with self: clear_output() display(self._tree)
[docs] def _observe_tree_selected_nodes(self, change): return self.set_trait( "selected_nodes", tuple( load_node( for node in change["new"] if hasattr(node, "pk") ), )
[docs] def _convert_to_tree_nodes(self, old_nodes, new_nodes): "Convert nodes into tree nodes while re-using already converted nodes." old_nodes_ = { node for node in old_nodes} assert len(old_nodes_) == len(old_nodes) # no duplicated nodes for node in new_nodes: if in old_nodes_: yield old_nodes_[] else: yield self._to_tree_node(node, opened=True)
@traitlets.observe("nodes") def _observe_nodes(self, change): self._tree.nodes = sorted( self._convert_to_tree_nodes( old_nodes=self._tree.nodes, new_nodes=change["new"] ), key=lambda node:, ) self.update() self._refresh_output()
[docs] @classmethod def _to_tree_node(cls, node, name=None, **kwargs): """Convert an AiiDA node to a tree node.""" if name is None: if isinstance(node, ProcessNode): name = calc_info(node) else: name = str(node) return cls.NODE_TYPE.get(type(node), UnknownTypeTreeNode)(, name=name, **kwargs )
[docs] @classmethod def _find_called(cls, root): assert isinstance(root, AiidaProcessNodeTreeNode) process_node = load_node( called = process_node.called called.sort(key=lambda p: p.ctime) for node in called: if not in root.nodes_registry: try: name = calc_info(node) except AttributeError: name = str(node) root.nodes_registry[] = cls._to_tree_node(node, name=name) yield root.nodes_registry[]
[docs] @classmethod def _find_outputs(cls, root): """ A generator for all (including nested) output nodes. Generates an AiidaOutputsTreeNode when encountering a namespace, keeping track of the full namespace path to make it accessible via the root node in form of a breadth-first search. """ process_node = load_node(root.parent_pk) # Gather outputs from node and its namespaces: outputs = functools.reduce( lambda attr_dict, namespace: attr_dict[namespace], root.namespaces or [], process_node.outputs, ) # Convert aiida.orm.LinkManager or AttributDict (if namespace presented) to dict output_nodes = {key: outputs[key] for key in outputs} for key in sorted( output_nodes.keys(), key=lambda k: getattr(outputs[k], "pk", -1) ): node = output_nodes[key] if isinstance(node, AttributeDict): # for namespace tree node attach label and continue recursively yield AiidaOutputsTreeNode( name=key, parent_pk=root.parent_pk, namespaces=(*root.namespaces, key), # attach nested namespace name ) else: if not in root.nodes_registry: root.nodes_registry[] = cls._to_tree_node( node, name=f"{key}<{}>" ) yield root.nodes_registry[]
[docs] @classmethod def _find_children(cls, root): """Find all children of the provided AiiDA node.""" if isinstance(root, AiidaProcessNodeTreeNode): yield root.outputs_node yield from cls._find_called(root) elif isinstance(root, AiidaOutputsTreeNode): yield from cls._find_outputs(root)
[docs] @classmethod def _build_tree(cls, root): """Recursively build a tree nodes graph for a given tree node.""" root.nodes = [cls._build_tree(child) for child in cls._find_children(root)] return root
[docs] @classmethod def _walk_tree(cls, root): """Breadth-first search of the node tree.""" yield root for node in root.nodes: yield from cls._walk_tree(node)
[docs] def _update_tree_node(self, tree_node): if isinstance(tree_node, AiidaProcessNodeTreeNode): process_node = load_node( = calc_info(process_node) # Override the process state in case that the process node has failed: # (This could be refactored with structural pattern matching with py>=3.10.) process_state = ( ProcessState.EXCEPTED if process_node.is_failed else process_node.process_state ) tree_node.icon_style = self.PROCESS_STATE_STYLE.get( process_state, self.PROCESS_STATE_STYLE_DEFAULT )
[docs] def update(self, _=None): """Refresh nodes based on the latest state of the root process and its children.""" for root_node in self._tree.nodes: self._build_tree(root_node) for tree_node in self._walk_tree(root_node): self._update_tree_node(tree_node)
[docs] def find_node(self, pk): for node in self._walk_tree(self._tree): if getattr(node, "pk", None) == pk: return node raise KeyError(pk)
[docs]class _AppIcon:
[docs] def __init__(self, app, path_to_root, node): name = app["name"] app_object = _AiidaLabApp.from_id(name) self.logo = app_object.metadata["logo"] if app_object.is_installed(): = f"{path_to_root}{app['name']}/{app['notebook']}?{app['parameter_name']}={node.uuid}" else: = f"{path_to_root}home/single_app.ipynb?app={app['name']}" self.description = app["description"]
[docs] def to_html_string(self): return f""" <table style="border-collapse:separate;border-spacing:15px;"> <tr> <td style="width:200px"> <a href="{}" target="_blank"> <img src="{self.logo}"> </a></td> <td style="width:800px"> <p style="font-size:16px;">{self.description} </p></td> </tr> </table> """
[docs]class OpenAiidaNodeInAppWidget(ipw.VBox): node = traitlets.Instance(Node, allow_none=True)
[docs] def __init__(self, path_to_root="../", **kwargs): self.path_to_root = path_to_root = ipw.Tab(style={"description_width": "initial"}) self.tab_selection = ipw.RadioButtons( options=[], description="", disabled=False, style={"description_width": "initial"}, layout=ipw.Layout(width="auto"), ) spacer = ipw.HTML("""<p style="margin-bottom:1cm;"></p>""") super().__init__(children=[self.tab_selection, spacer,], **kwargs)
@traitlets.observe("node") def _observe_node(self, change): if change["new"]: = [ self.get_tab_content(apps_type=calctype[0]) for calctype in CALCULATION_TYPES ] for i, calctype in enumerate(CALCULATION_TYPES):, calctype[1]) self.tab_selection.options = [ (calctype[1], i) for i, calctype in enumerate(CALCULATION_TYPES) ], "selected_index"), (self.tab_selection, "value")) else: = []
[docs] def get_tab_content(self, apps_type): tab_content = ipw.HTML("") for app in SELECTED_APPS: if app["calculation_type"] != apps_type: continue tab_content.value += _AppIcon( app=app, path_to_root=self.path_to_root, node=self.node, ).to_html_string() return tab_content