trivial question: how to compare dtype - but ignoring byteorder ?
![](https://secure.gravatar.com/avatar/6194b135cba546afa82516de1537de49.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/ad6828350535c3f86e21a97875a91941.jpg?s=120&d=mm&r=g)
On Monday 24 July 2006 06:47, Sebastian Haase wrote:
The condition will always be False, because you're comparing wrong things here, numpy.float32 is a scalar type, not a dtype.
And I think byteorder matters when comparing dtypes:
numpy.dtype('>f4') == numpy.dtype('<f4') False
Cheers, Karol -- written by Karol Langner pon lip 24 12:05:54 CEST 2006
![](https://secure.gravatar.com/avatar/6194b135cba546afa82516de1537de49.jpg?s=120&d=mm&r=g)
On Monday 24 July 2006 03:18, Karol Langner 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'
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
![](https://secure.gravatar.com/avatar/915f9e3213021d75d43bb4262167b067.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/6194b135cba546afa82516de1537de49.jpg?s=120&d=mm&r=g)
On Monday 24 July 2006 16:42, Bill Baxter wrote:
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
![](https://secure.gravatar.com/avatar/5dde29b54a3f1b76b2541d0a4a9b232c.jpg?s=120&d=mm&r=g)
Sebastian Haase wrote:
+1 that's the whole point of __str__. It should be human readable! -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
![](https://secure.gravatar.com/avatar/ad6828350535c3f86e21a97875a91941.jpg?s=120&d=mm&r=g)
On Tuesday 25 July 2006 01:42, Bill Baxter wrote:
Yep! And there are then four possiblities. '>' - big-endian '<' - little '|' - not-applicable '=' - native Karol -- written by Karol Langner wto lip 25 08:54:51 CEST 2006
![](https://secure.gravatar.com/avatar/49df8cd4b1b6056c727778925f86147a.jpg?s=120&d=mm&r=g)
Sebastian Haase wrote:
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
![](https://secure.gravatar.com/avatar/6194b135cba546afa82516de1537de49.jpg?s=120&d=mm&r=g)
On Monday 24 July 2006 12:36, Travis Oliphant wrote: 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
![](https://secure.gravatar.com/avatar/ad6828350535c3f86e21a97875a91941.jpg?s=120&d=mm&r=g)
On Monday 24 July 2006 06:47, Sebastian Haase wrote:
The condition will always be False, because you're comparing wrong things here, numpy.float32 is a scalar type, not a dtype.
And I think byteorder matters when comparing dtypes:
numpy.dtype('>f4') == numpy.dtype('<f4') False
Cheers, Karol -- written by Karol Langner pon lip 24 12:05:54 CEST 2006
![](https://secure.gravatar.com/avatar/6194b135cba546afa82516de1537de49.jpg?s=120&d=mm&r=g)
On Monday 24 July 2006 03:18, Karol Langner 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'
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
![](https://secure.gravatar.com/avatar/915f9e3213021d75d43bb4262167b067.jpg?s=120&d=mm&r=g)
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
![](https://secure.gravatar.com/avatar/6194b135cba546afa82516de1537de49.jpg?s=120&d=mm&r=g)
On Monday 24 July 2006 16:42, Bill Baxter wrote:
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
![](https://secure.gravatar.com/avatar/5dde29b54a3f1b76b2541d0a4a9b232c.jpg?s=120&d=mm&r=g)
Sebastian Haase wrote:
+1 that's the whole point of __str__. It should be human readable! -Chris -- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
![](https://secure.gravatar.com/avatar/ad6828350535c3f86e21a97875a91941.jpg?s=120&d=mm&r=g)
On Tuesday 25 July 2006 01:42, Bill Baxter wrote:
Yep! And there are then four possiblities. '>' - big-endian '<' - little '|' - not-applicable '=' - native Karol -- written by Karol Langner wto lip 25 08:54:51 CEST 2006
![](https://secure.gravatar.com/avatar/49df8cd4b1b6056c727778925f86147a.jpg?s=120&d=mm&r=g)
Sebastian Haase wrote:
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
![](https://secure.gravatar.com/avatar/6194b135cba546afa82516de1537de49.jpg?s=120&d=mm&r=g)
On Monday 24 July 2006 12:36, Travis Oliphant wrote: 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
participants (5)
-
Bill Baxter
-
Christopher Barker
-
Karol Langner
-
Sebastian Haase
-
Travis Oliphant