Printing to file, how do I do it efficiently?

Robert Kern robert.kern at gmail.com
Fri Nov 10 01:51:30 EST 2006


Cameron Walsh wrote:
> Hi all,
> 
> I have a numpy.array of 89x512x512 uint8's, set up with code like this:

numpy questions are best asked on the numpy list, not here.

  http://www.scipy.org/Mailing_Lists

> data=numpy.array([],dtype="uint8")
> data.resize((89,512,512))

You might want to look at using numpy.empty() here, instead.

> # Data filled in about 4 seconds from 89 image slices
> <snip lots of processing code>
> 
> 
> I first tried writing this data to a binary raw format (for use in a
> program called Drishti) as follows:
> 
> # The slow bit:
> volumeFile=file("/tmp/test.raw","wb")
> for z in xrange(Z):
>     for y in xrange(Y):
>         for x in xrange(X):
>             volumeFile.write("%c" %(data[z,y,x]))
> volumeFile.close()
> 
> That took about 39 seconds.
> 
> My second attempt was as follows:
> volumeFile = open("/tmp/test2.raw","wb")
> data.resize((X*Y*Z)) # Flatten the array
> for i in data:
>     volumeFile.write("%c" %i)
> data.resize((Z,Y,X))
> volumeFile.close()
> 
> This took 32 seconds.  (For those of you unfamiliar with numpy, the
> data.resize() operations take negligible amounts of time, all it does is
> allow the data to be accessed differently.)

No, if the total size is different, it will also copy the array. Use .reshape()
if you want to simply alter the shape, not the total number of elements.

> I'm guessing that the slow part is the fact that I am converting the
> data to character format and writing it one character at a time.  What
> is a better way of doing this, or where should I look to find a better way?

data.tostring()

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list