Skip to content

Commit

Permalink
add UI elements for dead time settings
Browse files Browse the repository at this point in the history
  • Loading branch information
searscr committed Jun 26, 2024
1 parent 23f9b00 commit 2e43346
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 1 deletion.
12 changes: 12 additions & 0 deletions reflectivity_ui/interfaces/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,12 @@ def to_q_settings(self, settings):
settings.setValue("do_final_rebin", self.do_final_rebin)
settings.setValue("final_rebin_step", self.final_rebin_step)

# Dead time options
settings.setValue("apply_deadtime", self.apply_deadtime)
settings.setValue("paralyzable_deadtime", self.paralyzable_deadtime)
settings.setValue("deadtime_value", self.deadtime_value)
settings.setValue("deadtime_tof_step", self.deadtime_tof_step)

# Off-specular options
settings.setValue("off_spec_x_axis", self.off_spec_x_axis)
settings.setValue("off_spec_slice", self.off_spec_slice)
Expand Down Expand Up @@ -331,6 +337,12 @@ def _verify_true(parameter, default):
Configuration.do_final_rebin = _verify_true("do_final_rebin", self.do_final_rebin)
Configuration.final_rebin_step = float(settings.value("final_rebin_step", self.final_rebin_step))

# Dead time options
Configuration.apply_deadtime = _verify_true("apply_deadtime", self.apply_deadtime)
Configuration.paralyzable_deadtime = _verify_true("paralyzable_deadtime", self.paralyzable_deadtime)
Configuration.deadtime_value = float(settings.value("deadtime_value", self.deadtime_value))
Configuration.deadtime_tof_step = float(settings.value("deadtime_tof_step", self.deadtime_tof_step))

# Off-specular options
self.off_spec_x_axis = int(settings.value("off_spec_x_axis", self.off_spec_x_axis))
self.off_spec_slice = _verify_true("off_spec_slice", self.off_spec_slice)
Expand Down
10 changes: 10 additions & 0 deletions reflectivity_ui/interfaces/event_handlers/main_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,11 @@ def get_configuration(self):
Configuration.do_final_rebin = self.ui.final_rebin_checkbox.isChecked()
Configuration.final_rebin_step = self.ui.q_rebin_spinbox.value()

Configuration.apply_deadtime = self.ui.deadtime_entry.applyCheckBox.isChecked()
Configuration.paralyzable_deadtime = self.main_window.deadtime_settings.paralyzable
Configuration.deadtime_value = self.main_window.deadtime_settings.dead_time
Configuration.deadtime_tof_step = self.main_window.deadtime_settings.tof_step

# UI elements
configuration.normalize_x_tof = self.ui.normalizeXTof.isChecked()
configuration.x_wl_map = self.ui.xLamda.isChecked()
Expand Down Expand Up @@ -1330,6 +1335,11 @@ def populate_from_configuration(self, configuration=None):
self.ui.final_rebin_checkbox.setChecked(configuration.do_final_rebin)
self.ui.q_rebin_spinbox.setValue(configuration.final_rebin_step)

self.ui.deadtime_entry.applyCheckBox.setChecked(configuration.apply_deadtime)
self.main_window.deadtime_settings.paralyzable = configuration.paralyzable_deadtime
self.main_window.deadtime_settings.dead_time = configuration.deadtime_value
self.main_window.deadtime_settings.tof_step = configuration.deadtime_tof_step

# UI elements
self.ui.normalizeXTof.setChecked(configuration.normalize_x_tof)
self.ui.xLamda.setChecked(configuration.x_wl_map)
Expand Down
20 changes: 20 additions & 0 deletions reflectivity_ui/interfaces/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from reflectivity_ui.interfaces.event_handlers.plot_handler import PlotHandler
from reflectivity_ui.interfaces.event_handlers.main_handler import MainHandler
from reflectivity_ui.interfaces import load_ui
from reflectivity_ui.ui.deadtime_settings import DeadTimeSettingsModel, DeadTimeSettingsView

# 3rd-party
from PyQt5 import QtCore, QtWidgets
Expand Down Expand Up @@ -57,6 +58,7 @@ def __init__(self):
# Object managers
self.data_manager = DataManager(self.settings.value("current_directory", os.path.expanduser("~")))
self.plot_manager = PlotManager(self)
self.deadtime_settings = DeadTimeSettingsModel()

r"""Setting `auto_change_active = True` bypasses execution of:
- MainWindow.file_open_from_list()
Expand All @@ -83,6 +85,13 @@ def __init__(self):

self.initiate_reflectivity_plot.connect(self.plot_manager.plot_refl)

self.ui.deadtime_entry.applyCheckBox.stateChanged.connect(self.apply_deadtime_update)
self.ui.deadtime_entry.settingsButton.clicked.connect(self.open_deadtime_settings)

def apply_deadtime_update(self, state):
"""TODO: Figure out if this is needed"""
pass

def closeEvent(self, event):
"""Close UI event"""
self.file_handler.get_configuration()
Expand Down Expand Up @@ -506,3 +515,14 @@ def open_polarization_window(self):

def open_rawdata_dialog(self):
return NotImplemented

def open_deadtime_settings(self):
r"""Show the dialog for dead-time options. Update attribue deadtime options upon closing the dialog."""
view = DeadTimeSettingsView(parent=self)
view.set_state(
self.deadtime_settings.paralyzable, self.deadtime_settings.dead_time, self.deadtime_settings.tof_step
)
view.exec_()
# update the dead time settings of the Main GUI after user has closed the dialog
for option in ["paralyzable", "dead_time", "tof_step"]:
setattr(self.deadtime_settings, option, view.options[option])
41 changes: 41 additions & 0 deletions reflectivity_ui/ui/deadtime_entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# third party imports
from qtpy.QtWidgets import QGroupBox, QHBoxLayout, QCheckBox, QPushButton


class DeadTimeEntryPoint(QGroupBox):
def __init__(self, title='Dead Time Correction'):
super().__init__(title)
self.initUI()

def initUI(self):
# Set the stylesheet for the group box to have a border
self.setStyleSheet(
"QGroupBox {"
" border: 1px solid gray;"
" border-radius: 5px;"
" margin-top: 1ex;" # space above the group box
"} "
"QGroupBox::title {"
" subcontrol-origin: margin;"
" subcontrol-position: top center;" # align the title to the center
" padding: 0 3px;"
"}"
)

self.applyCheckBox = QCheckBox('Apply', self)
self.applyCheckBox.stateChanged.connect(self.toggleSettingsButton)
self.settingsButton = QPushButton('Settings', self)
self.settingsButton.setEnabled(self.applyCheckBox.isChecked()) # enabled if we use the correction

# Create a horizontal layout for the checkbox and settings button
hbox = QHBoxLayout()
hbox.addWidget(self.applyCheckBox)
hbox.addWidget(self.settingsButton)
hbox.addStretch(1) # This adds a stretchable space after the button (optional)

# Set the layout for the group box
self.setLayout(hbox)

def toggleSettingsButton(self, state):
# Enable the settings button if the checkbox is checked, disable otherwise
self.settingsButton.setEnabled(state)
61 changes: 61 additions & 0 deletions reflectivity_ui/ui/deadtime_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# third-party imports
from qtpy.QtWidgets import QDialog, QWidget
from xml.dom.minidom import Document, Element
from typing import Any, Callable, Dict
import os

from qtpy.uic import loadUi


class DeadTimeSettingsModel():
"""Stores all options for the dead time correction. These are global options"""

apply_deadtime: bool = False
paralyzable: bool = True
dead_time: float = 4.2
tof_step: float = 100.0

class DeadTimeSettingsView(QDialog):
"""
Dialog to choose the dead time correction options.
"""

def __init__(self, parent: QWidget):
super().__init__(parent)
filepath = os.path.join(os.path.dirname(__file__), "deadtime_settings.ui")
self.ui = loadUi(filepath, baseinstance=self)
self.options = self.get_state_from_form()

def set_state(self, paralyzable, dead_time, tof_step):
"""
Store options and populate the form
:param apply_correction: If True, dead time correction will be applied
:param paralyzable: If True, a paralyzable correction will be used
:param dead_time: Value of the dead time in micro second
:param tof_step: TOF binning in micro second
"""
self.ui.use_paralyzable.setChecked(paralyzable)
self.ui.dead_time_value.setValue(dead_time)
self.ui.dead_time_tof.setValue(tof_step)
self.options = self.get_state_from_form()

def get_state_from_form(self) -> dict:
r"""Read the options from the form.
Returns
-------
Dictionary whose keys must match fields of class `DeadTimeSettingsModel`
"""
return {
'paralyzable': self.ui.use_paralyzable.isChecked(),
'dead_time': self.ui.dead_time_value.value(),
'tof_step': self.ui.dead_time_tof.value(),
}

def accept(self):
"""
Read in the options on the form when the OK button is
clicked.
"""
self.options = self.get_state_from_form()
self.close()
Loading

0 comments on commit 2e43346

Please sign in to comment.