[Numpy-discussion] use slicing as argument values?

Benjamin Root ben.root@ou....
Thu Jul 12 16:52:18 CDT 2012


On Thursday, July 12, 2012, Chao YUE wrote:

> Thanks all for the discussion. Actually I am trying to use something like
> numpy ndarray indexing in the function. Like when I call:
>
> func(a,'1:3,:,2:4'), it knows I want to retrieve a[1:3,:,2:4], and
> func(a,'1:3,:,4') for a[1:3,:,4] ect.
> I am very close now.
>
> #so this function changes the string to list of slice objects.
> def convert_string_to_slice(slice_string):
>     """
>     provide slice_string as '2:3,:', it will return [slice(2, 3, None),
> slice(None, None, None)]
>     """
>     slice_list=[]
>     split_slice_string_list=slice_string.split(',')
>     for sub_slice_string in split_slice_string_list:
>         split_sub=sub_slice_string.split(':')
>         if len(split_sub)==1:
>             sub_slice=slice(int(split_sub[0]))
>         else:
>             if split_sub[0]=='':
>                 sub1=None
>             else:
>                 sub1=int(split_sub[0])
>             if split_sub[1]=='':
>                 sub2=None
>             else:
>                 sub2=int(split_sub[1])
>             sub_slice=slice(sub1,sub2)
>         slice_list.append(sub_slice)
>     return slice_list
>
> In [119]: a=np.arange(3*4*5).reshape(3,4,5)
>
> for this it works fine.
> In [120]: convert_string_to_slice('1:3,:,2:4')
> Out[120]: [slice(1, 3, None), slice(None, None, None), slice(2, 4, None)]
>
> In [121]: a[slice(1, 3, None), slice(None, None, None), slice(2, 4,
> None)]==a[1:3,:,2:4]
> Out[121]:
> array([[[ True,  True],
>         [ True,  True],
>         [ True,  True],
>         [ True,  True]],
>
>        [[ True,  True],
>         [ True,  True],
>         [ True,  True],
>         [ True,  True]]], dtype=bool)
>
> And problems happens when I want to retrieve a single number along a given
> dimension:
> because it treats 1:3,:,4 as 1:3,:,:4, as shown below:
>
> In [122]: convert_string_to_slice('1:3,:,4')
> Out[122]: [slice(1, 3, None), slice(None, None, None), slice(None, 4,
> None)]
>
> In [123]: a[1:3,:,4]
> Out[123]:
> array([[24, 29, 34, 39],
>        [44, 49, 54, 59]])
>
> In [124]: a[slice(1, 3, None), slice(None, None, None), slice(None, 4,
> None)]
> Out[124]:
> array([[[20, 21, 22, 23],
>         [25, 26, 27, 28],
>         [30, 31, 32, 33],
>         [35, 36, 37, 38]],
>
>        [[40, 41, 42, 43],
>         [45, 46, 47, 48],
>         [50, 51, 52, 53],
>         [55, 56, 57, 58]]])
>
>
> Then I have a function:
>
> #this function retrieves data from ndarray a by specifying slice_string:
> def retrieve_data(a,slice_string):
>     slice_list=convert_string_to_slice(slice_string)
>     return a[*slice_list]
>
> In the list line of the fuction "retrieve_data" I have problem, I get an
> invalid syntax error.
>
>     return a[*slice_list]
>              ^
> SyntaxError: invalid syntax
>
> I hope it's not too long, please comment as you like. Thanks a lot!!!!
>
> Chao


I won't comment on the wisdom of your approach, but for you very last part,
don't try unpacking the slice list.  Also, I think it has to be a tuple,
but I could be wrong on that.

Ben Root

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20120712/5a116adc/attachment-0001.html 


More information about the NumPy-Discussion mailing list