def test_CellBoxModelModel(): file_path = QUrl(TEST_FILE).toLocalFile() calculator = CryspyCalculator(file_path) interface = QtCalculatorInterface(calculator) m = Model() m.setCalculatorInterface(interface) assert m._x_role == 257 assert m._y_role == 258 assert m._z_role == 259 assert isinstance(m._model, QStandardItemModel) assert isinstance(m._project_dict, ProjectDict) assert len(m._model.roleNames()) == 3 # assure _setModelFromProject got called assert m._model.rowCount() == 3000 assert m._model.columnCount() == 1 # Test stuff from _setModelFromProject here assert m._model.item(0, 0).data(role=m._x_role) == 0.0 assert m._model.item(0, 0).data(role=m._y_role) == 0.0 assert m._model.item(0, 0).data(role=m._z_role) == 0.0 assert m._model.item(2999, 0).data(role=m._x_role) == 8.36212 assert m._model.item(2999, 0).data(role=m._y_role) == 8.36212 assert m._model.item(2999, 0).data(role=m._z_role) == pytest.approx(8.32867) # test asModel assert m._model == m.asModel()
def test_CellParametersModel(): file_path = QUrl(TEST_FILE).toLocalFile() calculator = CryspyCalculator(file_path) interface = QtCalculatorInterface(calculator) m = Model() m.setCalculatorInterface(interface) assert m._a_role == 257 assert m._gamma_role == 262 assert isinstance(m._model, QStandardItemModel) assert isinstance(m._project_dict, ProjectDict) assert len(m._model.roleNames()) == 6 # assure _setModelFromProject got called assert m._model.rowCount() == 1 assert m._model.columnCount() == 1 # Test stuff from _setModelFromProject here assert m._model.item(0, 0).data(role=m._a_role) == 8.36212 assert m._model.item(0, 0).data(role=m._b_role) == 8.36212 assert m._model.item(0, 0).data(role=m._c_role) == 8.36212 assert m._model.item(0, 0).data(role=m._alpha_role) == 90.0 assert m._model.item(0, 0).data(role=m._beta_role) == 90.0 assert m._model.item(0, 0).data(role=m._gamma_role) == 90.0 # test asModel assert m._model == m.asModel()
def test_CalculatedDataModel(): file_path = QUrl(TEST_FILE).toLocalFile() calculator = CryspyCalculator(file_path) interface = QtCalculatorInterface(calculator) m = Model() m.setCalculatorInterface(interface) assert isinstance(m._model, QStandardItemModel) assert isinstance(m._headers_model, QStandardItemModel) assert isinstance(m._project_dict, ProjectDict) # assure _setModelFromProject got called assert m._model.rowCount() == 381 assert m._model.columnCount() == 8 assert m._headers_model.rowCount() == 1 assert m._headers_model.columnCount() == 8 # Test stuff from _setModelFromProject here assert m._model.item(0, 0).data(role=Qt.DisplayRole) == 4.0 assert pytest.approx( m._model.item(0, 3).data(role=Qt.DisplayRole), 438.3046174533981) assert m._model.item(380, 0).data(role=Qt.DisplayRole) == 80.0 assert pytest.approx( m._model.item(380, 3).data(role=Qt.DisplayRole), -58.83263649312255) # test asModel assert m._model == m.asModel() assert m._headers_model == m.asHeadersModel()
def test_FileStructureModel(): file_path = QUrl(TEST_FILE).toLocalFile() calculator = CryspyCalculator(file_path) interface = QtCalculatorInterface(calculator) m = Model() m.setCalculatorInterface(interface) assert isinstance(m._model, QStandardItemModel) # assure _setModelFromProject got called assert m._model.rowCount() == 1 assert m._model.columnCount() == 1 phaseRole = Qt.UserRole + 1 expRole = Qt.UserRole + 2 calcRole = Qt.UserRole + 3 assert len(m._model.roleNames()) == 3 assert m._model.roleNames()[phaseRole] == b"phasesRole" assert str(m._model.roleNames()[expRole]) == "b'experimentsRole'" assert str(m._model.roleNames()[calcRole]) == "b'calculationsRole'" assert 'data_Fe3O4' in m._model.item(0, 0).data(role=phaseRole) assert 'data_pd' in m._model.item(0, 0).data(role=expRole) assert '_refln_index_k' in m._model.item(0, 0).data(role=calcRole)
def __init__(self, release_config_file_path, parent=None): self.__log = logger.getLogger(__name__) self.__log.info("") super().__init__(parent) self.info = Config(release_config_file_path)['release'] self.projectChanged.connect(self.onProjectChanged) self._project_rcif_path = None self._samples_rcif_path = None self._experiment_rcif_path = None self._calculator_interface = QtCalculatorInterface(CryspyCalculator()) self._project_dict_copy = {} self._project_control = ProjectControl() self._measured_data_model = MeasuredDataModel() self._calculated_data_model = CalculatedDataModel() self._bragg_peaks_model = BraggPeaksModel() self._cell_parameters_model = CellParametersModel() self._cell_box_model = CellBoxModel() self._atom_sites_model = AtomSitesModel() self._atom_adps_model = AtomAdpsModel() self._atom_msps_model = AtomMspsModel() self._fitables_model = FitablesModel() self._status_model = StatusModel() self._refine_thread = None self._refinement_running = False self._refinement_done = False self._refinement_result = {} self._calculator_interface.clearUndoStack() self._need_to_save = False
def no_project_proxy(): no_project_proxy = ProxyPyQml(release_config_file_path) cal = CryspyCalculator(None) no_project_proxy._calculator_interface = CalculatorInterface(cal) no_project_proxy.initialize() no_project_proxy._calculator_interface.setPhaseDefinition('Tests/Data/samples.cif') return no_project_proxy
def test_AtomMspsModel(): file_path = QUrl(TEST_FILE).toLocalFile() calculator = CryspyCalculator(file_path) interface = QtCalculatorInterface(calculator) m = Model() m.setCalculatorInterface(interface) assert isinstance(m._model, QStandardItemModel) assert isinstance(m._project_dict, ProjectDict) assert m._label_role == 257 assert m._chi23_role == 265 # assure _setModelFromProject got called assert m._model.rowCount() == 3 assert m._model.columnCount() == 1 # Test stuff from _setModelFromProject here assert m._model.item(0, 0).data() == 'Fe3A' assert m._model.item(0, 0).data(role=m._type_role) == 'Cani' assert m._model.item(0, 0).data(role=m._chiiso_role) == '' assert m._model.item(0, 0).data(role=m._chi11_role) == -3.468 assert m._model.item(0, 0).data(role=m._chi22_role) == -3.468 assert m._model.item(0, 0).data(role=m._chi33_role) == -3.468 assert m._model.item(0, 0).data(role=m._chi12_role) == 0.0 assert m._model.item(0, 0).data(role=m._chi13_role) == 0.0 assert m._model.item(0, 0).data(role=m._chi23_role) == 0.0 assert m._model.item(1, 0).data() == 'Fe3B' assert m._model.item(1, 0).data(role=m._type_role) == 'Cani' assert m._model.item(1, 0).data(role=m._chiiso_role) == '' assert m._model.item(1, 0).data(role=m._chi11_role) == 3.041 assert m._model.item(1, 0).data(role=m._chi22_role) == 3.041 assert m._model.item(1, 0).data(role=m._chi33_role) == 3.041 assert m._model.item(1, 0).data(role=m._chi12_role) == 0.0 assert m._model.item(1, 0).data(role=m._chi13_role) == 0.0 assert m._model.item(1, 0).data(role=m._chi23_role) == 0.0 assert m._model.item(2, 0).data() == 'O' assert m._model.item(2, 0).data(role=m._type_role) is None assert m._model.item(2, 0).data(role=m._chi11_role) is None assert m._model.item(2, 0).data(role=m._chi11_role) is None assert m._model.item(2, 0).data(role=m._chi22_role) is None assert m._model.item(2, 0).data(role=m._chi33_role) is None assert m._model.item(2, 0).data(role=m._chi12_role) is None assert m._model.item(2, 0).data(role=m._chi13_role) is None assert m._model.item(2, 0).data(role=m._chi23_role) is None # test asModel assert m._model == m.asModel()
def test_CellBoxModel_bad_calculator(): calculator = None # null calculator with pytest.raises(AttributeError): m = Model() m.setCalculatorInterface(calculator) # null file file_path = None # with pytest.raises(TypeError): calculator = CryspyCalculator(file_path) # epty file file_path = QUrl("file:Tests/Data/empty.cif").toLocalFile() # apparently it is fine now - no exception raised calculator = CryspyCalculator(file_path) # old style rcif file_path = QUrl("file:Tests/Data/full.rcif").toLocalFile() # with pytest.raises(AttributeError): calculator = CryspyCalculator(file_path)
def test_AtomSitesModel(): file_path = QUrl(TEST_FILE).toLocalFile() calculator = CryspyCalculator(file_path) interface = QtCalculatorInterface(calculator) m = Model() m.setCalculatorInterface(interface) assert isinstance(m._model, QStandardItemModel) assert isinstance(m._project_dict, ProjectDict) assert m._label_role == 257 assert m._occupancy_role == 263 # assure _setModelFromProject got called assert m._model.rowCount() == 3 assert m._model.columnCount() == 1 # Test stuff from _setModelFromProject here assert m._model.item(0, 0).data() == 'Fe3A' assert m._model.item(0, 0).data(role=m._atom_role) == 'Fe3+' assert m._model.item(0, 0).data(role=m._color_role) == 0.945 assert m._model.item(0, 0).data(role=m._x_role) == 0.125 assert m._model.item(0, 0).data(role=m._y_role) == 0.125 assert m._model.item(0, 0).data(role=m._z_role) == 0.125 assert m._model.item(0, 0).data(role=m._occupancy_role) == 1.0 assert m._model.item(1, 0).data() == 'Fe3B' assert m._model.item(1, 0).data(role=m._atom_role) == 'Fe3+' assert m._model.item(1, 0).data(role=m._color_role) == 0.945 assert m._model.item(1, 0).data(role=m._x_role) == 0.5 assert m._model.item(1, 0).data(role=m._y_role) == 0.5 assert m._model.item(1, 0).data(role=m._z_role) == 0.5 assert m._model.item(1, 0).data(role=m._occupancy_role) == 1.0 assert m._model.item(2, 0).data() == 'O' assert m._model.item(2, 0).data(role=m._atom_role) == 'O2-' assert m._model.item(2, 0).data(role=m._color_role) == 0.5803 assert m._model.item(2, 0).data(role=m._x_role) == 0.25521 assert m._model.item(2, 0).data(role=m._y_role) == 0.25521 assert m._model.item(2, 0).data(role=m._z_role) == 0.25521 assert m._model.item(2, 0).data(role=m._occupancy_role) == 1.0 # test asModel assert m._model == m.asModel()
def test_StatusModelModel(): file_path = QUrl(TEST_FILE).toLocalFile() calculator = CryspyCalculator(file_path) interface = QtCalculatorInterface(calculator) m = Model() m.setCalculatorInterface(interface) assert isinstance(m._statusBarModel, QStandardItemModel) assert isinstance(m._chartDisplayModel, QStandardItemModel) # assure _setModelFromProject got called assert m._statusBarModel.rowCount() == 4 assert m._statusBarModel.columnCount() == 1 assert m._chartDisplayModel.rowCount() == 2 assert m._chartDisplayModel.columnCount() == 1 assert len(m._statusBarModel.roleNames()) == len(m._roles_dict['status']) assert len(m._chartDisplayModel.roleNames()) == len(m._roles_dict['plot']) assert b'label' in m._roles_dict['status'].values() assert b'value' in m._roles_dict['status'].values() assert b'label' in m._roles_dict['plot'].values() assert b'value' in m._roles_dict['plot'].values() fr = Qt.UserRole + 1 offset = 100 assert m._statusBarModel.item(0, 0).data(role=fr + 1) == pytest.approx(340.79) assert m._statusBarModel.item(2, 0).data(role=fr + 1) == 1 assert m._statusBarModel.item(3, 0).data(role=fr + 1) == 1 assert m._statusBarModel.item(1, 0).data(role=fr + 1) == 1 assert m._chartDisplayModel.item(0, 0).data(role=fr + offset + 1) == pytest.approx(340.79) assert m._chartDisplayModel.item(1, 0).data(role=fr + offset + 1) == 1 assert m._statusBarModel == m.returnStatusBarModel() assert m._chartDisplayModel == m.returnChartModel()
def initialize(self): self.__log.info("") self._project_rcif_path = self._project_control.project_rcif_path #logging.info(self._calculator.asCifDict()) # TODO This is where you would choose the calculator and import the module self._calculator_interface = QtCalculatorInterface( CryspyCalculator(self._project_rcif_path)) self._calculator_interface.project_dict['app']['name'] = self.info[ 'name'] self._calculator_interface.project_dict['app']['version'] = self.info[ 'version'] self._calculator_interface.project_dict['app']['url'] = self.info[ 'url'] self._calculator_interface.projectDictChanged.connect( self.projectChanged) self._calculator_interface.canUndoOrRedoChanged.connect( self.canUndoOrRedoChanged) self._calculator_interface.clearUndoStack() # TODO generates dictdiffer # `ValueError: The truth value of an array with more than one element is ambiguous` # Temp fix - Andrew self.onProjectSaved() models = [ self._measured_data_model, self._calculated_data_model, self._bragg_peaks_model, self._cell_parameters_model, self._cell_box_model, self._atom_sites_model, self._atom_adps_model, self._atom_msps_model, self._fitables_model, self._status_model ] for model in models: model.setCalculatorInterface(self._calculator_interface) self._refine_thread = Refiner(self._calculator_interface, 'refine') self._refine_thread.failed.connect(self._thread_failed) self._refine_thread.finished.connect(self._thread_finished) self._refine_thread.finished.connect( self._status_model.onRefinementDone) # We can't link signals as the manager signals emitted before the dict is updated :-( self.projectChanged.emit()
def test_FitablesModelModel(): file_path = QUrl(TEST_FILE).toLocalFile() calculator = CryspyCalculator(file_path) interface = QtCalculatorInterface(calculator) m = Model() m.setCalculatorInterface(interface) assert isinstance(m._model, QStandardItemModel) # assure _setModelFromProject got called assert m._model.rowCount() == 24 assert m._model.columnCount() == 1 assert len(m._model.roleNames()) == len(m._roles_dict) assert b'path' in m._roles_dict.values() assert b'refine' in m._roles_dict.values() # Test stuff from _setModelFromProject here # first and last row assert m._model.item(0, 0).data(role=Qt.UserRole+2) == 'phases Fe3O4 cell length_a' assert m._model.item(0, 0).data(role=Qt.UserRole+3) == 8.36212 assert m._model.item(0, 0).data(role=Qt.UserRole+4) == 0.0 assert m._model.item(0, 0).data(role=Qt.UserRole+5) == pytest.approx(6.68969) assert m._model.item(0, 0).data(role=Qt.UserRole+6) == 10.034544 assert m._model.item(0, 0).data(role=Qt.UserRole+7) is True assert m._model.item(0, 0).data(role=Qt.UserRole+8) == '\u212B' assert m._model.item(21, 0).data(role=Qt.UserRole+2) == 'experiments pd resolution y' assert m._model.item(21, 0).data(role=Qt.UserRole+3) == 0.0 assert m._model.item(21, 0).data(role=Qt.UserRole+4) == 0.0 assert m._model.item(21, 0).data(role=Qt.UserRole+5) == -1.0 assert m._model.item(21, 0).data(role=Qt.UserRole+6) == 1.0 assert m._model.item(21, 0).data(role=Qt.UserRole+7) is False assert m._model.item(21, 0).data(role=Qt.UserRole+8) == '' # test asModel assert m._model == m.asModel()
def test_MeasuredDataModel(): file_path = QUrl(TEST_FILE).toLocalFile() calculator = CryspyCalculator(file_path) interface = QtCalculatorInterface(calculator) m = Model() m.setCalculatorInterface(interface) assert isinstance(m._model, QStandardItemModel) assert isinstance(m._headers_model, QStandardItemModel) # assure _setModelFromProject got called assert m._model.rowCount() == 381 assert m._model.columnCount() == 9 assert m._headers_model.rowCount() == 1 assert m._headers_model.columnCount() == 9 # Test stuff from _setModelFromProject here assert m._model.item(0, 0).data(role=Qt.DisplayRole) == 4.0 assert m._model.item(0, 6).data(role=Qt.DisplayRole) == 128.97 assert m._model.item(380, 0).data(role=Qt.DisplayRole) == 80.0 assert m._model.item(380, 6).data(role=Qt.DisplayRole) == 27.81 assert m._headers_model.item(0, 0).data(role=Qt.DisplayRole) == 'x' assert m._headers_model.item(0, 1).data(role=Qt.DisplayRole) == 'y_obs' assert m._headers_model.item(0, 2).data(role=Qt.DisplayRole) == 'sy_obs' assert m._headers_model.item(0, 3).data(role=Qt.DisplayRole) == 'y_obs_diff' assert m._headers_model.item(0, 4).data(role=Qt.DisplayRole) == 'sy_obs_diff' assert m._headers_model.item(0, 5).data(role=Qt.DisplayRole) == 'y_obs_up' assert m._headers_model.item(0, 6).data(role=Qt.DisplayRole) == 'sy_obs_up' assert m._headers_model.item(0, 7).data(role=Qt.DisplayRole) == 'y_obs_down' assert m._headers_model.item(0, 8).data(role=Qt.DisplayRole) == 'sy_obs_down' # test asModel assert m._model == m.asModel() assert m._headers_model == m.asHeadersModel()
def test_onModelChanged(): file_path = QUrl(TEST_FILE).toLocalFile() calculator = CryspyCalculator(file_path) interface = QtCalculatorInterface(calculator) m = Model() m.setCalculatorInterface(interface) phase_index = m._model.index(2, 0) # 3rd element (from phase block) experiment_index = m._model.index(m._model.rowCount()-3, 0) # 4th from below (from experiment block) # ###################### # Check unsupported role # ###################### edit_role = Qt.UserRole + 101 # path edit role new_edit = [] m._model.setData(phase_index, new_edit, edit_role) # ###################### # Check refine parameter # ###################### display_role = Qt.UserRole + 7 edit_role = Qt.UserRole + 107 old_display = False new_display = True old_edit = None new_edit = True # Initial state assert m._model.data(phase_index, display_role) == old_display assert m._model.data(phase_index, edit_role) == old_edit assert m._model.data(experiment_index, display_role) == old_display assert m._model.data(experiment_index, edit_role) == old_edit # Model changes via display role m._model.setData(phase_index, new_display, display_role) assert m._model.data(phase_index, display_role) == new_display assert m._model.data(phase_index, edit_role) == old_edit m._model.setData(experiment_index, new_display, display_role) assert m._model.data(experiment_index, display_role) == new_display assert m._model.data(experiment_index, edit_role) == old_edit # Model changes via edit role m._model.setData(phase_index, new_edit, edit_role) assert m._model.data(phase_index, display_role) == new_display assert m._model.data(phase_index, edit_role) == old_edit m._model.setData(experiment_index, new_edit, edit_role) assert m._model.data(experiment_index, display_role) == new_display assert m._model.data(experiment_index, edit_role) == old_edit # ##################### # Check value parameter # ##################### display_role = Qt.UserRole + 3 edit_role = Qt.UserRole + 103 old_display = 0 old_edit = None new_display = 0.5 new_edit = 0.5 # Initial state assert m._model.data(phase_index, display_role) == old_display assert m._model.data(phase_index, edit_role) == old_edit assert m._model.data(experiment_index, display_role) == old_display assert m._model.data(experiment_index, edit_role) == old_edit # Model changes via display role m._model.setData(phase_index, new_display, display_role) assert m._model.data(phase_index, display_role) == new_display assert m._model.data(phase_index, edit_role) == old_edit m._model.setData(experiment_index, new_display, display_role) assert m._model.data(experiment_index, display_role) == new_display assert m._model.data(experiment_index, edit_role) == old_edit # Model changes via edit role m._model.setData(phase_index, new_edit, edit_role) assert m._model.data(phase_index, display_role) == new_display assert m._model.data(phase_index, edit_role) == old_edit m._model.setData(experiment_index, new_edit, edit_role) assert m._model.data(experiment_index, display_role) == new_display assert m._model.data(experiment_index, edit_role) == old_edit # Model changes via edit role outside min/max limits m._model.setData(phase_index, -100, Qt.UserRole + 103) assert m._model.data(phase_index, Qt.UserRole + 5) == -120 m._model.setData(phase_index, 100, Qt.UserRole + 103) assert m._model.data(phase_index, Qt.UserRole + 6) == 120 m._model.setData(phase_index, 1000, Qt.UserRole + 103) m._model.setData(phase_index, 1000, Qt.UserRole + 105) m._model.setData(phase_index, 1000, Qt.UserRole + 106) m._model.setData(phase_index, 100, Qt.UserRole + 103) assert m._model.data(phase_index, Qt.UserRole + 5) == 80 m._model.setData(phase_index, -1000, Qt.UserRole + 103) m._model.setData(phase_index, -1000, Qt.UserRole + 105) m._model.setData(phase_index, -1000, Qt.UserRole + 106) m._model.setData(phase_index, -100, Qt.UserRole + 103) assert m._model.data(phase_index, Qt.UserRole + 6) == -80 m._model.setData(phase_index, 0, Qt.UserRole + 105) m._model.setData(phase_index, 0, Qt.UserRole + 106) m._model.setData(phase_index, 0, Qt.UserRole + 103) assert m._model.data(phase_index, Qt.UserRole + 5) == -1 assert m._model.data(phase_index, Qt.UserRole + 6) == 1 m._model.setData(experiment_index, 0, Qt.UserRole + 105) m._model.setData(experiment_index, 0, Qt.UserRole + 106) m._model.setData(experiment_index, 0, Qt.UserRole + 103) assert m._model.data(experiment_index, Qt.UserRole + 5) == -1 assert m._model.data(experiment_index, Qt.UserRole + 6) == 1 # ################### # Check min parameter # ################### display_role = Qt.UserRole + 5 edit_role = Qt.UserRole + 105 old_display = -1 old_edit = None new_display = -0.5 new_edit = -0.5 # Initial state assert m._model.data(phase_index, display_role) == old_display assert m._model.data(phase_index, edit_role) == old_edit assert m._model.data(experiment_index, display_role) == old_display assert m._model.data(experiment_index, edit_role) == old_edit # Model changes via display role m._model.setData(phase_index, new_display, display_role) assert m._model.data(phase_index, display_role) == new_display assert m._model.data(phase_index, edit_role) == old_edit m._model.setData(experiment_index, new_display, display_role) assert m._model.data(experiment_index, display_role) == new_display assert m._model.data(experiment_index, edit_role) == old_edit # Model changes via edit role m._model.setData(phase_index, new_edit, edit_role) assert m._model.data(phase_index, display_role) == new_display assert m._model.data(phase_index, edit_role) == old_edit m._model.setData(experiment_index, new_edit, edit_role) assert m._model.data(experiment_index, display_role) == new_display assert m._model.data(experiment_index, edit_role) == old_edit # ################### # Check max parameter # ################### display_role = Qt.UserRole + 6 edit_role = Qt.UserRole + 106 old_display = 1 old_edit = None new_display = 0.5 new_edit = 0.5 # Initial state assert m._model.data(phase_index, display_role) == old_display assert m._model.data(phase_index, edit_role) == old_edit assert m._model.data(experiment_index, display_role) == old_display assert m._model.data(experiment_index, edit_role) == old_edit # Model changes via display role m._model.setData(phase_index, new_display, display_role) assert m._model.data(phase_index, display_role) == new_display assert m._model.data(phase_index, edit_role) == old_edit m._model.setData(experiment_index, new_display, display_role) assert m._model.data(experiment_index, display_role) == new_display assert m._model.data(experiment_index, edit_role) == old_edit # Model changes via edit role m._model.setData(phase_index, new_edit, edit_role) assert m._model.data(phase_index, display_role) == new_display assert m._model.data(phase_index, edit_role) == old_edit m._model.setData(experiment_index, new_edit, edit_role) assert m._model.data(experiment_index, display_role) == new_display assert m._model.data(experiment_index, edit_role) == old_edit