我写了一些代码,使我能够从DICOM文件中获取数据并将数据分成单独的FID信号。
from pylab import *
import dicom
import numpy as np
import scipy as 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 = []
for fidN in arange(frames):
offset = fidN * fid_pts
current_fid = cmplx_data[offset:offset+fid_pts]
fid_list.append(current_fid)
我现在想量化一些这样的数据,所以在应用傅立叶变换和移位后我试图使用Scipy的四元函数并遇到以下错误:
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 is 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 is 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 be a callable function.
有人可以建议一种让这个对象可以调用的方法吗?阅读What is a "callable" in Python?后,我仍然不是特别清楚。读完之后我试过了
def fid():
spec = fftshift(fft(fid_list[0]))
return spec
返回了相同的错误。
非常感谢任何帮助,谢谢。
答案 0 :(得分:3)
由于您正在集成仅在网格上定义的函数(即数字数组),因此您需要使用“Integration, given fixed samples”中的一个例程。
实际上,quad()
must be a function的第一个参数,你可以称之为(“可调用的”)。例如,你可以这样做:
>>> from scipy.integrate import quad
>>> def f(x):
... return x**2
...
>>> quad(f, 0, 1)
(0.33333333333333337, 3.700743415417189e-15)
这可以通过抽样f()
:
>>> from scipy.integrate import simps
>>> x_grid = numpy.linspace(0, 1, 101) # 101 numbers between 0 and 1
>>> simps(x_grid**2, dx=x_grid[1]-x_grid[0]) # x_grid**2 is an *array*. dx=0.01 between x_grid values
0.33333333333333337
您的情况与第二个示例类似:您整合了一个采样函数,因此很自然地使用一个或cumtrapz()
,simps()
或romb()
。