我正在尝试使用lmfit将图像的每个像素拟合为一个函数,并绘制自由参数的映射。我以前没有使用过lmfit,因此不确定我的语法是否正确。正在执行的代码给出错误“ /的不支持的操作数类型:”“列表”和“浮点数”。我怀疑这是由于灰色主体函数调用中传递了一个列表“频率”而发生的。但是,freq应该是x数据或输入,因此我适合greybody函数为freq中的每个元素提供的值,因此需要将其作为一个完整的列表传递。因此,我不明白如何消除此错误。
from astropy.io import fits
from scipy.optimize import curve_fit
import lmfit
import numpy as np
import math as m
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
from numpy import exp, sqrt, pi
from lmfit import minimize,Parameters,Parameter,report_fit
p70 = fits.open('70p_trim.fits')
data70 = p70[0].data
p160 = fits.open('160p_trim.fits')
data160 = p160[0].data
s250 = fits.open('250s_trim.fits')
data250 = s250[0].data
s350 = fits.open('350s_trim.fits')
data350 = s350[0].data
s500 = fits.open('500s_trim.fits')
data500 = s500[0].data
freq=[4.28e12,1.875e12,1.2e12,8.57e11,6e11]
nx=137
ny=206
colden=[[0 for i in range(ny)] for j in range(nx)]
temp70=[[0 for i in range(ny)] for j in range(nx)]
temp160=[[0 for i in range(ny)] for j in range(nx)]
temp250=[[0 for i in range(ny)] for j in range(nx)]
temp350=[[0 for i in range(ny)] for j in range(nx)]
temp500=[[0 for i in range(ny)] for j in range(nx)]
tk=[[0 for i in range(ny)] for j in range(nx)]
beta=[[0 for i in range(ny)] for j in range(nx)]
chi_sqred=[[0 for i in range(ny)] for j in range(nx)]
ydat=[0 for i in range(5)]
bg70=0.002370929
bg160=0.011766256
bg250=0.0034188613
bg350=0.0038749452
bg500=0.0017783735
o=7.0*7.0/(4.25*m.pow(10,10))
def greybodyfit(v,N,T,b):
h=6.626e-27
c=3.0e+10
u=2.86
kv=0.1*(v/1000.0e9)**b
k=1.38e-16
mh=1.67e-24
t=mh*u*N*kv
B=(2.0*h*v**3.0)/((c**2.0) * np.exp(h*v/(k*T))-1)
Fv=o*B*(1-np.exp(-t))
return Fv
def redchisqg(f,y):
sd,chi_sq=0,0
for z in range(5):
sd+=(f[z]-np.mean(f))**2
sd=m.sqrt(sd/4.0)
for q in range(5):
chi_sq+=(f[q]-y[q])**2/(sd)**2
return chi_sq/2.0
def fcn2fit(params,freq,F):
colden=params['colden'].value
tk=params['tk'].value
beta=params['beta'].value
model = greybodyfit(freq,colden,tk,beta)
return model - F
params = Parameters()
params.add('colden', value=1e20)
params.add('tk', value=5.0)
params.add('beta', value=1.0)
for i in range(nx):
for j in range(ny):
temp70[i][j]=data70[i][j]-bg70
temp160[i][j]=data160[i][j]-bg160
temp250[i][j]=data250[i][j]-bg250
temp350[i][j]=data350[i][j]-bg350
temp500[i][j]=data500[i][j]-bg500
F=[temp70[i][j],temp160[i][j],temp250[i][j],temp350[i][j],temp500[i][j]]
F=[F[p]*1e-23 for p in range(5)]
result = minimize(fcn2fit, params, args=(freq,F))
final = F + result.residual
report_fit (params)
for u in range(5):
ydat[u]=greybodyfit(freq[u],colden[i][j],tk[i][j],beta[i][j])
ydat=np.array(ydat)
chi_sqred[i][j]=redchisqg(F,ydat)
x=np.linspace(-7,7,ny)
y=np.linspace(-7,7,nx)
X,Y=np.meshgrid(x,y)
colden=np.array(colden)
fig,ax=plt.subplots()
c=ax.pcolormesh(x,y,colden,cmap=cm.jet)
fig.colorbar(c,ax=ax)
tk=np.array(tk)
fig,ax=plt.subplots()
c=ax.pcolormesh(x,y,tk,cmap=cm.jet)
fig.colorbar(c,ax=ax)
beta=np.array(beta)
fig,ax=plt.subplots()
c=ax.pcolormesh(x,y,beta,cmap=cm.jet)
fig.colorbar(c,ax=ax)
chi_sqred=np.array(chi_sqred)
fig,ax=plt.subplots()
c=ax.pcolormesh(x,y,chi_sqred,cmap=cm.jet)
fig.colorbar(c,ax=ax)
plt.show()
答案 0 :(得分:0)
您没有发布完整的追溯或错误消息,这对于查看错误发生的位置很有帮助。
但是,基本上,您不能将普通的python列表乘以浮点数-python列表可以包含任意python对象。您几乎可以肯定地说,该列表代表数字数组。因此,您应该使用numpy.array
将列表转换为numpy数组。您在代码中将它们放在很多位置。可能足以替换
colden=[[0 for i in range(ny)] for j in range(nx)]
(及其后的行)与
colden=np.array([[0 for i in range(ny)] for j in range(nx)])
您也可以在greybody()
函数内部或fc2fit
中明确地进行转换,例如使用
model = greybodyfit(np.array(freq), colden, tk, beta)
或类似地确保freq
是一个numpy数组,而不是python列表。