From: Sebastian H. <ha...@ms...> - 2006-08-11 00:42:54
|
Hi, Does numpy.ascontiguousarray(arr) "fix" the byteorder when arr is non-native byteorder ? If not, what functions does ? - Sebastian Haase |
From: Travis O. <oli...@ie...> - 2006-08-11 01:45:16
|
Sebastian Haase wrote: > Hi, > Does numpy.ascontiguousarray(arr) "fix" the byteorder when arr is non-native > byteorder ? > > If not, what functions does ? > It can if you pass in a data-type with the right byteorder (or use a native built-in data-type). In NumPy, it's the data-type that carries the "byte-order" information. So, there are lot's of ways to "fix" the byte-order. Of course there is still the difference between "fixing" the byte-order and simply "viewing" the memory in the correct byte-order. The former physically flips bytes around, the latter just flips them on calculation and presentation. -Travis |
From: Sebastian H. <ha...@ms...> - 2006-08-11 19:22:04
|
On Thursday 10 August 2006 21:32, Sebastian Haase wrote: > Travis Oliphant wrote: > > Sebastian Haase wrote: > >> Hi, > >> Does numpy.ascontiguousarray(arr) "fix" the byteorder when arr is > >> non-native byteorder ? > >> > >> If not, what functions does ? > > > > It can if you pass in a data-type with the right byteorder (or use a > > native built-in data-type). > > > > In NumPy, it's the data-type that carries the "byte-order" > > information. So, there are lot's of ways to "fix" the byte-order. > > So then the question is: what is the easiest way to say: > give me the equivalent type of dtype, but with byteorder '<' (or '=') !? > I would be cumbersome (and ugly ;-) ) if one would have to "manually > assemble" such a construct every time ... I just found this in myCVS/numpy/numpy/core/tests/test_numerictypes.py <code> def normalize_descr(descr): "Normalize a description adding the platform byteorder." out = [] for item in descr: dtype = item[1] if isinstance(dtype, str): if dtype[0] not in ['|','<','>']: onebyte = dtype[1:] == "1" if onebyte or dtype[0] in ['S', 'V', 'b']: dtype = "|" + dtype else: dtype = byteorder + dtype if len(item) > 2 and item[2] > 1: nitem = (item[0], dtype, item[2]) else: nitem = (item[0], dtype) out.append(nitem) elif isinstance(item[1], list): l = [] for j in normalize_descr(item[1]): l.append(j) out.append((item[0], l)) else: raise ValueError("Expected a str or list and got %s" % \ (type(item))) return out </code> Is that what I was talking about !? It's quite a big animal. Would this be needed "everytime" I want to get a "systembyte-ordered version" of a given type !? - Sebastian > > > Of course there is still the difference between "fixing" the byte-order > > and simply "viewing" the memory in the correct byte-order. The former > > physically flips bytes around, the latter just flips them on calculation > > and presentation. > > I understand. I need something that I can feed into my C routines that > are to dumb to handle non-contiguous or byte-swapped data . > > - Sebastian |
From: Sebastian H. <ha...@ms...> - 2006-08-11 04:32:31
|
Travis Oliphant wrote: > Sebastian Haase wrote: >> Hi, >> Does numpy.ascontiguousarray(arr) "fix" the byteorder when arr is >> non-native byteorder ? >> >> If not, what functions does ? >> > > It can if you pass in a data-type with the right byteorder (or use a > native built-in data-type). > > In NumPy, it's the data-type that carries the "byte-order" > information. So, there are lot's of ways to "fix" the byte-order. > So then the question is: what is the easiest way to say: give me the equivalent type of dtype, but with byteorder '<' (or '=') !? I would be cumbersome (and ugly ;-) ) if one would have to "manually assemble" such a construct every time ... > Of course there is still the difference between "fixing" the byte-order > and simply "viewing" the memory in the correct byte-order. The former > physically flips bytes around, the latter just flips them on calculation > and presentation. I understand. I need something that I can feed into my C routines that are to dumb to handle non-contiguous or byte-swapped data . - Sebastian |
From: Travis O. <oli...@ie...> - 2006-08-11 07:12:48
|
Sebastian Haase wrote: > Travis Oliphant wrote: >> Sebastian Haase wrote: >>> Hi, >>> Does numpy.ascontiguousarray(arr) "fix" the byteorder when arr is >>> non-native byteorder ? >>> >>> If not, what functions does ? >>> >> >> It can if you pass in a data-type with the right byteorder (or use a >> native built-in data-type). >> >> In NumPy, it's the data-type that carries the "byte-order" >> information. So, there are lot's of ways to "fix" the byte-order. >> > So then the question is: what is the easiest way to say: > give me the equivalent type of dtype, but with byteorder '<' (or '=') !? > I would be cumbersome (and ugly ;-) ) if one would have to "manually > assemble" such a construct every time ... Two things. Every dtype object has the method self.newbyteorder(endian) which can be used to either swap the byte order or apply a new one to every sub-field. endian can be '<', '>', '=', 'swap', 'little', 'big' If you want to swap bytes based on whether or not the data-type is machine native you can do something like the following if not a.dtype.isnative: a = a.astype(a.dtype.newbyteorder()) You can make sure the array has the correct data-type using .astype(newtype) or array(a, newtype). You can also set the data-type of the array a.dtype = newtype but this won't change anything just how they are viewed. You can always byteswap the data explicitly a.byteswap(True) will do it in-place. So, you can change both the data-type and the way it's stored using a.byteswap(True) # Changes the data but not the data-type a.dtype = a.dtype.newbyteorder() # changes the data-type but not the data -Travis |
From: Travis O. <oli...@ie...> - 2006-08-11 20:02:34
|
Sebastian Haase wrote: > I just found this in myCVS/numpy/numpy/core/tests/test_numerictypes.py > <code> > > def normalize_descr(descr): > "Normalize a description adding the platform byteorder." > > return out > </code> > > Is that what I was talking about !? It's quite a big animal. > Would this be needed "everytime" I want to get a "systembyte-ordered version" > of a given type !? > No, I'm not even sure why exactly that was written but it's just in the testing code. I think the email I sent yesterday got lost because I sent it CC: numpy-discussion with no To: address. Here's what I said (more or less) in that email: You can use the .newbyteorder(endian='s') method of the dtype object to get a new data-type with a different byteorder. The possibilities for endian are 'swap', 'big' ('>'), 'little' ('<'), or 'native' ('='). This will descend down a complicated data-type and change all the byte-orders appropriately. Then you can use .astype(newtype) to convert to the new byteorder. The .isnative attribute of the data-type will tell you if the data-type (or all of it's fields in recent SVN) are in native byte-order. -Travis |
From: Francesc A. <fa...@ca...> - 2006-08-11 21:59:43
|
A Divendres 11 Agost 2006 22:02, Travis Oliphant va escriure: > Sebastian Haase wrote: > > I just found this in myCVS/numpy/numpy/core/tests/test_numerictypes.py > > <code> > > > > def normalize_descr(descr): > > "Normalize a description adding the platform byteorder." > > > > return out > > </code> > > > > > > Is that what I was talking about !? It's quite a big animal. > > Would this be needed "everytime" I want to get a "systembyte-ordered > > version" of a given type !? > > No, I'm not even sure why exactly that was written but it's just in the > testing code. I think this is my fault. Some months ago I contributed some testing code f= or checking numerical types, and ended with this 'animal'. Sorry about that ;-) Cheers! =2D-=20 >0,0< Francesc Altet =A0 =A0 http://www.carabos.com/ V V C=E1rabos Coop. V. =A0=A0Enjoy Data "-" |