Skip to content

Commit

Permalink
Some cleanup and simplifications
Browse files Browse the repository at this point in the history
I removed the `use_physics_package` variable from `HexrdConfig()` because
it could have been simplified by simply checking if the physics package
existed.

I also fixed a few issues and cleaned up a few things.

Signed-off-by: Patrick Avery <patrick.avery@kitware.com>
  • Loading branch information
psavery committed Jan 10, 2025
1 parent 4a7a949 commit d363b6d
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 79 deletions.
8 changes: 7 additions & 1 deletion hexrdgui/absorption_correction_options_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ def load_additional_materials(self):
# FIXME: Update to use defaults once they've been added to HEXRD
return

@property
def any_detector_filters_applied(self):
det_names = HexrdConfig().detector_names
all_filters = [HexrdConfig().detector_filter(det) for det in det_names]
return any(filter.thickness > 0 for filter in all_filters)

def update_gui(self):
# Filter info is set per detector
self.ui.detectors.addItems(HexrdConfig().detector_names)
Expand Down Expand Up @@ -79,7 +85,7 @@ def update_gui(self):
self.ui.filter_material.setCurrentText(filter.material)
self.ui.filter_density.setValue(filter.density)
self.ui.filter_thickness.setValue(filter.thickness)
self.ui.apply_filters.setChecked(filter.thickness > 0)
self.ui.apply_filters.setChecked(self.any_detector_filters_applied)
# COATING
if coating.material not in self.mat_options:
self.ui.coating_material_input.setText(coating.material)
Expand Down
2 changes: 1 addition & 1 deletion hexrdgui/create_hedm_instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def create_hedm_instrument():

# Make sure that the physics package is included for instruments
# that expect it
if HexrdConfig().use_physics_package:
if HexrdConfig().has_physics_package:
iconfig['physics_package'] = HexrdConfig().physics_package
if HexrdConfig().apply_absorption_correction:
for det in HexrdConfig().detector_names:
Expand Down
74 changes: 34 additions & 40 deletions hexrdgui/hexrd_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import hexrd.imageseries.save
from hexrd.config.loader import NumPyIncludeLoader
from hexrd.instrument import HEDMInstrument
from hexrd.instrument.constants import PHYSICS_PACKAGE_DEFAULTS
from hexrd.instrument.constants import PHYSICS_PACKAGE_DEFAULTS, PINHOLE_DEFAULTS
from hexrd.instrument.physics_package import HEDPhysicsPackage
from hexrd.material import load_materials_hdf5, save_materials_hdf5, Material
from hexrd.rotations import RotMatEuler
Expand All @@ -27,9 +27,6 @@
from hexrdgui import utils
from hexrdgui.masking.constants import MaskType
from hexrdgui.singletons import QSingleton
from hexrdgui.utils.physics_package import (
ask_to_create_physics_package_if_missing
)

import hexrdgui.resources.calibration
import hexrdgui.resources.indexing
Expand Down Expand Up @@ -329,7 +326,6 @@ def __init__(self):
self._physics_package = None
self._detector_coatings = {}
self._instrument_rigid_body_params = {}
self._use_physics_package = False

# Make sure that the matplotlib font size matches the application
self.font_size = self.font_size
Expand Down Expand Up @@ -435,7 +431,6 @@ def _attributes_to_persist(self):
('_instrument_rigid_body_params', {}),
('recent_state_files', []),
('apply_absorption_correction', False),
('use_physics_package', False),
('physics_package_dictified', None),
('custom_polar_tth_distortion_object_serialized', None),
('detector_coatings_dictified', {}),
Expand Down Expand Up @@ -556,8 +551,6 @@ def load_from_state(self, state):
self.show_all_colormaps = self.show_all_colormaps == 'true'
if not isinstance(self.apply_absorption_correction, bool):
self.apply_absorption_correction = self.apply_absorption_correction == 'true'
if not isinstance(self.use_physics_package, bool):
self.use_physics_package = self.use_physics_package == 'true'

# This is None sometimes. Make sure it is an empty list instead.
if self.recent_state_files is None:
Expand Down Expand Up @@ -708,7 +701,7 @@ def overlays_dictified(self, v):
continue

if overlay_dict.get('tth_distortion_type') is not None:
if not self.use_physics_package:
if not self.has_physics_package:
# We need to create a default physics package
# This is for backward compatibility
self.create_default_physics_package()
Expand Down Expand Up @@ -2440,7 +2433,7 @@ def custom_polar_tth_distortion_object_serialized(self):
def custom_polar_tth_distortion_object_serialized(self, v):
obj = None
if v is not None:
if not self.use_physics_package:
if not self.has_physics_package:
# This requires a physics package to deserialize
self.create_default_physics_package()

Expand Down Expand Up @@ -3049,54 +3042,55 @@ def apply_absorption_correction(self, v):
self._apply_absorption_correction = v
self.deep_rerender_needed.emit()

@property
def use_physics_package(self):
return self._use_physics_package

@use_physics_package.setter
def use_physics_package(self, v):
if v != self.use_physics_package:
self._use_physics_package = v
self.physics_package_modified.emit()
if self.use_physics_package:
self.create_default_physics_package()
else:
self.physics_package = None

@property
def physics_package_dictified(self):
if not self.use_physics_package:
if not self.has_physics_package:
return None

return self.physics_package.serialize()

@physics_package_dictified.setter
def physics_package_dictified(self, v):
self.update_physics_package(**v)
def physics_package_dictified(self, kwargs):
if not kwargs:
self.physics_package = None
return

# Set defaults if missing
kwargs = {
**PHYSICS_PACKAGE_DEFAULTS.HED,
**kwargs,
}
self.physics_package = HEDPhysicsPackage(**kwargs)

def update_physics_package(self, **kwargs):
self.physics_package_dictified = {
**self.physics_package_dictified,
**kwargs,
}

@property
def physics_package(self):
return self._physics_package

@physics_package.setter
def physics_package(self, value):
self._physics_package = value
self.use_physics_package = bool(value is not None)
if value != self._physics_package:
self._physics_package = value
self.physics_package_modified.emit()

def update_physics_package(self, **kwargs):
if self.physics_package is None:
all_kwargs = PHYSICS_PACKAGE_DEFAULTS.HED
all_kwargs.update(**kwargs)
self.physics_package = HEDPhysicsPackage(**all_kwargs)
self.physics_package.deserialize(**kwargs)
self.physics_package_modified.emit()
@property
def has_physics_package(self) -> bool:
return self.physics_package is not None

def create_default_physics_package(self):
self.physics_package = HEDPhysicsPackage(
**PHYSICS_PACKAGE_DEFAULTS.HED)
self.physics_package_modified.emit()
# Our default will be an HED Physics package with a pinhole
self.physics_package_dictified = {
**PHYSICS_PACKAGE_DEFAULTS.HED,
**PINHOLE_DEFAULTS.TARDIS,
}

def absorption_length(self):
if not ask_to_create_physics_package_if_missing():
if not self.has_physics_package:
raise ValueError(
f'Cannot calculate absorption length without physics package')
return self.physics_package.pinhole_absorption_length(
Expand Down
40 changes: 23 additions & 17 deletions hexrdgui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ def setup_connections(self):
self.ui.action_edit_physics_package.triggered.connect(
self.on_action_edit_physics_package_triggered
)
self.ui.action_apply_physics_package.toggled.connect(
self.on_action_apply_physics_package_toggled
self.ui.action_include_physics_package.toggled.connect(
self.on_action_include_physics_package_toggled
)

self.image_mode_widget.polar_show_snip1d.connect(
Expand All @@ -336,6 +336,8 @@ def setup_connections(self):
self.update_mask_region_canvas)
HexrdConfig().update_instrument_toolbox.connect(
self.update_config_gui)
HexrdConfig().physics_package_modified.connect(
self.on_physics_package_modified)

ImageLoadManager().update_needed.connect(self.update_all)
ImageLoadManager().new_images_loaded.connect(self.new_images_loaded)
Expand Down Expand Up @@ -378,6 +380,11 @@ def setup_connections(self):
HexrdConfig().enable_canvas_focus_mode.connect(
self.enable_canvas_focus_mode)

# Always assume Physics Package is needed for LLNL import
self.llnl_import_tool_dialog.ui.complete.clicked.connect(
lambda: self.on_action_include_physics_package_toggled(True)
)

def on_state_loaded(self):
self.update_action_check_states()
self.update_action_enable_states()
Expand All @@ -397,7 +404,7 @@ def update_action_check_states(self):
'action_show_all_colormaps': 'show_all_colormaps',
'action_apply_absorption_correction':
'apply_absorption_correction',
'action_apply_physics_package': 'use_physics_package',
'action_include_physics_package': 'has_physics_package',
}

for cb_name, attr_name in checkbox_to_hexrd_config_mappings.items():
Expand All @@ -407,7 +414,7 @@ def update_action_check_states(self):

def update_action_enable_states(self):
enabled_to_hexrd_config_mappings = {
'action_edit_physics_package': 'use_physics_package',
'action_edit_physics_package': 'has_physics_package',
}

for en_name, attr_name in enabled_to_hexrd_config_mappings.items():
Expand Down Expand Up @@ -1560,10 +1567,6 @@ def on_action_hedm_import_tool_triggered(self):
def on_action_llnl_import_tool_triggered(self):
dialog = self.llnl_import_tool_dialog
dialog.show()
# Always assume Physics Package is needed for LLNL import
dialog.ui.complete.clicked.connect(
lambda: self.on_action_apply_physics_package_toggled(True)
)

def on_action_image_stack_triggered(self):
self.image_stack_dialog.show()
Expand Down Expand Up @@ -1664,24 +1667,27 @@ def on_action_open_preconfigured_instrument_file_triggered(self):
def on_action_edit_physics_package_triggered(self):
self.physics_package_manager_dialog.show()

def on_action_apply_physics_package_toggled(self, b):
def on_action_include_physics_package_toggled(self, b):
self.ui.action_edit_physics_package.setEnabled(b)
HexrdConfig().use_physics_package = b
if b and not HexrdConfig().has_physics_package:
HexrdConfig().create_default_physics_package()

if not b:
# Just turn it off and return
HexrdConfig().physics_package = None
return

# Get the user to select the physics package options
dialog = self.physics_package_manager_dialog
dialog.show()
dialog.show(delete_if_canceled=True)

def _cancel():
# Canceled... uncheck the action.
self.ui.action_apply_physics_package.setChecked(False)
self.ui.action_edit_physics_package.setEnabled(False)
HexrdConfig().use_physics_package = False
def on_physics_package_modified(self):
enable = HexrdConfig().has_physics_package
w = self.ui.action_include_physics_package
with block_signals(w):
w.setChecked(enable)

dialog.ui.rejected.connect(_cancel)
self.ui.action_edit_physics_package.setEnabled(enable)

def action_apply_absorption_correction_toggled(self, b):
if not b:
Expand Down
7 changes: 2 additions & 5 deletions hexrdgui/overlays/powder_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
from hexrdgui.utils.conversions import (
angles_to_cart, angles_to_stereo, cart_to_angles
)
from hexrdgui.utils.physics_package import (
ask_to_create_physics_package_if_missing
)
from hexrdgui.utils.tth_distortion import apply_tth_distortion_if_needed


Expand Down Expand Up @@ -86,9 +83,9 @@ def validate_tth_distortion_kwargs(self):
from hexrdgui.hexrd_config import HexrdConfig

if self.tth_distortion_type is not None:
if not ask_to_create_physics_package_if_missing():
if not HexrdConfig().has_physics_package:
# This will require a physics package
return
HexrdConfig().create_default_physics_package()

if self.tth_distortion_type == 'SampleLayerDistortion':
# We added pinhole_radius later. Set a default if it is missing.
Expand Down
21 changes: 18 additions & 3 deletions hexrdgui/physics_package_manager_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self, parent=None):
self.ui = loader.load_file('physics_package_manager_dialog.ui', parent)
self.additional_materials = {}
self.instrument_type = None
self.delete_if_canceled = False

canvas = FigureCanvas(Figure(tight_layout=True))
# Get the canvas to take up the majority of the screen most of the time
Expand All @@ -36,7 +37,8 @@ def __init__(self, parent=None):
self.update_instrument_type()
self.setup_connections()

def show(self):
def show(self, delete_if_canceled=False):
self.delete_if_canceled = delete_if_canceled
self.setup_form()
self.ui.show()

Expand Down Expand Up @@ -78,6 +80,18 @@ def setup_connections(self):
HexrdConfig().detectors_changed.connect(
self.initialize_detector_coatings)

self.ui.accepted.connect(self.on_accepted)
self.ui.rejected.connect(self.on_rejected)

def on_accepted(self):
self.delete_if_canceled = False

def on_rejected(self):
if self.delete_if_canceled:
HexrdConfig().physics_package = None

self.delete_if_canceled = False

def initialize_detector_coatings(self):
# Reset detector coatings to make sure they're in sync w/ current dets
HexrdConfig().detector_coatings_dictified = {}
Expand Down Expand Up @@ -133,7 +147,7 @@ def setup_form(self):
w.insertSeparator(2 + len(custom_mats))

# Set default values
if not HexrdConfig().use_physics_package:
if not HexrdConfig().has_physics_package:
return

physics = HexrdConfig().physics_package
Expand Down Expand Up @@ -180,6 +194,7 @@ def toggle_pinhole(self, enabled):

def material_changed(self, index, category):
material = self.material_selectors[category].currentText()

self.material_inputs[category].setEnabled(index == 0)
self.density_inputs[category].setEnabled(index == 0)
if category == 'pinhole':
Expand All @@ -196,7 +211,7 @@ def material_changed(self, index, category):
else:
self.density_inputs[category].setValue(0.0)

if HexrdConfig().use_physics_package:
if HexrdConfig().has_physics_package:
self.ui.absorption_length.setValue(HexrdConfig().absorption_length())

def accept_changes(self):
Expand Down
11 changes: 6 additions & 5 deletions hexrdgui/pinhole_correction_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ def correction_kwargs(self, v):
if v is None:
return

if not HexrdConfig().has_physics_package:
return

physics = HexrdConfig().physics_package

vp = v.copy()
# These units are in mm, but we display in micrometers
for key, value in v.items():
Expand All @@ -164,10 +169,6 @@ def correction_kwargs(self, v):

vp[key] = value * multiplier

if not ask_to_create_physics_package_if_missing():
return
physics = HexrdConfig().physics_package

# Values are (key, default)
values = {
'sample_layer_standoff': ('layer_standoff',
Expand Down Expand Up @@ -408,7 +409,7 @@ def enter_manually_idx(self):
return 0

def on_rygg_absorption_length_selector_changed(self):
if not ask_to_create_physics_package_if_missing():
if not HexrdConfig().has_physics_package:
# Cannot update
return

Expand Down
Loading

0 comments on commit d363b6d

Please sign in to comment.