Skip to content

Commit

Permalink
fixup! Fixed crash when par name is substring of ind. var
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesGaessler committed Nov 28, 2023
1 parent 2734cee commit d43990c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
11 changes: 1 addition & 10 deletions kafe2/fit/_base/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,7 @@ def parcount(self):
@property
def x_name(self):
"""The name of the independent variable. ``None`` for 0 independent variables."""
# TODO: don't use case differentiation to maintain compatibility with current fit objects
if self._independent_argcount == 0:
return None
_pars = list(self.signature.parameters.keys())
if self._independent_argcount == 1:
return _pars[0]
return _pars[0 : self._independent_argcount]

@property
Expand Down Expand Up @@ -262,12 +257,8 @@ def defaults(self, new_defaults):
def defaults_dict(self):
"""The default values for model function parameters as a dict"""
_defaults_dict = OrderedDict()
_x_name = self.x_name
_x_name = self.x_name # list of strings

# _x_name can be a list for >1 independent variables.
# Always convert to list for simpler logic.
if type(_x_name) is str:
_x_name = [_x_name]
for _par in self.signature.parameters.values():
# skip independent variable parameter
if _x_name is not None and _par.name in _x_name:
Expand Down
14 changes: 8 additions & 6 deletions kafe2/test/fit/test_model_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def model(x, a, b, c):
parameter_names=["a", "b", "c"],
argcount=4,
parcount=3,
x_name="x",
x_name=["x"],
defaults=[1.0, 1.0, 1.0],
latex_name=r"{\tt model}",
parameter_latex_names=["{a}", "{b}", "{c}"],
Expand All @@ -48,7 +48,7 @@ def other_model(tau, A_0=3.0, tau_0=1.0):
parameter_names=["A_0", "tau_0"],
argcount=3,
parcount=2,
x_name="tau",
x_name=["tau"],
defaults=[3.0, 1.0],
latex_name=r"{\tt other\_model}",
parameter_latex_names=["{A_0}", r"{\tau_0}"],
Expand Down Expand Up @@ -118,14 +118,16 @@ class TestSymPyIndexed(TestSymPy):

def setUp(self):
super().setUp()
self._ref_properties_1["parameter_names"].insert(0, self._ref_properties_1["x_name"])
self._ref_properties_1["parameter_names"] = \
self._ref_properties_1["x_name"] + self._ref_properties_1["parameter_names"]
self._ref_properties_1["parcount"] += 1
self._ref_properties_1["x_name"] = None
self._ref_properties_1["x_name"] = []
self._ref_properties_1["defaults"].insert(0, 1.0)
self._ref_properties_1["parameter_latex_names"].insert(0, r"{x}")

self._ref_properties_2["parameter_names"].insert(0, self._ref_properties_2["x_name"])
self._ref_properties_2["parameter_names"] = \
self._ref_properties_2["x_name"] + self._ref_properties_2["parameter_names"]
self._ref_properties_2["parcount"] += 1
self._ref_properties_2["x_name"] = None
self._ref_properties_2["x_name"] = []
self._ref_properties_2["defaults"].insert(0, 1.0)
self._ref_properties_2["parameter_latex_names"].insert(0, r"{\tau}")

0 comments on commit d43990c

Please sign in to comment.