2

I have a main program that runs in pypy that creates three 2D numpy arrays. I want to save these to a file and then open them using python and plot them using matplotlib.pyplot.

Currently pypy does not work with numpy.save, is there an easy alternative method to save a group of numpy arrays to a file whilst using pypy?

user1696811
  • 941
  • 4
  • 10
  • 20

3 Answers3

3

The library pickle works with pypy. He's how I saved/loaded the numpy array

import pickle 
import numpy

save (using pypy):

outfile1 = open(r'C:\pythontmp\numpyArray.pkl', 'w+b')
pickle.dump(numpyArray.tolist(), outfile1)
outfile1.close()

Load (using python):

infile1 = open(r'C:\pythontmp\numpyArray.pkl', 'r+b')
file1 = pickle.load(infile1)                           # This is a list
infile1.close()

numpyArray = numpy.array(file1)                        # This is a numpy array
user1696811
  • 941
  • 4
  • 10
  • 20
1

You might be able to use ndarray.tofile() and numpy.fromfile(). This loses the ability to move data between machines with different endian-ness, but should be even faster than save().

Example:

a = numpy.zeros( (5,5) )
a.tofile('a.dat')
b = numpy.fromfile('a.dat')
Alex I
  • 19,689
  • 9
  • 86
  • 158
  • 4
    `tofile` and `fromfile` are also [not supported](http://buildbot.pypy.org/numpy-status/latest.html) by pypy. – tiago Nov 20 '12 at 02:10
1

You can try using python's struct module. There are some examples in answers here and here.

Another option is to use an external library like pyfits, or pytables. But I suspect they may not be available on pypy.

Community
  • 1
  • 1
tiago
  • 22,602
  • 12
  • 72
  • 88