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): workspace = get_input_workspace_as_copy_if_not_same_as_output_workspace( self) progress = Progress(self, start=0.0, end=1.0, nreports=3) # Convert the units into wavelength progress.report("Converting workspace to wavelength units.") workspace = self._convert_units_to_wavelength(workspace) # Get the rebin option rebin_type = RebinType.from_string(self.getProperty("RebinMode").value) rebin_string = self._get_rebin_string(workspace) if rebin_type is RebinType.Rebin: rebin_options = { "InputWorkspace": workspace, "PreserveEvents": True, "Params": rebin_string } else: rebin_options = { "InputWorkspace": workspace, "Params": rebin_string } # Perform the rebin progress.report("Performing rebin.") workspace = self._perform_rebin(rebin_type, rebin_options, workspace) append_to_sans_file_tag(workspace, "_toWavelength") self.setProperty("OutputWorkspace", workspace) progress.report("Finished converting to wavelength.")
def validateInputs(self): errors = dict() # Check the wavelength wavelength_low = self.getProperty("WavelengthLow").value wavelength_high = self.getProperty("WavelengthHigh").value if wavelength_low is not None and wavelength_high is not None and wavelength_low > wavelength_high: errors.update({"WavelengthLow": "The lower wavelength setting needs to be smaller " "than the higher wavelength setting."}) if wavelength_low is not None and wavelength_low < 0: errors.update({"WavelengthLow": "The wavelength cannot be smaller than 0."}) if wavelength_high is not None and wavelength_high < 0: errors.update({"WavelengthHigh": "The wavelength cannot be smaller than 0."}) wavelength_step = self.getProperty("WavelengthStep").value if wavelength_step is not None and wavelength_step < 0: errors.update({"WavelengthStep": "The wavelength step cannot be smaller than 0."}) # Check the workspace workspace = self.getProperty("InputWorkspace").value rebin_type = RebinType.from_string(self.getProperty("RebinMode").value) if rebin_type is RebinType.InterpolatingRebin and isinstance(workspace, EventWorkspace): errors.update({"RebinMode": "An interpolating rebin cannot be applied to an EventWorkspace."}) return errors
def PyExec(self): workspace = get_input_workspace_as_copy_if_not_same_as_output_workspace(self) progress = Progress(self, start=0.0, end=1.0, nreports=3) # Convert the units into wavelength progress.report("Converting workspace to wavelength units.") workspace = self._convert_units_to_wavelength(workspace) # Get the rebin option rebin_type = RebinType.from_string(self.getProperty("RebinMode").value) rebin_string = self._get_rebin_string(workspace) if rebin_type is RebinType.Rebin: rebin_options = {"InputWorkspace": workspace, "PreserveEvents": True, "Params": rebin_string} else: rebin_options = {"InputWorkspace": workspace, "Params": rebin_string} # Perform the rebin progress.report("Performing rebin.") workspace = self._perform_rebin(rebin_type, rebin_options, workspace) append_to_sans_file_tag(workspace, "_toWavelength") self.setProperty("OutputWorkspace", workspace) progress.report("Finished converting to 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, "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 _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 _perform_rebin(self, rebin_options): rebin_type = RebinType(self.getProperty("RebinMode").value) rebin_name = "Rebin" if rebin_type is RebinType.REBIN else "InterpolatingRebin" rebin_alg = create_unmanaged_algorithm(rebin_name, **rebin_options) rebin_alg.setPropertyValue("OutputWorkspace", EMPTY_NAME) rebin_alg.execute() return rebin_alg.getProperty("OutputWorkspace").value
def validateInputs(self): errors = dict() # Check the wavelength wavelength_json = self.getProperty(self.WAV_PAIRS).value try: wavelengths: List[Tuple[float, float]] = json.loads(wavelength_json) except JSONDecodeError as e: errors.update( {self.WAV_PAIRS: f"Failed to decode JSON. Exception was: {e}"}) return errors if not all( isinstance(internal_item, list) for internal_item in wavelengths): errors.update({ self.WAV_PAIRS: "A list of pairs (i.e. list of lists) is required." " A single pair must be container within an outer list too." }) return errors # The below checks aren't really possible as we don't have a clue what we have now def _check_individual_pair(wavelength_low, wavelength_high): if wavelength_low is not None and wavelength_high is not None and wavelength_low > wavelength_high: errors.update({ self.WAV_PAIRS: "The lower wavelength setting needs to be smaller " "than the higher wavelength setting." }) if wavelength_low is not None and wavelength_low < 0: errors.update({ self.WAV_PAIRS: "The wavelength cannot be smaller than 0." }) if wavelength_high is not None and wavelength_high < 0: errors.update({ self.WAV_PAIRS: "The wavelength cannot be smaller than 0." }) for pair in wavelengths: _check_individual_pair(*pair) wavelength_step = self.getProperty("WavelengthStep").value if wavelength_step is not None and wavelength_step < 0: errors.update({ "WavelengthStep": "The wavelength step cannot be smaller than 0." }) # Check the workspace workspace = self.getProperty("InputWorkspace").value rebin_type = RebinType(self.getProperty("RebinMode").value) if rebin_type is RebinType.INTERPOLATING_REBIN and isinstance( workspace, EventWorkspace): errors.update({ "RebinMode": "An interpolating rebin cannot be applied to an EventWorkspace." }) return errors
def validateInputs(self): errors = dict() # Check the wavelength wavelength_low = self.getProperty("WavelengthLow").value wavelength_high = self.getProperty("WavelengthHigh").value if wavelength_low is not None and wavelength_high is not None and wavelength_low > wavelength_high: errors.update({"WavelengthLow": "The lower wavelength setting needs to be smaller " "than the higher wavelength setting."}) if wavelength_low is not None and wavelength_low < 0: errors.update({"WavelengthLow": "The wavelength cannot be smaller than 0."}) if wavelength_high is not None and wavelength_high < 0: errors.update({"WavelengthHigh": "The wavelength cannot be smaller than 0."}) wavelength_step = self.getProperty("WavelengthStep").value if wavelength_step is not None and wavelength_step < 0: errors.update({"WavelengthStep": "The wavelength step cannot be smaller than 0."}) # Check the workspace workspace = self.getProperty("InputWorkspace").value rebin_type = RebinType(self.getProperty("RebinMode").value) if rebin_type is RebinType.INTERPOLATING_REBIN and isinstance(workspace, EventWorkspace): errors.update({"RebinMode": "An interpolating rebin cannot be applied to an EventWorkspace."}) return errors
def _get_rebin_params(self, rebin_string, workspace): rebin_type = RebinType(self.getProperty("RebinMode").value) if rebin_type is RebinType.REBIN: rebin_options = { "InputWorkspace": workspace, "PreserveEvents": True, "Params": rebin_string } else: rebin_options = { "InputWorkspace": workspace, "Params": rebin_string } return rebin_options
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