【发布时间】:2013-11-26 10:20:54
【问题描述】:
我正在将 x,y,z 点文件 (LAS) 读入 python 并遇到内存错误。我正在为我正在从事的项目的已知点之间插入未知点。我开始处理小文件( 50,000,000 个点),现在我的代码因 MemoryError 而失败。
有哪些方法可以处理如此大量的数据?我不必一次将所有数据加载到内存中,但我需要使用scipy kd-tree 查看相邻点我在 64 位 Windows XP 操作系统上使用 Python 2.7 32 位。
提前致谢。
编辑:代码发布在下面。我取出了长计算和变量定义的代码。
from liblas import file
import numpy as np
f = file.File(las_file, mode='r')
num_points = int(f.__len__())
dt = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('i', 'u2'), ('c', 'u1'), ('t', 'datetime64[us]')]
xyzict = np.empty(shape=(num_points,), dtype = dt)
counter = 0
for p in f:
newrow = (p.x, p.y, p.z, p.intensity, p.classification, p.time)
xyzict[counter] = newrow
counter += 1
dropoutList = []
counter = 0
for i in np.nditer(xyzict):
# code to define P1x, P1y, P1z, P1t
if counter != 0:
# code to calculate n, tDiff, and seconds
if n > 1 and n < scanN:
# code to find v and vD
for d in range(1, int(n-1)):
# Code to interpolate x, y, z for points between P0 and P1
# Append tuple of x, y, and z to dropoutList
dropoutList.append(vD)
# code to set x, y, z, t for next iteration
counter += 1
【问题讨论】:
-
你能显示给出错误的代码吗? (或者是重现问题的小sn-p?)可能有办法让它更高效,但没有代码就无法判断。
-
您使用的是
np.loadtxt还是np.genfromtxt?如果是这样,它们对于大文件来说效率很低。 (不要插入我自己的答案,但它是相关的:stackoverflow.com/a/8964779/325565)您可以滚动自己的阅读器并使用fromiter,或者这些天,只使用熊猫。 (Pandas 恰好有一个非常高效的以空格分隔的 ascii 阅读器。)
标签: python numpy scipy out-of-memory