Click here to Skip to main content
15,615,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I need to pass a function (here is fun) to lmfit to do fitting as below:

import lmfit, Model
model = Model(fun, independent_vars=ind)

But function have to be this way because lmfit only works this way:

def fun(explicit names for all the parameters):
    return string expression of stdout

My issue is that the function parameters and the returned expression by fun must be changed dynamically in each iteration so I should be able to write a function that its parameters and returns is changing dynamically.

Even any suggestion on how to write a function from two strings (as parameter and return) would be welcomed.

I need the following function been created:
def fit_function(expr, list_var, list_para, dict_data(key_var,list_data) ) ->list_para:

What I have tried:

In fact based on a list of a string (as function parameter) and the stdout print (as return in fun), I should write the fun for each iteration. The following code shows how running lnfca() which is a sympy, symbolic string needs to be passed in fun for every iteration.

def fun_print(ff):
    return print(NumPyPrinter().doprint(ff))
import contextlib
import io
captured_output = io.StringIO()
with contextlib.redirect_stdout(captured_output):

This way stdout print can be captured, but I have no idea how one can write a function this way(changing parameters and return is really crucial here) Is there any way to do it??

in summer to fit a model using lmfit it should be passed to Model as follows:

def f(x, gamma, alpha, beta, eta):
    L = x[0]
    K = x[1]
    return gamma - (1/eta)*np.log(alpha*L**-eta + beta*K**-eta)

fmodel = Model(f)
params = Parameters()
params.add('gamma', value = 1,    vary=True, min = 1)
params.add('alpha', value = 0.01, vary=True, max = 1, min = 0)
params.add('beta',  value = 0.98, vary=True, max = 1, min = 0)
params.add('eta',   value = 1,    vary=True, min = -1)

result =, params, x=(L,K))

Please take a look here:

But in my case I should make the f function in a dynamic way for each iteration that includes different variables and expression to create the following:

def fit_function(expr, list_var, list_para, dict_data(key_var,list_data) ) :
return expr
Updated 6-Sep-22 3:38am
Richard MacCutchan 6-Sep-22 7:03am    
Passing different variables will obviously depend on the requirements of the Model class. Changing the expression in the function just requires you to create a different function, and a different Model object.

1 solution

You just need to use variables that are dynamic as the parameters. For example:
def fun(*, keyworda, keywordb):
    print(F'{keyworda = }, {keywordb = }')

fun('one', 'two') # will throw TypeError: fun() takes 0 positional arguments but 2 were given

# but
var1 = 'one'
var2 = 'two'
fun(keyworda=var1, keywordb=var2) # will print the values
Share this answer
Zohreh Karimzadeh 5-Sep-22 9:49am    
But this way the result of function will be obtained while I need it makes :
def fun(var1, var2): return expr(var1, var2)
Richard MacCutchan 5-Sep-22 9:56am    
Sorry, I don't understand what you are trying to do. What exactly is the syntax of the lmfit function that you are trying to call, and what value(s) does it return?
Richard MacCutchan 5-Sep-22 12:41pm    
Please use the Improve question link above, and add complete details into your question, correctly formatted. It is very difficult to read and understand unformatted code and comments.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900