I would like to write a curve-fitting script that allows me to fix parameters of a function of the form:
def func(x, *p):
assert len(p) % 2 == 0
fval = 0
for j in xrange(0, len(p), 2):
fval += p[j]*np.exp(-p[j+1]*t)
return fval
For example, let's say I want p = [p1, p2, p3, p4], and I want p2 and p3 to be constant A and B (going from a 4-parameter fit to a 2-parameter fit). I understand that functools.partial doesn't let me do this which is why I want to write my own wrapper. But I am having a bit of trouble doing so. This is what I have so far:
def fix_params(f, t, pars, fix_pars):
# fix_pars = ((ind1, A), (ind2, B))
new_pars = [None]*(len(pars) + len(fix_pars))
for ind, fix in fix_pars:
new_pars[ind] = fix
for par in pars:
for j, npar in enumerate(new_pars):
if npar == None:
new_pars[j] = par
break
assert None not in new_pars
return f(t, *new_pars)
The problem with this I think is that, scipy.optimize.curve_fit won't work well with a function passed through this kind of wrapper. How should I get around this?