Python Astropy.2d模型高斯问题

Python Astropy.2d模型高斯问题,python,python-2.7,gaussian,astropy,Python,Python 2.7,Gaussian,Astropy,我正在尝试将二维高斯变换成一个fits图像,以便找到图像中最亮的物体。我在这里尝试遵循1D示例。我的代码看起来像 import numpy as np import astropy.io.fits as fits import os from astropy.modeling import models, fitting import matplotlib as plt data = fits.getdata('AzTECC100.fits') med = np.median(data) d

我正在尝试将二维高斯变换成一个fits图像,以便找到图像中最亮的物体。我在这里尝试遵循1D示例。我的代码看起来像

import numpy as np
import astropy.io.fits as fits
import os
from astropy.modeling import models, fitting
import matplotlib as plt

data = fits.getdata('AzTECC100.fits')
med = np.median(data) 
data = data - med
data = data[0,0,:,:]

w = models.Gaussian2D(data, np.mean(data),np.mean(data), np.std(data),np.std(data),)


fit_w = fitting.LevMarLSQFitter()

max_index = np.where(data >= np.max(data))
x0 = max_index[1] #Middle of X axis
y0 = max_index[0]
sigma = np.std(data)
amp = np.max(data)

x = np.arange(0, data.shape[1], 1)
y = np.arange(0, data.shape[0], 1)


A = 1 / (2*sigma**2)
eq =  amp*np.exp(-A*((x-x0)**2 + (y-y0)**2))
g = fit_w(w, x, y,eq)
plt.figure(figsize=(8,5))
plt.plot(x, y,eq, 'ko')
plt.plot(x, g, label='Gaussian')
circle = Circle((x0, y0), 4, facecolor ='none', edgecolor = 'red', linewidth = 1)
 ax.scatter(x0, y0,s = 10, c = 'red', marker = 'x')
而我得到的错误是

TypeError                                 Traceback (most recent call 
last)
<ipython-input-33-45f671ba149d> in <module>()
----> 1 g = fit_w(w, x, y,eq)

/Users/samclyne/anaconda/lib/python2.7/site- 
packages/astropy/modeling/fitting.pyc in __call__(self, model, x, y, z, weights, maxiter, acc, epsilon, estimate_jacobian)
    557             self.objective_function, init_values, args=farg, 
Dfun=dfunc,
    558             col_deriv=model_copy.col_fit_deriv, maxfev=maxiter, epsfcn=epsilon,
--> 559             xtol=acc, full_output=True)
    560         _fitter_to_model_params(model_copy, fitparams)
    561         self.fit_info.update(dinfo)

/Users/samclyne/anaconda/lib/python2.7/site- 
packages/scipy/optimize/minpack.pyc in leastsq(func, x0, args, Dfun, 
full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
    378     m = shape[0]
    379     if n > m:
--> 380         raise TypeError('Improper input: N=%s must not exceed 
M=%s' % (n, m))
    381     if epsfcn is None:
    382         epsfcn = finfo(dtype).eps

TypeError: Improper input: N=65541 must not exceed M=65536
我知道参数的数量不能超过数据点的数量,但我不确定是什么原因造成的,或者在哪里可以修复它


如果我没有在我的问题中提供足够的信息,我很抱歉。我不是一个优秀的程序员,我是这个网站的新手,你的代码中有几个错误,但是造成这个崩溃的原因是你没有正确地创建模型:w=models.Gaussian2D的第一个参数应该是振幅,而不是数据!数据与模型无关,模型是一种理论上的依赖关系/函数,您计划将其与数据相适应

使用类似于中生成的数据,具体而言

y, x = np.indices((51,51))
data = 3 * np.exp(-0.7 * ((x - 24)**2 + (y - 26)**2))
同时注释掉不起作用的行,这里是对代码的一个修改,应该可以起作用注意:我将圆的半径增加了30倍,强度图在对数刻度上:

import astropy.io.fits as fits
import os
from astropy.modeling import models, fitting
import matplotlib.pyplot as plt

#data = fits.getdata('AzTECC100.fits')
med = np.median(data) 
# data = data - med
# data = data[0,0,:,:] # NOT SURE THIS IS NEEDED!

fit_w = fitting.LevMarLSQFitter()

y0, x0 = np.unravel_index(np.argmax(data), data.shape)
sigma = np.std(data)
amp = np.max(data)

w = models.Gaussian2D(amp, x0, y0, sigma, sigma)

yi, xi = np.indices(data.shape)

g = fit_w(w, xi, yi, data)
print(w)
model_data = g(xi, yi)

fig, ax = plt.subplots()
eps = np.min(model_data[model_data > 0]) / 10.0
# use logarithmic scale for sharp Gaussians
ax.imshow(np.log(eps + model_data), label='Gaussian') 
circle = Circle((g.x_mean.value, g.y_mean.value),
                30 * g.x_stddev.value, facecolor ='none',
                edgecolor = 'red', linewidth = 1)

ax.add_patch(circle)
plt.show()
打印的模型参数为:

Model: Gaussian2D
Inputs: ('x', 'y')
Outputs: ('z',)
Model set size: 1
Parameters:
    amplitude x_mean y_mean     x_stddev        y_stddev    theta
    --------- ------ ------ --------------- --------------- -----
          3.0   24.0   26.0 0.0881184627394 0.0881184627394   0.0
笔记: 您将在这里找到大量使用astropy.modeling的示例,以及关于图像数据数量级缩放效果的重要讨论:根据该问题,规范化图像重缩放强度以使其处于合理范围可能会有所帮助。此外,还可以通过笔记本的链接提供更多示例,例如,不幸的是,这些示例适用于一维数据,但可以轻松扩展到二维数据。

当观测数小于变量数时,Levenberg-Marquardt算法lm不起作用。我不知道astropy,但可能也是同样的问题。astropy还有其他拟合算法吗?不要把数据作为模型的第一个参数:这就是你最终得到太多参数的原因。上的示例说明了它的工作原理。我想知道AzTECC100.fits数据是否公开?在没有访问正在使用的数据的情况下,是否有可能回答这个问题?