1"""Panels to interact with GSR files."""
2import param
3import panel as pn
4import panel.widgets as pnw
5import bokeh.models.widgets as bkw
6
7from .core import PanelWithElectronBands, PanelWithEbandsRobot
8
9
10class GsrFilePanel(PanelWithElectronBands):
11    """
12    Panel with widgets to interact with a |GsrFile|.
13    """
14    def __init__(self, gsr, **params):
15        super().__init__(**params)
16        self.gsr = gsr
17
18    @property
19    def ebands(self):
20        """|ElectronBands|."""
21        return self.gsr.ebands
22
23    def get_panel(self):
24        """Return tabs with widgets to interact with the DDB file."""
25        tabs = pn.Tabs(); app = tabs.append
26        app(("Summary", pn.Row(bkw.PreText(text=self.gsr.to_string(verbose=self.verbose),
27                               sizing_mode="scale_both"))))
28        app(("e-Bands", pn.Row(self.get_plot_ebands_widgets(), self.on_plot_ebands_btn)))
29
30        # Add DOS tab only if k-sampling.
31        kpoints = self.gsr.ebands.kpoints
32        if kpoints.is_ibz:
33            app(("e-DOS", pn.Row(self.get_plot_edos_widgets(), self.on_plot_edos_btn)))
34
35            if self.gsr.ebands.supports_fermi_surface:
36                # Fermi surface requires gamma-centered k-mesh
37                app(("Fermi Surface", pn.Row(self.get_plot_fermi_surface_widgets(), self.on_plot_fermi_surface_btn)))
38
39        return tabs
40
41
42class GsrRobotPanel(PanelWithEbandsRobot):
43    """
44    A Panel to interoperate with multiple GSR files.
45    """
46
47    gsr_dataframe_btn = pnw.Button(name="Compute", button_type='primary')
48
49    def __init__(self, robot, **params):
50        super().__init__(**params)
51        self.robot = robot
52
53    @param.depends("gsr_dataframe_btn.clicks")
54    def on_gsr_dataframe_btn(self):
55        if self.gsr_dataframe_btn.clicks == 0: return
56        df = self.robot.get_dataframe(with_geo=True)
57        return pn.Column(self._df(df), sizing_mode='stretch_width')
58
59    def get_panel(self):
60        """Return tabs with widgets to interact with the |GsrRobot|."""
61        tabs = pn.Tabs(); app = tabs.append
62        app(("Summary", pn.Row(bkw.PreText(text=self.robot.to_string(verbose=self.verbose),
63                               sizing_mode="scale_both"))))
64        app(("e-Bands", pn.Row(self.get_ebands_plotter_widgets(), self.on_ebands_plotter_btn)))
65
66        # Add e-DOS tab only if all ebands have k-sampling.
67        if all(abifile.ebands.kpoints.is_ibz for abifile in self.robot.abifiles):
68            app(("e-DOS", pn.Row(self.get_edos_plotter_widgets(), self.on_edos_plotter_btn)))
69
70        app(("GSR-DataFrame", pn.Row(self.gsr_dataframe_btn, self.on_gsr_dataframe_btn)))
71
72        return tabs
73