From 843866be45854590c9f08838c9e6ecfcc4c30e2b Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Thu, 30 Nov 2023 19:21:21 +0100 Subject: [PATCH] Fit: Ignore complex_state, if no complex function is selected; closes #144 --- src/gui_qt/fit/fitfunction.py | 14 +++++++++++++- src/gui_qt/lib/codeeditor.py | 24 +++++++++++++++++++++++- src/nmreval/fit/minimizer.py | 5 ----- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/gui_qt/fit/fitfunction.py b/src/gui_qt/fit/fitfunction.py index 878f166..54ce567 100644 --- a/src/gui_qt/fit/fitfunction.py +++ b/src/gui_qt/fit/fitfunction.py @@ -235,7 +235,19 @@ class QFunctionWidget(QtWidgets.QWidget, Ui_Form): return all_parameters def get_complex_state(self): - return self.complex_comboBox.currentIndex() if self.iscomplex else None + iscomplex = False + iterator = QtWidgets.QTreeWidgetItemIterator(self.functree) + while iterator.value(): + item = iterator.value() + if item.checkState(0) != QtCore.Qt.CheckState.Unchecked: + f = self.functions[item.data(0, QtCore.Qt.UserRole)] + if hasattr(f, 'iscomplex') and f.iscomplex: + iscomplex = True + break + + iterator += 1 + + return self.complex_comboBox.currentIndex() if iscomplex else None def set_complex_state(self, state): if state is not None: diff --git a/src/gui_qt/lib/codeeditor.py b/src/gui_qt/lib/codeeditor.py index 1b80d4d..5bf0e04 100644 --- a/src/gui_qt/lib/codeeditor.py +++ b/src/gui_qt/lib/codeeditor.py @@ -1,5 +1,6 @@ # CodeEditor based on QT example, Python syntax highlighter found on Python site - +import typing +from ast import parse from ..Qt import QtGui, QtCore, QtWidgets @@ -184,6 +185,7 @@ class CodeEditor(QtWidgets.QPlainTextEdit): self.update_width_linenumber(0) self.highlight = PythonHighlighter(self.document()) + self.textChanged.connect(self._check_syntax) def keyPressEvent(self, evt): if evt.key() == QtCore.Qt.Key_Tab: @@ -260,3 +262,23 @@ class CodeEditor(QtWidgets.QPlainTextEdit): extra_selections.append(selection) self.setExtraSelections(extra_selections) + + def color_line(self, color): + # is_valid, exception = self._check_syntax() + # if is_valid == 1: + doc = self.document() + print(doc.findBlockByLineNumber(color)) + + def _check_syntax(self) -> (int, tuple[typing.Any]): + # Compile into an AST and check for syntax errors. + try: + _ = parse(self.toPlainText(), filename='') + except SyntaxError as e: + print('SyntaxError', e, e.args[0], e.lineno, e.offset, e.text) + self.color_line(e.lineno) + return 1, (e.lineno, e.offset) + except Exception as e: + print('Unexpected error', e) + return 2, (e.args[0],) + + return 0, tuple() diff --git a/src/nmreval/fit/minimizer.py b/src/nmreval/fit/minimizer.py index 9ed33fa..cafca40 100644 --- a/src/nmreval/fit/minimizer.py +++ b/src/nmreval/fit/minimizer.py @@ -227,8 +227,6 @@ class FitRoutine(object): if mode is None: mode = self.fitmethod - print('run') - fit_groups, linked_parameter = self.prepare_links() for data_groups in fit_groups: if len(data_groups) == 1 and not self.linked: @@ -236,8 +234,6 @@ class FitRoutine(object): # get variable parameter for fitter p0_k, lb_k, ub_k, var_pars_k = self._prep_data(data) - print(p0_k, var_pars_k) - if mode == 'lsq': self._least_squares_single(data, p0_k, lb_k, ub_k, var_pars_k) @@ -252,7 +248,6 @@ class FitRoutine(object): data_pars, p0, lb, ub, var_pars = self._prep_global(data_groups, linked_parameter) if mode == 'lsq': - print(data_pars, p0,var_pars) self._least_squares_global(data_groups, p0, lb, ub, var_pars, data_pars) elif mode == 'nm':