分配给numpy数组的包装切片 [英] assigning to a wrapped slice of a numpy array
问题描述
我有一张大图 A
和一张较小的图片 B
,两者都表示为2-D numpy
数组。我想使用 A
作为画布,并在其上写下 B
的翻译副本,以六边形排列。我无法理解的部分是如何处理它,使图像垂直和水平包裹 - 基本上我想要的是将(填充的,必要的)子图像定期镶嵌到圆环上。
I have a large image A
and a smaller image B
, both expressed as 2-D numpy
arrays. I want to use A
as the canvas, and write translated copies of B
all over it, packed in a hexagonal arrangement. The part I can't get my head around is how to handle it such that the image wraps both vertically and horizontally—essentially what I want is regular tessellation of a (padded, as necessary) sub-image onto a torus.
我见过讨论 numpy.take
和 numpy.roll
at
包装Python / numpy中的切片,这告诉我如何访问并返回 复制 包含的数组切片,但是我想分配给它 - 即,对于任意整数 rowOffset
和 columnOffset
我想做相当于:
I've seen the discussion of numpy.take
and numpy.roll
at
wrapping around slices in Python / numpy and that shows me how to access and return a copy of a wrapped slice of an array, but I want to assign to that—i.e., for arbitrary integers rowOffset
and columnOffset
I want to do the equivalent of:
A = numpy.zeros((5,11), int)
B = numpy.array([[1,2,3,4,5,6,7]]) * numpy.array([[10,100,1000]]).T
# OK, we wouldn't be able to fit more than one or two copies of B into A, but they demonstrate the wrapped placement problem
wrappedRowIndices = ( numpy.arange(B.shape[0]) + rowOffset ) % A.shape[0]
wrappedColumnIndices = ( numpy.arange(B.shape[1]) + columnOffset ) % A.shape[1]
A[ wrappedRowIndices, : ][ :, wrappedColumnIndices ] = B
我从评论中看到关于这个问题,
以及从时刻的反思 numpy
数组的代表,根据需要,包装切片无法以视图
的形式返回。
I see from a comment on the question,
and from a moment's reflection on the way numpy
arrays are represented, that there's no way a wrapped slice can be returned as a view
in the way this demands.
是否存在(Y)以这种方式分配给数组的包装切片的方法,或者(X)用于执行我想要实现的曲面细分的现有实用程序?
Is there (Y) a way of assigning to wrapped slices of an array in this way, or (X) an existing utility for performing the kind of tessellation I'm trying to achieve?
推荐答案
您当前的代码分解为 __ getitem __
和 __ setitem __
。正如您所指出的, __ getitem __
不会返回视图,因此 __ setitem __
最终会修改副本。
Your current code breaks down into a __getitem__
and a __setitem__
. The __getitem__
does not return a view, as you noted, so the __setitem__
just ends up modifying the copy.
你需要在一个 __ setitem __
(即一组括号)中完成整个事情:
You need to do the whole thing in one __setitem__
(i.e. one set of brackets):
A[wrappedRowIndices[:,np.newaxis], wrappedColumnIndices] = B
由于广播,这相当于:
A[wrappedRowIndices[:,np.newaxis], wrappedColumnIndices[np.newaxis,:]] = B
使用更多索引编制时比一个数组,规则是:
When indexing with more than one array, the rules is that:
# ... here is NOT the python Ellipsis!
y = x[a, b, c, ...]
y[i, j, ..] = x[a[i,j,...], b[i,j,...], ...]
实际上内置的, np.ix_( )
:
A[np.ix_(wrappedRowIndices, wrappedColumnIndices)] = B
推广到ND,你得到:
def place_wrapped(canvas, brush, position):
assert canvas.ndim == brush.ndim == len(position)
ind = np.ix_(*(
(np.arange(b_dim) + shift) % c_dim
for b_dim, c_dim, shift in zip(brush.shape, canvas.shape, position)
))
canvas[ind] = brush
这篇关于分配给numpy数组的包装切片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!