Positional arguments in lmfit cannot be SciPy CubicSpline datatype

44 Views Asked by At

Problem:
If the array of positional arguments I plug into lmfit minimize() is obtained from scipy.interpolate.CubicSpline.

I get the following error:

TypeError: unsupported operand type(s) for *: 'CubicSpline' and 'float'

The * operation is used in my model function where I multiply the positional argument array (x) with a fitting parameter (pars['a']) as shown below. Basically, lmfit thinks (x_list) is a type CubicSpline even though it is an array of floats.

My Code:

import scipy.interpolate as scpi
import numpy as np
from lmfit import Parameters, minimize

A = np.linspace(0,1,100)
B = A**2 

y_list = np.linspace(1,100,100)
x_list = scpi.CubicSpline(A, B)

def model(pars, x, data=None):
    model = x*pars['a']
    if data is None:
        return model
    return model - data

fit_params = Parameters()   
fit_params.add('a',  value = 1., min = 0.0, vary=True)
coeff = minimize(model, 
                 fit_params, 
                 args=(x_list(A),), 
                 kws={'data': y_list}, #cut off distance
                 method='basinhopping', 
             )

Here is what I have tried so far:

  • type(x_list) shows it is an array of floats
  • x_list*0.1 works in a cell
  • type casting x_list as an array of floats does not fix the problem: x_list = np.asarray([float(j) for j in x_list])
1

There are 1 best solutions below

0
ev-br On

Your x_list is not a list, it's a CubicSpline object.