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

Math parsing of fit function #7

Open
ckoerber opened this issue Oct 7, 2021 · 3 comments
Open

Math parsing of fit function #7

ckoerber opened this issue Oct 7, 2021 · 3 comments
Labels
Feature An optional addition to the project help wanted Extra attention is needed

Comments

@ckoerber
Copy link
Owner

ckoerber commented Oct 7, 2021

The function document method uses sympy to generate a latex expression for the fit function and returns None if it fails to generate the expression.

See also from lsqfitgui.util.function import parse_function_expression.

Unfortunately, parsing functions using numpy fail. This is due to

from sympy import S
import numpy as np

x = S("x")
np.exp(x)

fails.

A workaround is overloading the Symbol class

from sympy import Symbol, exp
import numpy as np

class MySymbol(Symbol):

   def exp(self):
       return exp(self)

x = MySymbol("x")
np.exp(x)

which may also work for other functions (maybe there is a more elegant way by looking up matching module attributes).

The main issue, I have however encountered is that, for example

x = MySymbol("x")
y = MySymbol("y")

np.exp(x + y)

fails again because this is now not a MySymbol but rather an Add class.

So, I believe, if it is somehow possible, it would be more effective to replace all the numpy expressions by sympy expressions within the function call. Maybe this possible in the spirit of unittest.mock.

@ckoerber ckoerber added Feature An optional addition to the project help wanted Extra attention is needed labels Oct 7, 2021
@millernb
Copy link
Collaborator

millernb commented Oct 7, 2021

A small suggestion: if lsqfit-gui is unable to properly parse the fitfcn as a latex expression, maybe fall-back to (optionally?) printing the python block. I know that, at least for me, my fit functions usually contain plenty of if/else statements (especially for xpt fits), which I wouldn't expect to be parsed correctly (though I would be tremendously impressed if they were).

@ckoerber
Copy link
Owner Author

ckoerber commented Oct 8, 2021

This is a good idea, Though it can be a bit nested if it is also an entire class. I would probably make it a collapsible section (or comparable) to not pollute the GUI too much.

Generally speaking, if the if statements don't use the x or p values, it should be possible. It just uses sympy expressions instead of actual numbers or gvars.

@ckoerber
Copy link
Owner Author

ckoerber commented Oct 8, 2021

The source code inspection is now implemented on the function-source-code branch. I'll wait with the merge because of related prior export dependencies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature An optional addition to the project help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants