Skip to content

Commit

Permalink
Do not limit the physics package to TARDIS and PXRDIP
Browse files Browse the repository at this point in the history
- Whether or not the Physics Package is used is now indicated by a separate
  flag: "use_physics_package".
- Users can only edit the physics package if "use_physics_package" is true
- If "apply physics package" is selected the package manager dialog will
  automatically be displayed
- While the physics package is no longer limited to PXRDIP and TARDIS it is
  still automatically applied after using the LLNL Import tool.

Signed-off-by: Brianna Major <brianna.major@taloid.khq.kitware.com>
  • Loading branch information
Brianna Major committed Jan 9, 2025
1 parent 0538311 commit 0098a81
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 49 deletions.
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().physics_package is not None:
if HexrdConfig().use_physics_package:
iconfig['physics_package'] = HexrdConfig().physics_package
if HexrdConfig().apply_absorption_correction:
for det in HexrdConfig().detector_names:
Expand Down
33 changes: 20 additions & 13 deletions hexrdgui/hexrd_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from hexrdgui import utils
from hexrdgui.masking.constants import MaskType
from hexrdgui.singletons import QSingleton
from hexrdgui.utils.guess_instrument_type import guess_instrument_type
from hexrdgui.utils.physics_package import ask_to_create_physics_package_if_missing

import hexrdgui.resources.calibration
Expand Down Expand Up @@ -328,6 +327,7 @@ 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 @@ -433,6 +433,7 @@ 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 @@ -553,6 +554,8 @@ 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 @@ -703,7 +706,7 @@ def overlays_dictified(self, v):
continue

if overlay_dict.get('tth_distortion_type') is not None:
if self.physics_package is None:
if not self.use_physics_package:
# We need to create a default physics package
# This is for backward compatibility
self.create_default_physics_package()
Expand Down Expand Up @@ -2435,7 +2438,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 self.physics_package is None:
if not self.use_physics_package:
# This requires a physics package to deserialize
self.create_default_physics_package()

Expand Down Expand Up @@ -3044,16 +3047,23 @@ 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):
self._use_physics_package = v

@property
def physics_package_dictified(self):
if self.physics_package is None:
if not self.use_physics_package:
return None
return self.physics_package.serialize()

@physics_package_dictified.setter
def physics_package_dictified(self, v):
instr_type = guess_instrument_type(self.detector_names)
self.update_physics_package(instr_type, **v)
self.update_physics_package(**v)

@property
def physics_package(self):
Expand All @@ -3062,14 +3072,10 @@ def physics_package(self):
@physics_package.setter
def physics_package(self, value):
self._physics_package = value
self.use_physics_package = bool(value is None)

def update_physics_package(self, instr_type=None, **kwargs):
if instr_type not in ('TARDIS', 'PXRDIP'):
raise ValueError(
f'Expected physics package instrument type to be either '
f'"TARDIS" or "PXRDIP", got {instr_type} instead.'
)
if self.physics_package is None:
def update_physics_package(self, **kwargs):
if not self.use_physics_package:
all_kwargs = PHYSICS_PACKAGE_DEFAULTS.HED
all_kwargs.update(**kwargs)
self.physics_package = HEDPhysicsPackage(**all_kwargs)
Expand All @@ -3078,6 +3084,7 @@ def update_physics_package(self, instr_type=None, **kwargs):
self.physics_package_modified.emit()

def create_default_physics_package(self):
self.use_physics_package = True
self.physics_package = HEDPhysicsPackage(
**PHYSICS_PACKAGE_DEFAULTS.HED)
self.physics_package_modified.emit()
Expand Down
1 change: 1 addition & 0 deletions hexrdgui/llnl_import_tool_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ def completed(self):
self.ui.instrument.setDisabled(False)
HexrdConfig().enable_canvas_toolbar.emit(True)
self.cmap.block_updates(False)
HexrdConfig().use_physics_package = True
self.physics_package_manager.show()

def show(self):
Expand Down
46 changes: 27 additions & 19 deletions hexrdgui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
from hexrdgui.ui_loader import UiLoader
from hexrdgui.utils import block_signals, unique_name
from hexrdgui.utils.dialog import add_help_url
from hexrdgui.utils.guess_instrument_type import guess_instrument_type
from hexrdgui.utils.physics_package import (
ask_to_create_physics_package_if_missing,
)
Expand Down Expand Up @@ -170,7 +169,6 @@ def __init__(self, parent=None, image_files=None):
self.setup_connections()

self.update_config_gui()
self.update_physics_package_visibilities()

self.update_action_check_states()

Expand Down Expand Up @@ -306,8 +304,11 @@ def setup_connections(self):
self.on_action_edit_apply_threshold_triggered)
self.ui.action_open_preconfigured_instrument_file.triggered.connect(
self.on_action_open_preconfigured_instrument_file_triggered)
self.ui.action_physics_package_editor.triggered.connect(
self.on_action_physics_package_editor_triggered
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.image_mode_widget.polar_show_snip1d.connect(
Expand Down Expand Up @@ -394,6 +395,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',
}

for cb_name, attr_name in checkbox_to_hexrd_config_mappings.items():
Expand Down Expand Up @@ -441,7 +443,6 @@ def enable_canvas_focus_mode(self, b):

def on_instrument_config_loaded(self):
self.update_config_gui()
self.update_physics_package_visibilities()

def on_action_open_config_file_triggered(self):
selected_file, selected_filter = QFileDialog.getOpenFileName(
Expand Down Expand Up @@ -471,19 +472,6 @@ def on_action_save_config_hexrd_triggered(self):
def on_action_save_config_yaml_triggered(self):
self._save_config('.yml', 'YAML files (*.yml)')

def update_physics_package_visibilities(self):
instr_type = guess_instrument_type(HexrdConfig().detector_names)
visible = instr_type in ('TARDIS', 'PXRDIP')

self.ui.action_physics_package_editor.setVisible(visible)

if not visible:
# Remove the physics package
HexrdConfig().physics_package = None

# Turn off all detector coatings
HexrdConfig().detector_coatings_dictified = {}

def open_grain_fitting_results(self):
selected_file, _ = QFileDialog.getOpenFileName(
self.ui, 'Open Grain Fitting File', HexrdConfig().working_dir,
Expand Down Expand Up @@ -1656,9 +1644,29 @@ def on_action_open_preconfigured_instrument_file_triggered(self):
with resource_loader.resource_path(instrument_templates, fname) as f:
HexrdConfig().load_instrument_config(Path(f))

def on_action_physics_package_editor_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):
self.ui.action_edit_physics_package.setEnabled(b)
if not b:
# Just turn it off and return
HexrdConfig().use_physics_package = b
return

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

dialog.ui.rejected.connect(
# Canceled... uncheck the action.
lambda: self.ui.action_apply_physics_package.setChecked(False)
)

# The user should have modified HexrdConfig's physics
# package options already. Just apply it now.
HexrdConfig().use_physics_package = b

def action_apply_absorption_correction_toggled(self, b):
if not b:
# Just turn it off and return
Expand Down
4 changes: 3 additions & 1 deletion hexrdgui/overlays/powder_overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
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.physics_package import (
ask_to_create_physics_package_if_missing
)
from hexrdgui.utils.tth_distortion import apply_tth_distortion_if_needed


Expand Down
23 changes: 9 additions & 14 deletions hexrdgui/physics_package_manager_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def setup_connections(self):
lambda index, k=k: self.material_changed(index, k))
HexrdConfig().instrument_config_loaded.connect(
self.update_instrument_type)
HexrdConfig().detectors_changed(self.initialize_detector_coatings)
HexrdConfig().detectors_changed.connect(self.initialize_detector_coatings)

def initialize_detector_coatings(self):
# Reset detector coatings to make sure they're in sync w/ current dets
Expand Down Expand Up @@ -103,27 +103,22 @@ def load_additional_materials(self):

def update_instrument_type(self):
new_instr_type = guess_instrument_type(HexrdConfig().detector_names)
if (
new_instr_type == self.instrument_type or
new_instr_type not in ('TARDIS', 'PXRDIP')
):
if new_instr_type == self.instrument_type:
return

self.initialize_detector_coatings()
if new_instr_type == 'TARDIS':
HexrdConfig().update_physics_package(
new_instr_type, **PINHOLE_DEFAULTS.TARDIS)
HexrdConfig().update_physics_package(**PINHOLE_DEFAULTS.TARDIS)
for det in HexrdConfig().detector_names:
HexrdConfig().update_detector_filter(
det, **FILTER_DEFAULTS.TARDIS)
elif new_instr_type == 'PXRDIP':
HexrdConfig().update_physics_package(
new_instr_type, **PINHOLE_DEFAULTS.PXRDIP)
HexrdConfig().update_physics_package(**PINHOLE_DEFAULTS.PXRDIP)
for det in HexrdConfig().detector_names:
HexrdConfig().update_detector_filter(
det, **FILTER_DEFAULTS.PXRDIP)
else:
HexrdConfig().physics_package = None
HexrdConfig().create_default_physics_package()
self.instrument_type = new_instr_type

def setup_form(self):
Expand All @@ -137,10 +132,10 @@ def setup_form(self):
w.insertSeparator(2 + len(custom_mats))

# Set default values
physics = HexrdConfig().physics_package
if physics is None:
if not HexrdConfig().use_physics_package:
return

physics = HexrdConfig().physics_package
# PINHOLE
self.ui.pinhole_material.setCurrentText(physics.pinhole_material)
self.ui.pinhole_density.setValue(physics.pinhole_density)
Expand Down Expand Up @@ -200,7 +195,7 @@ def material_changed(self, index, category):
else:
self.density_inputs[category].setValue(0.0)

if HexrdConfig().physics_package is not None:
if HexrdConfig().use_physics_package:
self.ui.absorption_length.setValue(HexrdConfig().absorption_length())

def accept_changes(self):
Expand All @@ -223,7 +218,7 @@ def accept_changes(self):
'pinhole_thickness': self.ui.pinhole_thickness.value(),
'pinhole_density': self.ui.pinhole_density.value(),
}
HexrdConfig().update_physics_package(self.instrument_type, **kwargs)
HexrdConfig().update_physics_package(**kwargs)

if HexrdConfig().apply_absorption_correction:
# Make sure changes are reflected
Expand Down
3 changes: 3 additions & 0 deletions hexrdgui/resources/ui/main_window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,9 @@
</property>
</action>
<action name="action_edit_physics_package">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Edit Physics Package</string>
</property>
Expand Down
2 changes: 1 addition & 1 deletion hexrdgui/utils/physics_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def ask_to_create_physics_package_if_missing() -> bool:
from hexrdgui.hexrd_config import HexrdConfig # Avoid circular import

if HexrdConfig().physics_package is not None:
if HexrdConfig().use_physics_package:
return True

msg = (
Expand Down

0 comments on commit 0098a81

Please sign in to comment.