Skip to content

Commit

Permalink
Save instrument rigid body constraint params
Browse files Browse the repository at this point in the history
These will be saved in the state file and re-used in the calibration
dialog. These numbers are essential for FIDDLE, so it's important that
they stay recorded in the state file and are retrievable.

Signed-off-by: Patrick Avery <[email protected]>
  • Loading branch information
psavery committed Dec 20, 2024
1 parent c0a4343 commit 830855b
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 35 deletions.
7 changes: 7 additions & 0 deletions hexrdgui/calibration/calibration_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class CalibrationDialog(QObject):
relative_constraints_changed = Signal(RelativeConstraintsType)
tilt_center_of_rotation_changed = Signal(RotationCenter)
engineering_constraints_changed = Signal(str)
reset_relative_params_to_zero_clicked = Signal()

pinhole_correction_settings_modified = Signal()

Expand Down Expand Up @@ -107,6 +108,8 @@ def setup_connections(self):
self.on_active_beam_changed)
self.ui.show_picks_from_all_xray_sources.toggled.connect(
self.show_picks_from_all_xray_sources_toggled)
self.ui.reset_relative_params_to_zero.clicked.connect(
self.on_reset_relative_params_to_zero_clicked)
self.ui.relative_constraints.currentIndexChanged.connect(
self.on_relative_constraints_changed)
self.ui.tilt_center_of_rotation.currentIndexChanged.connect(
Expand Down Expand Up @@ -245,6 +248,7 @@ def update_relative_constraint_visibilities(self):
visible = self.relative_constraints != RelativeConstraintsType.none

tilt_center_widgets = [
self.ui.reset_relative_params_to_zero,
self.ui.tilt_center_of_rotation,
self.ui.tilt_center_of_rotation_label,
]
Expand Down Expand Up @@ -432,6 +436,9 @@ def tth_distortion(self, v):
first = next(iter(v.values()))
self.pinhole_correction_editor.update_from_object(first)

def on_reset_relative_params_to_zero_clicked(self):
self.reset_relative_params_to_zero_clicked.emit()

def on_relative_constraints_changed(self):
# If the relative constraints is not None, then the engineering
# constraints must be set to None
Expand Down
54 changes: 52 additions & 2 deletions hexrdgui/calibration/calibration_dialog_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import lmfit

from hexrd.fitting.calibration.relative_constraints import (
RelativeConstraintsType,
)
from hexrd.fitting.calibration.lmfit_param_handling import (
create_instr_params,
update_instrument_from_params,
Expand All @@ -17,6 +20,10 @@
from hexrdgui.utils.abc_qobject import ABCQObject


# Number of decimal places to round calibration parameters
ROUND_DECIMALS = 3


class CalibrationDialogCallbacks(ABCQObject):
"""A class with default behavior for calibration dialog callbacks"""

Expand Down Expand Up @@ -59,6 +66,8 @@ def setup_connections(self):
dialog.load_picks_clicked.connect(self.on_load_picks_clicked)
dialog.relative_constraints_changed.connect(
self.on_relative_constraints_changed)
dialog.reset_relative_params_to_zero_clicked.connect(
self.on_reset_relative_params_to_zero_clicked)
dialog.tilt_center_of_rotation_changed.connect(
self.on_tilt_center_of_rotation_changed)
dialog.engineering_constraints_changed.connect(
Expand Down Expand Up @@ -162,18 +171,54 @@ def pop_undo_stack(self):
self.dialog.advanced_options = stack_item['advanced_options']

self.update_undo_enable_state()
self.save_constraint_params()

def update_undo_enable_state(self):
self.dialog.undo_enabled = bool(self.undo_stack)

def on_relative_constraints_changed(self, new_constraint):
self.calibrator.relative_constraints_type = new_constraint
self.restore_constraint_params()
# Reset the tilt center of rotation in the dialog
self.dialog.tilt_center_of_rotation = (
self.calibrator.relative_constraints.rotation_center
)
self.on_constraints_changed()

def on_reset_relative_params_to_zero_clicked(self):
self.reset_saved_constraint_params()

def save_constraint_params(self):
constraints = self.calibrator.relative_constraints
if constraints.type != RelativeConstraintsType.system:
# Instead of saving, reset them
self.reset_saved_constraint_params()
return

HexrdConfig()._instrument_rigid_body_params = copy.deepcopy(
constraints.params)

def restore_constraint_params(self):
constraints = self.calibrator.relative_constraints
if constraints.type != RelativeConstraintsType.system:
return

params = copy.deepcopy(HexrdConfig()._instrument_rigid_body_params)
if not params:
return

constraints.params.update(**params)
self.calibrator.reset_lmfit_params()
self.round_param_numbers()
self.update_dialog_from_calibrator()

def reset_saved_constraint_params(self):
HexrdConfig()._instrument_rigid_body_params.clear()
constraints = self.calibrator.relative_constraints
constraints.reset_params()
self.calibrator.reset_lmfit_params()
self.update_dialog_from_calibrator()

def on_tilt_center_of_rotation_changed(self, new_center):
relative_constraints = self.calibrator.relative_constraints
if relative_constraints.rotation_center != new_center:
Expand Down Expand Up @@ -270,6 +315,7 @@ def run_calibration(self, **extra_kwargs):

def on_calibration_finished(self):
self.update_config_from_instrument()
self.save_constraint_params()
self.dialog.params_dict = self.calibrator.params

def update_config_from_instrument(self):
Expand Down Expand Up @@ -299,7 +345,7 @@ def update_config_from_instrument(self):
def update_tth_distortion_from_dialog(self):
self.calibrator.tth_distortion = self.dialog.tth_distortion

def round_param_numbers(self, decimals=3):
def round_param_numbers(self):
params_dict = self.calibrator.params

attrs = [
Expand All @@ -309,7 +355,11 @@ def round_param_numbers(self, decimals=3):
]
for param in params_dict.values():
for attr in attrs:
setattr(param, attr, round(getattr(param, attr), 3))
setattr(
param,
attr,
round(getattr(param, attr), ROUND_DECIMALS)
)

def on_edit_picks_finished(self):
# Show this again
Expand Down
3 changes: 3 additions & 0 deletions hexrdgui/hexrd_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ def __init__(self):
self._apply_absorption_correction = False
self._physics_package = None
self._detector_coatings = {}
self._instrument_rigid_body_params = {}

# Make sure that the matplotlib font size matches the application
self.font_size = self.font_size
Expand Down Expand Up @@ -428,6 +429,7 @@ def _attributes_to_persist(self):
('sample_tilt', [0, 0, 0]),
('azimuthal_offset', 0.0),
('_active_beam_name', None),
('_instrument_rigid_body_params', {}),
('recent_state_files', []),
('apply_absorption_correction', False),
('physics_package_dictified', None),
Expand Down Expand Up @@ -467,6 +469,7 @@ def _load_state_from_settings(self, settings):
'azimuthal_overlays',
'azimuthal_offset',
'_recent_images',
'_instrument_rigid_body_params',
]

state = {}
Expand Down
74 changes: 41 additions & 33 deletions hexrdgui/resources/ui/calibration_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@
<string>Constraints</string>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="0" column="0">
<widget class="QLabel" name="relative_constraints_label">
<item row="0" column="1">
<widget class="QComboBox" name="relative_constraints">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Options to set relative constraints between the detectors.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;&amp;quot;None&amp;quot; means no relative constraints.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;&amp;quot;System&amp;quot; means all detectors are relatively constrained to one another. In this case, the mean center of the detectors and a mean tilt may be refined.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Relative constraints:</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Options to set relative constraints between the detectors.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;&amp;quot;None&amp;quot; means no relative constraints.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;&amp;quot;Instrument Rigid Body&amp;quot; means all detectors in the entire instrument are relatively constrained to one another. In this case, the mean center of the detectors and a mean tilt may be refined.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QCheckBox" name="delta_boundaries">
<item row="2" column="0">
<widget class="QLabel" name="tilt_center_of_rotation_label">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;The center of rotation to be used for tilt parameters when relative constraints are enabled.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;&amp;quot;Mean Instrument Center&amp;quot; will cause the panels to be rotated about the mean center of the detectors when the tilt is modified.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;&amp;quot;Origin&amp;quot; will cause the panels to be rotated about lab origin (i. e., [0, 0, 0]) when the tilt is modified.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Use delta for boundaries</string>
<string>Tilt center of rotation:</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<item row="5" column="0" colspan="2">
<widget class="QPushButton" name="mirror_constraints_from_first_detector">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;If clicked, the &amp;quot;Vary&amp;quot; and &amp;quot;Delta&amp;quot; (if &amp;quot;Use delta for boundaries&amp;quot; is checked) settings of the first detector's tilt/translation parameters will be copied to all other detectors' tilt/translation parameters.&lt;/p&gt;&lt;p&gt;This is helpful if you have many detectors and want to modify all of their &amp;quot;Vary&amp;quot; and &amp;quot;Delta&amp;quot; settings in a similar way.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
Expand All @@ -47,24 +47,7 @@
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="relative_constraints">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Options to set relative constraints between the detectors.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;&amp;quot;None&amp;quot; means no relative constraints.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;&amp;quot;Instrument Rigid Body&amp;quot; means all detectors in the entire instrument are relatively constrained to one another. In this case, the mean center of the detectors and a mean tilt may be refined.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="engineering_constraints_label">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Add engineering constraints for certain instrument types. This may add extra parameters to the table.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;For example, for TARDIS, the distance between IMAGE-PLATE-2 and IMAGE-PLATE-4 must be within a certain range. If TARDIS is selected, a new parameter is added with default values for this distance.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;If the instrument type can be guessed, it will be selected automatically when the dialog first appears. For example, TARDIS is automatically selected if any of the detector names are IMAGE-PLATE-2, IMAGE-PLATE-3, or IMAGE-PLATE-4.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Add engineering constraints for:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QComboBox" name="engineering_constraints">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Add engineering constraints for certain instrument types. This may add extra parameters to the table.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;For example, for TARDIS, the distance between IMAGE-PLATE-2 and IMAGE-PLATE-4 must be within a certain range. If TARDIS is selected, a new parameter is added with default values for this distance.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;If the instrument type can be guessed, it will be selected automatically when the dialog first appears. For example, TARDIS is automatically selected if any of the detector names are IMAGE-PLATE-2, IMAGE-PLATE-3, or IMAGE-PLATE-4.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
Expand All @@ -81,23 +64,47 @@
</item>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="tilt_center_of_rotation_label">
<item row="3" column="0">
<widget class="QLabel" name="engineering_constraints_label">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;The center of rotation to be used for tilt parameters when relative constraints are enabled.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;&amp;quot;Mean Instrument Center&amp;quot; will cause the panels to be rotated about the mean center of the detectors when the tilt is modified.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;&amp;quot;Origin&amp;quot; will cause the panels to be rotated about lab origin (i. e., [0, 0, 0]) when the tilt is modified.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Add engineering constraints for certain instrument types. This may add extra parameters to the table.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;For example, for TARDIS, the distance between IMAGE-PLATE-2 and IMAGE-PLATE-4 must be within a certain range. If TARDIS is selected, a new parameter is added with default values for this distance.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;If the instrument type can be guessed, it will be selected automatically when the dialog first appears. For example, TARDIS is automatically selected if any of the detector names are IMAGE-PLATE-2, IMAGE-PLATE-3, or IMAGE-PLATE-4.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Tilt center of rotation:</string>
<string>Add engineering constraints for:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<item row="0" column="0">
<widget class="QLabel" name="relative_constraints_label">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Options to set relative constraints between the detectors.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;&amp;quot;None&amp;quot; means no relative constraints.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;&amp;quot;System&amp;quot; means all detectors are relatively constrained to one another. In this case, the mean center of the detectors and a mean tilt may be refined.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Relative constraints:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QComboBox" name="tilt_center_of_rotation">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;The center of rotation to be used for tilt parameters when relative constraints are enabled.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;&amp;quot;Mean Instrument Center&amp;quot; will cause the panels to be rotated about the mean center of the detectors when the tilt is modified.&lt;/p&gt;&lt;p&gt;&lt;br/&gt;&lt;/p&gt;&lt;p&gt;&amp;quot;Origin&amp;quot; will cause the panels to be rotated about lab origin (i. e., [0, 0, 0]) when the tilt is modified.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QCheckBox" name="delta_boundaries">
<property name="text">
<string>Use delta for boundaries</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<widget class="QPushButton" name="reset_relative_params_to_zero">
<property name="text">
<string>Reset Relative Params to Zero</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down Expand Up @@ -501,6 +508,7 @@ See scipy.optimize.least_squares for more details.</string>
<tabstop>active_beam</tabstop>
<tabstop>show_picks_from_all_xray_sources</tabstop>
<tabstop>relative_constraints</tabstop>
<tabstop>reset_relative_params_to_zero</tabstop>
<tabstop>tilt_center_of_rotation</tabstop>
<tabstop>engineering_constraints</tabstop>
<tabstop>delta_boundaries</tabstop>
Expand Down

0 comments on commit 830855b

Please sign in to comment.