Skip to content

Commit

Permalink
Merge pull request #982 from lmfit/modelfunc_barestare
Browse files Browse the repository at this point in the history
Modelfunc barestar
  • Loading branch information
newville authored Jan 15, 2025
2 parents fb6600e + 200b069 commit 821af24
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lmfit/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,16 +529,20 @@ def _parse_params(self):
for fnam, fpar in sig.parameters.items():
if fpar.kind == fpar.VAR_KEYWORD:
keywords_ = fnam
elif fpar.kind in (fpar.POSITIONAL_ONLY, fpar.POSITIONAL_OR_KEYWORD):
elif fpar.kind in (fpar.KEYWORD_ONLY,
fpar.POSITIONAL_OR_KEYWORD):
default_vals[fnam] = fpar.default
if (isinstance(fpar.default, (float, int, complex))
and not isinstance(fpar.default, bool)):
kw_args[fnam] = fpar.default
pos_args.append(fnam)
elif fpar.default == fpar.empty:
pos_args.append(fnam)
else:
kw_args[fnam] = fpar.default
indep_vars.append(fnam)
elif fpar.kind == fpar.POSITIONAL_ONLY:
raise ValueError("positional only arguments with '/' are not supported")
elif fpar.kind == fpar.VAR_POSITIONAL:
raise ValueError(f"varargs '*{fnam}' is not supported")
# inspection done
Expand Down
29 changes: 29 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1697,3 +1697,32 @@ def test_model_refitting():
assert result.init_values['peak_amplitude'] < 21
assert result.init_values['peak_sigma'] > 2
assert result.init_values['peak_sigma'] < 4


def test_model_barestar():
"""Github #976 (and discussion #975)"""
def foo(x, *, a=1, b=1, kind='linear'):
if kind == 'linear':
return a + b * x
elif kind == 'exponential':
return a * np.exp(b * x)

modl = lmfit.Model(foo)
assert modl.independent_vars == ['x', 'kind']
assert modl._param_names == ['a', 'b']


def test_model_bareslash():
"""Github #976 (and discussion #975)"""
def foo(x, /, a=1, b=1, kind='linear'):
if kind == 'linear':
return a + b * x
elif kind == 'exponential':
return a * np.exp(b * x)
try:
lmfit.Model(foo)
that_failed = False
except ValueError:
that_failed = True

assert that_failed

0 comments on commit 821af24

Please sign in to comment.