Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the endiannes directly in the numpy datatype for a 16bit unsigned integer?

Tags:

python

numpy

I would think this is a simple question but I haven't found the answer so far.

I work with fairly large (~2 GB) binary data images. I load them into python with the line

data = np.memmap(filename, dtype=np.dtype('uint16'), mode='r').byteswap()

For large files this can take several seconds. Anyway, I notice that the same line but without the byteswapping takes only an instant.

So the question is this: is there a way to specify the Byte order directly in the datatype so I don't need to do the byteswap afterwards? According to http://docs.scipy.org/doc/numpy/reference/arrays.dtypes.html,

A data type object (...) describes the following aspects of the data: (...) 3. Byte order of the data (little-endian or big-endian) (...)

How do I do this for a 16-bit unsigned integer? Things like np.dtype('>uint16') haven't worked for me, they give me a TypeError: data type ">uint16" not understood error.

Thank you!

like image 616
Federico Barabas Avatar asked Dec 23 '13 15:12

Federico Barabas


1 Answers

specify dtype as >H (Numeric typecodes) or >u2 (Array-protocol type strings):

np.memmap('test.bin', dtype=np.dtype('>u2'), mode='r')
like image 95
falsetru Avatar answered Sep 23 '22 19:09

falsetru