【问题标题】:numpy memmap modify filesnumpy memmap 修改文件
【发布时间】:2018-01-15 23:17:50
【问题描述】:

我无法理解numpy.memmap 的工作方式。背景是我需要通过删除条目来减少保存在磁盘上的大型numpy 数组。读取数组并通过复制所需的部分来建立一个新的部分是行不通的——它只是不适合内存。所以这个想法是使用numpy.memmap - 即在光盘上工作。她是我的代码(带有一个小文件):

import numpy

in_file = './in.npy'
in_len = 10
out_file = './out.npy'
out_len = 5

# Set up input dummy-file
dummy_in = numpy.zeros(shape=(in_len,1),dtype=numpy.dtype('uint32'))
for i in range(in_len):
    dummy_in[i] = i + i
numpy.save(in_file, dummy_in)

# get dtype and shape from the in_file
in_npy = numpy.load(in_file)

in_dtype = in_npy.dtype
in_shape = (in_npy.shape[0],1)
del(in_npy)

# generate an 'empty' out_file with the desired dtype and shape
out_shape = (out_len,1)
out_npy = numpy.zeros(shape=out_shape, dtype=in_dtype)
numpy.save(out_file, out_npy)
del(out_npy)

# memmap both files
in_memmap = numpy.memmap( in_file,  mode='r',  shape=in_shape, dtype=in_dtype)
out_memmap = numpy.memmap(out_file, mode='r+', shape=out_shape, dtype=in_dtype)
print "in_memmap"
print in_memmap, "\n"
print "out_memmap before in_memmap copy"
print out_memmap, "\n"

# copy some parts
for i in range(out_len):
    out_memmap[i] = in_memmap[i]

print "out_memmap after in_memmap copy"
print out_memmap, "\n"
out_memmap.flush()

# test
in_data = numpy.load(in_file)
print "in.npy"
print in_data
print in_data.dtype, "\n"

out_data = numpy.load(out_file)
print "out.npy"
print out_data
print out_data.dtype, "\n"

运行这段代码我得到:

in_memmap
[[1297436307]
 [     88400]
 [ 662372422]
 [1668506980]
 [ 540682098]
 [ 880098343]
 [ 656419879]
 [1953656678]
 [1601069426]
 [1701081711]]

out_memmap before in_memmap copy
[[1297436307]
 [     88400]
 [ 662372422]
 [1668506980]
 [ 540682098]]

out_memmap after in_memmap copy
[[1297436307]
 [     88400]
 [ 662372422]
 [1668506980]
 [ 540682098]]

in.npy
[[ 0]
 [ 2]
 [ 4]
 [ 6]
 [ 8]
 [10]
 [12]
 [14]
 [16]
 [18]]
uint32

out.npy
[[0]
 [0]
 [0]
 [0]
 [0]]
uint32

从输出中可以看出我做错了什么:

1) memmaps 不包含数组中设置的值,in_memmapout_memmap 包含相同的值。

2) 不清楚复制命令是否将任何从in_memmap 复制到out_memmap(由于相同的值)。在调试模式下检查in_memmap[i]out_memmap[i] 的值我得到了:memmap([1297436307], dtype=uint32)。那么我可以像在代码中那样分配它们还是必须使用:out_memmap[i][0] = in_memmap[i][0]

3) out.npy 不会通过 flush() 操作更新为 out_memmap 值。

谁能帮我理解我在这里做错了什么。

非常感谢

【问题讨论】:

  • 您的问题似乎是np.savenp.memmap 的格式略有不同。检查this回答出来
  • 另外,如果您经常使用超出 RAM 处理能力的阵列,请查看 dask

标签: python-2.7 numpy numpy-memmap


【解决方案1】:

np.memmap 的每个实例替换为np.lib.format.open_memmap 并得到:

in_memmap 
[[ 0]
 [ 2]
 [ 4]
 [ 6]
 [ 8]
 [10]
 [12]
 [14]
 [16]
 [18]] 

out_memmap before in_memmap copy 
[[0]
 [0]
 [0]
 [0]
 [0]] 

out_memmap after in_memmap copy 
[[0]
 [2]
 [4]
 [6]
 [8]] 

in.npy 
[[ 0]
 [ 2]
 [ 4]
 [ 6]
 [ 8]
 [10]
 [12]
 [14]
 [16]
 [18]] 
 uint32 

out.npy 
[[0]
 [2]
 [4]
 [6]
 [8]] 
 uint32 

np.save 添加了一个 np.memmap 正在读取的标头,这就是为什么两者中的数据看起来相同(因为它是相同的标头)。这也是为什么当您将数据从一个复制到另一个时它没有效果(因为它只是复制标题,而不是数据)np.lib.format.open_memmap 自动跳过标题,以便您可以处理数据。

【讨论】:

    猜你喜欢
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多