In converting some code from numarray to numpy I had this: isBigendian = (arr.isbyteswapped() != numarray.isBigEndian) The only numpy version I've come up with is: isBigEndian = (arr.dtype.descr[0][1][0] == '>') which is short but very obscure. Has anyone got a suggestion for a clearer test? I found lots of *almost* useful flags and methods.  Russell
El dj 26 de 04 del 2007 a les 11:38 0700, en/na Russell E. Owen va escriure:
In converting some code from numarray to numpy I had this: isBigendian = (arr.isbyteswapped() != numarray.isBigEndian)
The only numpy version I've come up with is: isBigEndian = (arr.dtype.descr[0][1][0] == '>')
isBigEndian = (arr.dtype.str[0] == '>') is a little bit shorter. A more elegant approach could be: isBigEndian = arr.dtype.isnative ^ numpy.little_endian Cheers,  Francesc Altet  Be careful about using the following code  Carabos Coop. V.  I've only proven that it works, www.carabos.com  I haven't tested it.  Donald Knuth
In article <1177613824.2817.65.camel@localhost.localdomain>, Francesc Altet <faltet@carabos.com> wrote:
El dj 26 de 04 del 2007 a les 11:38 0700, en/na Russell E. Owen va escriure:
In converting some code from numarray to numpy I had this: isBigendian = (arr.isbyteswapped() != numarray.isBigEndian)
The only numpy version I've come up with is: isBigEndian = (arr.dtype.descr[0][1][0] == '>')
isBigEndian = (arr.dtype.str[0] == '>')
is a little bit shorter. A more elegant approach could be:
isBigEndian = arr.dtype.isnative ^ numpy.little_endian
Thank you. That's just what I wanted (I had looked for the numpy version of numarray.isBigEndian but somehow missed it). Your other suggestion is also much nicer than my version, but I'll use the latter.  Russell
Russell E. Owen wrote:
In converting some code from numarray to numpy I had this: isBigendian = (arr.isbyteswapped() != numarray.isBigEndian)
The only numpy version I've come up with is: isBigEndian = (arr.dtype.descr[0][1][0] == '>')
which is short but very obscure. Has anyone got a suggestion for a clearer test? I found lots of *almost* useful flags and methods.
How about looking at arr.dtype.byteorder? What are you trying to test for (where a datatype is bigendian or not?). sys.byteorder is either "little" or "big" numpy.little_endian is True if you are on a little endian system. arr.dtype.byteorder gives you either "=" for native or ">" for bigendian or "<" for littleendian or "" for it doesn't matter. To directly answer your question, however. On page 42 of the sample pages (online for free) of my book, the recommended translation of numarray's arr.isbyteswapped() is (not arr.dtype.isnative) The translation of numarray.isBigEndian is (not numpy.little_endian) Thus a direct translation of your code is: isBigEndian = ((not arr.dtype.isnative) != (not numpy.little_endian)) which is equivalent to isBigEndian = (arr.dtype.isnative != numpy.little_endian) and because != on boolean items is equivalent to XOR this is the same as isBigEndian = arr.dtype.isnative ^ numpy.little_endian Travis
In article <4630FDD9.9030307@ieee.org>, Travis Oliphant <oliphant.travis@ieee.org> wrote:
Russell E. Owen asked how best to translate some numarray code that determines if an array is in bigendian order...
What are you trying to test for (where a datatype is bigendian or not?).
I really want to know if the array is in bigendian order (I don't care whether it's native). This is for sending arrays to the ds9 image viewer via xpa (and communicating with ds9 is not easy). To do this reliably I need to indicate the byte order of the data. I suppose there are alternatives such as making a "native byte order" copy of the data and sending that without any flag. But I think it's easier to just send the flag.
To directly answer your question, however.
On page 42 of the sample pages (online for free) of my book, the recommended translation of numarray's arr.isbyteswapped() is (not arr.dtype.isnative)
I found that (I own your book).
The translation of numarray.isBigEndian is (not numpy.little_endian)
I missed that one (and I did look for it).
... isBigEndian = arr.dtype.isnative ^ numpy.little_endian
That should be much clearer than my translation. Thanks!  Russell
Russell, This should work as a consistent test for bigendian: > isBigEndian = (obj.dtype.str[0] == '>') Also, I have ported numarray's numdisplay to numpy if you would like to directly display an array in DS9. We haven't done an official release yet (sometime soon) but I can forward you a copy if you are interested. Chris
On Thu, Apr 26, 2007 at 05:22:42PM 0400, Christopher Hanley wrote:
This should work as a consistent test for bigendian:
> isBigEndian = (obj.dtype.str[0] == '>')
Is this test always safe, even on big endian machines? Couldn't the dtype.str[0] sometimes be '='? Regards Stéfan
Stefan van der Walt wrote:
On Thu, Apr 26, 2007 at 05:22:42PM 0400, Christopher Hanley wrote:
This should work as a consistent test for bigendian:
> isBigEndian = (obj.dtype.str[0] == '>')
Is this test always safe, even on big endian machines? Couldn't the dtype.str[0] sometimes be '='?
Regards Stéfan _______________________________________________
The test does would on big endian machines. What wouldn't work would be something like this: > isBigEndian = (obj.dtype.byteorder == '>') On a bigendian machine you would get '=' in this case. Cheers, Chris
On Fri, Apr 27, 2007 at 08:49:16AM 0400, Christopher Hanley wrote:
> isBigEndian = (obj.dtype.str[0] == '>')
Is this test always safe, even on big endian machines? Couldn't the dtype.str[0] sometimes be '='?
The test does would on big endian machines. What wouldn't work would be something like this:
> isBigEndian = (obj.dtype.byteorder == '>')
On a bigendian machine you would get '=' in this case.
Ah, yes, I was confused. What I meant to ask was, couldn't dtype.str[0] sometimes be ''? Cheers Stéfan
Stefan van der Walt wrote:
Ah, yes, I was confused. What I meant to ask was, couldn't dtype.str[0] sometimes be ''?
That is true. It would happen for an array of strings.
a = n.array(['1','2','3','4']) a.dtype.str[0] ''
I haven't needed to worry about that case in terms of doing byte swapping. Chris
In article <46311822.2010206@stsci.edu>, Christopher Hanley <chanley@stsci.edu> wrote:
Russell,
This should work as a consistent test for bigendian:
> isBigEndian = (obj.dtype.str[0] == '>')
Also, I have ported numarray's numdisplay to numpy if you would like to directly display an array in DS9. We haven't done an official release yet (sometime soon) but I can forward you a copy if you are interested.
I would very much like a copy. I've never heard of numdisplay before but am always interested in code that can talk to DS9. I'm porting RO.DS9. it works but has some rather ugly bits of code in it to deal with the many vagaries of xpa and ds9.  Russell
I really want to know if the array is in bigendian order (I don't care whether it's native). This is for sending arrays to the ds9 image viewer via xpa (and communicating with ds9 is not easy). To do this reliably I need to indicate the byte order of the data.
Thanks for the clarification and the original question. By the way, are you aware of the numpy.numarray module numpy.numarray.isBigEndian works as expected. Travis
Is there really no single method to call on an ndarray that asks: "what endian are you" I know not every twoliner should be made into a convenience method, but this seems like a good candidate to me. Chris  Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 5266959 voice 7600 Sand Point Way NE (206) 5266329 fax Seattle, WA 98115 (206) 5266317 main reception Chris.Barker@noaa.gov
On 4/27/07, Christopher Barker <Chris.Barker@noaa.gov> wrote:
Is there really no single method to call on an ndarray that asks: "what endian are you"
I know not every twoliner should be made into a convenience method, but this seems like a good candidate to me.
+1 I came across this source of minor confusion and excess code lines when writing the matlab io module for scipy. dtype.isbigendian? Matthew
participants (7)

Christopher Barker

Christopher Hanley

Francesc Altet

Matthew Brett

Russell E. Owen

Stefan van der Walt

Travis Oliphant