Skip to content

Commit

Permalink
Get the logic right
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiSavici committed Jan 3, 2025
1 parent 282ab71 commit d9773aa
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 60 deletions.
131 changes: 128 additions & 3 deletions src/hyspecppt/hppt/hppt_model.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,138 @@
"""Model for the Main tab"""

"""Model for the polarization planning tool"""
import numpy as np
import logging


logger = logging.getLogger("hyspecppt")

class SingleCrystalParameters:
"""Model for single crystal calculations"""
a: float
b: float
c: float
alpha: float
beta: float
gamma: float
h: float
k: float
l: float

def __init__(self) -> None:
"""Constructor"""
return

def set_parameters(self, params: dict[str, float]) -> None:
"""store single crystal parameters
Args:
params: dict - contains the following keys:
a, b, c, alpha, beta, gamma, h, k, l
"""
self.a = params["a"]
self.b = params["b"]
self.c = params["c"]
self.alpha = params["alpha"]
self.beta = params["beta"]
self.gamma = params["gamma"]
self.h = params["h"]
self.k = params["k"]
self.l = params["l"]

def get_paramters(self) -> dict[str, float]:
"""returns all the parameters as a dictionary"""
try:
return dict(a=self.a, b=self.b, c=self.c, alpha=self.alpha, beta=self.beta, gamma=self.gamma, h=self.h, k=self.k, l=self.l)
except AttributeError:
logger.error("The parameters were not initialized")

def calculate_modQ(self) -> float:
"""returns |Q| from lattice parameters and h, k, l"""
try:
ca = np.cos(np.radians(self.alpha))
sa = np.sin(np.radians(self.alpha))
cb = np.cos(np.radians(self.beta))
sb = np.sin(np.radians(self.beta))
cg = np.cos(np.radians(self.gamma))
sg = np.sin(np.radians(self.gamma))
vabg = np.sqrt(1 - ca**2 - cb**2 - cg**2 + 2 * ca * cb * cg)
astar = sa / (self.a * vabg)
bstar = sb / (self.b * vabg)
cstar = sg / (self.c * vabg)
cas = (cb * cg - ca) / (sb * sg)
cbs = (cg * ca - cb) / (sg * sa)
cgs = (ca * cb - cg) / (sa * sb)

# B matrix
B = np.array(
[
[astar, bstar * cgs, cstar * cbs],
[0, bstar * np.sqrt(1 - cgs**2), -cstar * np.sqrt(1 - cbs**2) * ca],
[0, 0, 1.0 / self.c],
]
)

modQ = 2 * np.pi * np.linalg.norm(B.dot([self.h, self.k, self.l]))
return modQ
except AttributeError:
logger.error("The parameters were not initialized")


class CrosshairParameters:
"""Model for the crosshair parameters"""
modQ: float
DeltaE: float
current_experiment_type: str
sc_parameters: SingleCrystalParameters

def __init__(self):
self.sc_parameters=SingleCrystalParameters()

def store_data(self, *, current_experiment_type: str = None, DeltaE: float = None,
modQ: float = None, sc_parameters:dict[str, float] = None) -> None:
"""store crosshair parameters including in SC mode"""
if current_experiment_type is not None:
self.current_experiment_type = current_experiment_type
if DeltaE is not None:
self.DeltaE = DeltaE
if modQ is not None:
self.modQ = modQ
if sc_parameters is not None:
self.sc_parameters.set_parameters(sc_parameters)

def get_crosshair(self) -> dict[str, float]:
"""get the crosshair"""
if self.current_experiment_type == "crystal":
self.modQ = self.sc_parameters.calculate_modQ()
return dict(DeltaE=self.DeltaE, modQ=self.modQ)


class HyspecPPTModel:
"""Main model"""
Ei: float = 0
S2: float = 0
alpha_p: float = 0
plot_type: str = ""
cp: CrosshairParameters

def __init__(self):
"""Constructor"""
return
self.cp = CrosshairParameters()

def store_experiment_data(self, Ei: float, S2: float, alpha_p: float, plot_type: str) -> None:
self.Ei = Ei
self.S2 = S2
self.alpha_p = alpha_p
self.plot_type = plot_type

def store_crosshair_data(self, *, current_experiment_type: str = None, DeltaE: float = None, modQ: float = None) -> None:
self.cp.store_data(current_experiment_type=current_experiment_type, DeltaE=DeltaE, modQ=modQ)

def store_single_crystal_data(self, params: dict[str, float]) -> None:
self.cp.store_data(sc_parameters=params)

def get_crosshair(self) -> dict[str, float]:
return self.cp.get_crosshair()

def calculate_graph_data(self):
return [1,2,3]
105 changes: 48 additions & 57 deletions src/hyspecppt/hppt/hppt_presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ def __init__(self, view: "[QWidget]", model: "[QWidget]"):
"""
self._view = view
self._model = model
self.view.SCW.set_values(DEFAULT_LATTICE)
#initialize widgets
self.view.EW.initializeCombo(PLOT_TYPES)
self.view.EW.set_values(DEFAULT_EXPERIMENT)
self.view.CW.set_values(DEFAULT_CROSSHAIR)
self.view.SCW.set_values(DEFAULT_LATTICE)
#select calculation mode
self.view.SelW.set_SC_toggle(True)

self.get_Experiment_values()
self.get_SingleCrystal_values()
self.get_Selector_values()
self.get_Crosshair_values()
#initialize model
self.model.store_experiment_data(**DEFAULT_EXPERIMENT)
self.model.store_crosshair_data(current_experiment_type="crystal", **DEFAULT_CROSSHAIR)
self.model.store_single_crystal_data(DEFAULT_LATTICE)

#connections
self.view.EW.valid_signal.connect(self.update_experiment_values)
self.view.SelW.sc_rb.toggled.connect(self.q_mode_SC)
self.view.SelW.powder_rb.toggled.connect(self.q_mode_powder)
self.view.SCW.valid_signal.connect(self.update_SC_values)
self.view.CW.valid_signal.connect(self.update_cursor_values)

@property
def view(self):
Expand All @@ -36,55 +44,38 @@ def model(self):
"""Return the model for this presenter"""
return self._model

def get_Experiment_values(self) -> dict:
"""Get Ei, Pangle, S2, Type values from Experiment
return: dict of Experiment key value pairs
"""
EW_dict = {}
EW_dict["Ei"] = self.view.EW.Ei_edit.text()
EW_dict["Pangle"] = self.view.EW.Pangle_edit.text()
EW_dict["S2"] = self.view.EW.S2_edit.text()
EW_dict["Type"] = self.view.EW.Type_combobox.currentText()
return EW_dict

def get_Selector_values(self):
"""Check if Single Crystal radio button is checked
return: True - Single Crystal radio button is toggled
False - Single Crystal radio button is not toggled. Powder radio button is toggled
"""
return self.view.SelW.sc_rb.isChecked()

def get_SingleCrystal_values(self):
"""Get Single Crystal mode specific values from SingleCrystalWidget
return: dict of Single Crystal key value pairs
"""
SC_dict = {}
SC_dict["a"] = self.view.SCW.a_edit.text()
SC_dict["b"] = self.view.SCW.b_edit.text()
SC_dict["c"] = self.view.SCW.c_edit.text()

SC_dict["alpha"] = self.view.SCW.alpha_edit.text()
SC_dict["beta"] = self.view.SCW.beta_edit.text()
SC_dict["gamma"] = self.view.SCW.gamma_edit.text()

SC_dict["h"] = self.view.SCW.h_edit.text()
SC_dict["k"] = self.view.SCW.k_edit.text()
SC_dict["l"] = self.view.SCW.l_edit.text()
return SC_dict

def get_Crosshair_values(self):
"""Get Crosshair mode specific values from CrosshairWidget
return: dict of Crosshair key value pairs
"""
CH_dict = {}
CH_dict["DeltaE"] = self.view.CW.DeltaE_edit.text()
CH_dict["modQ"] = self.view.CW.modQ_edit.text()
return CH_dict

def set_PlotWidget_values(self):
def q_mode_powder(self) -> None:
"""switch to powder mode"""
self.model.store_crosshair_data(current_experiment_type='powder')
crosshair = self.model.get_crosshair()
self.view.CW.set_values(crosshair)
self.set_PlotWidget_values(cursor_position=crosshair)

def q_mode_SC(self) -> None:
"""switch to crystal"""
self.model.store_crosshair_data(current_experiment_type='crystal')
crosshair = self.model.get_crosshair()
self.view.CW.set_values(crosshair)
self.set_PlotWidget_values(cursor_position=crosshair)

def update_experiment_values(self, new_values: dict[str, float]) -> None:
"""Ei/S2/alpha_p/plot_type update"""
self.model.store_experiment_data(**new_values)
new_intensity = self.model.calculate_graph_data()
self.set_PlotWidget_values(intensity = new_intensity)

def update_cursor_values(self, new_values: dict[str, float]) -> None:
"""DeltaE/modQ update"""
#TODO: check on DeltaE<-Ei
self.model.store_crosshair_data(**new_values)
self.set_PlotWidget_values(cursor_position=new_values)

def update_SC_values(self, new_values: dict[str, float]) -> None:
self.model.store_single_crystal_data(new_values)
new_position = self.model.get_crosshair()
self.view.CW.set_values(new_position)
self.set_PlotWidget_values(cursor_position=new_position)

def set_PlotWidget_values(self, intensity = "new_intensity", cursor_position = "new_position", label="new_label"):
"""Pass through intensity matrix into plot in view"""
pass
print('call plot:', intensity, cursor_position, label)
4 changes: 4 additions & 0 deletions src/hyspecppt/hppt/hppt_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ def __init__(self, parent: Optional["QObject"] = None) -> None:

self.switch_to_SC()

self.SelW.sc_rb.toggled.connect(self.switch_to_SC)
self.SelW.powder_rb.toggled.connect(self.switch_to_Powder)

def switch_to_SC(self) -> None:
"""Set visibility for Single Crystal mode"""
self.SCW.setVisible(True)
Expand Down Expand Up @@ -325,6 +328,7 @@ def validate_all_inputs(self):
out_signal[k] = float(edit.text())

if len(out_signal) == 9:
print('sc view emmit')
self.valid_signal.emit(out_signal)


Expand Down

0 comments on commit d9773aa

Please sign in to comment.