1
0
forked from IPKM/nmreval

catch errors in fit preparation

This commit is contained in:
Dominik Demuth 2023-09-07 19:52:53 +02:00
parent e2e52cebde
commit a406908a69
4 changed files with 79 additions and 60 deletions

View File

@ -298,7 +298,8 @@ class ParameterSingleWidget(QtWidgets.QWidget):
self._name = name self._name = name
self.label.setText(convert(name)) self.label.setText(convert(name))
self.label.setToolTip('If this is bold then this parameter is only for this data. otherwise the general parameter is used and displayed') self.label.setToolTip('If this is bold then this parameter is only for this data. '
'Otherwise, the general parameter is used and displayed')
self.value_line.setValidator(QtGui.QDoubleValidator()) self.value_line.setValidator(QtGui.QDoubleValidator())
self.value_line.textChanged.connect(lambda: self.valueChanged.emit(self.value) if self.value is not None else 0) self.value_line.textChanged.connect(lambda: self.valueChanged.emit(self.value) if self.value is not None else 0)

View File

@ -916,8 +916,10 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
self.action_odr_fit: 'odr' self.action_odr_fit: 'odr'
}[self.ac_group.checkedAction()] }[self.ac_group.checkedAction()]
fit_is_ready = self.management.prepare_fit(parameter, links, fit_options)
if fit_is_ready:
self.management.start_fit()
self.fit_dialog.fit_button.setEnabled(False) self.fit_dialog.fit_button.setEnabled(False)
self.management.start_fit(parameter, links, fit_options)
self.status.setText('Fit running...'.format(self.management.fitter.step)) self.status.setText('Fit running...'.format(self.management.fitter.step))
self.fit_timer.start(500) self.fit_timer.start(500)

View File

@ -424,9 +424,9 @@ class UpperManagement(QtCore.QObject):
for d in self.data.values(): for d in self.data.values():
d.mask = np.ones_like(d.mask, dtype=bool) d.mask = np.ones_like(d.mask, dtype=bool)
def start_fit(self, parameter: dict, links: list, fit_options: dict): def prepare_fit(self, parameter: dict, links: list, fit_options: dict) -> bool:
if self._fit_active: if self._fit_active:
return return False
self.__fit_options = (parameter, links, fit_options) self.__fit_options = (parameter, links, fit_options)
@ -436,6 +436,10 @@ class UpperManagement(QtCore.QObject):
fit_mode = fit_options['fit_mode'] fit_mode = fit_options['fit_mode']
we_option = fit_options['we'] we_option = fit_options['we']
self.fitter.fitmethod = fit_mode
# all-encompassing error catch
try:
for model_id, model_p in parameter.items(): for model_id, model_p in parameter.items():
m = Model(model_p['func']) m = Model(model_p['func'])
models[model_id] = m models[model_id] = m
@ -494,9 +498,18 @@ class UpperManagement(QtCore.QObject):
for links_i in links: for links_i in links:
self.fitter.set_link_parameter((models[links_i[0]], links_i[1]), self.fitter.set_link_parameter((models[links_i[0]], links_i[1]),
(models[links_i[2]], links_i[3])) (models[links_i[2]], links_i[3]))
return True
except Exception as e:
logger.error('Fit preparation failed', *e.args)
QtWidgets.QMessageBox.warning(QtWidgets.QWidget(),
'Fit prep failed',
f'Fit preparation failed with message\n{e.args}')
return False
def start_fit(self):
with busy_cursor(): with busy_cursor():
self.fit_worker = FitWorker(self.fitter, fit_mode) self.fit_worker = FitWorker(self.fitter)
self.fit_thread = QtCore.QThread() self.fit_thread = QtCore.QThread()
self.fit_worker.moveToThread(self.fit_thread) self.fit_worker.moveToThread(self.fit_thread)
@ -532,7 +545,8 @@ class UpperManagement(QtCore.QObject):
for set_id, set_parameter in parameter.items(): for set_id, set_parameter in parameter.items():
new_values = [v.value for v in res[set_id].parameter.values()] new_values = [v.value for v in res[set_id].parameter.values()]
parameter[set_id] = (new_values, set_parameter[1]) parameter[set_id] = (new_values, set_parameter[1])
self.start_fit(*self.__fit_options) if self.prepare_fit(*self.__fit_options):
self.start_fit()
def make_fits(self, res: dict, opts: list, param_graph: str, show_fit: bool, parts: bool, extrapolate: list) -> None: def make_fits(self, res: dict, opts: list, param_graph: str, show_fit: bool, parts: bool, extrapolate: list) -> None:
""" """
@ -1270,16 +1284,15 @@ class UpperManagement(QtCore.QObject):
class FitWorker(QtCore.QObject): class FitWorker(QtCore.QObject):
finished = QtCore.pyqtSignal(list, bool) finished = QtCore.pyqtSignal(list, bool)
def __init__(self, fitter, mode): def __init__(self, fitter):
super().__init__() super().__init__()
self.fitter = fitter self.fitter = fitter
self.mode = mode
@QtCore.pyqtSlot() @QtCore.pyqtSlot()
def run(self): def run(self):
try: try:
res = self.fitter.run(mode=self.mode) res = self.fitter.run()
success = True success = True
except Exception as e: except Exception as e:
res = [e] res = [e]

View File

@ -23,7 +23,7 @@ class FitAbortException(Exception):
class FitRoutine(object): class FitRoutine(object):
def __init__(self, mode='lsq'): def __init__(self, mode='lsq'):
self._fitmethod = mode self.fitmethod = mode
self.data = [] self.data = []
self.fit_model = None self.fit_model = None
self._no_own_model = [] self._no_own_model = []
@ -169,10 +169,13 @@ class FitRoutine(object):
logger.info('Fit aborted by user') logger.info('Fit aborted by user')
self._abort = True self._abort = True
def run(self, mode='lsq'): def run(self, mode: str=None):
self._abort = False self._abort = False
self.parameter = Parameters() self.parameter = Parameters()
if mode is None:
mode = self.fitmethod
fit_groups, linked_parameter = self.prepare_links() fit_groups, linked_parameter = self.prepare_links()
for data_groups in fit_groups: for data_groups in fit_groups: