Skip to content

Commit

Permalink
Re #10712 Added component choice to beam centre finder
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Andrew committed May 9, 2018
1 parent 02c505e commit 6228d9c
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ def PyExec(self):

instrument_file = get_instrument_paths_for_sans_file(state.data.sample_scatter)
position_1_step = get_named_elements_from_ipf_file(
instrument_file[1], "centre-finder-step-size", float)['centre-finder-step-size']
instrument_file[1], ["centre-finder-step-size"], float)['centre-finder-step-size']
try:
position_2_step = get_named_elements_from_ipf_file(
instrument_file[1], "centre-finder-step-size2", float)['centre-finder-step-size2']
instrument_file[1], ["centre-finder-step-size2"], float)['centre-finder-step-size2']
except:
position_2_step = position_1_step

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import (absolute_import, division, print_function)
from mantid.api import (DataProcessorAlgorithm, MatrixWorkspaceProperty, AlgorithmFactory, PropertyMode, Progress, IEventWorkspace)
from mantid.kernel import (Direction, PropertyManagerProperty)
from mantid.kernel import (Direction, PropertyManagerProperty, StringListValidator)
from sans.common.constants import EMPTY_NAME
from sans.common.general_functions import create_child_algorithm, append_to_sans_file_tag
from sans.state.state_base import create_deserialized_sans_state_from_property_manager
Expand Down Expand Up @@ -43,6 +43,12 @@ def PyInit(self):

self.declareProperty('Iterations', 10, direction=Direction.Input)

allowed_detectors = StringListValidator([DetectorType.to_string(DetectorType.LAB),
DetectorType.to_string(DetectorType.HAB)])
self.declareProperty("Component", DetectorType.to_string(DetectorType.LAB),
validator=allowed_detectors, direction=Direction.Input,
doc="The component of the instrument which is to be reduced.")

def PyExec(self):
# --------
# Clone the input workspaces
Expand All @@ -58,15 +64,17 @@ def PyExec(self):
if state.mask.phi_max:
state.mask.phi_max = 0.0

component = self.getProperty("Component").value
component_as_string = DetectorType.to_string(component)

# Set test centre
state.move.detectors[DetectorType.to_string(DetectorType.LAB)].sample_centre_pos1 = self.getProperty(
state.move.detectors[component_as_string].sample_centre_pos1 = self.getProperty(
"Centre1").value
state.move.detectors[DetectorType.to_string(DetectorType.LAB)].sample_centre_pos2 = self.getProperty(
state.move.detectors[component_as_string].sample_centre_pos2 = self.getProperty(
"Centre2").value

state_serialized = state.property_manager

component_as_string = 'LAB'
progress = self._get_progress()

# --------------------------------------------------------------------------------------------------------------
Expand Down
64 changes: 64 additions & 0 deletions scripts/Interface/ui/sans_isis/beam_centre.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from six import with_metaclass
import ui_beam_centre
from mantidqtpython import MantidQt
from sans.gui_logic.gui_common import get_detector_from_gui_selection, \
get_detector_strings_for_gui, get_string_for_gui_from_reduction_mode

try:
_fromUtf8 = QtCore.QString.fromUtf8
Expand All @@ -27,6 +29,7 @@ def __init__(self, parent=None):
super(BeamCentre, self).__init__(parent)
self.setupUi(self)
self._setup_log_widget()
self.instrument = None

# Hook up signal and slots
self.connect_signals()
Expand Down Expand Up @@ -83,6 +86,19 @@ def _attach_validators(self):
self.max_iterations_line_edit.setValidator(positive_integer_validator)
self.tolerance_line_edit.setValidator(positive_double_validator)

def on_update_instrument(self, instrument):
self.instrument = instrument
component_list = get_detector_strings_for_gui(self.instrument)
self.set_component_options(component_list)
if len(component_list) < 2:
self.hab_pos_1_line_edit.setEnabled(False)
self.hab_pos_2_line_edit.setEnabled(False)
self.update_hab_check_box.setEnabled(False)
else:
self.hab_pos_1_line_edit.setEnabled(True)
self.hab_pos_2_line_edit.setEnabled(True)
self.update_hab_check_box.setEnabled(True)

# ------------------------------------------------------------------------------------------------------------------
# Actions
# ------------------------------------------------------------------------------------------------------------------
Expand All @@ -97,6 +113,9 @@ def set_options(self, options):
self.COM = options.COM
self.q_min = options.q_min
self.q_max = options.q_max
self.component = options.component
self.update_lab = options.update_lab
self.update_hab = options.update_hab

def update_simple_line_edit_field(self, line_edit, value):
gui_element = getattr(self, line_edit)
Expand Down Expand Up @@ -229,3 +248,48 @@ def hab_pos_2(self):
@hab_pos_2.setter
def hab_pos_2(self, value):
self.update_simple_line_edit_field(line_edit="hab_pos_2_line_edit", value=value)

@property
def component(self):
component_as_string = self.component_combo_box.currentText()
return get_detector_from_gui_selection(component_as_string)

@component.setter
def component(self, value):
# There are two types of values that can be passed:
# String: we look for string and we set it
# Convert the value to the correct GUI string

# Set the correct selection of reduction modes which are available
component_list = get_detector_strings_for_gui(self.instrument)
self.set_component_options(component_list)

component_as_string = get_string_for_gui_from_reduction_mode(value, self.instrument)
if component_as_string:
index = self.reduction_mode_combo_box.findText(component_as_string)
if index != -1:
self.component_combo_box.setCurrentIndex(index)

def set_component_options(self, component_list):
current_index = self.component_combo_box.currentIndex()
self.component_combo_box.clear()
for element in component_list:
self.component_combo_box.addItem(element)
if current_index != -1:
self.component_combo_box.setCurrentIndex(current_index)

@property
def update_hab(self):
return self.update_hab_check_box.isChecked()

@update_hab.setter
def update_hab(self, value):
self.update_hab_check_box.setChecked(value)

@property
def update_lab(self):
return self.update_lab_check_box.isChecked()

@update_lab.setter
def update_lab(self, value):
self.update_lab_check_box.setChecked(value)
42 changes: 33 additions & 9 deletions scripts/Interface/ui/sans_isis/beam_centre.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>1015</width>
<height>527</height>
<height>649</height>
</rect>
</property>
<property name="windowTitle">
Expand All @@ -29,18 +29,28 @@
<string>Centre Position</string>
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="3" column="0">
<widget class="QLineEdit" name="hab_pos_1_line_edit"/>
</item>
<item row="1" column="0">
<widget class="QLineEdit" name="lab_pos_1_line_edit"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="lab_centre_label">
<property name="text">
<string>Centre Position LAB</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLineEdit" name="lab_pos_1_line_edit"/>
<item row="3" column="1">
<widget class="QLineEdit" name="hab_pos_2_line_edit"/>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="lab_pos_2_line_edit"/>
<item row="4" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Detector</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="hab_centre_label">
Expand All @@ -49,11 +59,25 @@
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLineEdit" name="hab_pos_1_line_edit"/>
<item row="1" column="1">
<widget class="QLineEdit" name="lab_pos_2_line_edit"/>
</item>
<item row="3" column="1">
<widget class="QLineEdit" name="hab_pos_2_line_edit"/>
<item row="5" column="0">
<widget class="QComboBox" name="component_combo_box"/>
</item>
<item row="4" column="1">
<widget class="QCheckBox" name="update_lab_check_box">
<property name="text">
<string>Update LAB</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QCheckBox" name="update_hab_check_box">
<property name="text">
<string>Update HAB</string>
</property>
</widget>
</item>
</layout>
</widget>
Expand Down
12 changes: 6 additions & 6 deletions scripts/SANS/sans/algorithm_detail/centre_finder_new.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import (absolute_import, division, print_function)

from sans.common.general_functions import create_managed_non_child_algorithm
from sans.common.enums import (SANSDataType, FindDirectionEnum)
from sans.common.enums import (SANSDataType, FindDirectionEnum, DetectorType)
from sans.algorithm_detail.batch_execution import (provide_loaded_data, get_reduction_packages)
from mantid.simpleapi import CreateEmptyTableWorkspace

Expand All @@ -10,7 +10,7 @@
# Functions for the execution of a single batch iteration
# ----------------------------------------------------------------------------------------------------------------------
def centre_finder_new(state, r_min = 0.06, r_max = 0.26, iterations = 10, position_1_start = 0.0, position_2_start = 0.0
, tolerance = 0.0001251, find_direction = FindDirectionEnum.All, verbose=False):
, tolerance = 0.0001251, find_direction = FindDirectionEnum.All, verbose=False, component=DetectorType.LAB):
"""
Finds the beam centre from a good initial guess.
Expand Down Expand Up @@ -48,15 +48,15 @@ def centre_finder_new(state, r_min = 0.06, r_max = 0.26, iterations = 10, positi
# ------------------------------------------------------------------------------------------------------------------
reduction_packages = get_reduction_packages(state, workspaces, monitors)
reduction_package = reduction_packages[0]

# ------------------------------------------------------------------------------------------------------------------
# Setup the beam centre finder algorithm.
# ------------------------------------------------------------------------------------------------------------------
beam_centre_finder = "SANSBeamCentreFinder"
beam_centre_finder_options = {"Iterations": iterations, "RMin": r_min/1000, "RMax": r_max/1000,
"Position1Start": position_1_start, "Position2Start": position_2_start,
"Tolerance": tolerance, "Direction" : FindDirectionEnum.to_string(find_direction),
"Verbose": verbose}
"Verbose": verbose, "Component": DetectorType.to_string(component)}
beam_centre_alg = create_managed_non_child_algorithm(beam_centre_finder, **beam_centre_finder_options)
beam_centre_alg.setChild(False)
set_properties_for_beam_centre_algorithm(beam_centre_alg, reduction_package,
Expand All @@ -76,7 +76,7 @@ def centre_finder_new(state, r_min = 0.06, r_max = 0.26, iterations = 10, positi


def centre_finder_mass(state, r_min = 0.06, max_iter=10, position_1_start = 0.0, position_2_start = 0.0,
tolerance = 0.0001251):
tolerance = 0.0001251, component=DetectorType.LAB):
"""
Finds the beam centre from an initial guess.
Expand Down Expand Up @@ -110,7 +110,7 @@ def centre_finder_mass(state, r_min = 0.06, max_iter=10, position_1_start = 0.0,
# ------------------------------------------------------------------------------------------------------------------
beam_centre_finder = "SANSBeamCentreFinderMassMethod"
beam_centre_finder_options = {"RMin": r_min/1000, "Centre1": position_1_start,
"Centre2": position_2_start, "Tolerance": tolerance}
"Centre2": position_2_start, "Tolerance": tolerance, "Component": DetectorType.to_string(component)}
beam_centre_alg = create_managed_non_child_algorithm(beam_centre_finder, **beam_centre_finder_options)
beam_centre_alg.setChild(False)

Expand Down
55 changes: 45 additions & 10 deletions scripts/SANS/sans/gui_logic/models/beam_centre_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from sans.common.enums import (SANSInstrument, FindDirectionEnum)
from sans.common.enums import (SANSInstrument, FindDirectionEnum, DetectorType)
from mantid.kernel import (Logger)
from sans.common.file_information import get_instrument_paths_for_sans_file
from sans.common.xml_parsing import get_named_elements_from_ipf_file


class BeamCentreModel(object):
Expand All @@ -11,10 +13,19 @@ def __init__(self, SANSCentreFinder):
def __eq__(self, other):
return self.__dict__ == other.__dict__

def reset_to_defaults_for_instrument(self, instrument = None):
def reset_to_defaults_for_instrument(self, state_data = None):
r_range = {}
instrument = None

if state_data:
instrument_file_path = get_instrument_paths_for_sans_file(state_data.sample_scatter)
r_range = get_named_elements_from_ipf_file(instrument_file_path[1],
["beam_centre_radius_min", "beam_centre_radius_max"], float)
instrument = state_data.instrument

self._max_iterations = 10
self._r_min = 60
self._r_max = 280
self._r_min = r_range["beam_centre_radius_min"] if "beam_centre_radius_min" in r_range else 60
self._r_max = r_range["beam_centre_radius_max"] if "beam_centre_radius_max" in r_range else 280
self._left_right = True
self._up_down = True
self._tolerance = 0.000125
Expand All @@ -28,9 +39,9 @@ def reset_to_defaults_for_instrument(self, instrument = None):
self.verbose = False
self.q_min = 0.01
self.q_max = 0.1

if instrument == SANSInstrument.LOQ:
self.r_max = 200
self._component = DetectorType.LAB
self.update_lab = True
self.update_hab = True

if instrument == SANSInstrument.LARMOR:
self.scale_1 = 1.0
Expand Down Expand Up @@ -72,20 +83,20 @@ def find_beam_centre(self, state):
max_iter=self.max_iterations,
x_start=self.lab_pos_1, y_start=self.lab_pos_2,
tolerance=self.tolerance,
find_direction=find_direction, reduction_method=False)
find_direction=find_direction, reduction_method=False, component=self.component)

centre = centre_finder(state, r_min=self.r_min, r_max=self.r_max,
max_iter=self.max_iterations,
x_start=centre['pos1'], y_start=centre['pos2'],
tolerance=self.tolerance,
find_direction=find_direction, reduction_method=True,
verbose=self.verbose)
verbose=self.verbose, component=self.component)
else:
centre = centre_finder(state, r_min=self.r_min, r_max=self.r_max,
max_iter=self.max_iterations, x_start=self.lab_pos_1,
y_start=self.lab_pos_2, tolerance=self.tolerance,
find_direction=find_direction, reduction_method=True,
verbose=self.verbose)
verbose=self.verbose, component=self.component)
return centre

@property
Expand Down Expand Up @@ -199,3 +210,27 @@ def hab_pos_2(self):
@hab_pos_2.setter
def hab_pos_2(self, value):
self._hab_pos_2 = value

@property
def component(self):
return self._component

@component.setter
def component(self, value):
self._component = value

@property
def update_hab(self):
return self._update_hab

@update_hab.setter
def update_hab(self, value):
self._update_hab = value

@property
def update_lab(self):
return self._update_lab

@update_lab.setter
def update_lab(self, value):
self._update_lab = value

0 comments on commit 6228d9c

Please sign in to comment.