- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 numpy 的 python 脚本,它应该拍摄图像并在返回单个值之前执行一些计算。当我单独执行每一行时,它会按预期工作。当我将它们全部放在 .py 脚本中并从命令行或 Canopy 中运行它时,它返回一个数组。
我稍微修改了代码,不需要通常的图像输入,结果是相同的:
import numpy as np
# Instead of loading an image, generate a test case (w or wo structured noise)
roi = np.random.poisson(38,(256,256));
blob = np.random.poisson(5,(128,128));
roi[64:192,64:192] = roi[64:192,64:192]+blob;
# Load the other variables if necessary (i.e., no DICOM to load)
[xDim,yDim] = [512,512];
roiLength = xDim/2;
pix = 1.18958;
# Declare memory for the FFTs
sizeFFT = xDim;
NPS2D = np.zeros((sizeFFT,sizeFFT)); # declare memory for fft results
fftslice = np.zeros((sizeFFT,sizeFFT));
# Set the dimension of the ROI and pull the pixel size. This will be
# used for the scaling factor in the 2D NPS.
deltaX = pix;
deltaY = pix;
scaleFactor = (deltaX/roiLength)*(deltaY/roiLength);
# Calculate the NPS
roiMean = np.mean(roi);
fftslice = np.fft.fft2((roi-roiMean),s=[sizeFFT,sizeFFT]);
NPS2D = scaleFactor*np.fft.fftshift(np.multiply(fftslice,np.conj(fftslice)));
NPS2D = NPS2D.real;
# Subtract the white noise from the NPS to get the structured NPS
stNPS = NPS2D - roiMean*deltaX*deltaY;
# Calculate SNI
SNI=sum(stNPS)/sum(NPS2D);
# Display the result
print SNI;
如果我执行每一行,结果是 0.107213670449 (或类似的,因为它每次都会重新生成一个随机数组)。如果我使用 python foo.py
从命令行运行脚本或单击 Canopy 中的播放按钮,结果是一个 512 长度的数组 [4.64940089e-03 ... -4.59789051e -02 -7.15113682e-02]
,其中我手动删除了 509 个条目。
有什么想法吗?我错过了一些明显的东西吗?
最佳答案
使用内置 sum
函数与使用 numpy.sum
或数组的 sum
方法不同。
对于 >1d 数组,Python 的 sum
将给出非常不同的结果:
In [1]: import numpy as np
In [2]: x = np.arange(100).reshape(10, 10)
In [3]: x
Out[3]:
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
In [4]: sum(x)
Out[4]: array([450, 460, 470, 480, 490, 500, 510, 520, 530, 540])
In [5]: x.sum()
Out[5]: 4950
In [6]: np.sum(x)
Out[6]: 4950
这是因为 python 的求和基本上是对对象的 for 循环求和。
循环 >1d 数组将返回沿第一个轴的切片。例如
In [7]: for item in x:
...: print item
...:
[0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]
在这种情况下,Python 的 sum
有效地为您提供了列的总和(即 row1 + row2 + row3 ...
)
关于Python 脚本返回一个数组而不是单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29472599/
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
在编码时,我问了自己这个问题: 这样更快吗: if(false) return true; else return false; 比这个? if(false) return true; return
如何在逻辑条件下进行“返回”? 在这样的情况下这会很有用 checkConfig() || return false; var iNeedThis=doSomething() || return fa
这是我的正则表达式 demo 如问题所述: 如果第一个数字是 1 则返回 1 但如果是 145 则返回 145 但如果是 133 则返回 133 样本数据a: K'8134567 K'81345678
在代码高尔夫问答部分查看谜题和答案时,我遇到了 this solution返回 1 的最长和最晦涩的方法 引用答案, int foo(void) { return! 0; } int bar(
我想在下面返回 JSON。 { "name": "jackie" } postman 给我错误。说明 Unexpected 'n' 这里是 Spring Boot 的新手。 1日龄。有没有正确的方法来
只要“is”返回 True,“==”不应该返回 True 吗? In [101]: np.NAN is np.nan is np.NaN Out[101]: True In [102]: np.NAN
我需要获取所有在 6 号或 7 号房间或根本不在任何房间的学生的详细信息。如果他们在其他房间,简单地说,我不希望有那个记录。 我的架构是: students(roll_no, name,class,.
我有一个表单,我将它发送到 php 以通过 ajax 插入到 mysql 数据库中。一切顺利,php 返回 "true" 值,但在 ajax 中它显示 false 消息。 在这里你可以查看php代码:
我在 Kotlin 中遇到了一个非常奇怪的无法解释的值比较问题,以下代码打印 假 data class Foo ( val a: Byte ) fun main() { val NUM
请注意,这并非特定于 Protractor。问题在于 Angular 2 的内置 Testability service Protractor 碰巧使用。 Protractor 调用 Testabil
在调试窗口中,以下表达式均返回 1。 Application.WorksheetFunction.CountA(Cells(4 + (i - 1) * rows_per_record, 28) & "
我在本地使用 jsonplaceholder ( http://jsonplaceholder.typicode.com/)。我正在通过 extjs rest 代理测试我的 GET 和 POST 调用
这是 Postman 为成功调用我的页面而提供的(修改后的)代码段。 var client = new RestClient("http://sub.example.com/wp-json/wp/v2
这个问题在这里已经有了答案: What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must
我想我对 C 命令行参数有点生疏。我查看了我的一些旧代码,但无论这个版本是什么,都会出现段错误。 运行方式是 ./foo -n num(其中 num 是用户在命令行中输入的数字) 但不知何故它不起作用
我已经编写了一个类来处理命名管道连接,如果我创建了一个实例,关闭它,然后尝试创建另一个实例,调用 CreateFile() 返回 INVALID_HANDLE_VALUE,并且 GetLastErro
即使 is_writable() 返回 true,我也无法写入文件。当然,该文件存在并且显然是可读的。这是代码: $file = "data"; echo file_get_contents($fil
下面代码中的变量 $response 为 NULL,尽管它应该是 SOAP 请求的值。 (潮汐列表)。当我调用 $client->__getLastResponse() 时,我从 SOAP 服务获得了
我一直在网上的不同论坛上搜索答案,但似乎没有与我的情况相符的... 我正在使用 Windows 7,VS2010。 我有一个使用定时器来调用任务栏刷新功能的应用程序。在该任务栏函数中包含对 LoadI
我是一名优秀的程序员,十分优秀!