Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recover a numpy array from npy.gz file

Tags:

python

numpy

I've saved a number of numpy objects with the following code:

f = gzip.GzipFile('/some/path/file.npy.gz', "w")
np.save(file=f, arr=np.rint(trimmed).astype('int16'))
f.close()

And now I have a bunch of npy.gz files, but I can't figure out how to programmatically return them back into python. np.fromtext or np.fromstring don't seem to work, and wouldn't preserve shape information anyway.

I've tried:

gzipfile = gzip.GzipFile('/some/path/file.npy.gz', 'rb')
text = gzipfile.read() 

And text looks like this:

b'\x93NUMPY\x01\x00F\x00{\'descr\': \'<i2\', \'fortran_order\': False, \'shape\': (132, 248, 291), } \n0\xf80\xf80...'

But what can I do next to get that string back into a numpy object?

like image 320
jstaker7 Avatar asked Mar 17 '17 05:03

jstaker7


1 Answers

If it works to save to a gzip file, it might also work to read from one. load is the counterpart to save:

In [193]: import gzip
In [194]: f = gzip.GzipFile('file.npy.gz', "w")
In [195]: np.save(f, np.arange(100))
In [196]: f.close()

In [200]: f = gzip.GzipFile('file.npy.gz', "r")
In [201]: np.load(f)
Out[201]: 
array([ 0,  1,  2,  3,  4,  .... 98, 99])

There is also a savez(compressed) that saves multiple arrays to a zip archive.

like image 165
hpaulj Avatar answered Nov 17 '22 14:11

hpaulj