[Numpy-discussion] 6 new messages in 4 topics - abridged

Bernhard Voigt bernhard.voigt at gmail.com
Thu May 10 07:18:20 EDT 2007


Dear Pierre,

I've attached the arraydict implementation file. You can run it and take a
look at the following example on your own:

In [25]: run arraydict.py

creates an arraydict with 10 elements in the current scope. Keys are the
index numbers of items

In [26]: a
Out[26]:
arraydict([(-0.51430764775177518, 0.17962503931139237),
       (-1.4037792804089142, 0.37263515556827359),
       (1.9048324627948983, 1.4155903391279885),
       (0.077070370958404841, -1.4284963747790793),
       (0.20177037521016888, 0.25023158062312373),
       (0.88821059412119174, 0.29415143595187959),
       (0.46224769848661729, -0.80670670514715426),
       (-0.079049832245684654, -2.5738917233959899),
       (-0.562854982548048, 2.0708323443154897),
       (-2.4176013660591691, 0.36401660943002978)],
      dtype=[('x', '<f8'), ('y', '<f8')])

In [27]: a.keys()
Out[27]: set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Now select items from a, based on a cut criteria on some field (this could
also be from another arraydict object)
In [28]: foo = a[a.x>0]

In [29]: foo
Out[29]:
arraydict([(1.9048324627948983, 1.4155903391279885),
       (0.077070370958404841, -1.4284963747790793),
       (0.20177037521016888, 0.25023158062312373),
       (0.88821059412119174, 0.29415143595187959),
       (0.46224769848661729, -0.80670670514715426)],
      dtype=[('x', '<f8'), ('y', '<f8')])

In [30]: foo.keys()
Out[30]: set([2, 3, 4, 5, 6])

This works, because I modified the __getitem__ method and deal with keys of
arraydict type as given back by calling a.x>0. A new arraydict is created
with the selected items and keys.

Here's the stuff I couldn't figure out how to deal with, making selections
on slices, lists etc...
In [31]: bar = a[1:6:2]

The selection is correct, because it's passed to ndarray.__getitem__
In [32]: bar
Out[32]:
arraydict([(-1.4037792804089142, 0.37263515556827359),
       (0.077070370958404841, -1.4284963747790793),
       (0.88821059412119174, 0.29415143595187959)],
      dtype=[('x', '<f8'), ('y', '<f8')])

In [33]: bar.keys()
Out[33]: set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

However, the keys are not restricted :-( since bar is not a new arraydict.
If I could get the indexes
in the __array_finalize__ method it would be easy to set the mapping of the
bar instance.
Another solution could be to overwrite also the handling of slices, lists
etc. in the __getitem__ method.

Thanks for your help! Bernhard


On May 9, 10:29 pm, Pierre GM <pgmdevl... at gmail.com> wrote:
> On Wednesday 09 May 2007 08:54:37 Bernhard Voigt wrote:
>
> > I'm trying to subclass ndarray or recarray to build a record array that
has
> > a dictionary with a mapping of keys to array indexes and vice versa.
>
> Bernhard,
> Could you send me the rest of your code ? I'd like to test a couple of
things
> before committing a proper answer.
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discuss... at scipy
.orghttp://projects.scipy.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070510/db8795e5/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: arraydict.py
Type: text/x-python
Size: 6119 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070510/db8795e5/attachment.py>


More information about the NumPy-Discussion mailing list