gpt4 book ai didi

python - Numpy reshape 产生不同的大小错误

转载 作者:行者123 更新时间:2023-12-01 05:39:39 25 4
gpt4 key购买 nike

我有一段 Python 代码,已经使用了大约一年,没有任何问题(它读取、解压缩/解包数据、选择一个窗口并使用 Numpy/Matplotplib 绘制它)。

我们最近得到了一台新机器,它以 64 位编码格式存储数据,而不是 32 位编码,这应该不是一个真正的问题。我重写了几行代码来处理这个问题(下面包含了两个感兴趣的函数),但我不断收到一个我不太明白的错误。

错误:

Traceback (most recent call last):
File "./3D_Visualizer.py", line 238, in <module>

File "./3D_Visualizer.py", line 232, in main
main()
File "./3D_Visualizer.py", line 93, in plot_data
ax.set_ylabel('Time (s)',color='r')
File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 170, in reshape
return _wrapit(a, 'reshape', newshape, order=order)
File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 37, in _wrapit
result = getattr(asarray(obj),method)(*args, **kwds)
ValueError: total size of new array must be unchanged

如果有人能够向我解释为什么会发生此错误,因为我理解 numpy reshape 函数采用输入数组的大小来进行实际调整大小,我会很高兴。导致此错误的两个函数的代码如下:

阅读功能:

def numpy_array(data, peaks):
"""Fills the NumPy array 'data' with m/z-intensity values acquired
from b64 decoding and unpacking the binary string read from the
mzXML file, which is stored in the list 'peaks'.

The m/z values are assumed to be ordered without validating this
assumption.

Note: This function is the performance bottleneck
"""
rt_counter=0
for x in peaks:
if rt_counter %(len(peaks)/20) == 0:
update_progress()
peak_counter=0
data_buff=base64.b64decode(x)
endian = '!'
precision = 'd'
buff_size = len(data_buff) / struct.calcsize(endian + precision)
index=0
for y in struct.unpack(endian + precision * buff_size, data_buff[0:len(data_buff)]):
if (index % 2 == 0):
data[rt_counter][1][peak_counter][0]= y
else:
data[rt_counter][1][peak_counter][1]= y
peak_counter+=1
index+=1
rt_counter+=1

绘图功能:

def plot_data(X,Y,Z):
"""Plots a 3D wireframe based on the x, y and z datapoints passed
to this function in the python lists 'X', 'Y' and 'Z'. Custom
labels are created for the Y (m/z) axis since matplotlib creates
'ugly' labels by default (TODO: labelling goes wrong).
"""
fig=plt.figure()
x=sorted(set(X))
y=sorted(set(Y))
labels=['%.2f'%k for k in y]
XX,YY=np.meshgrid(y,x)
ZZ=np.reshape(Z,XX.shape)
ax=fig.add_subplot(111,projection='3d')
ax.plot_wireframe(XX,YY,ZZ)
ax.set_title('3D projection of LC-MS region',size='large',color='r')
ax.set_xlabel('m/z',color='r',style='italic')
ax.set_xticklabels(labels)
ax.set_ylabel('Time (s)',color='r')
ax.set_zlabel('Intensity',color='r')
plt.show()

-- 2013年7月30日 2:10 --

在引发此错误的测试用例中,X、Y 和 Z 的长度都相同(即 184)。在错误测试用例中的 set 行之后,x 和 y 的长度分别为 18 和 20,而在工作测试用例中的 set 行之后,x 和 y 的长度分别为 18 和 11。 p>

错误情况下y的示例内容(64位编码):

[1398.51513671875, 1398.5152587890625, 1398.5225830078125, 1398.522705078125, 1398.530029296875, 1398.5301513671875, 1398.5374755859375, 1398.53759765625, 1398.5447998046875, 1398.544921875, 1398.55224609375, 1398.5523681640625, 1398.5596923828125, 1398.559814453125, 1398.567138671875, 1398.5672607421875, 1398.5745849609375, 1398.5819091796875, 1398.58203125, 1398.58935546875]

工作案例中“y”的示例内容(32 位编码):

[1398.51171875, 1398.5191650390625, 1398.526611328125, 1398.533935546875, 1398.5413818359375, 1398.548828125, 1398.5562744140625, 1398.5635986328125, 1398.571044921875, 1398.5784912109375, 1398.5859375]

我认为,这表明在错误情况下解码在拟合值方面存在问题?

-- 2013年7月31日 10:20 --

我深入研究了从实际机器获取的原始数据,结果表明所有时间点的测量坐标都不相同(机器+控制软件的所有早期版本都是这种情况)。这会导致某些坐标稍微移动一点(即 1398.5152... vs 1398.5151),从而导致 reshape 失败。

我目前正在将每个坐标分配给一个整数值(1、2 ...),以暂时“热修复”此问题。

最佳答案

也许不是真正的答案,但评论字段有些有限:

我发现回溯很奇怪:它从 ax.set_ylabel 跳转到 return _wrapit(a, 'reshape', newshape, order=order),没有任何中间调用在 matplotlib 内部(例如,我希望从 set_ylabel 内部调用 fromnumeric.reshape。代码中唯一出现的 reshape

ZZ=np.reshape

所以也许罪魁祸首就是这个,不知何故顺序在回溯中被搞乱了。

ZZ=np.reshape(Z,XX.shape)
如果 ZXX 具有不兼容的形状,

将会失败。我假设在调用 plot_data() 时,XYZ 确实具有兼容的形状。由于 XX 是在 meshgrid() 调用中创建的,因此 yx 的尺寸可能不正确。由于 xy 是通过在 XY 上调用 set() 创建的,这可能就是出问题的地方:在 XY 中多次出现的值可能会搞乱事情,即使它们都涉及浮点值。

因此,我将验证 XYZ 的形状(长度),然后验证 x 的形状(长度) code> 和 y 也是如此。

我注意到

unpack_format=">%dL" % buff_size`

line 表明数据实际上是长整型,而不是 float (使得更容易遇到多次出现值的问题)。话又说回来,unpack_format 没有被使用。

关于python - Numpy reshape 产生不同的大小错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17942036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com