[Numpy-discussion] wierd numpy.void behavior

Olivier Delalleau shish at keba.be
Tue Aug 30 09:09:50 EDT 2011


It looks like numpy.void does not properly implement __hash__:

In [35]: arr[0]['quantize'] == arr[1]['quantize']
Out[35]: True

In [34]: hash(arr[0]['quantize']) == hash(arr[1]['quantize'])
Out[34]: False

I'm not familiar enough with this kind of data type to tell you if you are
using it as it should be used though. Maybe such data is not supposed to be
hashed (but then shouldn'it it raise an exception?).

-=- Olivier

2011/8/30 Neal Becker <ndbecker2 at gmail.com>

> I've encountered something weird about numpy.void.
>
> arr = np.empty ((len(results),), dtype=[('deltaf', float),
>                                        ('quantize', [('int', int), ('frac',
> int)])])
>
> for i,r in enumerate (results):
>    arr[i] = (r[0]['deltaf'],
>              tuple(r[0]['quantize_mf']))
>
>
> from collections import defaultdict, namedtuple
> experiments = defaultdict(list)
>
> testcase = namedtuple ('testcase', ['quantize'])
>
> for e in arr:
>    experiments[testcase(e['quantize'])].append (e)
>
> Now it seems that when e['quantize'] is used as a dictionary key, equal
> values
> are not compared as equal:
>
> In [36]: experiments
> Out[36]: defaultdict(<type 'list'>, {testcase(quantize=(0, 0)): [(1.25, (0,
> 0))], testcase(quantize=(0, 0)): [(1.25, (0, 0))], testcase(quantize=(0,
> 0)):
> [(1.25, (0, 0))]})
>
> See, there are 3 'testcases' inserted, all with keys quantize=(0,0).
>
> In [37]: e['quantize']
> Out[37]: (0, 0)
>
> In [38]: type(e['quantize'])
> Out[38]: <type 'numpy.void'>
>
> There's something weird here.  If instead I do:
>
> for e in arr:
>    experiments[testcase(tuple(e['quantize']))].append (e)
>
> that is, convert e['quantize'] to a tuple before using it as a key, I get
> the
> expected behavior:
>
> In [40]: experiments
> Out[40]: defaultdict(<type 'list'>, {testcase(quantize=(0, 0)): [(1.25, (0,
> 0)),
> (1.25, (0, 0)), (1.25, (0, 0))]})
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110830/7a953279/attachment.html>


More information about the NumPy-Discussion mailing list