SciTools / biggus

:no_entry: [DEPRECATED] Virtual large arrays and lazy evaluation.
http://biggus.readthedocs.io/
GNU Lesser General Public License v3.0
54 stars 27 forks source link

Support for boolean indexing #97

Closed esc24 closed 10 years ago

esc24 commented 10 years ago

Biggus is used in Iris. Before the switch to use Biggus one could used boolean indexing on cubes (see https://github.com/SciTools/iris/pull/643). Biggus does not currently support this ability e.g.

>>> import biggus
>>> import numpy as np
>>> a = biggus.NumpyArrayAdapter(np.arange(12).reshape(3,4))
>>> a
<NumpyArrayAdapter shape=(3, 4) dtype=dtype('int64')>
>>> a[np.array([True, False, True])]
<NumpyArrayAdapter shape=(3, 4) dtype=dtype('int64')>

This should be

<NumpyArrayAdapter shape=(2, 4) dtype=dtype('int64')>

This PR adds this functionality.

esc24 commented 10 years ago

Looks like this needs a little extra work as

a = biggus.NumpyArrayAdapter(np.arange(12).reshape(3,4))[:, :]
a[np.array([True, False, True])]

gives an unexpected answer.

pelson commented 10 years ago

Just checking you know about:

>>> import numpy as np
>>> a = np.arange(12)
>>> a[np.zeros(12, dtype=np.bool)]
array([], dtype=int64)
>>> a[np.zeros(10, dtype=np.bool)]
array([], dtype=int64)
>>> a[np.ones(10, dtype=np.bool)]
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
esc24 commented 10 years ago

Thanks @pelson. I'd missed that. I've relaxed the restriction so the key is effectively zero padded if it is short. Overall, that was more complicated than I'd expected, but I've learnt something.

pelson commented 10 years ago

:+1: - I would merge. My only hesitation would be to test the actual index values, not just the shapes.

I'll give this a couple of days before merging.

rhattersley commented 10 years ago

the key is effectively zero padded if it is short

This behaviour might have changed in NumPy 1.9.

rhattersley commented 10 years ago

This PR adds [boolean indexing].

Super! :smile: