def test_that_extracts_workspace_indices_of_monitor_when_monitors_are_present(self):
     # Act
     workspace_indices_generator = get_workspace_indices_for_monitors(self.immutable_test_workspace)
     # Assert
     workspace_indices = list(workspace_indices_generator)
     self.assertTrue(len(workspace_indices) == 2)
     self.assertTrue(workspace_indices[0] == 0)
     self.assertTrue(workspace_indices[1] == 1)
 def test_that_extracts_workspace_indices_of_monitor_when_monitors_are_present(
         self):
     # Act
     workspace_indices_generator = get_workspace_indices_for_monitors(
         self.immutable_test_workspace)
     # Assert
     workspace_indices = list(workspace_indices_generator)
     self.assertEqual(len(workspace_indices), 2)
     self.assertEqual(workspace_indices[0], 0)
     self.assertEqual(workspace_indices[1], 1)
 def test_that_returns_empty_generator_if_no_monitors_are_present(self):
     # Arrange
     test_workspace_for_monitors = CreateSampleWorkspace("Histogram")
     # Act
     workspace_indices_generator = get_workspace_indices_for_monitors(test_workspace_for_monitors)
     # Assert
     workspace_indices = list(workspace_indices_generator)
     self.assertTrue(workspace_indices == [])
     # Clean up
     DeleteWorkspace(test_workspace_for_monitors)
 def test_that_returns_empty_generator_if_no_monitors_are_present(self):
     # Arrange
     test_workspace_for_monitors = CreateSampleWorkspace("Histogram")
     # Act
     workspace_indices_generator = get_workspace_indices_for_monitors(
         test_workspace_for_monitors)
     # Assert
     workspace_indices = list(workspace_indices_generator)
     self.assertEqual(workspace_indices, [])
     # Clean up
     DeleteWorkspace(test_workspace_for_monitors)
    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