[pypy-commit] pypy numpy-multidim-shards: make create_slice have a better interface
fijal
noreply at buildbot.pypy.org
Thu Nov 17 10:08:46 CET 2011
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch: numpy-multidim-shards
Changeset: r49489:9b0de2f82bac
Date: 2011-11-17 11:08 +0200
http://bitbucket.org/pypy/pypy/changeset/9b0de2f82bac/
Log: make create_slice have a better interface
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -370,7 +370,6 @@
res.append(dtype.str_format(concrete.getitem(i.offset)))
i = i.next()
return space.wrap(res.build())
-
res = StringBuilder()
res.append("array([")
concrete = self.get_concrete()
@@ -412,6 +411,7 @@
res.append(")")
return space.wrap(res.build())
+
def to_str(self, comma, builder, indent=' '):
dtype = self.find_dtype()
ndims = len(self.shape)
@@ -480,7 +480,7 @@
v += self.shape[i]
if v < 0 or v >= self.shape[i]:
raise OperationError(space.w_IndexError,
- space.wrap("index (%d) out of range (0<=index<%d" % (index[i], self.shape[i])))
+ space.wrap("index (%d) out of range (0<=index<%d" % (i, self.shape[i])))
item += v * self.shards[i]
return item
@@ -516,12 +516,21 @@
return False
return True
+ def _prepare_slice_args(self, space, w_idx):
+ if (space.isinstance_w(w_idx, space.w_int) or
+ space.isinstance_w(w_idx, space.w_slice)):
+ return [space.decode_index4(w_idx, self.shape[0])]
+ return [space.decode_index4(w_item, self.shape[i]) for i, w_item in
+ enumerate(space.fixedview(w_idx))]
+
+
def descr_getitem(self, space, w_idx):
if self._single_item_result(space, w_idx):
concrete = self.get_concrete()
item = concrete._index_of_single_item(space, w_idx)
return concrete.getitem(item).wrap(space)
- return space.wrap(self.create_slice(space, w_idx))
+ chunks = self._prepare_slice_args(space, w_idx)
+ return space.wrap(self.create_slice(space, chunks))
def descr_setitem(self, space, w_idx, w_value):
self.invalidated()
@@ -539,16 +548,16 @@
assert isinstance(w_value, BaseArray)
else:
w_value = convert_to_array(space, w_value)
- view = self.create_slice(space, w_idx)
+ chunks = self._prepare_slice_args(space, w_idx)
+ view = self.create_slice(space, chunks)
view.setslice(space, w_value)
- def create_slice(self, space, w_idx):
+ def create_slice(self, space, chunks):
new_sig = signature.Signature.find_sig([
NDimSlice.signature, self.signature
])
- if (space.isinstance_w(w_idx, space.w_int) or
- space.isinstance_w(w_idx, space.w_slice)):
- start, stop, step, lgt = space.decode_index4(w_idx, self.shape[0])
+ if len(chunks) == 1:
+ start, stop, step, lgt = chunks[0]
if step == 0:
shape = self.shape[1:]
shards = self.shards[1:]
@@ -565,9 +574,7 @@
backshards = []
start = self.start
i = -1
- for i, w_item in enumerate(space.fixedview(w_idx)):
- start_, stop, step, lgt = space.decode_index4(w_item,
- self.shape[i])
+ for i, (start_, stop, step, lgt) in enumerate(chunks):
if step != 0:
shape.append(lgt)
shards.append(self.shards[i] * step)
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -34,61 +34,55 @@
def test_create_slice_f(self):
space = self.space
a = NDimArray(10*5*3, [10, 5, 3], MockDtype(), 'F')
- s = a.create_slice(space, space.wrap(3))
+ s = a.create_slice(space, [(3, 0, 0, 1)])
assert s.start == 3
assert s.shards == [10, 50]
assert s.backshards == [40, 100]
- s = a.create_slice(space, self.newslice(1, 9, 2))
+ s = a.create_slice(space, [(1, 9, 2, 4)])
assert s.start == 1
assert s.shards == [2, 10, 50]
assert s.backshards == [6, 40, 100]
- s = a.create_slice(space, space.newtuple([
- self.newslice(1, 5, 3), self.newslice(1, 2, 1), space.wrap(1)]))
- assert s.start == 61
+ s = a.create_slice(space, [(1, 5, 3, 2), (1, 2, 1, 1), (1, 0, 0, 1)])
assert s.shape == [2, 1]
assert s.shards == [3, 10]
assert s.backshards == [3, 0]
- s = a.create_slice(space, self.newtuple(
- self.newslice(None, None, None), space.wrap(2)))
+ s = a.create_slice(space, [(0, 10, 1, 10), (2, 0, 0, 1)])
assert s.start == 20
assert s.shape == [10, 3]
def test_create_slice_c(self):
space = self.space
a = NDimArray(10*5*3, [10, 5, 3], MockDtype(), 'C')
- s = a.create_slice(space, space.wrap(3))
+ s = a.create_slice(space, [(3, 0, 0, 1)])
assert s.start == 45
assert s.shards == [3, 1]
assert s.backshards == [12, 2]
- s = a.create_slice(space, self.newslice(1, 9, 2))
+ s = a.create_slice(space, [(1, 9, 2, 4)])
assert s.start == 15
assert s.shards == [30, 3, 1]
assert s.backshards == [90, 12, 2]
- s = a.create_slice(space, space.newtuple([
- self.newslice(1, 5, 3), self.newslice(1, 2, 1), space.wrap(1)]))
+ s = a.create_slice(space, [(1, 5, 3, 2), (1, 2, 1, 1), (1, 0, 0, 1)])
assert s.start == 19
assert s.shape == [2, 1]
assert s.shards == [45, 3]
assert s.backshards == [45, 0]
- s = a.create_slice(space, self.newtuple(
- self.newslice(None, None, None), space.wrap(2)))
+ s = a.create_slice(space, [(0, 10, 1, 10), (2, 0, 0, 1)])
assert s.start == 6
assert s.shape == [10, 3]
def test_slice_of_slice_f(self):
space = self.space
a = NDimArray(10*5*3, [10, 5, 3], MockDtype(), 'F')
- s = a.create_slice(space, space.wrap(5))
+ s = a.create_slice(space, [(5, 0, 0, 1)])
assert s.start == 5
- s2 = s.create_slice(space, space.wrap(3))
+ s2 = s.create_slice(space, [(3, 0, 0, 1)])
assert s2.shape == [3]
assert s2.shards == [50]
assert s2.parent is a
assert s2.backshards == [100]
assert s2.start == 35
- s = a.create_slice(space, self.newslice(1, 5, 3))
- s2 = s.create_slice(space, space.newtuple([
- self.newslice(None, None, None), space.wrap(2)]))
+ s = a.create_slice(space, [(1, 5, 3, 2)])
+ s2 = s.create_slice(space, [(0, 2, 1, 2), (2, 0, 0, 1)])
assert s2.shape == [2, 3]
assert s2.shards == [3, 50]
assert s2.backshards == [3, 100]
@@ -97,17 +91,16 @@
def test_slice_of_slice_c(self):
space = self.space
a = NDimArray(10*5*3, [10, 5, 3], MockDtype(), order='C')
- s = a.create_slice(space, space.wrap(5))
+ s = a.create_slice(space, [(5, 0, 0, 1)])
assert s.start == 15*5
- s2 = s.create_slice(space, space.wrap(3))
+ s2 = s.create_slice(space, [(3, 0, 0, 1)])
assert s2.shape == [3]
assert s2.shards == [1]
assert s2.parent is a
assert s2.backshards == [2]
assert s2.start == 5*15 + 3*3
- s = a.create_slice(space, self.newslice(1, 5, 3))
- s2 = s.create_slice(space, space.newtuple([
- self.newslice(None, None, None), space.wrap(2)]))
+ s = a.create_slice(space, [(1, 5, 3, 2)])
+ s2 = s.create_slice(space, [(0, 2, 1, 2), (2, 0, 0, 1)])
assert s2.shape == [2, 3]
assert s2.shards == [45, 1]
assert s2.backshards == [45, 2]
@@ -116,7 +109,7 @@
def test_negative_step_f(self):
space = self.space
a = NDimArray(10*5*3, [10, 5, 3], MockDtype(), 'F')
- s = a.create_slice(space, self.newslice(None, None, -2))
+ s = a.create_slice(space, [(9, -1, -2, 5)])
assert s.start == 9
assert s.shards == [-2, 10, 50]
assert s.backshards == [-8, 40, 100]
@@ -124,7 +117,7 @@
def test_negative_step_c(self):
space = self.space
a = NDimArray(10*5*3, [10, 5, 3], MockDtype(), order='C')
- s = a.create_slice(space, self.newslice(None, None, -2))
+ s = a.create_slice(space, [(9, -1, -2, 5)])
assert s.start == 135
assert s.shards == [-30, 3, 1]
assert s.backshards == [-120, 12, 2]
@@ -133,8 +126,7 @@
a = NDimArray(10*5*3, [10, 5, 3], MockDtype(), 'F')
r = a._index_of_single_item(self.space, self.newtuple(1, 2, 2))
assert r == 1 + 2 * 10 + 2 * 50
- s = a.create_slice(self.space, self.newtuple(
- self.newslice(None, None, None), 2))
+ s = a.create_slice(self.space, [(0, 10, 1, 10), (2, 0, 0, 1)])
r = s._index_of_single_item(self.space, self.newtuple(1, 0))
assert r == a._index_of_single_item(self.space, self.newtuple(1, 2, 0))
r = s._index_of_single_item(self.space, self.newtuple(1, 1))
@@ -144,8 +136,7 @@
a = NDimArray(10*5*3, [10, 5, 3], MockDtype(), 'C')
r = a._index_of_single_item(self.space, self.newtuple(1, 2, 2))
assert r == 1 * 3 * 5 + 2 * 3 + 2
- s = a.create_slice(self.space, self.newtuple(
- self.newslice(None, None, None), 2))
+ s = a.create_slice(self.space, [(0, 10, 1, 10), (2, 0, 0, 1)])
r = s._index_of_single_item(self.space, self.newtuple(1, 0))
assert r == a._index_of_single_item(self.space, self.newtuple(1, 2, 0))
r = s._index_of_single_item(self.space, self.newtuple(1, 1))
More information about the pypy-commit
mailing list