def _create_state_objs(self, data_info):
        self.adjustment = StateAdjustment()
        self.compatibility = StateCompatibility()
        self.convert_to_q = StateConvertToQ()
        self.calculate_transmission = get_calculate_transmission(
            instrument=self.instrument)
        self.mask = get_mask_builder(data_info=data_info).build()
        self.move = get_move_builder(data_info=data_info).build()
        self.normalize_to_monitor = get_normalize_to_monitor_builder(
            data_info=data_info).build()
        self.reduction_mode = StateReductionMode()
        self.scale = StateScale()
        self.wavelength = StateWavelength()
        self.wavelength_and_pixel = get_wavelength_and_pixel_adjustment_builder(
            data_info=data_info).build()

        # Ensure they are linked up correctly
        self.adjustment.calculate_transmission = self.calculate_transmission
        self.adjustment.normalize_to_monitor = self.normalize_to_monitor
        self.adjustment.wavelength_and_pixel_adjustment = self.wavelength_and_pixel
    def _get_state(
            transmission_radius_on_detector=None,
            transmission_roi_files=None,
            transmission_mask_files=None,  # noqa: C901
            transmission_monitor=None,
            incident_monitor=None,
            rebin_type=None,
            wavelength_low=None,
            wavelength_high=None,
            wavelength_step=None,
            wavelength_step_type=None,
            use_full_wavelength_range=None,
            prompt_peak_correction_min=None,
            prompt_peak_correction_max=None,
            background_TOF_general_start=None,
            background_TOF_general_stop=None,
            background_TOF_monitor_start=None,
            background_TOF_monitor_stop=None,
            background_TOF_roi_start=None,
            background_TOF_roi_stop=None,
            sample_fit_type=None,
            sample_polynomial_order=None,
            sample_wavelength_low=None,
            sample_wavelength_high=None,
            can_fit_type=None,
            can_polynomial_order=None,
            can_wavelength_low=None,
            can_wavelength_high=None):
        test_director = TestDirector()
        state = test_director.construct()
        instrument = state.data.instrument
        calculate_transmission_obj = get_calculate_transmission(instrument)
        if transmission_radius_on_detector:
            calculate_transmission_obj.transmission_radius_on_detector = transmission_radius_on_detector
        if transmission_roi_files:
            calculate_transmission_obj.transmission_roi_files = transmission_roi_files
        if transmission_mask_files:
            calculate_transmission_obj.transmission_mask_files = transmission_mask_files
        if transmission_monitor:
            calculate_transmission_obj.transmission_monitor = transmission_monitor

        if incident_monitor:
            calculate_transmission_obj.incident_monitor = incident_monitor
        if rebin_type:
            calculate_transmission_obj.rebin_type = rebin_type
        if wavelength_low and wavelength_high:
            wavelength_range = (wavelength_low, wavelength_high)
            calculate_transmission_obj.wavelength_interval.wavelength_full_range = wavelength_range
            calculate_transmission_obj.wavelength_interval.selected_ranges = [
                wavelength_range
            ]
        if wavelength_step:
            calculate_transmission_obj.wavelength_interval.wavelength_step = wavelength_step
        if wavelength_step_type:
            calculate_transmission_obj.wavelength_step_type = wavelength_step_type
        if use_full_wavelength_range:
            calculate_transmission_obj.use_full_wavelength_range = use_full_wavelength_range

        if prompt_peak_correction_min:
            calculate_transmission_obj.prompt_peak_correction_min = prompt_peak_correction_min
        if prompt_peak_correction_max:
            calculate_transmission_obj.prompt_peak_correction_max = prompt_peak_correction_max
        if prompt_peak_correction_min and prompt_peak_correction_max:
            calculate_transmission_obj.prompt_peak_correction_enabled = True

        if background_TOF_general_start:
            calculate_transmission_obj.background_TOF_general_start = background_TOF_general_start
        if background_TOF_general_stop:
            calculate_transmission_obj.background_TOF_general_stop = background_TOF_general_stop
        if background_TOF_monitor_start:
            calculate_transmission_obj.background_TOF_monitor_start = background_TOF_monitor_start
        if background_TOF_monitor_stop:
            calculate_transmission_obj.background_TOF_monitor_stop = background_TOF_monitor_stop
        if background_TOF_roi_start:
            calculate_transmission_obj.background_TOF_roi_start = background_TOF_roi_start
        if background_TOF_roi_stop:
            calculate_transmission_obj.background_TOF_roi_stop = background_TOF_roi_stop

        if sample_fit_type:
            calculate_transmission_obj.set_sample_fit_type(sample_fit_type)
        if sample_polynomial_order:
            calculate_transmission_obj.set_sample_polynomial_order(
                sample_polynomial_order)
        if sample_wavelength_low:
            calculate_transmission_obj.set_sample_wavelength_low(
                sample_wavelength_low)
        if sample_wavelength_high:
            calculate_transmission_obj.set_sample_wavelength_high(
                sample_wavelength_high)

        if can_fit_type:
            calculate_transmission_obj.set_can_fit_type(can_fit_type)
        if can_polynomial_order:
            calculate_transmission_obj.set_can_polynomial_order(
                can_polynomial_order)
        if can_wavelength_low:
            calculate_transmission_obj.set_can_wavelength_low(
                can_wavelength_low)
        if can_wavelength_high:
            calculate_transmission_obj.set_can_wavelength_high(
                can_wavelength_high)
        state.adjustment.calculate_transmission = calculate_transmission_obj
        return state
Beispiel #3
0
    def construct(self):
        facility = SANSFacility.ISIS
        file_information = SANSFileInformationMock(run_number=22024, instrument=SANSInstrument.SANS2D)

        # Build the SANSStateData
        if self.data_state is None:
            data_builder = get_data_builder(facility, file_information)
            data_builder.set_sample_scatter("SANS2D00022024")
            data_builder.set_can_scatter("SANS2D00022024")
            self.data_state = data_builder.build()

        # Build the SANSStateMove
        if self.move_state is None:
            move_builder = get_move_builder(self.data_state)
            if hasattr(move_builder, "set_HAB_x_translation_correction"):
                move_builder.set_HAB_x_translation_correction(21.2)
            move_builder.set_LAB_x_translation_correction(12.1)
            self.move_state = move_builder.build()

        if self.inst_info_state is None:
            self.inst_info_state = StateInstrumentInfo.build_from_data_info(self.data_state)

        # Build the SANSStateReduction
        if self.reduction_state is None:
            state_reduction = StateReductionMode()
            state_reduction.reduction_dimensionality = ReductionDimensionality.ONE_DIM
            state_reduction.merge_fit_mode = FitModeForMerge.BOTH
            state_reduction.merge_shift = 324.2
            state_reduction.merge_scale = 3420.98
            state_reduction.reduction_mode = ReductionMode.MERGED
            self.reduction_state = state_reduction

        # Build the SANSStateSliceEvent
        if self.slice_state is None:
            slice_builder = get_slice_event_builder(self.data_state)
            slice_builder.set_start_time([0.1, 1.3])
            slice_builder.set_end_time([0.2, 1.6])
            self.slice_state = slice_builder.build()

        # Build the SANSStateMask
        if self.mask_state is None:
            mask_builder = get_mask_builder(self.data_state)
            mask_builder.set_radius_min(10.0)
            mask_builder.set_radius_max(20.0)
            self.mask_state = mask_builder.build()

        # Build the SANSStateWavelength
        if self.wavelength_state is None:
            wavelength_builder = get_wavelength_builder(self.data_state)
            wavelength_range = (1.0, 10.0)
            wavelength_builder.state.wavelength_interval.wavelength_full_range = wavelength_range
            wavelength_builder.state.wavelength_interval.selected_ranges = [wavelength_range]
            wavelength_builder.set_wavelength_step_type(RangeStepType.LIN)
            wavelength_builder.set_rebin_type(RebinType.REBIN)
            self.wavelength_state = wavelength_builder.build()

        # Build the SANSStateSave
        if self.save_state is None:
            save_builder = get_save_builder(self.data_state)
            save_builder.set_user_specified_output_name("test_file_name")
            save_builder.set_file_format([SaveType.NEXUS])
            self.save_state = save_builder.build()

        # Build the SANSStateScale
        if self.scale_state is None:
            scale_builder = get_scale_builder(self.data_state, file_information)
            scale_builder.set_shape(SampleShape.FLAT_PLATE)
            scale_builder.set_width(1.0)
            scale_builder.set_height(2.0)
            scale_builder.set_thickness(3.0)
            scale_builder.set_scale(4.0)
            self.scale_state = scale_builder.build()

        instrument = self.data_state.instrument

        # Build the SANSAdjustmentState
        if self.adjustment_state is None:
            # NormalizeToMonitor
            normalize_to_monitor_builder = get_normalize_to_monitor_builder(self.data_state)
            wavelength_range = (1.0, 10.0)
            normalize_to_monitor_builder.state.wavelength_interval.wavelength_full_range = wavelength_range
            normalize_to_monitor_builder.state.wavelength_interval.selected_ranges = [wavelength_range]
            normalize_to_monitor_builder.state.wavelength_interval.wavelength_step = 2.0
            normalize_to_monitor_builder.set_wavelength_step_type(RangeStepType.LIN)
            normalize_to_monitor_builder.set_rebin_type(RebinType.REBIN)
            normalize_to_monitor_builder.set_background_TOF_general_start(1000.)
            normalize_to_monitor_builder.set_background_TOF_general_stop(2000.)
            normalize_to_monitor_builder.set_incident_monitor(1)
            normalize_to_monitor = normalize_to_monitor_builder.build()

            # CalculateTransmission
            calculate_transmission_obj = get_calculate_transmission(instrument=instrument)
            calculate_transmission_obj.transmission_monitor = 3
            calculate_transmission_obj.incident_monitor = 2
            wavelength_range = (1.0, 10.0)
            calculate_transmission_obj.wavelength_interval.wavelength_full_range = wavelength_range
            calculate_transmission_obj.wavelength_interval.selected_ranges = [wavelength_range]
            calculate_transmission_obj.wavelength_interval.wavelength_step = 2.0
            calculate_transmission_obj.wavelength_step_type = RangeStepType.LIN
            calculate_transmission_obj.rebin_type = RebinType.REBIN
            calculate_transmission_obj.background_TOF_general_start = 1000.
            calculate_transmission_obj.background_TOF_general_stop = 2000.

            calculate_transmission_obj.set_sample_fit_type(FitType.LINEAR)
            calculate_transmission_obj.set_sample_polynomial_order(0)
            calculate_transmission_obj.set_sample_wavelength_low(1.0)
            calculate_transmission_obj.set_sample_wavelength_high(10.0)
            calculate_transmission_obj.set_can_fit_type(FitType.POLYNOMIAL)
            calculate_transmission_obj.set_can_polynomial_order(3)
            calculate_transmission_obj.set_can_wavelength_low(10.0)
            calculate_transmission_obj.set_can_wavelength_high(20.0)

            # Wavelength and pixel adjustment
            wavelength_and_pixel_builder = get_wavelength_and_pixel_adjustment_builder(self.data_state)
            wavelength_range = (1.0, 10.0)
            wavelength_and_pixel_builder.state.wavelength_interval.wavelength_full_range = wavelength_range
            wavelength_and_pixel_builder.state.wavelength_interval.selected_ranges = [wavelength_range]
            wavelength_and_pixel_builder.state.wavelength_interval.wavelength_step = 2.0
            wavelength_and_pixel_builder.set_wavelength_step_type(RangeStepType.LIN)
            wavelength_and_pixel = wavelength_and_pixel_builder.build()

            # Adjustment
            adjustment_builder = get_adjustment_builder(self.data_state)
            adjustment_builder.set_normalize_to_monitor(normalize_to_monitor)
            adjustment_builder.set_calculate_transmission(calculate_transmission_obj)
            adjustment_builder.set_wavelength_and_pixel_adjustment(wavelength_and_pixel)
            self.adjustment_state = adjustment_builder.build()

        # SANSStateConvertToQ
        if self.convert_to_q_state is None:
            convert_to_q_builder = get_convert_to_q_builder(self.data_state)
            convert_to_q_builder.set_reduction_dimensionality(ReductionDimensionality.ONE_DIM)
            convert_to_q_builder.set_use_gravity(False)
            convert_to_q_builder.set_radius_cutoff(0.002)
            convert_to_q_builder.set_wavelength_cutoff(12.)
            convert_to_q_builder.set_q_min(0.1)
            convert_to_q_builder.set_q_max(0.8)
            convert_to_q_builder.set_q_1d_rebin_string("0.1,0.01,0.8")
            convert_to_q_builder.set_use_q_resolution(False)
            self.convert_to_q_state = convert_to_q_builder.build()

        # Set the sub states on the SANSState
        state_builder = get_all_states_builder(self.data_state)
        state_builder.set_data(self.data_state)
        state_builder.set_move(self.move_state)
        state_builder.set_reduction(self.reduction_state)
        state_builder.set_slice(self.slice_state)
        state_builder.set_mask(self.mask_state)
        state_builder.set_wavelength(self.wavelength_state)
        state_builder.set_save(self.save_state)
        state_builder.set_scale(self.scale_state)
        state_builder.set_adjustment(self.adjustment_state)
        state_builder.set_convert_to_q(self.convert_to_q_state)
        state_builder.state.instrument_info = self.inst_info_state
        return state_builder.build()
Beispiel #4
0
    def test_that_reduction_state_can_be_built(self):
        # Arrange
        facility = SANSFacility.ISIS
        file_information = SANSFileInformationMock(
            instrument=SANSInstrument.LOQ, run_number=74044)
        data_builder = get_data_builder(facility, file_information)
        data_builder.set_sample_scatter("LOQ74044")
        data_info = data_builder.build()

        # Act
        state_transmission = get_calculate_transmission(data_info.instrument)

        state_transmission.prompt_peak_correction_min = 12.0
        state_transmission.prompt_peak_correction_max = 17.0

        state_transmission.incident_monitor = 1
        state_transmission.default_incident_monitor = 2
        state_transmission.transmission_monitor = 3
        state_transmission.default_transmission_monitor = 4
        state_transmission.transmission_radius_on_detector = 1.
        state_transmission.transmission_roi_files = ["sdfs", "sddfsdf"]
        state_transmission.transmission_mask_files = ["sdfs", "bbbbbb"]

        state_transmission.rebin_type = RebinType.REBIN
        state_transmission.wavelength_low = [1.5]
        state_transmission.wavelength_high = [2.7]
        state_transmission.wavelength_step = 0.5
        state_transmission.wavelength_step_type = RangeStepType.LIN
        state_transmission.use_full_wavelength_range = True
        state_transmission.wavelength_full_range_low = 12.
        state_transmission.wavelength_full_range_high = 24.

        state_transmission.background_TOF_general_start = 1.4
        state_transmission.background_TOF_general_stop = 34.4
        state_transmission.background_TOF_monitor_start = {"1": 123, "2": 123}
        state_transmission.background_TOF_monitor_stop = {"1": 234, "2": 2323}
        state_transmission.background_TOF_roi_start = 1.4
        state_transmission.background_TOF_roi_stop = 34.4

        state_transmission.set_sample_fit_type(FitType.LINEAR)
        state_transmission.set_sample_polynomial_order(0)
        state_transmission.set_sample_wavelength_low(10.0)
        state_transmission.set_sample_wavelength_high(20.0)

        state_transmission.set_can_fit_type(FitType.POLYNOMIAL)
        state_transmission.set_can_polynomial_order(3)
        state_transmission.set_can_wavelength_low(10.0)
        state_transmission.set_can_wavelength_high(20.0)

        # Assert
        self.assertEqual(state_transmission.prompt_peak_correction_min, 12.0)
        self.assertEqual(state_transmission.prompt_peak_correction_max, 17.0)

        self.assertEqual(state_transmission.incident_monitor, 1)
        self.assertEqual(state_transmission.default_incident_monitor, 2)
        self.assertEqual(state_transmission.transmission_monitor, 3)
        self.assertEqual(state_transmission.default_transmission_monitor, 4)
        self.assertEqual(state_transmission.transmission_radius_on_detector,
                         1.)
        self.assertEqual(state_transmission.transmission_roi_files,
                         ["sdfs", "sddfsdf"])
        self.assertEqual(state_transmission.transmission_mask_files,
                         ["sdfs", "bbbbbb"])

        self.assertEqual(state_transmission.rebin_type, RebinType.REBIN)
        self.assertEqual(state_transmission.wavelength_low, [1.5])
        self.assertEqual(state_transmission.wavelength_high, [2.7])
        self.assertEqual(state_transmission.wavelength_step, 0.5)
        self.assertEqual(state_transmission.wavelength_step_type,
                         RangeStepType.LIN)
        self.assertEqual(state_transmission.use_full_wavelength_range, True)
        self.assertEqual(state_transmission.wavelength_full_range_low, 12.)
        self.assertEqual(state_transmission.wavelength_full_range_high, 24.)

        self.assertEqual(state_transmission.background_TOF_general_start, 1.4)
        self.assertEqual(state_transmission.background_TOF_general_stop, 34.4)
        self.assertEqual(
            len(
                set(state_transmission.background_TOF_monitor_start.items())
                & set({
                    "1": 123,
                    "2": 123
                }.items())), 2)
        self.assertEqual(
            len(
                set(state_transmission.background_TOF_monitor_stop.items())
                & set({
                    "1": 234,
                    "2": 2323
                }.items())), 2)
        self.assertEqual(state_transmission.background_TOF_roi_start, 1.4)
        self.assertEqual(state_transmission.background_TOF_roi_stop, 34.4)

        self.assertEqual(
            state_transmission.fit[DataType.SAMPLE.value].fit_type,
            FitType.LINEAR)
        self.assertEqual(
            state_transmission.fit[DataType.SAMPLE.value].polynomial_order, 0)
        self.assertEqual(
            state_transmission.fit[DataType.SAMPLE.value].wavelength_low, 10.)
        self.assertEqual(
            state_transmission.fit[DataType.SAMPLE.value].wavelength_high, 20.)

        self.assertEqual(state_transmission.fit[DataType.CAN.value].fit_type,
                         FitType.POLYNOMIAL)
        self.assertEqual(
            state_transmission.fit[DataType.CAN.value].polynomial_order, 3)
        self.assertEqual(
            state_transmission.fit[DataType.CAN.value].wavelength_low, 10.)
        self.assertEqual(
            state_transmission.fit[DataType.CAN.value].wavelength_high, 20.)