-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathnode_view.py
516 lines (421 loc) · 17.3 KB
/
node_view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
"""Results view widgets (MOVE TO OTHER MODULE!)
Authors:
* Carl Simon Adorf <simon.adorf@epfl.ch>
"""
import json
import shutil
import typing
from importlib import resources
from pathlib import Path
from tempfile import TemporaryDirectory
import ipywidgets as ipw
import nglview
import traitlets
from aiida.cmdline.utils.common import get_workchain_report
from aiida.common import LinkType
from aiida.orm import CalcJobNode, Node, WorkChainNode
from aiidalab_widgets_base import ProcessMonitor, register_viewer_widget
from aiidalab_widgets_base.viewers import StructureDataViewer
from ase import Atoms
from filelock import FileLock, Timeout
from IPython.display import HTML, display
from jinja2 import Environment
from monty.json import MontyEncoder, jsanitize
from traitlets import Instance, Int, List, Unicode, Union, default, observe, validate
from widget_bandsplot import BandsPlotWidget
from aiidalab_qe import static
from aiidalab_qe.report import generate_report_dict
class MinimalStructureViewer(ipw.VBox):
structure = Union([Instance(Atoms), Instance(Node)], allow_none=True)
_displayed_structure = Instance(Atoms, allow_none=True, read_only=True)
background = Unicode()
supercell = List(Int)
def __init__(self, structure, *args, **kwargs):
self._viewer = nglview.NGLWidget()
self._viewer.camera = "orthographic"
self._viewer.stage.set_parameters(mouse_preset="pymol")
ipw.link((self, "background"), (self._viewer, "background"))
self.structure = structure
super().__init__(
children=[
self._viewer,
],
*args,
**kwargs,
)
@default("background")
def _default_background(self):
return "#FFFFFF"
@default("supercell")
def _default_supercell(self):
return [1, 1, 1]
@validate("structure")
def _valid_structure(self, change): # pylint: disable=no-self-use
"""Update structure."""
structure = change["value"]
if structure is None:
return None # if no structure provided, the rest of the code can be skipped
if isinstance(structure, Atoms):
return structure
if isinstance(structure, Node):
return structure.get_ase()
raise ValueError(
"Unsupported type {}, structure must be one of the following types: "
"ASE Atoms object, AiiDA CifData or StructureData."
)
@observe("structure")
def _update_displayed_structure(self, change):
"""Update displayed_structure trait after the structure trait has been modified."""
# Remove the current structure(s) from the viewer.
if change["new"] is not None:
self.set_trait("_displayed_structure", change["new"].repeat(self.supercell))
else:
self.set_trait("_displayed_structure", None)
@observe("_displayed_structure")
def _update_structure_viewer(self, change):
"""Update the view if displayed_structure trait was modified."""
with self.hold_trait_notifications():
for (
comp_id
) in self._viewer._ngl_component_ids: # pylint: disable=protected-access
self._viewer.remove_component(comp_id)
self.selection = list()
if change["new"] is not None:
self._viewer.add_component(nglview.ASEStructure(change["new"]))
self._viewer.clear()
self._viewer.stage.set_parameters(clipDist=0)
self._viewer.add_representation("unitcell", diffuse="#df0587")
self._viewer.add_representation("ball+stick", aspectRatio=3.5)
def export_bands_data(work_chain_node):
if "band_structure" in work_chain_node.outputs:
data = json.loads(
work_chain_node.outputs.band_structure._exportcontent(
"json", comments=False
)[0]
)
data["fermi_level"] = work_chain_node.outputs.band_parameters["fermi_energy"]
return jsanitize(data)
def export_pdos_data(work_chain_node):
if "dos" in work_chain_node.outputs:
fermi_energy = work_chain_node.outputs.band_parameters["fermi_energy"]
x_label, energy_dos, energy_units = work_chain_node.outputs.dos.get_x()
tdos_values = {
f"{n} | {u}": v for n, v, u in work_chain_node.outputs.dos.get_y()
}
pdos_orbitals = []
for orbital, pdos, energy in work_chain_node.outputs.projections.get_pdos():
orbital_data = orbital.get_orbital_dict()
kind_name = orbital_data["kind_name"]
orbital_name = orbital.get_name_from_quantum_numbers(
orbital_data["angular_momentum"], orbital_data["magnetic_number"]
)
pdos_orbitals.append(
{
"kind": kind_name,
"orbital": orbital_name,
"energy | eV": energy,
"pdos | states/eV": pdos,
}
)
data_dict = {
"fermi_energy": fermi_energy,
"tdos": {f"energy | {energy_units}": energy_dos, "values": tdos_values},
"pdos": pdos_orbitals,
}
# And this is why we shouldn't use special encoders...
return json.loads(json.dumps(data_dict, cls=MontyEncoder))
fermi_energy = work_chain_node.outputs.nscf__output_parameters.get_dict()[
"fermi_energy"
]
(
xlabel,
energy_dos,
energy_units,
) = work_chain_node.outputs.dos__output_dos.get_x()
tdos_values = {
f"{n} | {u}": v
for n, v, u in work_chain_node.outputs.dos__output_dos.get_y()
}
def export_data(work_chain_node):
return dict(
bands=export_bands_data(work_chain_node), dos=export_pdos_data(work_chain_node)
)
class VBoxWithCaption(ipw.VBox):
def __init__(self, caption, body, *args, **kwargs):
super().__init__(children=[ipw.HTML(caption), body], *args, **kwargs)
class SummaryView(ipw.VBox):
def __init__(self, wc_node, **kwargs):
self.wc_node = wc_node
def _fmt_yes_no(truthy):
return "Yes" if truthy else "No"
report = generate_report_dict(self.wc_node)
env = Environment()
env.filters.update(
{
"fmt_yes_no": _fmt_yes_no,
}
)
template = resources.read_text(static, "workflow_summary.jinja")
style = resources.read_text(static, "style.css")
self.summary_view = ipw.HTML(
env.from_string(template).render(style=style, **report)
)
super().__init__(
children=[self.summary_view],
**kwargs,
)
class WorkChainOutputs(ipw.VBox):
_busy = traitlets.Bool(read_only=True)
def __init__(self, node, export_dir=None, **kwargs):
self.export_dir = Path.cwd().joinpath("exports")
if node.process_label != "QeAppWorkChain":
raise KeyError(str(node.node_type))
self.node = node
self._create_archive_indicator = ipw.HTML(
"""<button disabled>
<i class="fa fa-spinner fa-spin" aria-hidden="true"></i>
Creating archive...
</button>"""
)
self._download_archive_button = ipw.Button(
description="Download output",
icon="download",
)
self._download_archive_button.on_click(self._download_archive)
self._download_button_container = ipw.Box([self._download_archive_button])
if node.exit_status != 0:
title = ipw.HTML(
f"<h4>Workflow failed with exit status [{ node.exit_status }]</h4>"
)
final_calcjob = self._get_final_calcjob(node)
env = Environment()
template = resources.read_text(static, "workflow_failure.jinja")
style = resources.read_text(static, "style.css")
output = ipw.HTML(
env.from_string(template).render(
style=style,
process_report=get_workchain_report(node, "REPORT"),
calcjob_exit_message=final_calcjob.exit_message,
)
)
else:
title = ipw.HTML("<h4>Workflow completed successfully!</h4>")
output = ipw.HTML()
super().__init__(
children=[
ipw.HBox(
children=[title, self._download_button_container],
layout=ipw.Layout(justify_content="space-between", margin="10px"),
),
output,
],
**kwargs,
)
@traitlets.default("_busy")
def _default_busy(self):
return False
@traitlets.observe("_busy")
def _observe_busy(self, change):
self._download_button_container.children = [
self._create_archive_indicator
if change["new"]
else self._download_archive_button
]
def _download_archive(self, _):
fn_archive = self.export_dir.joinpath(str(self.node.uuid)).with_suffix(".zip")
fn_lockfile = fn_archive.with_suffix(".lock")
try:
self.set_trait("_busy", True)
# Create exports archive directory.
fn_archive.parent.mkdir(parents=True, exist_ok=True)
# Try to obtain lock for creating archive...
with FileLock(fn_lockfile, timeout=0):
# Check whether archive file already exists.
if not fn_archive.is_file():
# Create archive file.
with TemporaryDirectory() as tmpdir:
self._prepare_calcjob_io(self.node, Path(tmpdir))
shutil.make_archive(fn_archive.with_suffix(""), "zip", tmpdir)
Path(fn_lockfile).unlink() # Delete lock file.
except Timeout:
# Failed to obtain lock, presuming some other process is working on it.
with FileLock(fn_lockfile, timeout=20):
assert fn_archive.is_file()
finally:
self.set_trait("_busy", False)
id = f"dl_{self.node.uuid}"
display(
HTML(
f"""
<html>
<body>
<a
id="{id}"
href="{fn_archive.relative_to(Path.cwd())}"
download="{fn_archive.stem}"
></a>
<script>
(function download() {{document.getElementById("{id}").click();
}})()
</script>
</body>
</html>
"""
)
)
@classmethod
def _prepare_calcjob_io(cls, node: WorkChainNode, root_folder: Path):
"""Prepare the calculation job input and output files.
:param node: QeAppWorkChain node.
"""
counter = 1
for link1 in node.get_outgoing(link_type=LinkType.CALL_WORK):
wc_node = link1.node
for link2 in wc_node.get_outgoing(link_type=LinkType.CALL_WORK):
base_node = link2.node
base_label = (
f"iter{link2.link_label[-1]}"
if link1.link_label == "relax"
else link2.link_label
)
for link3 in base_node.get_outgoing(
link_type=LinkType.CALL_CALC, link_label_filter="iteration_%"
):
counter_str = f"0{counter}" if counter < 10 else str(counter)
pw_label = f"pw{link3.link_label[-1]}"
fdname = f"{counter_str}-{link1.link_label}-{base_label}-{pw_label}"
folder_path = root_folder / fdname
cls._write_calcjob_io(link3.node, folder_path)
counter += 1
@staticmethod
def _get_final_calcjob(node: WorkChainNode) -> typing.Union[None, CalcJobNode]:
"""Get the final calculation job node called by a work chain node.
:param node: Work chain node.
"""
try:
final_calcjob = [
process
for process in node.called_descendants
if isinstance(process, CalcJobNode) and process.is_finished
][-1]
except IndexError:
final_calcjob = None
return final_calcjob
@staticmethod
def _write_calcjob_io(calcjob: CalcJobNode, folder: Path) -> None:
"""Write the ``calcjob`` in and output files to ``folder``.
:param calcjob: calculation job node for which to write the IO files.
:param folder: folder to which to write the IO files.
"""
folder.mkdir(exist_ok=True)
input_filepath = folder / "aiida.in"
with calcjob.open(calcjob.get_option("input_filename"), "r") as ihandle:
with input_filepath.open("w") as ohandle:
ohandle.write(ihandle.read())
pseudo_folder = folder / "pseudo"
pseudo_folder.mkdir(exist_ok=True)
for _, pseudo in calcjob.inputs.pseudos.items():
pseudo_path = pseudo_folder / pseudo.filename
with pseudo_path.open("w") as handle:
handle.write(pseudo.get_content())
retrieved = calcjob.outputs.retrieved
for filename in retrieved.list_object_names():
out_filepath = folder / filename
with out_filepath.open("w") as handle:
handle.write(retrieved.get_object_content(filename))
@register_viewer_widget("process.workflow.workchain.WorkChainNode.")
class WorkChainViewer(ipw.VBox):
_results_shown = traitlets.Set()
def __init__(self, node, **kwargs):
if node.process_label != "QeAppWorkChain":
raise KeyError(str(node.node_type))
self.node = node
self.title = ipw.HTML(
f"""
<hr style="height:2px;background-color:#2097F3;">
<h4>QE App Work Chain (pk: {self.node.pk}) —
{self.node.inputs.structure.get_formula()}
</h4>
"""
)
self.workflows_summary = SummaryView(self.node)
self.summary_tab = ipw.VBox(children=[self.workflows_summary])
self.structure_tab = ipw.VBox(
[ipw.Label("Structure not available.")],
layout=ipw.Layout(min_height="380px"),
)
self.bands_tab = ipw.VBox(
[ipw.Label("Electronic Structure not available.")],
layout=ipw.Layout(min_height="380px"),
)
self.result_tabs = ipw.Tab(
children=[self.summary_tab, self.structure_tab, self.bands_tab]
)
self.result_tabs.set_title(0, "Workflow Summary")
self.result_tabs.set_title(1, "Final Geometry (n/a)")
self.result_tabs.set_title(2, "Electronic Structure (n/a)")
# An ugly fix to the structure appearance problem
# https://github.com/aiidalab/aiidalab-qe/issues/69
def on_selected_index_change(change):
index = change["new"]
# Accessing the viewer only if the corresponding tab is present.
if self.result_tabs._titles[str(index)] == "Final Geometry":
self._structure_view._viewer.handle_resize()
def toggle_camera():
"""Toggle camera between perspective and orthographic."""
self._structure_view._viewer.camera = (
"perspective"
if self._structure_view._viewer.camera == "orthographic"
else "orthographic"
)
toggle_camera()
toggle_camera()
self.result_tabs.observe(on_selected_index_change, "selected_index")
self._update_view()
super().__init__(
children=[self.title, self.result_tabs],
**kwargs,
)
self._process_monitor = ProcessMonitor(
process=self.node,
callbacks=[
self._update_view,
],
)
def _update_view(self):
with self.hold_trait_notifications():
if self.node.is_finished:
self._show_workflow_output()
if (
"structure" not in self._results_shown
and "structure" in self.node.outputs
):
self._show_structure()
self._results_shown.add("structure")
if (
"band_structure" not in self._results_shown
and "band_structure" in self.node.outputs
):
self._show_band_structure()
self._results_shown.add("band_structure")
def _show_structure(self):
self._structure_view = StructureDataViewer(
structure=self.node.outputs.structure
)
self.result_tabs.children[1].children = [self._structure_view]
self.result_tabs.set_title(1, "Final Geometry")
def _show_band_structure(self):
data = export_data(self.node)
bands_data = data.get("bands", None)
dos_data = data.get("dos", None)
self._bands_plot_view = BandsPlotWidget(
bands=[bands_data], dos=dos_data, plot_fermilevel=True
)
self.result_tabs.children[2].children = [self._bands_plot_view]
self.result_tabs.set_title(2, "Electronic Structure")
def _show_workflow_output(self):
self.workflows_output = WorkChainOutputs(self.node)
self.result_tabs.children[0].children = [
self.workflows_summary,
self.workflows_output,
]