[Numpy-discussion] wierd numpy.void behavior

Neal Becker ndbecker2@gmail....
Tue Aug 30 07:30:54 CDT 2011


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))]})




More information about the NumPy-Discussion mailing list