def PyInit(self): # Workspace which is to be masked self.declareProperty(MatrixWorkspaceProperty("InputWorkspace", '', optional=PropertyMode.Mandatory, direction=Direction.Input), doc='The workspace which is to be converted to wavelength') self.declareProperty('WavelengthLow', defaultValue=Property.EMPTY_DBL, direction=Direction.Input, doc='The low value of the wavelength binning.') self.declareProperty('WavelengthHigh', defaultValue=Property.EMPTY_DBL, direction=Direction.Input, doc='The high value of the wavelength binning.') self.declareProperty('WavelengthStep', defaultValue=Property.EMPTY_DBL, direction=Direction.Input, doc='The step size of the wavelength binning.') # Step type allowed_step_types = StringListValidator([RangeStepType.to_string(RangeStepType.Log), RangeStepType.to_string(RangeStepType.Lin)]) self.declareProperty('WavelengthStepType', RangeStepType.to_string(RangeStepType.Lin), validator=allowed_step_types, direction=Direction.Input, doc='The step type for rebinning.') # Rebin type allowed_rebin_methods = StringListValidator([RebinType.to_string(RebinType.Rebin), RebinType.to_string(RebinType.InterpolatingRebin)]) self.declareProperty("RebinMode", RebinType.to_string(RebinType.Rebin), validator=allowed_rebin_methods, direction=Direction.Input, doc="The method which is to be applied to the rebinning.") self.declareProperty(MatrixWorkspaceProperty('OutputWorkspace', '', optional=PropertyMode.Mandatory, direction=Direction.Output), doc='The output workspace.')
def PyInit(self): # Workspace which is to be masked self.declareProperty( MatrixWorkspaceProperty("InputWorkspace", '', optional=PropertyMode.Mandatory, direction=Direction.Input), doc='The workspace which is to be converted to wavelength') self.declareProperty('WavelengthLow', defaultValue=Property.EMPTY_DBL, direction=Direction.Input, doc='The low value of the wavelength binning.') self.declareProperty('WavelengthHigh', defaultValue=Property.EMPTY_DBL, direction=Direction.Input, doc='The high value of the wavelength binning.') self.declareProperty('WavelengthStep', defaultValue=Property.EMPTY_DBL, direction=Direction.Input, doc='The step size of the wavelength binning.') # Step type allowed_step_types = StringListValidator([ RangeStepType.to_string(RangeStepType.Log), RangeStepType.to_string(RangeStepType.Lin) ]) self.declareProperty('WavelengthStepType', RangeStepType.to_string(RangeStepType.Lin), validator=allowed_step_types, direction=Direction.Input, doc='The step type for rebinning.') # Rebin type allowed_rebin_methods = StringListValidator([ RebinType.to_string(RebinType.Rebin), RebinType.to_string(RebinType.InterpolatingRebin) ]) self.declareProperty( "RebinMode", RebinType.to_string(RebinType.Rebin), validator=allowed_rebin_methods, direction=Direction.Input, doc="The method which is to be applied to the rebinning.") self.declareProperty(MatrixWorkspaceProperty( 'OutputWorkspace', '', optional=PropertyMode.Mandatory, direction=Direction.Output), doc='The output workspace.')
def _convert_to_wavelength(self, workspace, normalize_to_monitor_state): """ Converts the workspace from time-of-flight units to wavelength units :param workspace: a time-of-flight workspace. :param normalize_to_monitor_state: a SANSStateNormalizeToMonitor object. :return: a wavelength workspace. """ wavelength_low = normalize_to_monitor_state.wavelength_low wavelength_high = normalize_to_monitor_state.wavelength_high wavelength_step = normalize_to_monitor_state.wavelength_step wavelength_step_type = normalize_to_monitor_state.wavelength_step_type wavelength_rebin_mode = normalize_to_monitor_state.rebin_type convert_name = "SANSConvertToWavelengthAndRebin" convert_options = {"InputWorkspace": workspace, "WavelengthLow": wavelength_low, "WavelengthHigh": wavelength_high, "WavelengthStep": wavelength_step, "WavelengthStepType": RangeStepType.to_string(wavelength_step_type), "RebinMode": RebinType.to_string(wavelength_rebin_mode)} convert_alg = create_unmanaged_algorithm(convert_name, **convert_options) convert_alg.setPropertyValue("OutputWorkspace", EMPTY_NAME) convert_alg.setProperty("OutputWorkspace", workspace) convert_alg.execute() return convert_alg.getProperty("OutputWorkspace").value
def PyExec(self): # State state_property_manager = self.getProperty("SANSState").value state = create_deserialized_sans_state_from_property_manager( state_property_manager) wavelength_state = state.wavelength # Input workspace workspace = get_input_workspace_as_copy_if_not_same_as_output_workspace( self) wavelength_name = "SANSConvertToWavelengthAndRebin" wavelength_options = { "InputWorkspace": workspace, "WavelengthLow": wavelength_state.wavelength_low, "WavelengthHigh": wavelength_state.wavelength_high, "WavelengthStep": wavelength_state.wavelength_step, "WavelengthStepType": RangeStepType.to_string(wavelength_state.wavelength_step_type), "RebinMode": RebinType.to_string(wavelength_state.rebin_type) } wavelength_alg = create_unmanaged_algorithm(wavelength_name, **wavelength_options) wavelength_alg.setPropertyValue("OutputWorkspace", EMPTY_NAME) wavelength_alg.setProperty("OutputWorkspace", workspace) wavelength_alg.execute() converted_workspace = wavelength_alg.getProperty( "OutputWorkspace").value append_to_sans_file_tag(converted_workspace, "_wavelength") self.setProperty("OutputWorkspace", converted_workspace)
def test_that_event_workspace_with_conversion_is_still_event_workspace( self): workspace = provide_workspace(is_event=True) convert_options = { "InputWorkspace": workspace, "OutputWorkspace": EMPTY_NAME, "RebinMode": "Rebin", "WavelengthLow": 1.0, "WavelengthHigh": 10.0, "WavelengthStep": 1.0, "WavelengthStepType": RangeStepType.to_string(RangeStepType.Lin) } convert_alg = create_unmanaged_algorithm( "SANSConvertToWavelengthAndRebin", **convert_options) convert_alg.execute() self.assertTrue(convert_alg.isExecuted()) output_workspace = convert_alg.getProperty("OutputWorkspace").value self.assertTrue(isinstance(output_workspace, EventWorkspace)) # Check the rebinning part data_x0 = output_workspace.dataX(0) self.assertTrue(len(data_x0) == 10) self.assertTrue(data_x0[0] == 1.0) self.assertTrue(data_x0[-1] == 10.0) # Check the units part axis0 = output_workspace.getAxis(0) unit = axis0.getUnit() self.assertTrue(unit.unitID() == "Wavelength")
def _convert_to_wavelength(self, workspace, normalize_to_monitor_state): """ Converts the workspace from time-of-flight units to wavelength units :param workspace: a time-of-flight workspace. :param normalize_to_monitor_state: a SANSStateNormalizeToMonitor object. :return: a wavelength workspace. """ wavelength_low = normalize_to_monitor_state.wavelength_low wavelength_high = normalize_to_monitor_state.wavelength_high wavelength_step = normalize_to_monitor_state.wavelength_step wavelength_step_type = normalize_to_monitor_state.wavelength_step_type wavelength_rebin_mode = normalize_to_monitor_state.rebin_type convert_name = "SANSConvertToWavelengthAndRebin" convert_options = { "InputWorkspace": workspace, "WavelengthLow": wavelength_low, "WavelengthHigh": wavelength_high, "WavelengthStep": wavelength_step, "WavelengthStepType": RangeStepType.to_string(wavelength_step_type), "RebinMode": RebinType.to_string(wavelength_rebin_mode) } convert_alg = create_unmanaged_algorithm(convert_name, **convert_options) convert_alg.setPropertyValue("OutputWorkspace", EMPTY_NAME) convert_alg.setProperty("OutputWorkspace", workspace) convert_alg.execute() return convert_alg.getProperty("OutputWorkspace").value
def test_binning_commands_parsed(self): # Wavelength for bin_type in ["Lin", "Log"]: wavelength_dict = {"binning": {"wavelength": {"start": 1.1, "step": 0.1, "stop": 2.2, "type": bin_type}}} wavelength = self._setup_parser(wavelength_dict).get_state_wavelength() self.assertEqual((1.1, 2.2), wavelength.wavelength_interval.wavelength_full_range) self.assertEqual(0.1, wavelength.wavelength_interval.wavelength_step) self.assertEqual(RangeStepType(bin_type), wavelength.wavelength_step_type) one_d_reduction_q_dict = {"binning": {"1d_reduction": {"binning": "1.0, 0.1, 2.0, -0.2, 3.0", "radius_cut": 12.3, "wavelength_cut": 23.4}}} one_d_convert_to_q = self._setup_parser(one_d_reduction_q_dict).get_state_convert_to_q() self.assertEqual(1.0, one_d_convert_to_q.q_min) self.assertEqual(3.0, one_d_convert_to_q.q_max) self.assertEqual("1.0, 0.1, 2.0, -0.2, 3.0", one_d_convert_to_q.q_1d_rebin_string.strip()) self.assertEqual(12.3, one_d_convert_to_q.radius_cutoff) self.assertEqual(23.4, one_d_convert_to_q.wavelength_cutoff) two_d_reduction_q_dict = {"binning": {"2d_reduction": {"step": 1.0, "stop": 5.0, "type": "Lin", "interpolate": True}}} results = self._setup_parser(two_d_reduction_q_dict) two_d_convert_to_q = results.get_state_convert_to_q() self.assertEqual(5.0, two_d_convert_to_q.q_xy_max) self.assertEqual(1.0, two_d_convert_to_q.q_xy_step) self.assertTrue(two_d_convert_to_q.q_xy_step_type is RangeStepType.LIN) self.assertTrue(results.get_state_calculate_transmission().rebin_type is RebinType.INTERPOLATING_REBIN)
def test_that_not_setting_upper_bound_takes_it_from_original_value(self): workspace = provide_workspace(is_event=True) convert_options = { "InputWorkspace": workspace, "OutputWorkspace": EMPTY_NAME, "RebinMode": "Rebin", "WavelengthLow": 1.0, "WavelengthStep": 1.0, "WavelengthStepType": RangeStepType.to_string(RangeStepType.Lin) } convert_alg = create_unmanaged_algorithm( "SANSConvertToWavelengthAndRebin", **convert_options) convert_alg.execute() self.assertTrue(convert_alg.isExecuted()) output_workspace = convert_alg.getProperty("OutputWorkspace").value self.assertTrue(isinstance(output_workspace, EventWorkspace)) # Check the rebinning part data_x0 = output_workspace.dataX(0) self.assertTrue(data_x0[0] == 1.0) expected_upper_bound = 5.27471197274 self.assertEqual(round(data_x0[-1], 11), expected_upper_bound) # Check the units part axis0 = output_workspace.getAxis(0) unit = axis0.getUnit() self.assertTrue(unit.unitID() == "Wavelength")
def _parse_binning(self): binning_dict = self._get_val(["binning"]) def set_wavelength(state_obj): wavelength_start = self._get_val(["wavelength", "start"], binning_dict) # Weirdly start and stop in user file parser are lists whilst # step is a float val? if wavelength_start: state_obj.wavelength_low = [wavelength_start] wavelength_stop = self._get_val(["wavelength", "stop"], binning_dict) if wavelength_stop: state_obj.wavelength_high = [wavelength_stop] state_obj.wavelength_step = self._get_val(["wavelength", "step"], binning_dict, 0.0) step_str = self._get_val(["wavelength", "type"], binning_dict) if step_str: state_obj.wavelength_step_type = RangeStepType(step_str) # For legacy reasons where information is duplicated across our # state objects we set the same thing in 4 different locations. set_wavelength(self.calculate_transmission) set_wavelength(self.normalize_to_monitor) set_wavelength(self.wavelength) set_wavelength(self.wavelength_and_pixel) one_d_dict = self._get_val("1d_reduction", binning_dict) if one_d_dict: one_d_binning = self._get_val(["1d_reduction", "binning"], binning_dict) q_min, q_max = self._get_1d_min_max(one_d_binning) self.convert_to_q.q_min = q_min self.convert_to_q.q_1d_rebin_string = one_d_binning self.convert_to_q.q_max = q_max self.convert_to_q.radius_cutoff = self._get_val("radius_cut", one_d_dict, default=0.0) self.convert_to_q.wavelength_cutoff = self._get_val( "wavelength_cut", one_d_dict, default=0.0) self.convert_to_q.q_xy_max = self._get_val(["2d_reduction", "stop"], binning_dict) self.convert_to_q.q_xy_step = self._get_val(["2d_reduction", "step"], binning_dict) two_d_step_type = self._get_val(["2d_reduction", "type"], binning_dict) if two_d_step_type: self.convert_to_q.q_xy_step_type = RangeStepType(two_d_step_type) rebin_type = self._get_val(["2d_reduction", "interpolate"], binning_dict, default=False) rebin_type = RebinType.INTERPOLATING_REBIN if rebin_type else RebinType.REBIN self.calculate_transmission.rebin_type = rebin_type self.normalize_to_monitor.rebin_type = rebin_type
def _default_gui_setup(self): """ Provides a default setup of the GUI. This is important for the initial start up, when the view is being set. """ # Set the possible reduction modes reduction_mode_list = get_reduction_mode_strings_for_gui() self._view.set_reduction_modes(reduction_mode_list) # Set the step type options for wavelength range_step_types = [ RangeStepType.to_string(RangeStepType.Lin), RangeStepType.to_string(RangeStepType.Log) ] self._view.wavelength_step_type = range_step_types # Set the geometry options. This needs to include the option to read the sample shape from file. sample_shape = [ "Read from file", SampleShape.to_string(SampleShape.CylinderAxisUp), SampleShape.to_string(SampleShape.Cuboid), SampleShape.to_string(SampleShape.CylinderAxisAlong) ] self._view.sample_shape = sample_shape # Set the q range self._view.q_1d_step_type = range_step_types self._view.q_xy_step_type = range_step_types # Set the fit options fit_types = [ FitType.to_string(FitType.Linear), FitType.to_string(FitType.Logarithmic), FitType.to_string(FitType.Polynomial) ] self._view.transmission_sample_fit_type = fit_types self._view.transmission_can_fit_type = fit_types
def test_that_lower_wavelength_larger_than_higher_wavelength_raises(self): workspace = provide_workspace(is_event=True) convert_options = {"InputWorkspace": workspace, "OutputWorkspace": EMPTY_NAME, "RebinMode": "Rebin", "WavelengthLow": 4.0, "WavelengthHigh": 3.0, "WavelengthStep": 1.5, "WavelengthStepType": RangeStepType.to_string(RangeStepType.Log)} convert_alg = create_unmanaged_algorithm("SANSConvertToWavelengthAndRebin", **convert_options) had_run_time_error = False try: convert_alg.execute() except RuntimeError: had_run_time_error = True self.assertTrue(had_run_time_error)
def _get_rebin_string(self, workspace, wavelength_low, wavelength_high): # If the wavelength has not been specified, then get it from the workspace. Only the first spectrum is checked # The lowest wavelength value is to be found in the spectrum located at workspaces index 0 is a very # strong assumption, but it existed in the previous implementation. if wavelength_low is None or wavelength_low == Property.EMPTY_DBL: wavelength_low = min(workspace.readX(0)) if wavelength_high is None or wavelength_high == Property.EMPTY_DBL: wavelength_high = max(workspace.readX(0)) wavelength_step = self.getProperty("WavelengthStep").value step_type = RangeStepType(self.getProperty("WavelengthStepType").value) pre_factor = -1 if step_type in [ RangeStepType.LOG, RangeStepType.RANGE_LOG ] else 1 wavelength_step *= pre_factor return str(wavelength_low) + "," + str(wavelength_step) + "," + str( wavelength_high)
def _get_rebin_string(self, workspace): wavelength_low = self.getProperty("WavelengthLow").value wavelength_high = self.getProperty("WavelengthHigh").value # If the wavelength has not been specified, then get it from the workspace. Only the first spectrum is checked # The lowest wavelength value is to be found in the spectrum located at workspaces index 0 is a very # strong assumption, but it existed in the previous implementation. if wavelength_low is None or wavelength_low == Property.EMPTY_DBL: wavelength_low = min(workspace.readX(0)) if wavelength_high is None or wavelength_high == Property.EMPTY_DBL: wavelength_high = max(workspace.readX(0)) wavelength_step = self.getProperty("WavelengthStep").value step_type = RangeStepType.from_string(self.getProperty("WavelengthStepType").value) pre_factor = -1 if step_type == RangeStepType.Log else 1 wavelength_step *= pre_factor return str(wavelength_low) + "," + str(wavelength_step) + "," + str(wavelength_high)
def set_wavelength(state_obj): wavelength_start = self._get_val(["wavelength", "start"], binning_dict) # Weirdly start and stop in user file parser are lists whilst # step is a float val? if wavelength_start: state_obj.wavelength_low = [wavelength_start] wavelength_stop = self._get_val(["wavelength", "stop"], binning_dict) if wavelength_stop: state_obj.wavelength_high = [wavelength_stop] state_obj.wavelength_step = self._get_val(["wavelength", "step"], binning_dict, 0.0) step_str = self._get_val(["wavelength", "type"], binning_dict) if step_str: state_obj.wavelength_step_type = RangeStepType(step_str)
def set_wavelength_details(self, state_objs: DuplicateWavelengthStates): binning_dict = self.get_val(["binning"]) if not self.get_val("wavelength", binning_dict): return step_type = RangeStepType(self.get_mandatory_val(["binning", "wavelength", "type"])) wavelength_step = float(self.get_mandatory_val(["binning", "wavelength", "step"])) for i in state_objs.iterate_fields(): i.wavelength_interval.wavelength_step = wavelength_step i.wavelength_step_type = step_type if step_type in (RangeStepType.RANGE_LIN, RangeStepType.RANGE_LOG): self._parse_range_wavelength(state_objs) else: assert step_type in (RangeStepType.LIN, RangeStepType.LOG) self._parse_linear_wavelength(state_objs)
def test_that_lower_wavelength_larger_than_higher_wavelength_raises(self): workspace = provide_workspace(is_event=True) convert_options = { "InputWorkspace": workspace, "OutputWorkspace": EMPTY_NAME, "RebinMode": "Rebin", "WavelengthLow": 4.0, "WavelengthHigh": 3.0, "WavelengthStep": 1.5, "WavelengthStepType": RangeStepType.to_string(RangeStepType.Log) } convert_alg = create_unmanaged_algorithm( "SANSConvertToWavelengthAndRebin", **convert_options) had_run_time_error = False try: convert_alg.execute() except RuntimeError: had_run_time_error = True self.assertTrue(had_run_time_error)
def _parse_binning(self): binning_dict = self.get_val(["binning"]) to_set = DuplicateWavelengthStates( transmission=self.calculate_transmission, normalize=self.normalize_to_monitor, wavelength=self.wavelength, pixel=self.wavelength_and_pixel) WavelengthTomlParser(toml_dict=self._input).set_wavelength_details( state_objs=to_set) one_d_dict = self.get_val("1d_reduction", binning_dict) if one_d_dict: one_d_binning = self.get_val(["1d_reduction", "binning"], binning_dict) q_min, q_max = self._get_1d_min_max(one_d_binning) self.convert_to_q.q_min = q_min self.convert_to_q.q_1d_rebin_string = one_d_binning self.convert_to_q.q_max = q_max self.convert_to_q.radius_cutoff = self.get_val("radius_cut", one_d_dict, default=0.0) self.convert_to_q.wavelength_cutoff = self.get_val( "wavelength_cut", one_d_dict, default=0.0) self.convert_to_q.q_xy_max = self.get_val(["2d_reduction", "stop"], binning_dict) self.convert_to_q.q_xy_step = self.get_val(["2d_reduction", "step"], binning_dict) two_d_step_type = self.get_val(["2d_reduction", "type"], binning_dict) if two_d_step_type: self.convert_to_q.q_xy_step_type = RangeStepType(two_d_step_type) rebin_type = self.get_val(["2d_reduction", "interpolate"], binning_dict, default=False) rebin_type = RebinType.INTERPOLATING_REBIN if rebin_type else RebinType.REBIN self.calculate_transmission.rebin_type = rebin_type self.normalize_to_monitor.rebin_type = rebin_type
def test_that_event_workspace_with_conversion_is_still_event_workspace(self): workspace = provide_workspace(is_event=True) convert_options = {"InputWorkspace": workspace, "OutputWorkspace": EMPTY_NAME, "RebinMode": "Rebin", "WavelengthLow": 1.0, "WavelengthHigh": 10.0, "WavelengthStep": 1.0, "WavelengthStepType": RangeStepType.to_string(RangeStepType.Lin)} convert_alg = create_unmanaged_algorithm("SANSConvertToWavelengthAndRebin", **convert_options) convert_alg.execute() self.assertTrue(convert_alg.isExecuted()) output_workspace = convert_alg.getProperty("OutputWorkspace").value self.assertTrue(isinstance(output_workspace, EventWorkspace)) # Check the rebinning part data_x0 = output_workspace.dataX(0) self.assertTrue(len(data_x0) == 10) self.assertTrue(data_x0[0] == 1.0) self.assertTrue(data_x0[-1] == 10.0) # Check the units part axis0 = output_workspace.getAxis(0) unit = axis0.getUnit() self.assertTrue(unit.unitID() == "Wavelength")
def test_that_not_setting_upper_bound_takes_it_from_original_value(self): workspace = provide_workspace(is_event=True) convert_options = {"InputWorkspace": workspace, "OutputWorkspace": EMPTY_NAME, "RebinMode": "Rebin", "WavelengthLow": 1.0, "WavelengthStep": 1.0, "WavelengthStepType": RangeStepType.to_string(RangeStepType.Lin)} convert_alg = create_unmanaged_algorithm("SANSConvertToWavelengthAndRebin", **convert_options) convert_alg.execute() self.assertTrue(convert_alg.isExecuted()) output_workspace = convert_alg.getProperty("OutputWorkspace").value self.assertTrue(isinstance(output_workspace, EventWorkspace)) # Check the rebinning part data_x0 = output_workspace.dataX(0) self.assertTrue(data_x0[0] == 1.0) expected_upper_bound = 5.27471197274 self.assertEqual(round(data_x0[-1],11), expected_upper_bound) # Check the units part axis0 = output_workspace.getAxis(0) unit = axis0.getUnit() self.assertTrue(unit.unitID() == "Wavelength")
def PyExec(self): # State state_property_manager = self.getProperty("SANSState").value state = create_deserialized_sans_state_from_property_manager(state_property_manager) wavelength_state = state.wavelength # Input workspace workspace = get_input_workspace_as_copy_if_not_same_as_output_workspace(self) wavelength_name = "SANSConvertToWavelengthAndRebin" wavelength_options = {"InputWorkspace": workspace, "WavelengthLow": wavelength_state.wavelength_low[0], "WavelengthHigh": wavelength_state.wavelength_high[0], "WavelengthStep": wavelength_state.wavelength_step, "WavelengthStepType": RangeStepType.to_string( wavelength_state.wavelength_step_type), "RebinMode": RebinType.to_string(wavelength_state.rebin_type)} wavelength_alg = create_unmanaged_algorithm(wavelength_name, **wavelength_options) wavelength_alg.setPropertyValue("OutputWorkspace", EMPTY_NAME) wavelength_alg.setProperty("OutputWorkspace", workspace) wavelength_alg.execute() converted_workspace = wavelength_alg.getProperty("OutputWorkspace").value append_to_sans_file_tag(converted_workspace, "_wavelength") self.setProperty("OutputWorkspace", converted_workspace)
def _get_corrected_wavelength_workspace(self, workspace, detector_ids, calculate_transmission_state): """ Performs a prompt peak correction, a background correction, converts to wavelength and rebins. :param workspace: the workspace which is being corrected. :param detector_ids: a list of relevant detector ids :param calculate_transmission_state: a SANSStateCalculateTransmission state :return: a corrected workspace. """ # Extract the relevant spectra. These include # 1. The incident monitor spectrum # 2. The transmission spectra, be it monitor or ROI based. # A previous implementation of this code had a comment which suggested # that we have to exclude unused spectra as the interpolation runs into # problems if we don't. extract_name = "ExtractSpectra" extract_options = {"InputWorkspace": workspace, "OutputWorkspace": EMPTY_NAME, "DetectorList": detector_ids} extract_alg = create_unmanaged_algorithm(extract_name, **extract_options) extract_alg.execute() workspace = extract_alg.getProperty("OutputWorkspace").value # Make sure that we still have spectra in the workspace if workspace.getNumberHistograms() == 0: raise RuntimeError("SANSCalculateTransmissionCorrection: The transmission workspace does " "not seem to have any spectra.") # ---------------------------------- # Perform the prompt peak correction # ---------------------------------- prompt_peak_correction_min = calculate_transmission_state.prompt_peak_correction_min prompt_peak_correction_max = calculate_transmission_state.prompt_peak_correction_max prompt_peak_correction_enabled = calculate_transmission_state.prompt_peak_correction_enabled workspace = self._perform_prompt_peak_correction(workspace, prompt_peak_correction_min, prompt_peak_correction_max, prompt_peak_correction_enabled) # --------------------------------------- # Perform the flat background correction # --------------------------------------- # The flat background correction has two parts: # 1. Corrections on monitors # 2. Corrections on regular detectors # Monitor flat background correction workspace_indices_of_monitors = list(get_workspace_indices_for_monitors(workspace)) background_tof_monitor_start = calculate_transmission_state.background_TOF_monitor_start background_tof_monitor_stop = calculate_transmission_state.background_TOF_monitor_stop background_tof_general_start = calculate_transmission_state.background_TOF_general_start background_tof_general_stop = calculate_transmission_state.background_TOF_general_stop workspace = apply_flat_background_correction_to_monitors(workspace, workspace_indices_of_monitors, background_tof_monitor_start, background_tof_monitor_stop, background_tof_general_start, background_tof_general_stop) # Detector flat background correction flat_background_correction_start = calculate_transmission_state.background_TOF_roi_start flat_background_correction_stop = calculate_transmission_state.background_TOF_roi_stop workspace = apply_flat_background_correction_to_detectors(workspace, flat_background_correction_start, flat_background_correction_stop) # --------------------------------------- # Convert to wavelength and rebin # --------------------------------------- # The wavelength setting is reasonably complex. # 1. Use full wavelength range # 2. Use standard settings if calculate_transmission_state.use_full_wavelength_range: wavelength_low = calculate_transmission_state.wavelength_full_range_low wavelength_high = calculate_transmission_state.wavelength_full_range_high else: wavelength_low = calculate_transmission_state.wavelength_low[0] wavelength_high = calculate_transmission_state.wavelength_high[0] wavelength_step = calculate_transmission_state.wavelength_step rebin_type = calculate_transmission_state.rebin_type wavelength_step_type = calculate_transmission_state.wavelength_step_type convert_name = "SANSConvertToWavelengthAndRebin" convert_options = {"InputWorkspace": workspace, "WavelengthLow": wavelength_low, "WavelengthHigh": wavelength_high, "WavelengthStep": wavelength_step, "WavelengthStepType": RangeStepType.to_string(wavelength_step_type), "RebinMode": RebinType.to_string(rebin_type)} convert_alg = create_unmanaged_algorithm(convert_name, **convert_options) convert_alg.setPropertyValue("OutputWorkspace", EMPTY_NAME) convert_alg.setProperty("OutputWorkspace", workspace) convert_alg.execute() return convert_alg.getProperty("OutputWorkspace").value
def _get_corrected_wavelength_workspace(self, workspace, detector_ids, calculate_transmission_state): """ Performs a prompt peak correction, a background correction, converts to wavelength and rebins. :param workspace: the workspace which is being corrected. :param detector_ids: a list of relevant detector ids :param calculate_transmission_state: a SANSStateCalculateTransmission state :return: a corrected workspace. """ # Extract the relevant spectra. These include # 1. The incident monitor spectrum # 2. The transmission spectra, be it monitor or ROI based. # A previous implementation of this code had a comment which suggested # that we have to exclude unused spectra as the interpolation runs into # problems if we don't. extract_name = "ExtractSpectra" extract_options = {"InputWorkspace": workspace, "OutputWorkspace": EMPTY_NAME, "DetectorList": detector_ids} extract_alg = create_unmanaged_algorithm(extract_name, **extract_options) extract_alg.execute() workspace = extract_alg.getProperty("OutputWorkspace").value # Make sure that we still have spectra in the workspace if workspace.getNumberHistograms() == 0: raise RuntimeError("SANSCalculateTransmissionCorrection: The transmission workspace does " "not seem to have any spectra.") # ---------------------------------- # Perform the prompt peak correction # ---------------------------------- prompt_peak_correction_min = calculate_transmission_state.prompt_peak_correction_min prompt_peak_correction_max = calculate_transmission_state.prompt_peak_correction_max prompt_peak_correction_enabled = calculate_transmission_state.prompt_peak_correction_enabled workspace = self._perform_prompt_peak_correction(workspace, prompt_peak_correction_min, prompt_peak_correction_max, prompt_peak_correction_enabled) # --------------------------------------- # Perform the flat background correction # --------------------------------------- # The flat background correction has two parts: # 1. Corrections on monitors # 2. Corrections on regular detectors # Monitor flat background correction workspace_indices_of_monitors = list(get_workspace_indices_for_monitors(workspace)) background_tof_monitor_start = calculate_transmission_state.background_TOF_monitor_start background_tof_monitor_stop = calculate_transmission_state.background_TOF_monitor_stop background_tof_general_start = calculate_transmission_state.background_TOF_general_start background_tof_general_stop = calculate_transmission_state.background_TOF_general_stop workspace = apply_flat_background_correction_to_monitors(workspace, workspace_indices_of_monitors, background_tof_monitor_start, background_tof_monitor_stop, background_tof_general_start, background_tof_general_stop) # Detector flat background correction flat_background_correction_start = calculate_transmission_state.background_TOF_roi_start flat_background_correction_stop = calculate_transmission_state.background_TOF_roi_stop workspace = apply_flat_background_correction_to_detectors(workspace, flat_background_correction_start, flat_background_correction_stop) # --------------------------------------- # Convert to wavelength and rebin # --------------------------------------- # The wavelength setting is reasonably complex. # 1. Use full wavelength range # 2. Use standard settings if calculate_transmission_state.use_full_wavelength_range: wavelength_low = calculate_transmission_state.wavelength_full_range_low wavelength_high = calculate_transmission_state.wavelength_full_range_high else: data_type_string = self.getProperty("DataType").value fit_state = calculate_transmission_state.fit[data_type_string] wavelength_low = fit_state.wavelength_low if fit_state.wavelength_low\ else calculate_transmission_state.wavelength_low[0] wavelength_high = fit_state.wavelength_high if fit_state.wavelength_high\ else calculate_transmission_state.wavelength_high[0] wavelength_step = calculate_transmission_state.wavelength_step rebin_type = calculate_transmission_state.rebin_type wavelength_step_type = calculate_transmission_state.wavelength_step_type convert_name = "SANSConvertToWavelengthAndRebin" convert_options = {"InputWorkspace": workspace, "WavelengthLow": wavelength_low, "WavelengthHigh": wavelength_high, "WavelengthStep": wavelength_step, "WavelengthStepType": RangeStepType.to_string(wavelength_step_type), "RebinMode": RebinType.to_string(rebin_type)} convert_alg = create_unmanaged_algorithm(convert_name, **convert_options) convert_alg.setPropertyValue("OutputWorkspace", EMPTY_NAME) convert_alg.setProperty("OutputWorkspace", workspace) convert_alg.execute() return convert_alg.getProperty("OutputWorkspace").value