Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

propagate funcdefs to Parameters in model.loads #932

Merged

Conversation

cassberk
Copy link
Contributor

Description

From ISSUE # #931

Using the dumps and loads methods on the ModelResult class does not properly take into account loading user added functional parameter expressions. The funcdefs keyword in loads enables users to pass functions to add to the the asteval interpreter, but these are not subsequently passed into Parameters() where they are actually added to the asteval symtable. This caused deserialization to not work with ModelResults that have parameters with user defined functions as expressions.

Type of Changes
  • [ x] Bug fix
  • New feature
  • Refactoring / maintenance
  • Documentation / examples
Tested on

Python: 3.9.7 (default, Oct 15 2021, 14:20:07)
[Clang 13.0.0 (clang-1300.0.29.3)]

lmfit: 1.2.2, scipy: 1.11.1, numpy: 1.23.5,asteval: 0.9.31, uncertainties: 3.1.7

Verification

Have you

  • [x ] included docstrings that follow PEP 257?
  • [ x] referenced existing Issue and/or provided relevant link to mailing list?
  • [ x] verified that existing tests pass locally?
  • [x ] verified that the documentation builds locally?
  • [ x] squashed/minimized your commits and written descriptive commit messages?
  • [ x] added or updated existing tests to cover the changes?
  • updated the documentation and/or added an entry to the release notes (doc/whatsnew.rst)?
  • added an example? N/A

@newville
Copy link
Member

@cassberk Thanks - I think this might be the right solution, but as said in #931, I think I want to spend a little time investigating the history of this... I feel like I am forgetting something.

Not ignoring, but sort of want to be a bit cautious...

lmfit/model.py Outdated
@@ -1885,13 +1885,14 @@ def loads(self, s, funcdefs=None, **kws):
self.model = _buildmodel(decode4js(modres['model']), funcdefs=funcdefs)

# params
funcdefs.pop(self.model.func.__name__) # Remove model function to not pass into _asteval
Copy link
Contributor Author

@cassberk cassberk Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may not be the most elegant means to do this, but something I noticed was that if the model function is not removed from the funcdefs argument, it gets added to the _asteval.symtable. Although a small change, undesired changes to the objects through de/serialization should be minimized, so I think it should be handled somehow. I added an assert to the test to ensure that the symtables are the same.

@cassberk cassberk force-pushed the fix-serialization-with-expression-function branch from c8af978 to 120e0ff Compare December 21, 2023 16:40
@newville
Copy link
Member

@cassberk thanks -- I'm hoping to get to this before the end of the year/

@newville newville merged commit a4d0ec3 into lmfit:master Dec 29, 2023
12 of 14 checks passed
@newville
Copy link
Member

@cassberk thanks -- I am going to merge this for the test of the doc fix. But, I think I have a better fix for the actual code that I will try to add shortly....

@newville newville mentioned this pull request Dec 30, 2023
12 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants