|
| 1 | +"""Module for components relating to AiiDA database management.""" |
| 2 | + |
| 3 | +import datetime |
| 4 | + |
| 5 | +import ipywidgets as ipw |
| 6 | +import traitlets as tl |
| 7 | +from aiida.orm import ( |
| 8 | + CalcFunctionNode, |
| 9 | + CalcJobNode, |
| 10 | + Data, |
| 11 | + Node, |
| 12 | + QueryBuilder, |
| 13 | + WorkChainNode, |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +class AiiDADatabaseWidget(ipw.VBox, tl.HasTraits): |
| 18 | + """Widget for AiiDA database querying.""" |
| 19 | + |
| 20 | + data_object = tl.Instance(Data, allow_none=True) |
| 21 | + |
| 22 | + def __init__(self, title: str = "", query: list = None): |
| 23 | + if query is None: |
| 24 | + query = [] |
| 25 | + self.title = title |
| 26 | + self.query_type = tuple(query) |
| 27 | + |
| 28 | + qbuilder = QueryBuilder().append((CalcJobNode, WorkChainNode), project="label") |
| 29 | + |
| 30 | + self.drop_down = ipw.Dropdown( |
| 31 | + options=sorted({"All"}.union({i[0] for i in qbuilder.iterall() if i[0]})), |
| 32 | + value="All", |
| 33 | + description="Process Label", |
| 34 | + disabled=True, |
| 35 | + style={"description_width": "120px"}, |
| 36 | + layout={"width": "50%"}, |
| 37 | + ) |
| 38 | + self.drop_down.observe(self.search, names="value") |
| 39 | + |
| 40 | + # Disable process labels selection if we are not looking for the calculated |
| 41 | + # structures. |
| 42 | + def disable_drop_down(change): |
| 43 | + self.drop_down.disabled = not change["new"] == "calculated" |
| 44 | + |
| 45 | + # Select structures kind. |
| 46 | + self.mode = ipw.RadioButtons( |
| 47 | + options=["all", "uploaded", "calculated"], layout={"width": "25%"} |
| 48 | + ) |
| 49 | + self.mode.observe(self.search, names="value") |
| 50 | + self.mode.observe(disable_drop_down, names="value") |
| 51 | + |
| 52 | + # Date range. |
| 53 | + # Note: there is Date picker widget, but it currently does not work in Safari: |
| 54 | + # https://ipywidgets.readthedocs.io/en/latest/examples/Widget%20List.html#Date-picker |
| 55 | + date_text = ipw.HTML(value="<p>Select the date range:</p>") |
| 56 | + self.start_date_widget = ipw.Text( |
| 57 | + value="", description="From: ", style={"description_width": "120px"} |
| 58 | + ) |
| 59 | + self.end_date_widget = ipw.Text(value="", description="To: ") |
| 60 | + |
| 61 | + # Search button. |
| 62 | + btn_search = ipw.Button( |
| 63 | + description="Search", |
| 64 | + button_style="info", |
| 65 | + layout={"width": "initial", "margin": "2px 0 0 2em"}, |
| 66 | + ) |
| 67 | + btn_search.on_click(self.search) |
| 68 | + |
| 69 | + age_selection = ipw.VBox( |
| 70 | + [ |
| 71 | + date_text, |
| 72 | + ipw.HBox([self.start_date_widget, self.end_date_widget, btn_search]), |
| 73 | + ], |
| 74 | + layout={"border": "1px solid #fafafa", "padding": "1em"}, |
| 75 | + ) |
| 76 | + |
| 77 | + h_line = ipw.HTML("<hr>") |
| 78 | + box = ipw.VBox([age_selection, h_line, ipw.HBox([self.mode, self.drop_down])]) |
| 79 | + |
| 80 | + self.results = ipw.Dropdown(layout={"width": "900px"}) |
| 81 | + self.results.observe(self._on_select_structure, names="value") |
| 82 | + self.search() |
| 83 | + super().__init__([box, h_line, self.results]) |
| 84 | + |
| 85 | + def search(self, _=None) -> None: |
| 86 | + """Search structures in the AiiDA database.""" |
| 87 | + qbuild = QueryBuilder() |
| 88 | + |
| 89 | + # If the date range is valid, use it for the search |
| 90 | + try: |
| 91 | + start_date = datetime.datetime.strptime( |
| 92 | + self.start_date_widget.value, "%Y-%m-%d" |
| 93 | + ) |
| 94 | + end_date = datetime.datetime.strptime( |
| 95 | + self.end_date_widget.value, "%Y-%m-%d" |
| 96 | + ) + datetime.timedelta(hours=24) |
| 97 | + |
| 98 | + # Otherwise revert to the standard (i.e. last 7 days) |
| 99 | + except ValueError: |
| 100 | + start_date = datetime.datetime.now() - datetime.timedelta(days=7) |
| 101 | + end_date = datetime.datetime.now() + datetime.timedelta(hours=24) |
| 102 | + |
| 103 | + self.start_date_widget.value = start_date.strftime("%Y-%m-%d") |
| 104 | + self.end_date_widget.value = end_date.strftime("%Y-%m-%d") |
| 105 | + |
| 106 | + filters = {} |
| 107 | + filters["ctime"] = {"and": [{">": start_date}, {"<=": end_date}]} |
| 108 | + |
| 109 | + if self.mode.value == "uploaded": |
| 110 | + qbuild2 = ( |
| 111 | + QueryBuilder() |
| 112 | + .append(self.query_type, project=["id"], tag="structures") |
| 113 | + .append(Node, with_outgoing="structures") |
| 114 | + ) |
| 115 | + processed_nodes = [n[0] for n in qbuild2.all()] |
| 116 | + if processed_nodes: |
| 117 | + filters["id"] = {"!in": processed_nodes} |
| 118 | + qbuild.append(self.query_type, filters=filters) |
| 119 | + |
| 120 | + elif self.mode.value == "calculated": |
| 121 | + if self.drop_down.value == "All": |
| 122 | + qbuild.append((CalcJobNode, WorkChainNode), tag="calcjobworkchain") |
| 123 | + else: |
| 124 | + qbuild.append( |
| 125 | + (CalcJobNode, WorkChainNode), |
| 126 | + filters={"label": self.drop_down.value}, |
| 127 | + tag="calcjobworkchain", |
| 128 | + ) |
| 129 | + qbuild.append( |
| 130 | + self.query_type, |
| 131 | + with_incoming="calcjobworkchain", |
| 132 | + filters=filters, |
| 133 | + ) |
| 134 | + |
| 135 | + elif self.mode.value == "edited": |
| 136 | + qbuild.append(CalcFunctionNode) |
| 137 | + qbuild.append( |
| 138 | + self.query_type, |
| 139 | + with_incoming=CalcFunctionNode, |
| 140 | + filters=filters, |
| 141 | + ) |
| 142 | + |
| 143 | + elif self.mode.value == "all": |
| 144 | + qbuild.append(self.query_type, filters=filters) |
| 145 | + |
| 146 | + qbuild.order_by({self.query_type: {"ctime": "desc"}}) |
| 147 | + matches = {n[0] for n in qbuild.iterall()} |
| 148 | + matches = sorted(matches, reverse=True, key=lambda n: n.ctime) |
| 149 | + |
| 150 | + options = [(f"Select a Structure ({len(matches)} found)", False)] |
| 151 | + for mch in matches: |
| 152 | + label = f"PK: {mch.pk}" |
| 153 | + label += " | " + mch.ctime.strftime("%Y-%m-%d %H:%M") |
| 154 | + label += " | " + mch.base.extras.get("formula", "") |
| 155 | + label += " | " + mch.node_type.split(".")[-2] |
| 156 | + label += " | " + mch.label |
| 157 | + label += " | " + mch.description |
| 158 | + options.append((label, mch)) |
| 159 | + |
| 160 | + self.results.options = options |
| 161 | + return |
| 162 | + |
| 163 | + def _on_select_structure(self, _) -> None: |
| 164 | + self.data_object = self.results.value or None |
| 165 | + return |
| 166 | + |
| 167 | + def disable(self, val: bool) -> None: |
| 168 | + """Disable the widget.""" |
| 169 | + self.results.disabled = True |
| 170 | + # self. |
| 171 | + return |
0 commit comments