Hi, if I have a numpy array 'a' and say: a.dtype == numpy.float32
Is the result independent of a's byteorder ? (That's what I would expect ! Just checking !)
Thanks, Sebastian Haase
On Monday 24 July 2006 06:47, Sebastian Haase wrote:
Hi, if I have a numpy array 'a' and say: a.dtype == numpy.float32
Is the result independent of a's byteorder ? (That's what I would expect ! Just checking !)
Thanks, Sebastian Haase
The condition will always be False, because you're comparing wrong things here, numpy.float32 is a scalar type, not a dtype.
numpy.float32
<type 'float32scalar'>
type(numpy.dtype('>f4'))
<type 'numpy.dtype'>
And I think byteorder matters when comparing dtypes:
numpy.dtype('>f4') == numpy.dtype('<f4')
False
Cheers, Karol
On Monday 24 July 2006 03:18, Karol Langner wrote:
On Monday 24 July 2006 06:47, Sebastian Haase wrote:
Hi, if I have a numpy array 'a' and say: a.dtype == numpy.float32
Is the result independent of a's byteorder ? (That's what I would expect ! Just checking !)
Thanks, Sebastian Haase
The condition will always be False, because you're comparing wrong things here, numpy.float32 is a scalar type, not a dtype.
Hi ! Thanks for the reply. Did you actually run this ? I get: #>>> a=N.arange(10, dtype=N.float32) #>>> a.dtype == N.float32 #True #>>> N.__version__ #'0.9.9.2823'
numpy.float32
<type 'float32scalar'>
type(numpy.dtype('>f4'))
<type 'numpy.dtype'>
And I think byteorder matters when comparing dtypes:
numpy.dtype('>f4') == numpy.dtype('<f4')
False
OK - I did a test now: #>>> b= a.copy() #>>> b=b.newbyteorder('big') #>>> a.dtype == b.dtype #False #>>> a.dtype #'<f4' #>>> b.dtype #'>f4'
How can I do a comparison showing that both a and b are float32 ??
Thanks, Sebastian Haase
On Monday 24 July 2006 20:10, Sebastian Haase wrote:
Hi ! Thanks for the reply. Did you actually run this ? I get: #>>> a=N.arange(10, dtype=N.float32) #>>> a.dtype == N.float32 #True #>>> N.__version__ #'0.9.9.2823'
Hi,
Looks like I need to upgrade my working version or something...
import numpy as N N.__version__
'0.9.8'
a=N.arange(10, dtype=N.float32) a.dtype == N.float32
False
Cheers, Karol
And I think byteorder matters when comparing dtypes:
numpy.dtype('>f4') == numpy.dtype('<f4')
False
Ohhhhh -- that '<' part is indicating *byte order* ?! I thought it was odd that numpy could only tell me the type was "less than f4", which I assumed must be shorthand for "less than or equal to f4". Makes much more sense now!
--bb
On Monday 24 July 2006 16:42, Bill Baxter wrote:
And I think byteorder matters when comparing dtypes:
numpy.dtype('>f4') == numpy.dtype('<f4')
False
Ohhhhh -- that '<' part is indicating *byte order* ?! I thought it was odd that numpy could only tell me the type was "less than f4", which I assumed must be shorthand for "less than or equal to f4". Makes much more sense now!
--bb
Which is why I was trying to change the str() representation of a type to something more intuitive. If nothing else one could even leave repr(a.dtype) --> '<i4' but str(a.dtype) --> 'int32 (little endian)'
I do now understand that (as opposed to numarray and numeric) the byteorder is now part of the data-type - but I would really encourage keeping the string for such an important (and often used !) thing more readable than "<i4". Most people will thankfully never have to think about byteorder - it should be like an implementation detail that numpy can transparently handle !
What what it's worth , Sebastian Haase
Sebastian Haase wrote:
Which is why I was trying to change the str() representation of a type to something more intuitive. If nothing else one could even leave repr(a.dtype) --> '<i4' but str(a.dtype) --> 'int32 (little endian)'
+1
that's the whole point of __str__. It should be human readable!
-Chris
On Tuesday 25 July 2006 01:42, Bill Baxter wrote:
And I think byteorder matters when comparing dtypes:
numpy.dtype('>f4') == numpy.dtype('<f4')
False
Ohhhhh -- that '<' part is indicating *byte order* ?! I thought it was odd that numpy could only tell me the type was "less than f4", which I assumed must be shorthand for "less than or equal to f4". Makes much more sense now!
--bb
Yep! And there are then four possiblities.
'>' - big-endian '<' - little '|' - not-applicable '=' - native
Karol
Sebastian Haase wrote:
Hi, if I have a numpy array 'a' and say: a.dtype == numpy.float32
Is the result independent of a's byteorder ?
The byteorder is a property of the data-type (not of the array) --- this is different from numarray where byteorder is a property of the array.
a.dtype == numpy.float32 will always set the data-type to a machine-native float32 data-type.
-Travis
Sebastian Haase wrote:
Hi, if I have a numpy array 'a' and say: a.dtype == numpy.float32
Is the result independent of a's byteorder ? (That's what I would expect ! Just checking !)
I think I misread the question and saw "==" as "="
But, the answer I gave should still help: the byteorder is a property of the data-type. There is no such thing as "a's" byteorder. Thus, numpy.float32 (which is actually an array-scalar and not a true data-type) is interepreted as a machine-byte-order IEEE floating-point data-type with 32 bits. Thus, the result will depend on whether or not a.dtype is machine-order or not.
-Travis
On Monday 24 July 2006 12:36, Travis Oliphant wrote:
Sebastian Haase wrote:
Hi, if I have a numpy array 'a' and say: a.dtype == numpy.float32
Is the result independent of a's byteorder ? (That's what I would expect ! Just checking !)
I think I misread the question and saw "==" as "="
But, the answer I gave should still help: the byteorder is a property of the data-type. There is no such thing as "a's" byteorder. Thus, numpy.float32 (which is actually an array-scalar and not a true data-type) is interepreted as a machine-byte-order IEEE floating-point data-type with 32 bits. Thus, the result will depend on whether or not a.dtype is machine-order or not.
-Travis
Hi, I just realized that this question did actually not get sorted out. Now I'm just about to convert my code to compare arr.dtype.type to the (default scalar!) dtype numpy.uint8 like this: if self.img.dtype.type == N.uint8: self.hist_min, self.hist_max = 0, 1<<8 elif self.img.dtype.type == N.uint16: self.hist_min, self.hist_max = 0, 1<<16 ...
This seems to work independent of byteorder - (but looks ugly(er)) ...
Is this the best way of doing this ?
- Sebastian Haase
Hi, I just realized that this question did actually not get sorted out. Now I'm just about to convert my code to compare arr.dtype.type to the (default scalar!) dtype numpy.uint8 like this: if self.img.dtype.type == N.uint8: self.hist_min, self.hist_max = 0, 1<<8 elif self.img.dtype.type == N.uint16: self.hist_min, self.hist_max = 0, 1<<16 ...
Yes, you can do this and it should work independent of byteorder. The dtype comparison will take into account the byte-order but comparing the type objects directly won't. So, if that is your intent, then great.
-Travis