forked from IPKM/nmreval
209-fit-tree (#222)
closes #209 Co-authored-by: Dominik Demuth <dominik.demuth@physik.tu-darmstadt.de> Reviewed-on: IPKM/nmreval#222
This commit is contained in:
parent
465fb0c09a
commit
575cb5e8f6
@ -70,7 +70,7 @@ class FitModelTree(QtWidgets.QTreeWidget):
|
|||||||
self.remove_function(item)
|
self.remove_function(item)
|
||||||
|
|
||||||
elif evt.key() == QtCore.Qt.Key.Key_Space:
|
elif evt.key() == QtCore.Qt.Key.Key_Space:
|
||||||
for item in self.treeWidget.selectedItems():
|
for item in self.selectedItems():
|
||||||
cs = item.checkState(0)
|
cs = item.checkState(0)
|
||||||
if cs == QtCore.Qt.CheckState.Unchecked:
|
if cs == QtCore.Qt.CheckState.Unchecked:
|
||||||
item.setCheckState(0, QtCore.Qt.CheckState.Checked)
|
item.setCheckState(0, QtCore.Qt.CheckState.Checked)
|
||||||
|
@ -275,7 +275,7 @@ class QFitDialog(QtWidgets.QWidget, Ui_FitDialog):
|
|||||||
|
|
||||||
func_dict = {}
|
func_dict = {}
|
||||||
for model_name, model_parameter in self.models.items():
|
for model_name, model_parameter in self.models.items():
|
||||||
func, order, param_len = ModelFactory.create_from_list(model_parameter)
|
func, order, param_len, _ = ModelFactory.create_from_list(model_parameter)
|
||||||
multiple_funcs = isinstance(func, MultiModel)
|
multiple_funcs = isinstance(func, MultiModel)
|
||||||
if func is None:
|
if func is None:
|
||||||
continue
|
continue
|
||||||
@ -387,7 +387,7 @@ class QFitDialog(QtWidgets.QWidget, Ui_FitDialog):
|
|||||||
|
|
||||||
func_dict = {}
|
func_dict = {}
|
||||||
for k, mod in self.models.items():
|
for k, mod in self.models.items():
|
||||||
func, order, param_len = ModelFactory.create_from_list(mod)
|
func, order, param_len, _ = ModelFactory.create_from_list(mod)
|
||||||
multiple_funcs = isinstance(func, MultiModel)
|
multiple_funcs = isinstance(func, MultiModel)
|
||||||
|
|
||||||
if k in data:
|
if k in data:
|
||||||
|
@ -9,7 +9,13 @@ from inspect import signature, Parameter
|
|||||||
class ModelFactory:
|
class ModelFactory:
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def create_from_list(funcs: list, left=None, func_order=None, param_len=None, left_cnt=None):
|
def create_from_list(
|
||||||
|
funcs: list,
|
||||||
|
left=None,
|
||||||
|
func_order: list[int] = None,
|
||||||
|
param_len: list[int] = None,
|
||||||
|
left_cnt: int = 0,
|
||||||
|
):
|
||||||
if func_order is None:
|
if func_order is None:
|
||||||
func_order = []
|
func_order = []
|
||||||
|
|
||||||
@ -20,32 +26,50 @@ class ModelFactory:
|
|||||||
if not func['active']:
|
if not func['active']:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
func_order.append(func['cnt'])
|
|
||||||
param_len.append(len(func['func'].params))
|
|
||||||
|
|
||||||
if func['children']:
|
if func['children']:
|
||||||
right, _, _ = ModelFactory.create_from_list(func['children'], left_cnt=func['pos'],
|
f = func.copy()
|
||||||
func_order=func_order, param_len=param_len)
|
f['children'] = []
|
||||||
right_cnt = None
|
right, _, _, right_cnt = ModelFactory.create_from_list(
|
||||||
right = MultiModel(func['func'], right, func['children'][0]['op'], left_idx=func['cnt'], right_idx=None)
|
[f] + func['children'],
|
||||||
|
left_cnt=func['pos'],
|
||||||
|
func_order=func_order,
|
||||||
|
param_len=param_len,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
right = func['func']
|
right = func['func']
|
||||||
right_cnt = func['cnt']
|
right_cnt = func['cnt']
|
||||||
|
|
||||||
|
func_order.append(func['cnt'])
|
||||||
|
param_len.append(len(func['func'].params))
|
||||||
|
|
||||||
if left is None:
|
if left is None:
|
||||||
left = right
|
left = right
|
||||||
left_cnt = right_cnt
|
left_cnt = right_cnt
|
||||||
else:
|
else:
|
||||||
left = MultiModel(left, right, func['op'],
|
left = MultiModel(left, right, func['op'], left_idx=left_cnt, right_idx=right_cnt)
|
||||||
left_idx=left_cnt, right_idx=right_cnt)
|
|
||||||
|
|
||||||
return left, func_order, param_len
|
return left, func_order, param_len, left_cnt
|
||||||
|
|
||||||
|
|
||||||
class MultiModel:
|
class MultiModel:
|
||||||
op_repr = {operator.add: ' + ', operator.mul: ' * ', operator.sub: ' - ', operator.truediv: ' / '}
|
op_repr = {
|
||||||
str_op = {'+': operator.add, '*': operator.mul, '-': operator.sub, '/': operator.truediv}
|
operator.add: ' + ',
|
||||||
int_op = {0: operator.add, 1: operator.mul, 2: operator.sub, 3: operator.truediv}
|
operator.mul: ' * ',
|
||||||
|
operator.sub: ' - ',
|
||||||
|
operator.truediv: ' / ',
|
||||||
|
}
|
||||||
|
str_op = {
|
||||||
|
'+': operator.add,
|
||||||
|
'*': operator.mul,
|
||||||
|
'-': operator.sub,
|
||||||
|
'/': operator.truediv,
|
||||||
|
}
|
||||||
|
int_op = {
|
||||||
|
0: operator.add,
|
||||||
|
1: operator.mul,
|
||||||
|
2: operator.sub,
|
||||||
|
3: operator.truediv,
|
||||||
|
}
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
left: Any,
|
left: Any,
|
||||||
@ -69,6 +93,9 @@ class MultiModel:
|
|||||||
if self._op is None:
|
if self._op is None:
|
||||||
raise ValueError('Invalid binary operator.')
|
raise ValueError('Invalid binary operator.')
|
||||||
|
|
||||||
|
if right_idx is None:
|
||||||
|
right_idx = left_idx + 1
|
||||||
|
|
||||||
self.name = '('
|
self.name = '('
|
||||||
self.params = []
|
self.params = []
|
||||||
self.bounds = []
|
self.bounds = []
|
||||||
|
Loading…
Reference in New Issue
Block a user