Skip to content

Commit

Permalink
Merge pull request #788 from Julian-Hochhaus/master
Browse files Browse the repository at this point in the history
enabled use of different independent_vars...
  • Loading branch information
newville committed Jul 4, 2022
2 parents b544ea9 + e9f4bf7 commit 6bea572
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions doc/whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Bug fixes/enhancements:
- make sure variable ``spercent`` is always defined in ``params_html_table`` functions (reported by @MySlientWind; Issue #768, PR #770)
- always initialize the variables ``success`` and ``covar`` the ``MinimizerResult`` (reported by Marc W. Pound; PR #771)
- build package following PEP517/PEP518; use ``pyproject.toml`` and ``setup.cfg``; leave ``setup.py`` for now (PR #777)
- components used to create a ``CompositeModel`` can now have different independent variables (@Julian-Hochhaus; Discussion #787; PR #788))


Deprecations:
Expand Down
8 changes: 5 additions & 3 deletions lmfit/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,7 @@ def __init__(self, left, right, op, **kws):
Notes
-----
The two models must use the same independent variable.
The two models can use different independent variables.
"""
if not isinstance(left, Model):
Expand All @@ -1116,9 +1116,11 @@ def __init__(self, left, right, op, **kws):
"use distinct names.")
raise NameError(msg)

# we assume that all the sub-models have the same independent vars
# the unique ``independent_vars`` of the left and right model are
# combined to ``independent_vars`` of the ``CompositeModel``
if 'independent_vars' not in kws:
kws['independent_vars'] = self.left.independent_vars
ivars = self.left.independent_vars + self.right.independent_vars
kws['independent_vars'] = list(np.unique(ivars))
if 'nan_policy' not in kws:
kws['nan_policy'] = self.left.nan_policy

Expand Down
15 changes: 15 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,21 @@ def test_wrapped_model_func(self):
self.assertTrue(abs(result.params['a'].value - 2.0) < 0.05)
self.assertTrue(abs(result.params['b'].value - 3.0) < 0.41)

def test_different_independent_vars_composite_modeld(self):
"""Regression test for different independent variables in CompositeModel.
See: https://github.com/lmfit/lmfit-py/discussions/787
"""
def two_independent_vars(y, z, a):
return a * y + z

BackgroundModel = Model(two_independent_vars,
independent_vars=["y", "z"], prefix="yz_")
PeakModel = Model(gaussian, independent_vars=["x"], prefix="x_")
CompModel = BackgroundModel + PeakModel
assert CompModel.independent_vars == ['x', 'y', 'z']


class TestLinear(CommonTests, unittest.TestCase):

Expand Down

0 comments on commit 6bea572

Please sign in to comment.