difficulties with numpy.tofile()
Hello, I'm having problems writing a 2-dimensional numpy array out to a binary file using the "tofile" method. The call to "tofile" appears to succeed, but when I check the length of the binary file I find that it's longer than what is expected, given the calculation of nrows*ncolumns*nbytes. The array that I'm writing out to the file is the result of running kmeans2 (from scipy), so I'm expecting that the datatype is numpy.int32. Is there an easy way to check what the datatype of a numpy array is? Yet, the file that results from the call: data.tofile('./data1.bin','int32') is longer than it should be. Using "float32" as the datatype results in an even larger file, so it's still longer than expected. Does the "tofile" call insert some extra bytes at the beginning or end of the binary file? What is the best way of dumping a large numpy array to a binary file that can then be read in from Fortran? I'm running python 2.5, numpy 1.2.1 on a Linux box with Fedora Core 7. Catherine
On Sun, Nov 16, 2008 at 23:37, Catherine Moroney <catherine.m.moroney@jpl.nasa.gov> wrote:
Hello,
I'm having problems writing a 2-dimensional numpy array out to a binary file using the "tofile" method. The call to "tofile" appears to succeed, but when I check the length of the binary file I find that it's longer than what is expected, given the calculation of nrows*ncolumns*nbytes.
The array that I'm writing out to the file is the result of running kmeans2 (from scipy), so I'm expecting that the datatype is numpy.int32. Is there an easy way to check what the datatype of a numpy array is?
some_array.dtype
Yet, the file that results from the call: data.tofile('./data1.bin','int32') is longer than it should be.
Using "float32" as the datatype results in an even larger file, so it's still longer than expected.
That argument is not the datatype. If you provide that argument, it specifies that you are outputting ASCII, not binary, and are using that value as the separator. Just use the filename and nothing else. If you need the data as a different type, coerce the array using the .astype() method. http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tofile.htm... -- 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
On Sun, Nov 16, 2008 at 10:37 PM, Catherine Moroney < catherine.m.moroney@jpl.nasa.gov> wrote:
Hello,
I'm having problems writing a 2-dimensional numpy array out to a binary file using the "tofile" method. The call to "tofile" appears to succeed, but when I check the length of the binary file I find that it's longer than what is expected, given the calculation of nrows*ncolumns*nbytes.
You might find the following attributes useful in debugging this. In [1]: x = ones((2,2)) In [2]: x.size Out[2]: 4 In [3]: x.itemsize Out[3]: 8 In [4]: x.nbytes Out[4]: 32 In [5]: x.dtype Out[5]: dtype('float64') In [6]: x.tofile('foo.dat') In [7]: ls -l foo.dat -rw-rw-r-- 1 charris charris 32 2008-11-16 22:43 foo.dat The file should have the same size as x.nbytes. What operating system are you using? Chuck
participants (3)
-
Catherine Moroney
-
Charles R Harris
-
Robert Kern