Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ENH indexing with 1d bool larry; closes #61

  • Loading branch information...
commit bbf7c9654cfe6959f49cb2f80c8db469ffcfe38c 1 parent 49ff728
@kwgoodman authored
View
4 README.rst
@@ -122,9 +122,9 @@ After you have installed ``la``, run the suite of unit tests::
>>> import la
>>> la.test()
<snip>
- Ran 3012 tests in 9.225s
+ Ran 3014 tests in 9.225s
OK
- <nose.result.TextTestResult run=3012 errors=0 failures=0>
+ <nose.result.TextTestResult run=3014 errors=0 failures=0>
The ``la`` package contains C extensions that speed up common alignment
operations such as adding two unaligned larrys. If the C extensions don't
View
1  RELEASE.rst
@@ -25,6 +25,7 @@ la 0.7
**Enhancements**
+- Can now index into a larry with a 1d bool larry
- Added setitem ability to label indexing (lix)
**Breakage from la 0.6**
View
30 la/deflarry.py
@@ -1678,11 +1678,26 @@ def __getitem__(self, index):
lab = [self.label[ax][z] for z in idx]
except IndexError:
raise IndexError, 'index out of range'
- validate = True
+ validate = True
allscalar = False
elif typ is slice:
lab = self.label[ax][idx]
allscalar = False
+ elif typ is larry:
+ if idx.dtype.type != np.bool_:
+ raise ValueError("If index is larry it must of bool dtype")
+ if idx.ndim != 1:
+ raise ValueError("If index is larry it must be 1d")
+ try:
+ lab = [self.label[0][j] for j, z in
+ enumerate(idx.x) if z]
+ except IndexError:
+ raise IndexError, 'index out of range'
+ index = list(index)
+ index[ax] = index[ax].x
+ index = tuple(index)
+ allscalar = False
+ validate = True
else:
msg = 'I do not recognize the way you are indexing'
raise IndexError, msg
@@ -1729,6 +1744,19 @@ def __getitem__(self, index):
x = self.x.take(index, axis=0)
label = self.copylabel()
label[0] = lab
+ elif typidx is larry:
+ if index.dtype.type != np.bool_:
+ raise ValueError("If index is larry it must of bool dtype")
+ if index.ndim != 1:
+ raise ValueError("If index is larry it must be 1d")
+ try:
+ lab = [self.label[0][j] for j, z in
+ enumerate(index) if z]
+ except IndexError:
+ raise IndexError, 'index out of range'
+ label = self.copylabel()
+ label[0] = lab
+ x = self.x[index.x]
else:
msg = 'Only slice, integer, and seq (list, tuple, 1d array)'
msg = msg + ' indexing supported'
View
31 la/tests/deflarry_getitem_test.py
@@ -176,14 +176,14 @@ def test_getitem_20():
[ 5.0, 6.0]], [[1, 2], [0, 1]])
lar1, lar2 = make_larrys()
actual = lar1[np.array([1, 2]),:]
- ale(actual, desired, original=lar1)
+ ale(actual, desired, original=lar1)
def test_getitem_21():
"larry.getitem #21"
desired = larry([ 3.0, 5.0], [[1, 2]])
lar1, lar2 = make_larrys()
actual = lar1[np.array([1, 2]), 0]
- ale(actual, desired, original=lar1)
+ ale(actual, desired, original=lar1)
def test_getitem_22():
"larry.getitem #22"
@@ -219,4 +219,29 @@ def test_getitem_25():
desired = larry(np.ones((3,0)))
lar1, lar2 = make_larrys()
actual = lar1[:,1:1]
- ale(actual, desired, original=lar1)
+ ale(actual, desired, original=lar1)
+
+# ------------------------------------------------------------------------
+# indexing with bool larrys
+
+def test_getitem_26():
+ "larry.getitem #26"
+ lar1, lar2 = make_larrys()
+ idx1 = larry([True, False, True])
+ actual = lar1[idx1]
+ arr = lar1.x[idx1.x]
+ assert_equal(arr.shape, actual.shape)
+ idx2 = [0, 2]
+ desired = lar1[idx2]
+ ale(actual, desired, original=lar1)
+
+def test_getitem_27():
+ "larry.getitem #27"
+ lar1, lar2 = make_larrys()
+ idx1 = larry([True, False])
+ actual = lar1[:,idx1]
+ arr = lar1.x[:,idx1.x]
+ assert_equal(arr.shape, actual.shape)
+ idx2 = [0]
+ desired = lar1[:,idx2]
+ ale(actual, desired, original=lar1)
Please sign in to comment.
Something went wrong with that request. Please try again.