[Numpy-discussion] Slice assignment and overlapping views (was: Strange behavior in setting masked array values in Numpy 1.1.0)
Pauli Virtanen
pav@iki...
Sat May 31 17:09:20 CDT 2008
la, 2008-05-31 kello 17:56 -0400, Tony Yu kirjoitti:
[clip]
> I've been playing around with some software using numpy 1.0.4 and took
> a crack at upgrading it to numpy 1.1.0, but I ran into some strange
> behavior when assigning to slices of a masked array.
[clip]
> In [1]: import numpy
>
> In [2]: masked = numpy.ma.masked_array([[1, 2, 3, 4, 5]], mask=False)
>
> In [3]: masked[:] = numpy.fliplr(masked.copy())
>
> In [4]: print masked
> [[5 4 3 2 1]]
>
> In [5]: masked[:] = numpy.fliplr(masked)
>
> In [6]: print masked
> [[1 2 3 2 1]]
Note that
>>> numpy.fliplr(masked).base.base.base.base is masked.base
True
The reason for the strange behavior of slice assignment is that when the
left and right sides in a slice assignment are overlapping views of the
same array, the result is currently effectively undefined. Same is true
for ndarrays:
>>> import numpy
>>> a = numpy.array([1, 2, 3, 4, 5])
>>> a[::-1]
array([5, 4, 3, 2, 1])
>>> a[:] = a[::-1]
>>> a
array([5, 4, 3, 4, 5])
This is a known issue. I'm not sure how easy would it be to arrange the
assignment loops so that the overlapping data would be handled
correctly.
I think that numpy should at least raise a warning if not an error in
__setitem__, when the two arrays have the same ancestor (needs walking
up the .base links). What's the general opinion on this?
Pauli
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digitaalisesti allekirjoitettu viestin osa
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20080601/15c7f22b/attachment.bin
More information about the Numpy-discussion
mailing list