6

Is there are a way to use Numpy's multidimensional array slicing without using the [slice, slice] syntax?

I need to be able to use it from normal function calls, but I haven't found a way to use the slice object to do it.

I cannot use the syntax [(slice,slice)] for my program because [] is special syntax outside of regular function calls. The language I am using is Hy, a Lisp for Python, and it does not support this syntax. More importantly, it shouldn't support this syntax. Numpy, however, doesn't seem to support multidimensional slicing without using the [] syntax.

What's tripping me up is that the mix of C and Python in the Numpy source makes it difficult to discern how the [slice,slice] is implemented. It may not even be possible to circumvent this syntax.

EDIT:

The answer provided below by @Joe Kington allows one to slice Numpy matrices like so:

x = np.array([list(range(5)) for x in list(range(5))]) x.getitem(slice(1,4)) array([[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]) x.getitem(tuple([slice(1,4),slice(1,4)])) array([[1, 2, 3], [1, 2, 3], [1, 2, 3]])

5
  • 2
    Could you please clarify what you are trying to achieve, what you have tried so far and how exactly it failed? What constitutes an abnormal function call? Commented Mar 20, 2014 at 14:53
  • [bla, blah] is just [(blah, blah)] I bet there are many questions about this... Commented Mar 20, 2014 at 19:25
  • I have edited the question to further clarify. Please let me know if it remains unclear. Commented Mar 21, 2014 at 17:20
  • this question may be a better fit for the numpy github, but i'm surprised this hasn't been encountered before and thought it might be best if instead done in a more public forum like stackoverflow in case someone in the future needs clarification Commented Mar 21, 2014 at 17:40
  • It sounds like you want arr.__getitem__(tuple_of_slice_objects), and __setitem__ for assignment. (Incidentally, your edits helped make the question much more clear. I think it's a good question.) Commented Mar 21, 2014 at 17:44

2 Answers 2

8

From your description, it seems like you're asking what function calls are used to implement slicing and slice assignment.

Python uses the "special" methods __getitem__ and __setitem__ to implement and/or allow customization of how slicing works. Any class that implements these can be sliced. There's actually nothing numpy-specific about this.

In other words

x = arr[4:10, 9:15, ::-1]
x[0] = 100

is translated into

x = arr.__getitem__((slice(4, 6), slice(9, 10), slice(None, None, -1)))
x.__setitem__(0, 100)

For example:

class Foo(object):
    def __getitem__(self, index):
        print 'Getting', index
    def __setitem__(self, index, val):
        print 'Setting', index, 'to', val

f = Foo()
print 'Getting...'
f[:]
f[4:10, ::-1, ...]

print 'Equivalently:'
f.__getitem__(slice(None))
f.__getitem__((slice(4, 10), slice(None, None, -1), Ellipsis))

print 'Setting...'
f[0] = 1
f[5:10, 100] = 2
f[...] = 100

print 'Equivalently:'
f.__setitem__(0, 1)
f.__setitem__((slice(5,10), 100), 2)
f.__setitem__(Ellipsis, 100)

Also, it can be handy to know about numpy.index_exp (or equivalently, np.s_). It's nothing fancy -- it just translates slicing into the equivalent tuple, etc. It's quite similar to our Foo class above. For example:

In [1]: np.index_exp[10:4, ::-1, ...]
Out[1]: (slice(10, 4, None), slice(None, None, -1), Ellipsis)
Sign up to request clarification or add additional context in comments.

2 Comments

fantastic! that was unexpectedly straightforward, thanks so much! i'm going to edit my question to give a summary of this solution's use in numpy
Nice explanation about how slicing works under the hood and how slice objects are represented.
1

I suspect you are trying to pass the slice through as a parameter?

def do_slice(sl, mystring):
    return mystring[sl]

sl = slice(0,2)
mystr = "Hello"

print do_slice(sl, mystr)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.