2017-08-08 11 views
0

numpy.memmapの仕組みを理解することに問題があります。背景には、エントリを削除することによって大きなディスクnumpyをディスクに保存する必要があるということです。配列を読み込み、必要な部分をコピーして新しい配列を作成することはできません。これはメモリに収まらないだけです。したがって、考え方はnumpy.memmapです。つまり、ディスクで作業しています。彼女は(小さなファイルで)私のコードです: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" 

このコードを実行する私が取得:

1:

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 

フォーム出力、私が何か間違ったことをやっていることは明らかです)memmapには配列に設定された値が含まれておらず、in_memmapout_memmapには同じ値が含まれています。

2)copyコマンドがin_memmapからout_memmapまでのものを(同じ値のために)コピーするかどうかは不明です。デバッグモードで、in_memmap[i]out_memmap[i]の値をチェックすると、両方の値が得られます:memmap([1297436307], dtype=uint32)。だから私はコードにそれらを割り当てることができますか使用する必要がありますか:?

3)out.npyflush()操作でout_memmapの値に更新されません。

誰でも私がここで間違っていることを理解するのを手伝ってもらえますか?

どうもありがとう

+0

あなたの問題は 'np.save'と' np.memmap'のように若干異なるフォーマットをしているようです。 [this](https://stackoverflow.com/questions/23062674/numpy-memmap-map-to-save-file)の回答 –

+0

をチェックしてください。また、RAMよりも大きいアレイを定期的に使用している場合は、チェックアウトしてください[dask](https://dask.pydata.org/en/latest/) –

答えて

0

np.lib.format.open_memmapnp.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はヘッダを自動的にスキップしてデータを処理できます)。

関連する問題