Using arr.dtype.type to check byteorder-independed dtype fails for bool
Hi, I need to check the array dtype in a way that it is ignoring differences coming only from big-endian vs. little-endian. So far I was using a test like this successfully: if arr.dtype.type == N.uint8: .... This would test positively for both byte-orders independent of the system's byteorder. I just noticed however, that if arr.dtype.type == N.bool: ... always fails (evaluates to False) Testing arr.dtype == N.bool, on the other hand works (Sitting at my Windows-PC cannot test the byte-order problem though !) While writing this email, I just found that if arr.dtype.type == N.bool_: ... works. (Note the trailing underscore !). What is this ? Thanks for help,hints and comments, Sebastian Haase
Hi Sebastian On Tue, Nov 13, 2007 at 01:11:33PM +0100, Sebastian Haase wrote:
Hi, I need to check the array dtype in a way that it is ignoring differences coming only from big-endian vs. little-endian.
Does N.issubdtype(first_dtype, second_dtype) work? Cheers Stéfan
On Nov 13, 2007 2:18 PM, Stefan van der Walt <stefan@sun.ac.za> wrote:
Hi Sebastian
On Tue, Nov 13, 2007 at 01:11:33PM +0100, Sebastian Haase wrote:
Hi, I need to check the array dtype in a way that it is ignoring differences coming only from big-endian vs. little-endian.
Does
N.issubdtype(first_dtype, second_dtype)
work?
Hi Stéfan ! It appears to work :
N.empty(5, dtype=">f").dtype==N.float32 False N.empty(5, dtype="<f").dtype==N.float32 True a = N.empty(5, dtype=">f") a.dtype f4 a.dtype.type <type 'numpy.float32'> N.issubdtype(a.dtype, N.float32) True a = N.empty(5, dtype=">?") a.dtype bool a = N.empty(5, dtype="<?") a.dtype bool a = N.empty(5, dtype=">?") N.issubdtype(a.dtype, N.bool) True N.issubdtype(a.dtype, N.bool_) True
Furthermore however,
N.empty(5, dtype=">?").dtype == N.empty(5, dtype="<?").dtype True
So, for "symmetry reasons" with my existing non-bool code, I would likely use "arr.dtype.type == N.bool_". Still wondering what the "_" means here. (reading the book did not enlighten....) Thanks, Sebastian
On Nov 13, 2007 6:57 AM, Sebastian Haase <haase@msg.ucsf.edu> wrote:
On Nov 13, 2007 2:18 PM, Stefan van der Walt <stefan@sun.ac.za> wrote:
Hi Sebastian
On Tue, Nov 13, 2007 at 01:11:33PM +0100, Sebastian Haase wrote:
Hi, I need to check the array dtype in a way that it is ignoring differences coming only from big-endian vs. little-endian.
Does
N.issubdtype(first_dtype, second_dtype)
work?
Hi Stéfan ! It appears to work :
N.empty(5, dtype=">f").dtype==N.float32 False N.empty(5, dtype="<f").dtype==N.float32 True a = N.empty(5, dtype=">f") a.dtype f4 a.dtype.type <type 'numpy.float32'> N.issubdtype(a.dtype, N.float32) True a = N.empty(5, dtype=">?") a.dtype bool a = N.empty(5, dtype="<?") a.dtype bool a = N.empty(5, dtype=">?") N.issubdtype(a.dtype, N.bool) True N.issubdtype(a.dtype, N.bool_) True
Furthermore however,
N.empty(5, dtype=">?").dtype == N.empty(5, dtype="<?").dtype True
So, for "symmetry reasons" with my existing non-bool code, I would likely use "arr.dtype.type == N.bool_". Still wondering what the "_" means here. (reading the book did not enlighten....)
Out of curiosity, does it work with arr.dtype.type == N.float? I would expect not, and I imagine that is the same reason that it doesn't work for N.bool. Which is that N.bool is __builtin__.bool or, in other words, N.boolis just Python's boolean type. I believe this is to protect those foolish enough to use "from x import *". Anyway, N.bool_ is the real, numpy boolean dtype that is analogous to unit8 and friends.
Thanks, Sebastian _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- . __ . |-\ . . tim.hochberg@ieee.org
participants (3)
-
Sebastian Haase -
Stefan van der Walt -
Timothy Hochberg