- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一些二维数据,特别是扫描的 X 光片。这些具有重叠点源曝光的测量值。数据示例:http://i.stack.imgur.com/oawlU.png
我想通过对数据拟合二维高斯总和来找到峰值位置。我已经尝试了其他几种方法并取得了一些成功,包括定位全局最大值的“星形搜索”方法,适合高斯并减去它。循环这个可以很好地找到所有的峰,但它不稳定并且不是很准确。我想使用星号搜索输出作为拟合的第一个猜测,但我在实现 scipy.optimize.curve_fit
时遇到了麻烦。
我创建了一个函数 twoD_envelope
,它创建了从星搜索中找到的所有高斯分布的二维包络。这会产生以下输出:http://i.stack.imgur.com/4KnpG.png
我的印象是我可以使用它作为 curve_fit
中的初始猜测,但是我得到以下 TypeError
:
TypeError: twoD_envelope() takes 4 positional arguments but 358802 were given
358802 比数据大小多 1,这是一个巨大的线索,但我无法弄清楚问题出在哪里!我是一名具有“实用”编码知识的物理学家,因此非常感谢任何输入。
代码如下。
def twoD_envelope(npoints, xls, yls, pars):
envl = copy.copy(sq_image)
envl[:] = 0
for n in range(0,npoints):
height, cx, cy, width_x, width_y = pars[n]
FWHM = 0.5*(width_x+width_y)
g=makeGaussian(shape(sq_image)[0],fwhm=FWHM,center=[cx+xls[n],cy+yls[n]])
envl = envl + g
return envl.ravel()
# Create x and y indices
x = np.linspace(0, np.size(sq_image[0]), np.size(sq_image[0])+1)
y = np.linspace(0, np.size(sq_image[1]), np.size(sq_image[1])+1)
x, y = np.meshgrid(x, y)
coords = [x,y]
data = sq_image
initial_guess = twoD_envelope(9,xls,yls,pars)
pars_opt, pars_cov = opt.curve_fit(twoD_envelope, coords, data, p0=initial_guess)
(sq_image
是数据,一个形状为 (599,599)
的 ndarray
)
(pars, xls, yxl
= 来自星搜索的高斯拟合参数列表)
(makeGaussian
是在别处定义的函数)
最佳答案
我认为您的错误消息是您传递给 curve_fit
的函数的结果,它看起来不像是正确的形式。这可能会导致 data
数组被解释为 vars
,从而导致您看到的 TypeError
(无法运行代码很难判断) .根据文档,传递给 curve_fit 的函数应该,
take the independent variable as the first argument and the parameters to fit as separate remaining arguments.
您可能还需要在 initial_guess 之前使用星号以将其作为元组传递,例如
pars_opt, pars_cov = opt.curve_fit(twoD_envelope, coords, data, p0=*initial_guess)
要拟合单个 2D 高斯分布,其中 p0 大约有 7 个参数,有一个很好的答案可能会有所帮助:Fitting a 2D Gaussian function using scipy.optimize.curve_fit - ValueError and minpack.error .
您的问题的一个可能解决方案可能是遍历您的 2D sq_image 并在本地使用单个 2D 高斯拟合以及来自星搜索的每个初始参数...
编辑:适合高斯的代码。
import scipy.optimize as opt
import numpy as np
import pylab as plt
import matplotlib.cm as cm
import Image
def twoD_Gaussian((x, y), amplitude, xo, yo, sigma_x, sigma_y, theta):
xo = float(xo)
yo = float(yo)
a = (np.cos(theta)**2)/(2*sigma_x**2) + (np.sin(theta)**2)/(2*sigma_y**2)
b = -(np.sin(2*theta))/(4*sigma_x**2) + (np.sin(2*theta))/(4*sigma_y**2)
c = (np.sin(theta)**2)/(2*sigma_x**2) + (np.cos(theta)**2)/(2*sigma_y**2)
g = amplitude*np.exp( - (a*((x-xo)**2) + 2*b*(x-xo)*(y-yo)
+ c*((y-yo)**2)))
return g.ravel()
# Create x and y indices
I = Image.open('./30155885.png')
p = np.asarray(I).astype('float')
w,h = I.size
x, y = np.mgrid[0:h, 0:w]
#Use only one channel of image
p = p[:,:,0]
#Fit 2D Gaussian
initial_guess = (3,10,10,20,40,0)
popt, pcov = opt.curve_fit(twoD_Gaussian, (x, y), p.ravel(), p0=initial_guess)
data_fitted = twoD_Gaussian((x, y), *popt)
fig, ax = plt.subplots(1, 1)
cb = ax.imshow(p, cmap=plt.cm.jet, origin='bottom',
extent=(x.min(), x.max(), y.min(), y.max()))
ax.contour(x, y, data_fitted.reshape(x.shape[0], y.shape[1]), 8, colors='w')
plt.colorbar(cb)
plt.show()
图像 30155885 在哪里,
请注意,只使用了图像数据的一个 channel (您应该将数据数组替换为 sq_image 中的一个)。这导致,
关于python - 在 python 中将 2D 高斯总和拟合到 2d 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30155885/
我正在寻找一种方法来创建根据价格选择我的产品的过滤器(选择下拉菜单)。 我知道这样的查询是完全可能的: SELECT * FROM products ORDER BY price ASC SELECT
函数参数中或显示尺寸时(高度,宽度)的顺序是否有约定? 最佳答案 我不知道大量的语言,但我使用过的语言(宽度,高度)。它更适合沿着 (x, y) 坐标线。 关于language-agnostic -
在我的表单中,我让用户输入房间的长度高度和宽度以获得 m2、m3 和瓦特的计算值。但是用户也应该能够直接输入 height 和 m2 来获取值。我尝试了很多语法,但 if else 不能正常工作。我知
我在 Elasticsearch 中创建了一个索引,看起来像 {"amazingdocs":{"aliases":{},"mappings":{"properties":{"Adj Close":{"
我有以下功能,我需要清除数据库中的所有图片列并移动到文件系统。当我一次性完成这一切时,内存太多并且会崩溃。我切换到递归函数并执行 20 次写入和批量操作。 我需要为大约 6 个表执行此操作。我的 Re
我正在编写一个函数来计算 PI 的值,并将其作为 double 值返回。到目前为止,一切都很好。但是一旦函数到达小数点后14位,它就不能再保存了。我假设这是因为 double 有限。我应该怎么做才能继
2020年是中国CDN行业从98年诞生到今天快速发展的第二十四年,相关数据显示,全国感知网速持续上扬,达到了3.29兆/秒,标志着在宽带中国的政策指导下,中国的网速水平正在大步赶上世界发达国家的水平
在 aerospike 集合中,我们有四个 bin userId、adId、timestamp、eventype,主键是 userId:timestamp。在 userId 上创建二级索引以获取特定用
$('#container').highcharts('Map', { title : { text : 'Highmaps basic demo'
有没有办法显示自定义宽度/高度的YouTube视频? 最佳答案 在YouTube网站上的this link中: You can resize the player by editing the obj
我使用 Highcharts ,我想在 Highcharts 状态下悬停时制作动态不同的颜色。 正如你可以看到不同的颜色,这就是我做的 var usMapChart , data = [] ; va
在所有节点上运行 tpstats 后。我看到很多节点都有大量的 ALL TIME BLOCKED NTR。我们有一个 4 节点集群,NTR ALL TIME BLOCKED 的值为: 节点 1:239
我发现 APC 上存在大量碎片 (>80%),但实际上性能似乎相当不错。我有 read another post这建议在 wordpress/w3tc 中禁用对象缓存,但我想知道减少碎片是否比首先缓存
对于我的脚本类(class),我们必须制作更高/更低的游戏。到目前为止,这是我的代码: import random seedVal = int(input("What seed should be u
我发现 APC 上存在大量碎片 (>80%),但实际上性能似乎相当不错。我有 read another post这建议在 wordpress/w3tc 中禁用对象缓存,但我想知道减少碎片是否比首先缓存
对于我的脚本类(class),我们必须制作更高/更低的游戏。到目前为止,这是我的代码: import random seedVal = int(input("What seed should be u
我已经 seen >2 字节的 unicode 代码点,如 U+10000 可以成对编写,如 \uD800\uDC00。它们似乎以半字节 d 开头,但我只注意到了这一点。 这个 split Actio
有人可以帮我理解为什么我的饼图百分比计算不正确吗?看截图: 根据我的计算,如 RHS 上所示,支出百分比应为 24.73%。传递给 Highcharts 的值如下:- 花费:204827099.36-
我阅读了有关该问题的所有答案,但我还没有找到任何解决方案。 我有一个应用程序,由我的 api 服务器提供。 Wildfly 8.1 和 Mysql 5.6。当查看时间到来时(Wildfly 服务器连接
我正在用选定的项目创建圆形导航。当用户单击任何项目时,它将移动到定义的特定点。一切都很好,除了当你继续点击项目时,当动画表现不同并且项目在 360 度圆中移动并且它被重置直到你重复场景时,我希望它
我是一名优秀的程序员,十分优秀!