Skip to content

Commit

Permalink
ENH More work on assignment with lix
Browse files Browse the repository at this point in the history
  • Loading branch information
kwgoodman committed May 24, 2012
1 parent 65776ca commit f3f2c7e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion la/deflarry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions la/tests/lix_test.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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.