python - "argument must be a callable function" when using scipy.integrate.quad -


i've written code enables me take data dicom files , separate data individual fid signals.

from pylab import * import dicom import numpy np import scipy sp  plan=dicom.read_file("1.3.46.670589.11.38085.5.22.3.1.4792.2013050818105496124") all_points = array(plan.spectroscopydata) cmplx_data = all_points[0::2] -1j*all_points[1::2] frames = int(plan.numberofframes) fid_pts = len(cmplx_data)/frames del_t = plan.acquisitionduration / (frames * fid_pts)  fid_list = [] fidn in arange(frames):     offset = fidn * fid_pts      current_fid = cmplx_data[offset:offset+fid_pts]     fid_list.append(current_fid) 

i'd quantify of data so, after applying fourier transform , shift i've tried use scipy's quad function , encountered following error:

spec = fftshift(fft(fid_list[0]))) sp.integrate.quad(spec, 660.0, 700.0)    error                                     traceback (most recent call last) /home/dominicc/experiments/in vitro/glu, cr/phantom 1: 10mm cr, 5mm glu/wip_sv_press_me_128tes_5ms_spacing_1828/<ipython-input-107-17cb50e45927> in <module>() ----> 1 sp.integrate.quad(fid, 660.0, 700.0)  /usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.pyc in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst) 243     if type(args) != type(()): args = (args,) 244     if (weight none): --> 245         retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points) 246     else: 247         retval = _quad_weight(func,a,b,args,full_output,epsabs,epsrel,limlst,limit,maxp1,weight,wvar,wopts)  /usr/lib/python2.7/dist-packages/scipy/integrate/quadpack.pyc in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points) 307     if points none: 308         if infbounds == 0: --> 309             return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit) 310         else: 311             return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)  error: first argument must callable function. 

could please suggest way make object callable? i'm still not particularly clear after reading what "callable" in python?. after reading tried

def fid():     spec = fftshift(fft(fid_list[0]))      return spec 

which returned same error.

any appreciated, thanks.

since integrating function defined on grid (i.e., array of numbers), need use 1 of routines "integration, given fixed samples".

in fact, first argument of quad() must function, can call (a "callable"). instance, can do:

>>> scipy.integrate import quad >>> def f(x): ...     return x**2 ...  >>> quad(f, 0, 1) (0.33333333333333337, 3.700743415417189e-15) 

this can done instead sampling f():

>>> scipy.integrate import simps >>> x_grid = numpy.linspace(0, 1, 101)  # 101 numbers between 0 , 1 >>> simps(x_grid**2, dx=x_grid[1]-x_grid[0])  # x_grid**2 *array*.  dx=0.01 between x_grid values 0.33333333333333337 

your situation in second example: integrate sampled function, natural use 1 or cumtrapz(), simps() or romb().


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -