I noticed strange behaviour of scipy.optimize.curve_fit function on computing cluster that I am using. I have the following code:
import numpy as np
from scipy.optimize import curve_fit
ntime = 30000
def f(x, t1, freq):
return np.exp(-1.0*x/t1)*np.cos(2.0*np.pi*x*freq)
time_axis = np.array([0.01*x for x in range(ntime)])
nfreq = 30
ntimes = 30
ndata = nfreq*ntimes
pars = np.array([[0.0 for x in range(2)] for y in range(ndata)])
for ifreq in range(nfreq):
for itime in range(ntimes):
pars[itime + ifreq*ntimes][0] = (ifreq+1)*0.002
pars[itime + ifreq*ntimes][1] = (itime+1)*0.001
data = np.array([[0.0 for x in range(ntime)] for y in range(ndata)])
for idat in range(ndata):
if(idat%10 == 0):
print(idat, flush = True)
rands = np.random.rand(ntime)/10.0
for itime in range(ntime):
data[idat][itime] = f(time_axis[itime], pars[idat][1], pars[idat][0]) + rands[itime]
fits = np.zeros_like(pars)
for idat in range(ndata):
if(idat%10 == 0):
print(idat, flush = True)
guess = [pars[idat][1]/2.0, pars[idat][1]*2.0]
popt, pcov = curve_fit(f, time_axis, data[idat],\
p0 = guess, max_nfev = 10000000,\
bounds = ([0.0,0.0],[pars[idat][1]*20.0, 20.0*pars[idat][1]]), method = 'trf')
fits[idat][0] = popt[1]
fits[idat][1] = popt[0]
with open('rez', 'w+') as outfile:
for idat in range(ndata):
for j in range(2):
outfile.write(3*' ' + format(fits[idat][j], '.12e'))
outfile.write(3*' ' + format(pars[idat][j], '.12e'))
outfile.write('\n')
When I run it and check the CPU usage using top command I find that CPU usage is going above 100 percent. Also TIME+ column of the top command for this process is showing CPU time larger than the runtime of the code.
Does anyone know what could be the source of this behaviour and how to avoid it?