Permalink
Browse files

ENH More work on assignment with lix

  • Loading branch information...
1 parent 65776ca commit f3f2c7e5b28bedec54a6e6838d313c91c01f6fef @kwgoodman committed May 24, 2012
Showing with 36 additions and 3 deletions.
  1. +2 −2 README.rst
  2. +10 −1 la/deflarry.py
  3. +24 −0 la/tests/lix_test.py
View
@@ -122,9 +122,9 @@ After you have installed ``la``, run the suite of unit tests::
>>> import la
>>> la.test()
<snip>
- Ran 3009 tests in 9.225s
+ Ran 3012 tests in 9.225s
OK
- <nose.result.TextTestResult run=3009 errors=0 failures=0>
+ <nose.result.TextTestResult run=3012 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
@@ -4961,7 +4961,16 @@ def __setitem__(self2, index, value):
index2.append([idx])
else:
raise IndexError, 'Unsupported indexing operation.'
- y.x[np.ix_(*index2)] = value
+ if isinstance(value, larry):
+ if value.ndim != len(index2):
+ raise IndexError, '`value` has wrong ndim'
+ for ax, ix2 in enumerate(index2):
+ lab = [y.label[ax][i] for i in ix2]
+ if lab != value.label[ax]:
+ raise IndexError, 'larry labels are not aligned'
+ y.x[np.ix_(*index2)] = value.x
+ else:
+ y.x[np.ix_(*index2)] = value
elif isscalar(index):
# Example: lar.lix[0]
y[index] = value
View
@@ -1,5 +1,7 @@
"Unit tests of larry's indexing by label method, lix"
+from nose.tools import assert_raises
+
import numpy as np
from numpy.testing import assert_equal
@@ -159,3 +161,25 @@ def lix_setitem_test_07():
desired = la.lrange(label=[['a', 'b', 'c'], ['1', '2', '3']])
desired[:2, :2] = np.arange(4).reshape(2, 2)
ale(actual, desired)
+
+def lix_setitem_test_08():
+ actual = la.lrange(label=[['a', 'b'], ['c', 'd']])
+ actual.lix[['a']] = la.larry([9, 10], [['c', 'd']])
+ desired = la.lrange(label=[['a', 'b'], ['c', 'd']])
+ desired[0] = [9, 10]
+ ale(actual, desired)
+
+def lix_setitem_test_09():
+ def lixit(lar, index, value):
+ lar.lix[index] = value
+ actual = la.lrange(label=[['a', 'b'], ['c', 'd']])
+ index = ['a']
+ value = la.larry([10, 9], [['d', 'c']])
+ assert_raises(IndexError, lixit, actual, index, value)
+
+def lix_setitem_test_10():
+ actual = la.lrange(label=[['a', 'b'], ['c', 'd']])
+ actual.lix[:,['d']] = la.larry([[9], [10]], [['a', 'b'],['d']])
+ desired = la.lrange(label=[['a', 'b'], ['c', 'd']])
+ desired[:,-1] = [9, 10]
+ ale(actual, desired)

0 comments on commit f3f2c7e

Please sign in to comment.