def _create_states(self, state_model, table_model, row_index=None): """ Here we create the states based on the settings in the models :param state_model: the state model object :param table_model: the table model object :param row_index: the selected row, if None then all rows are generated """ number_of_rows = self._view.get_number_of_rows() if row_index is not None: # Check if the selected index is valid if row_index >= number_of_rows: return None rows = [row_index] else: rows = range(number_of_rows) states = {} gui_state_director = GuiStateDirector(table_model, state_model, self._facility) for row in rows: self.sans_logger.information( "Generating state for row {}".format(row)) if not self.is_empty_row(row): try: state = gui_state_director.create_state(row) states.update({row: state}) except ValueError as e: self.sans_logger.error( "There was a bad entry for row {}. Ensure that the path to your files has " "been added to the Mantid search directories! See here for more " "details: {}".format(row, str(e))) raise RuntimeError( "There was a bad entry for row {}. Ensure that the path to your files has " "been added to the Mantid search directories! See here for more " "details: {}".format(row, str(e))) return states
def _create_row_state(row_entry, state_model, facility, file_lookup, gui_state_director, user_file): sans_logger.information("Generating state for row {}".format(row_entry)) state = None try: if not row_entry.file_information and file_lookup: error_message = "Trying to find the SANS file {0}, but cannot find it. Make sure that " \ "the relevant paths are added and the correct instrument is selected." raise RuntimeError(error_message.format(row_entry.sample_scatter)) if not row_entry.is_empty(): row_user_file = row_entry.user_file if row_user_file: row_state_model = create_gui_state_from_userfile( row_user_file, state_model) row_gui_state_director = GuiStateDirector( row_state_model, facility) state = row_gui_state_director.create_state( row_entry, file_lookup=file_lookup, user_file=row_user_file) else: state = gui_state_director.create_state( row_entry, file_lookup=file_lookup, user_file=user_file) return state except (ValueError, RuntimeError) as e: return "{}".format(str(e))
def create_state(state_model_with_view_update, file, period, facility): table_row = RowEntries(sample_scatter=file, sample_scatter_period=period) gui_state_director = GuiStateDirector(state_model_with_view_update, facility) state = gui_state_director.create_state(table_row).all_states return state
def test_that_sample_thickness_set_on_state(self): table_model = self._get_table_model(sample_thickness = 78.0) state_model = self._get_state_gui_model() director = GuiStateDirector(table_model, state_model, SANSFacility.ISIS) state = director.create_state(0) self.assertTrue(isinstance(state, State)) self.assertEqual(state.scale.thickness, 78.0)
def test_that_column_options_are_set_on_state(self): table_model = self._get_table_model(option_string="WavelengthMin=3.14,WavelengthMax=10.3") state_model = self._get_state_gui_model() director = GuiStateDirector(table_model, state_model, SANSFacility.ISIS) state = director.create_state(0) self.assertTrue(isinstance(state, State)) self.assertTrue(state.wavelength.wavelength_low == [3.14]) self.assertTrue(state.wavelength.wavelength_high == [10.3])
def test_that_sample_thickness_set_on_state(self): table_model = self._get_table_model() state_model = self._get_state_gui_model() director = GuiStateDirector(table_model, state_model, SANSFacility.ISIS) state = director.create_state(0) self.assertTrue(isinstance(state, State)) self.assertEqual(state.scale.thickness, 1.0)
def test_that_shift_and_scale_set_on_state_from_options_column(self): table_model = self._get_table_model(option_string="MergeScale=1.2,MergeShift=0.5") state_model = self._get_state_gui_model() director = GuiStateDirector(table_model, state_model, SANSFacility.ISIS) state = director.create_state(0) self.assertTrue(isinstance(state, State)) self.assertEqual(state.reduction.merge_scale, 1.2) self.assertEqual(state.reduction.merge_shift, 0.5)
def test_that_column_options_are_set_on_state(self): table_model = self._get_table_model(option_string="WavelengthMin=3.14,WavelengthMax=10.3") state_model = self._get_state_gui_model() director = GuiStateDirector(table_model, state_model, SANSFacility.ISIS) state = director.create_state(0) self.assertTrue(isinstance(state, State)) self.assertTrue(state.wavelength.wavelength_low == [3.14]) self.assertTrue(state.wavelength.wavelength_high == [10.3])
def create_state(state_model_with_view_update, file, period, facility): table_row = TableIndexModel(file, period, '', '', '', '', '', '', '', '', '', '') table = TableModel() table.add_table_entry_no_thread_or_signal(0, table_row) gui_state_director = GuiStateDirector(table, state_model_with_view_update, facility) state = gui_state_director.create_state(0) return state
def test_that_column_options_are_set_on_state(self): state_model = self._get_state_gui_model() director = GuiStateDirector(state_model, SANSFacility.ISIS) row_entry = self._get_row_entry( option_string="WavelengthMin=3.14,WavelengthMax=10.3") state = director.create_state(row_entry) self.assertTrue(isinstance(state, AllStates)) self.assertEqual(state.wavelength.wavelength_low, [3.14]) self.assertEqual(state.wavelength.wavelength_high, [10.3])
def create_state(state_model_with_view_update, file, period, facility): table_row = TableIndexModel(file, period, '', '', '', '', '', '', '', '', '', '') table = TableModel() table.add_table_entry_no_thread_or_signal(0, table_row) gui_state_director = GuiStateDirector(table, state_model_with_view_update, facility) state = gui_state_director.create_state(0) return state
def test_that_shift_and_scale_set_on_state_from_options_column(self): state_model = self._get_state_gui_model() director = GuiStateDirector(state_model, SANSFacility.ISIS) state = self._get_row_entry( option_string="MergeScale=1.2,MergeShift=0.5") state = director.create_state(state) self.assertTrue(isinstance(state, StateGuiModel)) self.assertEqual(state.all_states.reduction.merge_scale, 1.2) self.assertEqual(state.all_states.reduction.merge_shift, 0.5)
def test_that_column_options_are_set_on_state(self): state_model = self._get_state_gui_model() director = GuiStateDirector(state_model, SANSFacility.ISIS) row_entry = self._get_row_entry( option_string="WavelengthMin=3.14,WavelengthMax=10.3") state = director.create_state(row_entry) self.assertTrue(isinstance(state, StateGuiModel)) self.assertEqual( state.all_states.wavelength.wavelength_interval. wavelength_full_range, (3.14, 10.3))
def test_state_created_with_default_sample_thickness_when_file_lookup_disabled(self): table_model = self._get_table_model() state_model = self._get_state_gui_model() director = GuiStateDirector(table_model, state_model, SANSFacility.ISIS) state = director.create_state(0, file_lookup=False) self.assertTrue(isinstance(state, State)) self.assertEqual(state.scale.thickness_from_file, 1.0) self.assertEqual(state.scale.height_from_file, 8.0) self.assertEqual(state.scale.width_from_file, 8.0) self.assertEqual(state.scale.thickness, 8.0)
def test_that_can_construct_state_from_models(self): state_model = self._get_state_gui_model() director = GuiStateDirector(state_model, SANSFacility.ISIS) state = director.create_state(self._get_row_entry()) self.assertTrue(isinstance(state, AllStates)) try: state.validate() has_raised = False except ValueError: has_raised = True self.assertFalse(has_raised) self.assertEqual(state.wavelength.wavelength_low, [1.5]) self.assertEqual(state.wavelength.wavelength_high, [12.5])
def test_that_can_construct_state_from_models(self): table_model = self._get_table_model() state_model = self._get_state_gui_model() director = GuiStateDirector(table_model, state_model, SANSFacility.ISIS) state = director.create_state(0) self.assertTrue(isinstance(state, State)) try: state.validate() has_raised = False except ValueError: has_raised = True self.assertFalse(has_raised) self.assertTrue(state.wavelength.wavelength_low == [1.5]) self.assertTrue(state.wavelength.wavelength_high == [12.5])
def test_that_can_construct_state_from_models(self): table_model = self._get_table_model() state_model = self._get_state_gui_model() director = GuiStateDirector(table_model, state_model, SANSFacility.ISIS) state = director.create_state(0) self.assertTrue(isinstance(state, State)) try: state.validate() has_raised = False except ValueError: has_raised = True self.assertFalse(has_raised) self.assertTrue(state.wavelength.wavelength_low == [1.5]) self.assertTrue(state.wavelength.wavelength_high == [12.5])
def test_save_settings_copied_from_gui(self): state_model = mock.Mock(spec=self._get_state_gui_model()) state_model.all_states.save = mock.NonCallableMock() # Copy the top level model and reset save rather than load a file copied_state = copy.deepcopy(state_model) copied_state.all_states.save = None director = GuiStateDirector(state_model, SANSFacility.ISIS) director._load_current_state = mock.Mock(return_value=copied_state) new_state = director.create_state(self._get_row_entry(), row_user_file="NotThere.txt") self.assertEqual(state_model.all_states.save, new_state.all_states.save)
def test_that_can_construct_state_from_models(self): state_model = self._get_state_gui_model() director = GuiStateDirector(state_model, SANSFacility.ISIS) state = director.create_state(self._get_row_entry()) self.assertTrue(isinstance(state, StateGuiModel)) state = state.all_states try: state.validate() has_raised = False except ValueError: has_raised = True self.assertFalse(has_raised) self.assertEqual( state.wavelength.wavelength_interval.wavelength_full_range, (1.5, 12.5))
def create_states(state_model, table_model, instrument, facility, row_index=None, file_lookup=True): """ Here we create the states based on the settings in the models :param state_model: the state model object :param table_model: the table model object :param row_index: the selected row, if None then all rows are generated """ number_of_rows = table_model.get_number_of_rows() rows = [x for x in row_index if x < number_of_rows] states = {} errors = {} gui_state_director = GuiStateDirector(table_model, state_model, facility) for row in rows: state = _create_row_state(row, table_model, state_model, facility, instrument, file_lookup, gui_state_director) if isinstance(state, State): states.update({row: state}) elif isinstance(state, str): errors.update({row: state}) return states, errors
def test_reduction_dim_copied_from_gui(self): state_model = mock.Mock(spec=self._get_state_gui_model()) expected_dim = mock.NonCallableMock() # This is set by the user on the main GUI, so should be copied in with a custom user file still state_model.reduction_dimensionality = expected_dim # Copy the top level model and reset dim rather than load a file copied_state = copy.deepcopy(state_model) copied_state.reduction_dimensionality = None director = GuiStateDirector(state_model, SANSFacility.ISIS) director._load_current_state = mock.Mock(return_value=copied_state) state = director.create_state(self._get_row_entry(), row_user_file="NotThere.txt") self.assertEqual(expected_dim, state.reduction_dimensionality)
def create_states(state_model, facility, row_entries=None, file_lookup=True, user_file=""): """ Here we create the states based on the settings in the models :param state_model: the state model object :param row_entries: a list of row entry objects to create state for :param user_file: the user file under which the data is reduced """ states = {} errors = {} gui_state_director = GuiStateDirector(state_model, facility) for row in row_entries: _get_thickness_for_row(row) state = _create_row_state(row, state_model, facility, file_lookup, gui_state_director, user_file) if isinstance(state, AllStates): states.update({row: state}) elif isinstance(state, str): errors.update({row: state}) return states, errors
def test_that_will_raise_when_models_are_incomplete(self): table_index_model = TableIndexModel(0, "", "", "", "", "", "", "", "", "", "", "", "") table_model = TableModel() table_model.add_table_entry(0, table_index_model) state_model = self._get_state_gui_model() director = GuiStateDirector(table_model, state_model, SANSFacility.ISIS) self.assertRaises(ValueError, director.create_state, 0)
def create_states(state_model, table_model, instrument, facility, row_index=None, file_lookup=True): """ Here we create the states based on the settings in the models :param state_model: the state model object :param table_model: the table model object :param row_index: the selected row, if None then all rows are generated """ number_of_rows = table_model.get_number_of_rows() if row_index is not None: # Check if the selected index is valid if row_index >= number_of_rows: return None rows = [row_index] else: rows = range(number_of_rows) states = {} gui_state_director = GuiStateDirector(table_model, state_model, facility) for row in rows: sans_logger.information("Generating state for row {}".format(row)) if not __is_empty_row(row, table_model): row_user_file = table_model.get_row_user_file(row) if row_user_file: row_state_model = create_gui_state_from_userfile( row_user_file, state_model) row_gui_state_director = GuiStateDirector( table_model, row_state_model, facility) state = __create_row_state(row_gui_state_director, row, instrument, file_lookup=file_lookup) states.update({row: state}) else: state = __create_row_state(gui_state_director, row, instrument, file_lookup=file_lookup) states.update({row: state}) return states
def _create_row_state(row, table_model, state_model, facility, instrument, file_lookup, gui_state_director): try: sans_logger.information("Generating state for row {}".format(row)) state = None if not __is_empty_row(row, table_model): row_user_file = table_model.get_row_user_file(row) if row_user_file: row_state_model = create_gui_state_from_userfile( row_user_file, state_model) row_gui_state_director = GuiStateDirector( table_model, row_state_model, facility) state = row_gui_state_director.create_state( row, instrument=instrument, file_lookup=file_lookup) else: state = gui_state_director.create_state( row, instrument=instrument, file_lookup=file_lookup) return state except (ValueError, RuntimeError) as e: return "{}".format(str(e))
def _create_states(self, state_model, table_model, row_index=None): """ Here we create the states based on the settings in the models :param state_model: the state model object :param table_model: the table model object :param row_index: the selected row, if None then all rows are generated """ number_of_rows = self._view.get_number_of_rows() if row_index is not None: # Check if the selected index is valid if row_index >= number_of_rows: return None rows = [row_index] else: rows = range(number_of_rows) states = {} gui_state_director = GuiStateDirector(table_model, state_model, self._facility) for row in rows: self.sans_logger.information( "Generating state for row {}".format(row)) if not self.is_empty_row(row): row_user_file = table_model.get_row_user_file(row) if row_user_file: user_file_path = FileFinder.getFullPath(row_user_file) if not os.path.exists(user_file_path): raise RuntimeError( "The user path {} does not exist. Make sure a valid user file path" " has been specified.".format(user_file_path)) user_file_reader = UserFileReader(user_file_path) user_file_items = user_file_reader.read_user_file() row_state_model = StateGuiModel(user_file_items) row_gui_state_director = GuiStateDirector( table_model, row_state_model, self._facility) self._create_row_state(row_gui_state_director, states, row) else: self._create_row_state(gui_state_director, states, row) return states
def test_that_will_raise_when_models_are_incomplete(self): state_model = self._get_state_gui_model() director = GuiStateDirector(state_model, SANSFacility.ISIS) with self.assertRaises(ValueError): director.create_state(RowEntries())