def _process_trans_fit(self, command): def fit_type_to_data_type(fit_type_to_convert): return DataType.CAN if fit_type_to_convert is FitData.Can else DataType.SAMPLE fit_data = command.values[0] wavelength_low = command.values[1] wavelength_high = command.values[2] fit_type = command.values[3] polynomial_order = command.values[4] if fit_data is FitData.Both: data_to_fit = [FitData.Sample, FitData.Can] else: data_to_fit = [fit_data] new_state_entries = {} for element in data_to_fit: data_type = fit_type_to_data_type(element) new_state_entries.update({ FitId.GENERAL: fit_general(start=wavelength_low, stop=wavelength_high, fit_type=fit_type, data_type=data_type, polynomial_order=polynomial_order) }) self.add_to_processed_state_settings(new_state_entries)
def _set_transmission_fit(self, data_type, start=None, stop=None, fit_type=None, polynomial_order=None): if FitId.GENERAL in self._user_file_items: # Gather all settings which correspond to the data type and where the data type is none settings = self._user_file_items[FitId.GENERAL] settings_general = [setting for setting in settings if setting.data_type is None] settings_for_data_type = [setting for setting in settings if setting.data_type is data_type] # We check if there are data-type specific settings. # 1. There are data type specific settings. Then we are good. # 2. There are no data type specific settings. We create one data type specific setting and populate it # with a general setting if it exists else we create a new entry if not settings_for_data_type: if settings_general: setting_general = settings_general[-1] settings.append(fit_general(start=setting_general.start, stop=setting_general.stop, data_type=data_type, fit_type=setting_general.fit_type, polynomial_order=setting_general.polynomial_order)) else: settings.append(fit_general(start=None, stop=None, data_type=data_type, fit_type=FitType.NO_FIT, polynomial_order=2)) else: settings = [fit_general(start=None, stop=None, data_type=data_type, fit_type=FitType.NO_FIT, polynomial_order=2)] new_settings = [] for setting in settings: # We only want to modify the settings which are either the data type specific ones or the ones which # don't have a specific data type if setting.data_type is data_type and setting.data_type is not None: new_start = start if start is not None else setting.start new_stop = stop if stop is not None else setting.stop new_fit_type = fit_type if fit_type is not None else setting.fit_type new_polynomial_order = polynomial_order if polynomial_order is not None else setting.polynomial_order new_settings.append(fit_general(start=new_start, stop=new_stop, fit_type=new_fit_type, data_type=setting.data_type, polynomial_order=new_polynomial_order)) else: new_settings.append(setting) self._user_file_items.update({FitId.GENERAL: new_settings})
def _set_transmission_fit(self, data_type, start=None, stop=None, fit_type=None, polynomial_order=None): if FitId.general in self._user_file_items: # Gather all settings which correspond to the data type and where the data type is none settings = self._user_file_items[FitId.general] settings_general = [setting for setting in settings if setting.data_type is None] settings_for_data_type = [setting for setting in settings if setting.data_type is data_type] # We check if there are data-type specific settings. # 1. There are data type specific settings. Then we are good. # 2. There are no data type specific settings. We create one data type specific setting and populate it # with a general setting if it exists else we create a new entry if not settings_for_data_type: if settings_general: setting_general = settings_general[-1] settings.append(fit_general(start=setting_general.start, stop=setting_general.stop, data_type=data_type, fit_type=setting_general.fit_type, polynomial_order=setting_general.polynomial_order)) else: settings.append(fit_general(start=None, stop=None, data_type=data_type, fit_type=FitType.NoFit, polynomial_order=2)) else: settings = [fit_general(start=None, stop=None, data_type=data_type, fit_type=FitType.NoFit, polynomial_order=2)] new_settings = [] for setting in settings: # We only want to modify the settings which are either the data type specific ones or the ones which # don't have a specific data type if setting.data_type is data_type and setting.data_type is not None: new_start = start if start is not None else setting.start new_stop = stop if stop is not None else setting.stop new_fit_type = fit_type if fit_type is not None else setting.fit_type new_polynomial_order = polynomial_order if polynomial_order is not None else setting.polynomial_order new_settings.append(fit_general(start=new_start, stop=new_stop, fit_type=new_fit_type, data_type=setting.data_type, polynomial_order=new_polynomial_order)) else: new_settings.append(setting) self._user_file_items.update({FitId.general: new_settings})
def _process_trans_fit(self, command): def fit_type_to_data_type(fit_type_to_convert): return DataType.Can if fit_type_to_convert is FitData.Can else DataType.Sample fit_data = command.values[0] wavelength_low = command.values[1] wavelength_high = command.values[2] fit_type = command.values[3] polynomial_order = command.values[4] if fit_data is FitData.Both: data_to_fit = [FitData.Sample, FitData.Can] else: data_to_fit = [fit_data] new_state_entries = {} for element in data_to_fit: data_type = fit_type_to_data_type(element) new_state_entries.update({FitId.general: fit_general(start=wavelength_low, stop=wavelength_high, fit_type=fit_type, data_type=data_type, polynomial_order=polynomial_order)}) self.add_to_processed_state_settings(new_state_entries)
def test_that_can_read_user_file(self): # Arrange user_file_path = create_user_file(sample_user_file) reader = UserFileReader(user_file_path) # Act output = reader.read_user_file() # Assert expected_values = {LimitsId.wavelength: [simple_range(start=1.5, stop=12.5, step=0.125, step_type=RangeStepType.Lin)], LimitsId.q: [q_rebin_values(min=.001, max=.2, rebin_string="0.001,0.001,0.0126,-0.08,0.2")], LimitsId.qxy: [simple_range(0, 0.05, 0.001, RangeStepType.Lin)], BackId.single_monitors: [back_single_monitor_entry(1, 35000, 65000), back_single_monitor_entry(2, 85000, 98000)], DetectorId.reduction_mode: [ISISReductionMode.LAB], GravityId.on_off: [True], FitId.general: [fit_general(start=1.5, stop=12.5, fit_type=FitType.Logarithmic, data_type=None, polynomial_order=0)], MaskId.vertical_single_strip_mask: [single_entry_with_detector(191, DetectorType.LAB), single_entry_with_detector(191, DetectorType.HAB), single_entry_with_detector(0, DetectorType.LAB), single_entry_with_detector(0, DetectorType.HAB)], MaskId.horizontal_single_strip_mask: [single_entry_with_detector(0, DetectorType.LAB), single_entry_with_detector(0, DetectorType.HAB)], MaskId.horizontal_range_strip_mask: [range_entry_with_detector(190, 191, DetectorType.LAB), range_entry_with_detector(167, 172, DetectorType.LAB), range_entry_with_detector(190, 191, DetectorType.HAB), range_entry_with_detector(156, 159, DetectorType.HAB) ], MaskId.time: [range_entry_with_detector(17500, 22000, None)], MonId.direct: [monitor_file("DIRECTM1_15785_12m_31Oct12_v12.dat", DetectorType.LAB), monitor_file("DIRECTM1_15785_12m_31Oct12_v12.dat", DetectorType.HAB)], MonId.spectrum: [monitor_spectrum(1, True, True), monitor_spectrum(1, False, True)], SetId.centre: [position_entry(155.45, -169.6, DetectorType.LAB)], SetId.scales: [set_scales_entry(0.074, 1.0, 1.0, 1.0, 1.0)], SampleId.offset: [53.0], DetectorId.correction_x: [single_entry_with_detector(-16.0, DetectorType.LAB), single_entry_with_detector(-44.0, DetectorType.HAB)], DetectorId.correction_y: [single_entry_with_detector(-20.0, DetectorType.HAB)], DetectorId.correction_z: [single_entry_with_detector(47.0, DetectorType.LAB), single_entry_with_detector(47.0, DetectorType.HAB)], DetectorId.correction_rotation: [single_entry_with_detector(0.0, DetectorType.HAB)], LimitsId.events_binning: ["7000.0,500.0,60000.0"], MaskId.clear_detector_mask: [True], MaskId.clear_time_mask: [True], LimitsId.radius: [range_entry(12, 15)], TransId.spec_shift: [-70.], PrintId.print_line: ["for changer"], BackId.all_monitors: [range_entry(start=3500, stop=4500)], FitId.monitor_times: [range_entry(start=1000, stop=2000)], TransId.spec: [4], BackId.trans: [range_entry(start=123, stop=466)], TransId.radius: [7.0], TransId.roi: ["test.xml", "test2.xml"], TransId.mask: ["test3.xml", "test4.xml"], SampleId.path: [True], LimitsId.radius_cut: [200.0], LimitsId.wavelength_cut: [8.0], QResolutionId.on: [True], QResolutionId.delta_r: [11.], QResolutionId.collimation_length: [12.], QResolutionId.a1: [13.], QResolutionId.a2: [14.], QResolutionId.moderator: ["moderator_rkh_file.txt"], TubeCalibrationFileId.file: ["TUBE_SANS2D_BOTH_31681_25Sept15.nxs"]} self.assertEqual(len(expected_values), len(output)) for key, value in list(expected_values.items()): self.assertTrue(key in output) self.assertEqual(len(output[key]), len(value)) elements = output[key] # Make sure that the different entries are sorted UserFileReaderTest._sort_list(elements) UserFileReaderTest._sort_list(value) self.assertEqual(elements, value, "{} is not {}".format(elements, value)) # clean up if os.path.exists(user_file_path): os.remove(user_file_path)
def test_that_can_read_user_file(self): # Arrange user_file_path = create_user_file(sample_user_file) reader = UserFileReader(user_file_path) # Act output = reader.read_user_file() # Assert expected_values = { LimitsId.wavelength: [ simple_range(start=1.5, stop=12.5, step=0.125, step_type=RangeStepType.Lin) ], LimitsId.q: [ q_rebin_values(min=.001, max=.2, rebin_string="0.001,0.001,0.0126,-0.08,0.2") ], LimitsId.qxy: [simple_range(0, 0.05, 0.001, RangeStepType.Lin)], BackId.single_monitors: [ back_single_monitor_entry(1, 35000, 65000), back_single_monitor_entry(2, 85000, 98000) ], DetectorId.reduction_mode: [ISISReductionMode.LAB], GravityId.on_off: [True], FitId.general: [ fit_general(start=1.5, stop=12.5, fit_type=FitType.Logarithmic, data_type=None, polynomial_order=0) ], MaskId.vertical_single_strip_mask: [ single_entry_with_detector(191, DetectorType.LAB), single_entry_with_detector(191, DetectorType.HAB), single_entry_with_detector(0, DetectorType.LAB), single_entry_with_detector(0, DetectorType.HAB) ], MaskId.horizontal_single_strip_mask: [ single_entry_with_detector(0, DetectorType.LAB), single_entry_with_detector(0, DetectorType.HAB) ], MaskId.horizontal_range_strip_mask: [ range_entry_with_detector(190, 191, DetectorType.LAB), range_entry_with_detector(167, 172, DetectorType.LAB), range_entry_with_detector(190, 191, DetectorType.HAB), range_entry_with_detector(156, 159, DetectorType.HAB) ], MaskId.time: [range_entry_with_detector(17500, 22000, None)], MonId.direct: [ monitor_file("DIRECTM1_15785_12m_31Oct12_v12.dat", DetectorType.LAB), monitor_file("DIRECTM1_15785_12m_31Oct12_v12.dat", DetectorType.HAB) ], MonId.spectrum: [ monitor_spectrum(1, True, True), monitor_spectrum(1, False, True) ], SetId.centre: [position_entry(155.45, -169.6, DetectorType.LAB)], SetId.scales: [set_scales_entry(0.074, 1.0, 1.0, 1.0, 1.0)], SampleId.offset: [53.0], DetectorId.correction_x: [ single_entry_with_detector(-16.0, DetectorType.LAB), single_entry_with_detector(-44.0, DetectorType.HAB) ], DetectorId.correction_y: [single_entry_with_detector(-20.0, DetectorType.HAB)], DetectorId.correction_z: [ single_entry_with_detector(47.0, DetectorType.LAB), single_entry_with_detector(47.0, DetectorType.HAB) ], DetectorId.correction_rotation: [single_entry_with_detector(0.0, DetectorType.HAB)], LimitsId.events_binning: ["7000.0,500.0,60000.0"], MaskId.clear_detector_mask: [True], MaskId.clear_time_mask: [True], LimitsId.radius: [range_entry(12, 15)], TransId.spec_shift: [-70.], PrintId.print_line: ["for changer"], BackId.all_monitors: [range_entry(start=3500, stop=4500)], FitId.monitor_times: [range_entry(start=1000, stop=2000)], TransId.spec: [4], BackId.trans: [range_entry(start=123, stop=466)], TransId.radius: [7.0], TransId.roi: ["test.xml", "test2.xml"], TransId.mask: ["test3.xml", "test4.xml"], SampleId.path: [True], LimitsId.radius_cut: [200.0], LimitsId.wavelength_cut: [8.0], QResolutionId.on: [True], QResolutionId.delta_r: [11.], QResolutionId.collimation_length: [12.], QResolutionId.a1: [13.], QResolutionId.a2: [14.], QResolutionId.moderator: ["moderator_rkh_file.txt"], TubeCalibrationFileId.file: ["TUBE_SANS2D_BOTH_31681_25Sept15.nxs"] } self.assertTrue(len(expected_values) == len(output)) for key, value in list(expected_values.items()): self.assertTrue(key in output) self.assertTrue(len(output[key]) == len(value)) elements = output[key] # Make sure that the different entries are sorted UserFileReaderTest._sort_list(elements) UserFileReaderTest._sort_list(value) self.assertTrue(elements == value) # clean up if os.path.exists(user_file_path): os.remove(user_file_path)
def test_that_can_read_user_file(self): # Arrange user_file_path = create_user_file(sample_user_file) reader = UserFileReader(user_file_path) # Act output = reader.read_user_file() # Assert expected_values = { LimitsId.WAVELENGTH: [ simple_range(start=1.5, stop=12.5, step=0.125, step_type=RangeStepType.LIN) ], LimitsId.Q: [ q_rebin_values(min=.001, max=.2, rebin_string="0.001,0.001,0.0126,-0.08,0.2") ], LimitsId.QXY: [simple_range(0, 0.05, 0.001, RangeStepType.LIN)], BackId.SINGLE_MONITORS: [ back_single_monitor_entry(1, 35000, 65000), back_single_monitor_entry(2, 85000, 98000) ], DetectorId.REDUCTION_MODE: [ReductionMode.LAB], GravityId.ON_OFF: [True], FitId.GENERAL: [ fit_general(start=1.5, stop=12.5, fit_type=FitType.LOGARITHMIC, data_type=None, polynomial_order=0) ], MaskId.VERTICAL_SINGLE_STRIP_MASK: [ single_entry_with_detector(191, DetectorType.LAB), single_entry_with_detector(191, DetectorType.HAB), single_entry_with_detector(0, DetectorType.LAB), single_entry_with_detector(0, DetectorType.HAB) ], MaskId.HORIZONTAL_SINGLE_STRIP_MASK: [ single_entry_with_detector(0, DetectorType.LAB), single_entry_with_detector(0, DetectorType.HAB) ], MaskId.HORIZONTAL_RANGE_STRIP_MASK: [ range_entry_with_detector(190, 191, DetectorType.LAB), range_entry_with_detector(167, 172, DetectorType.LAB), range_entry_with_detector(190, 191, DetectorType.HAB), range_entry_with_detector(156, 159, DetectorType.HAB) ], MaskId.TIME: [range_entry_with_detector(17500, 22000, None)], MonId.DIRECT: [ monitor_file("DIRECTM1_15785_12m_31Oct12_v12.dat", DetectorType.LAB), monitor_file("DIRECTM1_15785_12m_31Oct12_v12.dat", DetectorType.HAB) ], MonId.SPECTRUM: [ monitor_spectrum(1, True, True), monitor_spectrum(1, False, True) ], SetId.CENTRE: [position_entry(155.45, -169.6, DetectorType.LAB)], SetId.SCALES: [set_scales_entry(0.074, 1.0, 1.0, 1.0, 1.0)], SampleId.OFFSET: [53.0], DetectorId.CORRECTION_X: [ single_entry_with_detector(-16.0, DetectorType.LAB), single_entry_with_detector(-44.0, DetectorType.HAB) ], DetectorId.CORRECTION_Y: [single_entry_with_detector(-20.0, DetectorType.HAB)], DetectorId.CORRECTION_Z: [ single_entry_with_detector(47.0, DetectorType.LAB), single_entry_with_detector(47.0, DetectorType.HAB) ], DetectorId.CORRECTION_ROTATION: [single_entry_with_detector(0.0, DetectorType.HAB)], LimitsId.EVENTS_BINNING: ["7000.0,500.0,60000.0"], MaskId.CLEAR_DETECTOR_MASK: [True], MaskId.CLEAR_TIME_MASK: [True], LimitsId.RADIUS: [range_entry(12, 15)], TransId.SPEC_4_SHIFT: [-70.], PrintId.PRINT_LINE: ["for changer"], BackId.ALL_MONITORS: [range_entry(start=3500, stop=4500)], FitId.MONITOR_TIMES: [range_entry(start=1000, stop=2000)], TransId.SPEC: [4], BackId.TRANS: [range_entry(start=123, stop=466)], TransId.RADIUS: [7.0], TransId.ROI: ["test.xml", "test2.xml"], TransId.MASK: ["test3.xml", "test4.xml"], SampleId.PATH: [True], LimitsId.RADIUS_CUT: [200.0], LimitsId.WAVELENGTH_CUT: [8.0], QResolutionId.ON: [True], QResolutionId.DELTA_R: [11.], QResolutionId.COLLIMATION_LENGTH: [12.], QResolutionId.A1: [13.], QResolutionId.A2: [14.], QResolutionId.MODERATOR: ["moderator_rkh_file.txt"], TubeCalibrationFileId.FILE: ["TUBE_SANS2D_BOTH_31681_25Sept15.nxs"] } self.assertEqual(len(expected_values), len(output)) for key, value in list(expected_values.items()): self.assertTrue(key in output) self.assertEqual(len(output[key]), len(value)) elements = output[key] # Make sure that the different entries are sorted UserFileReaderTest._sort_list(elements) UserFileReaderTest._sort_list(value) self.assertEqual(elements, value, "{} is not {}".format(elements, value)) # clean up if os.path.exists(user_file_path): os.remove(user_file_path)