Hi, I am finding it less than useful to have the negative index wrapping on nd-arrays. Here is a short example: import numpy as np a = np.zeros((3, 3)) a[:,2] = 1000 print a[0,-1] print a[0,-1] print a[-1,-1] In all cases 1000 is printed out. What I am after is a way to say "please don't wrap around" and have negative indices behave in a way I choose. I know this is a standard thing - but is there a way to override that behaviour that doesn't involve cython or rolling my own resampler? Kind Regards, Nathan.
On 15/01/12 00:53, Nathan Faggian wrote:
Hi,
I am finding it less than useful to have the negative index wrapping on nd-arrays. Here is a short example:
import numpy as np a = np.zeros((3, 3)) a[:,2] = 1000 print a[0,-1] print a[0,-1] print a[-1,-1]
In all cases 1000 is printed out.
What else would you expect?
What I am after is a way to say "please don't wrap around" and have negative indices behave in a way I choose. I know this is a standard thing - but is there a way to override that behaviour that doesn't involve cython or rolling my own resampler?
What other behavior would you choose? I don't see any other that would make sense and that would be consistent with positive indexing. Cheers, -- Daniele
On Sat, Jan 14, 2012 at 11:53 PM, Nathan Faggian <nathan.faggian@gmail.com> wrote:
Hi,
I am finding it less than useful to have the negative index wrapping on nd-arrays. Here is a short example:
import numpy as np a = np.zeros((3, 3)) a[:,2] = 1000 print a[0,-1] print a[0,-1] print a[-1,-1]
In all cases 1000 is printed out.
What I am after is a way to say "please don't wrap around" and have negative indices behave in a way I choose. I know this is a standard thing - but is there a way to override that behaviour that doesn't involve cython or rolling my own resampler?
Although it could be possible with lots of work, it would most likely be a bad idea. You will need to wrap something around your model/data/etc... Could you explain a bit more what you have in mind ? David
On Sun, Jan 15, 2012 at 6:54 AM, David Cournapeau <cournape@gmail.com>wrote: > On Sat, Jan 14, 2012 at 11:53 PM, Nathan Faggian > <nathan.faggian@gmail.com> wrote: > > Hi, > > > > I am finding it less than useful to have the negative index wrapping on > nd-arrays. Here is a short example: > > > > import numpy as np > > a = np.zeros((3, 3)) > > a[:,2] = 1000 > > print a[0,-1] > > print a[0,-1] > > print a[-1,-1] > > > > In all cases 1000 is printed out. > > > > What I am after is a way to say "please don't wrap around" and have > negative indices behave in a way I choose. I know this is a standard thing > - but is there a way to override that behaviour that doesn't involve cython > or rolling my own resampler? > > Although it could be possible with lots of work, it would most likely > be a bad idea. You will need to wrap something around your > model/data/etc... Could you explain a bit more what you have in mind ? > > David > Another approach that might be useful, depending on the needs, is to use `np.ravel_multi_index()`, in which ndim coords can be passed in and flatten coords are returned. It has options of 'raise', 'wrap' and 'clip' for handling out-of-bounds indices. It wouldn't be built directly into the arrays, but if that isn't needed, this might work. Ben Root
On Sat, Jan 14, 2012 at 4:53 PM, Nathan Faggian <nathan.faggian@gmail.com>wrote:
Hi,
I am finding it less than useful to have the negative index wrapping on nd-arrays. Here is a short example:
import numpy as np a = np.zeros((3, 3)) a[:,2] = 1000 print a[0,-1] print a[0,-1] print a[-1,-1]
In all cases 1000 is printed out.
Looks right to me, the whole last column is 1000. What exactly do you want to do and what is the problem? <snip> Chuck
On Mon, Jan 16, 2012 at 3:24 PM, Charles R Harris <charlesr.harris@gmail.com
wrote:
On Sat, Jan 14, 2012 at 4:53 PM, Nathan Faggian <nathan.faggian@gmail.com>wrote:
Hi,
I am finding it less than useful to have the negative index wrapping on nd-arrays. Here is a short example:
import numpy as np a = np.zeros((3, 3)) a[:,2] = 1000 print a[0,-1] print a[0,-1] print a[-1,-1]
In all cases 1000 is printed out.
Looks right to me, the whole last column is 1000. What exactly do you want to do and what is the problem?
<snip>
Chuck
I would imagine that it is some sort of image processing use-case, where sometimes you want the data to reflect at the boundaries, or be constant, or have some other value used for access outside the domain. So, for reflect, I would guess that he would have wanted 0.0 for the first two and 1000 for the last one. Ben Root
On Mon, Jan 16, 2012 at 3:30 PM, Benjamin Root <ben.root@ou.edu> wrote:
On Mon, Jan 16, 2012 at 3:24 PM, Charles R Harris < charlesr.harris@gmail.com> wrote:
On Sat, Jan 14, 2012 at 4:53 PM, Nathan Faggian <nathan.faggian@gmail.com
wrote:
Hi,
I am finding it less than useful to have the negative index wrapping on nd-arrays. Here is a short example:
import numpy as np a = np.zeros((3, 3)) a[:,2] = 1000 print a[0,-1] print a[0,-1] print a[-1,-1]
In all cases 1000 is printed out.
Looks right to me, the whole last column is 1000. What exactly do you want to do and what is the problem?
<snip>
Chuck
I would imagine that it is some sort of image processing use-case, where sometimes you want the data to reflect at the boundaries, or be constant, or have some other value used for access outside the domain. So, for reflect, I would guess that he would have wanted 0.0 for the first two and 1000 for the last one.
Ben Root
Errr, I mean 0.0 for the last one. I can't think today. Ben Root
Hi, I am sorry for the late reply. Benjamin has hit the nail on the head. I guess I am seeing numpy "fancy indexing" as equivalent to integer based coordinate sampling and trying to compare numpy's fancy indexing to something like map_coordinates in scipy. I have never used np.ravel_multi_index() and will have a look at this now. -N On 17 January 2012 08:42, Benjamin Root <ben.root@ou.edu> wrote:
On Mon, Jan 16, 2012 at 3:30 PM, Benjamin Root <ben.root@ou.edu> wrote:
On Mon, Jan 16, 2012 at 3:24 PM, Charles R Harris <charlesr.harris@gmail.com> wrote:
On Sat, Jan 14, 2012 at 4:53 PM, Nathan Faggian <nathan.faggian@gmail.com> wrote:
Hi,
I am finding it less than useful to have the negative index wrapping on nd-arrays. Here is a short example:
import numpy as np a = np.zeros((3, 3)) a[:,2] = 1000 print a[0,-1] print a[0,-1] print a[-1,-1]
In all cases 1000 is printed out.
Looks right to me, the whole last column is 1000. What exactly do you want to do and what is the problem?
<snip>
Chuck
I would imagine that it is some sort of image processing use-case, where sometimes you want the data to reflect at the boundaries, or be constant, or have some other value used for access outside the domain. So, for reflect, I would guess that he would have wanted 0.0 for the first two and 1000 for the last one.
Ben Root
Errr, I mean 0.0 for the last one. I can't think today.
Ben Root
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (5)
-
Benjamin Root
-
Charles R Harris
-
Daniele Nicolodi
-
David Cournapeau
-
Nathan Faggian