Skip to content

Commit

Permalink
setting IR/Raman Results tab
Browse files Browse the repository at this point in the history
  • Loading branch information
AndresOrtegaGuerrero committed Dec 2, 2024
1 parent eaa337c commit 194c3b3
Show file tree
Hide file tree
Showing 9 changed files with 431 additions and 353 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies = [
"pre-commit",
"euphonic",
"kaleido",
"weas-widget==0.1.15",
"weas-widget==0.1.19",
]

[tool.ruff.lint]
Expand Down
46 changes: 0 additions & 46 deletions src/aiidalab_qe_vibroscopy/app/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from IPython.display import display
import numpy as np

from ..utils.raman.result import export_iramanworkchain_data
from ..utils.phonons.result import export_phononworkchain_data

from ..utils.euphonic import (
Expand All @@ -19,8 +18,6 @@
import plotly.graph_objects as go
import ipywidgets as ipw

from ..utils.raman.result import SpectrumPlotWidget, ActiveModesWidget


class PhononBandPdosPlotly(BandPdosPlotly):
def __init__(self, bands_data=None, pdos_data=None):
Expand Down Expand Up @@ -82,7 +79,6 @@ def _update_view(self):
children_result_widget = ()
tab_titles = [] # this is needed to name the sub panels

spectra_data = export_iramanworkchain_data(self.node)
phonon_data = export_phononworkchain_data(self.node)
ins_data = export_euphonic_data(self.node)

Expand Down Expand Up @@ -164,48 +160,6 @@ def _update_view(self):
),
) # the comma is required! otherwise the tuple is not detected.

if spectra_data:
# Here we should provide the possibility to have both IR and Raman,
# as the new logic can provide both at the same time.
# We are gonna use the same widget, providing the correct spectrum_type: "Raman" or "Ir".
children_spectra = ()
for spectrum, data in spectra_data.items():
if not data:
continue

elif isinstance(data, str):
# No Modes are detected. So we explain why
no_mode_widget = ipw.HTML(data)
explanation_widget = ipw.HTML(
"This may be due to the fact that the current implementation of aiida-vibroscopy plugin only considers first-order effects."
)

children_spectra += (
ipw.VBox([no_mode_widget, explanation_widget]),
)

else:
subwidget_title = ipw.HTML(f"<h3>{spectrum} spectroscopy</h3>")
spectrum_widget = SpectrumPlotWidget(
node=self.node, output_node=data, spectrum_type=spectrum
)
modes_animation = ActiveModesWidget(
node=self.node, output_node=data, spectrum_type=spectrum
)

children_spectra += (
ipw.VBox([subwidget_title, spectrum_widget, modes_animation]),
)
children_result_widget += (
ipw.VBox(
children=children_spectra,
layout=ipw.Layout(
width="100%",
),
),
)
tab_titles.append("Raman/IR spectra")

# euphonic
if ins_data:
intensity_maps = EuphonicSuperWidget(
Expand Down
16 changes: 10 additions & 6 deletions src/aiidalab_qe_vibroscopy/app/result/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
import traitlets as tl


from aiidalab_qe_vibroscopy.utils.phonons.result import export_phononworkchain_data
from aiidalab_qe_vibroscopy.utils.euphonic import export_euphonic_data


class VibroResultsModel(ResultsModel):
identifier = "vibronic"

Expand All @@ -30,8 +26,16 @@ def needs_raman_tab(self):

# Here we use _fetch_child_process_node() since the function needs the input_structure in inputs
def needs_phonons_tab(self):
return export_phononworkchain_data(self._fetch_child_process_node())
node = self.get_vibro_node()
if not any(
key in node for key in ["phonon_bands", "phonon_thermo", "phonon_pdos"]
):
return False
return True

# Here we use _fetch_child_process_node() since the function needs the input_structure in inputs
def needs_euphonic_tab(self):
return export_euphonic_data(self._fetch_child_process_node())
node = self.get_vibro_node()
if not any(key in node for key in ["phonon_bands"]):
return False
return True
16 changes: 10 additions & 6 deletions src/aiidalab_qe_vibroscopy/app/result/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from aiidalab_qe_vibroscopy.app.widgets.dielectricmodel import DielectricModel
import ipywidgets as ipw

from aiidalab_qe_vibroscopy.app.widgets.ramanwidget import RamanWidget
from aiidalab_qe_vibroscopy.app.widgets.ramanmodel import RamanModel
from aiidalab_qe_vibroscopy.app.widgets.ir_ramanwidget import IRRamanWidget
from aiidalab_qe_vibroscopy.app.widgets.ir_ramanmodel import IRRamanModel


class VibroResultsPanel(ResultsPanel[VibroResultsModel]):
Expand Down Expand Up @@ -37,12 +37,16 @@ def render(self):

needs_raman_tab = self._model.needs_raman_tab()
if needs_raman_tab:
raman_model = RamanModel()
raman_widget = RamanWidget(
model=raman_model,
vibroscopy_node = self._model._fetch_child_process_node()
input_structure = vibroscopy_node.inputs.structure.get_ase()
irraman_model = IRRamanModel()
irraman_widget = IRRamanWidget(
model=irraman_model,
node=vibro_node,
input_structure=input_structure,
)
tab_data.append(("Raman", raman_widget))

tab_data.append(("Raman/IR spectra", irraman_widget))

needs_dielectri_tab = self._model.needs_dielectric_tab()

Expand Down
26 changes: 26 additions & 0 deletions src/aiidalab_qe_vibroscopy/app/widgets/ir_ramanmodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from __future__ import annotations
from aiidalab_qe.common.mvc import Model
from aiida.common.extendeddicts import AttributeDict
from ase.atoms import Atoms
import traitlets as tl

from aiidalab_qe_vibroscopy.utils.raman.result import export_iramanworkchain_data


class IRRamanModel(Model):
vibro = tl.Instance(AttributeDict, allow_none=True)
input_structure = tl.Instance(Atoms, allow_none=True)

needs_raman_tab = tl.Bool()
needs_ir_tab = tl.Bool()

def fetch_data(self):
spectra_data = export_iramanworkchain_data(self.vibro)
if spectra_data["Ir"] == "No IR modes detected.":
self.needs_ir_tab = False
else:
self.needs_ir_tab = True
if spectra_data["Raman"] == "No Raman modes detected.":
self.needs_raman_tab = False
else:
self.needs_raman_tab = True
58 changes: 58 additions & 0 deletions src/aiidalab_qe_vibroscopy/app/widgets/ir_ramanwidget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from aiidalab_qe_vibroscopy.app.widgets.ir_ramanmodel import IRRamanModel
from aiidalab_qe_vibroscopy.app.widgets.ramanwidget import RamanWidget
from aiidalab_qe_vibroscopy.app.widgets.ramanmodel import RamanModel
import ipywidgets as ipw


class IRRamanWidget(ipw.VBox):
def __init__(self, model: IRRamanModel, node: None, input_structure, **kwargs):
super().__init__(
children=[ipw.HTML("Loading Raman data...")],
**kwargs,
)
self._model = model
self._model.vibro = node
self._model.input_structure = input_structure
self.rendered = False

def render(self):
if self.rendered:
return

self.children = []

self.rendered = True
self._model.fetch_data()
self._needs_raman_widget()
self._needs_ir_widget()
self.render_widgets()

def _needs_raman_widget(self):
if self._model.needs_raman_tab:
self.raman_model = RamanModel()
self.raman_widget = RamanWidget(
model=self.raman_model,
node=self._model.vibro,
input_structure=self._model.input_structure,
spectrum_type="Raman",
)
self.children = (*self.children, self.raman_widget)

def _needs_ir_widget(self):
if self._model.needs_ir_tab:
self.ir_model = RamanModel()
self.ir_widget = RamanWidget(
model=self.ir_model,
node=self._model.vibro,
input_structure=self._model.input_structure,
spectrum_type="IR",
)
self.children = (*self.children, self.ir_widget)
else:
self.children = (*self.children, ipw.HTML("No IR modes detected."))

def render_widgets(self):
if self._model.needs_raman_tab:
self.raman_widget.render()
if self._model.needs_ir_tab:
self.ir_widget.render()
Loading

0 comments on commit 194c3b3

Please sign in to comment.