def test_that_angle_masking_is_applied(self):
        # Arrange
        file_information_factory = SANSFileInformationFactory()
        file_information = file_information_factory.create_sans_file_information("SANS2D00028827")
        data_builder = get_data_builder(SANSFacility.ISIS, file_information)
        data_builder.set_sample_scatter("SANS2D00028827")
        data_info = data_builder.build()

        mask_builder = get_mask_builder(data_info)

        # Expected_spectra
        phi_mirror = False
        phi_min = 0.
        phi_max = 90.
        # This should mask everything except for the upper right quadrant
        # | 120              |-------------------|
        # |                 |---------------------|
        # | 60               |-------------------|
        # |                 |----------------------|
        # |
        # |
        # |-------------------|------------------|
        # 512                256                 0

        expected_spectra = []
        # The strange double pattern arises from the offset of the SANS2D tube geometry (see InstrumentView)
        for y in range(60, 120):
            if y % 2 == 0:
                expected_spectra.extend(((y * 512) + 9 + x for x in range(0, 255)))
            else:
                expected_spectra.extend(((y * 512) + 9 + x for x in range(0, 257)))
        expected_spectra.extend((x for x in range(92169, 122889)))  # HAB

        mask_builder.set_use_mask_phi_mirror(phi_mirror)
        mask_builder.set_phi_min(phi_min)
        mask_builder.set_phi_max(phi_max)

        mask_info = mask_builder.build()

        test_director = TestDirector()
        test_director.set_states(data_state=data_info, mask_state=mask_info)
        state = test_director.construct()

        state.instrument_info = StateInstrumentInfo.build_from_data_info(data_info)

        returned_data = SANSLoad(SANSState=Serializer.to_json(state), SampleScatterWorkspace="mask_sans_ws",
                                 SampleScatterMonitorWorkspace="dummy")

        workspace = returned_data[0]
        DeleteWorkspace(returned_data[1])

        # Act
        workspace = mask_workspace(state=state, component_as_string="LAB", workspace=workspace)

        # Assert
        self._do_assert_non_masked(workspace, expected_spectra)
    def test_builder(self, init_monitor_names, init_detector_names):
        data_info = mock.NonCallableMock()
        data_info.idf_file_path = "idf_path"
        returned = StateInstrumentInfo.build_from_data_info(
            data_info=data_info)

        init_detector_names.assert_called_once()
        init_monitor_names.assert_called_once()
        self.assertIsInstance(returned, StateInstrumentInfo)
        self.assertEqual("idf_path", returned.idf_path)
    def test_set_monitor_names(self, mocked_idf_names):
        expected = mock.NonCallableMock()
        mocked_idf_names.return_value = expected

        inst_info = StateInstrumentInfo()
        state_instrument_info._set_monitor_names(inst_info_state=inst_info,
                                                 idf_path=None,
                                                 invalid_monitor_names=None)

        self.assertEqual(expected, inst_info.monitor_names)
    def get_state_obj(monitor_4_dist, monitor_5_dist):
        facility = SANSFacility.ISIS
        file_information = SANSFileInformationMock(instrument=SANSInstrument.ZOOM, run_number=6113)
        data_builder = get_data_builder(facility, file_information)
        data_builder.set_sample_scatter("ZOOM00006113")
        data_builder.set_sample_scatter_period(3)
        data_info = data_builder.build()

        state = AllStates()
        state.move = get_move_builder(data_info).build()
        state.instrument_info = StateInstrumentInfo.build_from_data_info(data_info)
        state.move.monitor_4_offset = monitor_4_dist
        state.move.monitor_5_offset = monitor_5_dist
        return state
Exemple #5
0
    def __init__(self):

        super(AllStates, self).__init__()
        self.data: StateData = StateData()
        self.move: StateMove = StateMove()
        self.instrument_info: StateInstrumentInfo = StateInstrumentInfo()
        self.reduction: StateReductionMode = StateReductionMode()
        self.slice: StateSliceEvent = StateSliceEvent()
        self.mask: StateMask = StateMask()
        self.wavelength: StateWavelength = StateWavelength()
        self.save: StateSave = StateSave()
        self.scale: StateScale = StateScale()
        self.adjustment: StateAdjustment = StateAdjustment()
        self.convert_to_q: StateConvertToQ = StateConvertToQ()
        self.compatibility: StateCompatibility = StateCompatibility()
    def test_that_mask_files_are_applied(self):
        def create_shape_xml_file(xml_string):
            temp_dir = tempfile.gettempdir()
            f_name = os.path.join(temp_dir, 'sample_mask_file.xml')
            if os.path.exists(f_name):
                os.remove(f_name)
            with open(f_name, 'w') as f:
                f.write(xml_string)
            return f_name

        # Arrange
        shape_xml = "<?xml version=\"1.0\"?>\n"\
                    "<detector-masking>\n" \
                    "<group>\n" \
                    "<detids>\n" \
                    "1313191-1313256\n" \
                    "</detids>\n" \
                    "</group>\n" \
                    "</detector-masking >"
        file_name = create_shape_xml_file(shape_xml)

        # Arrange
        data_info = self._build_data_info()
        mask_builder = get_mask_builder(data_info)

        # Mask file
        # Got the spectra from the detector view
        expected_spectra = [x for x in range(31432, 31498)]
        mask_builder.set_mask_files([file_name])
        mask_info = mask_builder.build()

        test_director = TestDirector()
        test_director.set_states(data_state=data_info, mask_state=mask_info)
        state = test_director.construct()

        state.instrument_info = StateInstrumentInfo.build_from_data_info(data_info)

        workspace = self._sans_data

        # Act
        workspace = mask_workspace(state=state, component_as_string='LAB', workspace=workspace)

        # Assert
        self._do_assert(workspace, expected_spectra)

        # Remove
        if os.path.exists(file_name):
            os.remove(file_name)
Exemple #7
0
    def get_all_states(self, file_information) -> AllStates:
        all_states = AllStates()
        all_states.move = self.get_state_move(file_information)
        all_states.reduction = self.get_state_reduction_mode()
        all_states.slice = self.get_state_slice_event()
        all_states.mask = self.get_state_mask(file_information)
        all_states.wavelength = self.get_state_wavelength()
        all_states.save = self.get_state_save()
        all_states.scale = self.get_state_scale(file_information)
        all_states.adjustment = self.get_state_adjustment(file_information)
        all_states.convert_to_q = self.get_state_convert_to_q()
        all_states.compatibility = self.get_state_compatibility()
        all_states.data = self.get_state_data(file_information)
        all_states.instrument_info = StateInstrumentInfo.build_from_data_info(
            all_states.data)

        return all_states
    def test_setting_det_names(self, mocked_ipf_loader):
        mocked_ipf_data = self._prep_mock_det_ipf()
        mocked_ipf_loader.return_value = mocked_ipf_data
        mocked_data_info = mock.NonCallableMock()

        inst_info = StateInstrumentInfo()
        state_instrument_info._set_detector_names(inst_info, mocked_data_info)
        ipf_names = self._get_ipf_names()

        lab_values = inst_info.detector_names[DetectorType.LAB.value]
        hab_values = inst_info.detector_names[DetectorType.HAB.value]
        self.assertEqual(mocked_ipf_data[ipf_names["lab_short"]],
                         lab_values.detector_name_short)
        self.assertEqual(mocked_ipf_data[ipf_names["lab_full"]],
                         lab_values.detector_name)
        self.assertEqual(mocked_ipf_data[ipf_names["hab_short"]],
                         hab_values.detector_name_short)
        self.assertEqual(mocked_ipf_data[ipf_names["hab_full"]],
                         hab_values.detector_name)
def _get_state_obj(instrument, x_translation=None, z_translation=None):
    facility = SANSFacility.ISIS
    file_information = SANSFileInformationMock(instrument=instrument,
                                               run_number=123)
    data_builder = get_data_builder(facility, file_information)
    data_builder.set_sample_scatter("NotSet")
    data_builder.set_sample_scatter_period(3)
    data_info = data_builder.build()

    state_builder = get_move_builder(data_info)
    if x_translation:
        state_builder.set_LAB_x_translation_correction(x_translation)
    if z_translation:
        state_builder.set_LAB_z_translation_correction(z_translation)

    state = AllStates()
    state.move = state_builder.build()
    state.instrument_info = StateInstrumentInfo.build_from_data_info(data_info)

    return state
Exemple #10
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()