Python 2D高斯拟合数据中的NaN值(Python 2D Gaussian Fit with NaN Values in Data)
我是Python的新手,但我正在尝试为某些数据生成2D高斯拟合。 具体而言,恒星通量与坐标系/网格中的某些位置相关联。 然而,并非我的网格中的所有位置都具有相应的通量值。 我真的不想将这些值设置为零,以防它偏向我,但我似乎无法将它们设置为
nan
并且仍然可以使我的Gaussian适合工作。 这是我正在使用的代码(从这里略微修改):import numpy import scipy from numpy import * from scipy import optimize def gaussian(height, center_x, center_y, width_x, width_y): width_x = float(width_x) width_y = float(width_y) return lambda x,y: height*exp(-(((center_x-x)/width_x)**2+((center_y-y)/width_y)**2)/2) def moments(data): total = nansum(data) X, Y = indices(data.shape) center_x = nansum(X*data)/total center_y = nansum(Y*data)/total row = data[int(center_x), :] col = data[:, int(center_y)] width_x = nansum(sqrt(abs((arange(col.size)-center_y)**2*col))/nansum(col)) width_y = nansum(sqrt(abs((arange(row.size)-center_x)**2*row))/nansum(row)) height = nanmax(data) return height, center_x, center_y, width_x, width_y def fitgaussian(data): params = moments(data) errorfunction = lambda p: ravel(gaussian(*p)(*indices(data.shape)) - data) p, success = optimize.leastsq(errorfunction, params) return p parameters = fitgaussian(data) fit = gaussian(*parameters)
我的通量值位于称为
data
的2D数组中。 如果我在这个数组中有0
而不是nan
值,代码可以工作,但是否则我的parameters
总是出现为[nan nan nan nan nan]
。 如果有办法解决这个问题,我将非常感谢您的见解! 解释越详细越好。 提前致谢!I'm very new to Python but I'm trying to produce a 2D Gaussian fit for some data. Specifically, stellar fluxes linked to certain positions in a coordinate system/grid. However not all of the positions in my grid have corresponding flux values. I don't really want to set these values to zero in case it biases my fit, but I can't seem to set them to
nan
and still get my Gaussian fit to work. This is the code I'm using (modified slightly from here):import numpy import scipy from numpy import * from scipy import optimize def gaussian(height, center_x, center_y, width_x, width_y): width_x = float(width_x) width_y = float(width_y) return lambda x,y: height*exp(-(((center_x-x)/width_x)**2+((center_y-y)/width_y)**2)/2) def moments(data): total = nansum(data) X, Y = indices(data.shape) center_x = nansum(X*data)/total center_y = nansum(Y*data)/total row = data[int(center_x), :] col = data[:, int(center_y)] width_x = nansum(sqrt(abs((arange(col.size)-center_y)**2*col))/nansum(col)) width_y = nansum(sqrt(abs((arange(row.size)-center_x)**2*row))/nansum(row)) height = nanmax(data) return height, center_x, center_y, width_x, width_y def fitgaussian(data): params = moments(data) errorfunction = lambda p: ravel(gaussian(*p)(*indices(data.shape)) - data) p, success = optimize.leastsq(errorfunction, params) return p parameters = fitgaussian(data) fit = gaussian(*parameters)
My flux values are in a 2D array called
data
. The code works if I have0
instead ofnan
values in this array, but otherwise myparameters
always come out as[nan nan nan nan nan]
. If there's a way to fix this, I would really appreciate your insight! The more detailed the explanation, the better. Thanks in advance!
原文:https://stackoverflow.com/questions/30790234
最满意答案
首先,是否可以选择响应断言并选中“忽略状态”复选框?
您的代码没有任何问题,它应该可以正常工作。
尝试添加
debug();
指令作为脚本的第一行并查看JMeter控制台以输出或向脚本添加一些log.info(...)
调用以查看值的实际“值”以及一些其他有用的信息,例如:有关使用 Beanshell脚本和对其进行故障排除的全面信息,请参见如何使用BeanShell:JMeter最喜欢的内置组件 。
First of all, can it be the case you have Response Assertion with "Ignore Status" box checked?
There is nothing wrong with your code, it should work fine.
Try adding
debug();
directive as first line of your script and look into JMeter console for output or add somelog.info(...)
calls to your script to see the actual "value" of yourvalue
and some other helpful information like:See How to Use BeanShell: JMeter's Favorite Built-in Component for comprehensive information on using and troubleshooting your Beanshell scripts.
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
对于整数数据类型,请尝试以下代码int a = Integer.parseInt(a); for integer datatype please try following code int a = Integer.parseInt(a);
-
我为其他事情做了一些BeanShell断言后发现了问题。 我现在也因为没有意识到这一点而感到非常愚蠢...... 问题是属性textEdit中的值是\"Changed the TEXT\" ,因此以反斜杠开头。 由于这个反斜杠,程序在尝试将其分配给String变量sentText或直接在if语句中使用属性时不知道如何处理它。 通过在引号之间放置属性调用,程序可以正确地将它保存在String变量中。 喜欢这个: ${__setProperty(textEditPost,${textEditPost})} St ...
-
在线程组中选择“停止线程”将帮助您在出现任何错误时停止线程组 - 假设您有其他线程组要连续执行。 如果没有,测试将停止。 在beanshell中包括 else if (ResponseCode.equals("409") == true) { Failure = true; FailureMessage = "Creation of a new CAE record failed: Attempting to create a duplicate record."; } Selectin ...
-
在Java语言中使用撇号来表示char。 你不能拥有''char值。 然而,早期版本的beanshell可能已经进行了一些隐式转换为一个空字符串,在升级之后它会以某种方式被破坏。 使用toString()检查有效值对我来说似乎有点浪费,但如果'contact'为非null,但仍然没有toString()表示中的值,则检查String值,如下所示: if(contact != null) { contactStr = contact.toString(); if(contactStr != nul ...
-
如何在Beanshell断言中停止进一步执行程序(How to stop the further execution of a Program in Beanshell Assertion)[2022-03-18]
在任何想要停止执行BeanShell代码的地方添加return关键字。 Add return keyword wherever you want to stop the execution of BeanShell code. -
我们怎样才能在Jmeter中使用Beanshell验证Json响应?(How can we Verify Json response using Beanshell in Jmeter?)[2022-12-28]
你可以使用beanshell断言。它会帮助你很多。 prev.getResponseDataAsString()将以字符串形式返回您的json响应。 我希望您将使用Jsonpath在线评估程序来评估json响应。 假设我的json在线评估代码看起来像$.items[2].CM_SEQNUMBER在beanshell断言中我使用过 JsonObject jsonobj = JsonObject.readFrom(jsonString); JsonArray jsonarr = jsonobj.get ... -
如何在beanshell断言中检查值是否为负数?(How to check that a value is not a negative number in beanshell assertion?)[2023-11-29]
首先,是否可以选择响应断言并选中“忽略状态”复选框? 您的代码没有任何问题,它应该可以正常工作。 尝试添加debug(); 指令作为脚本的第一行并查看JMeter控制台以输出或向脚本添加一些log.info(...)调用以查看值的实际“值”以及一些其他有用的信息,例如: 有关使用 Beanshell脚本和对其进行故障排除的全面信息,请参见如何使用BeanShell:JMeter最喜欢的内置组件 。 First of all, can it be the case you have Response Asse ... -
你有2个拼写错误,正确的陈述是: Failure = true; FailureMessage = "Here goes the failure message"; 断言是成功的,因为从Beanshell角度来看代码很好,在Beanshell中你不需要显式定义对象类。 只要它是有效的代码 - 您的断言是成功的。 以下是一些故障排除技巧: 添加debug(); 作为Beanshell脚本的第一行将触发调试输出到stdout 通过在try / catch块中包含代码,如: try { //your c ...