How to slice range of array along a (a-priori unknown) axis?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maarten van Reeuwijk

    How to slice range of array along a (a-priori unknown) axis?

    Hello,

    I am using the Numeric package, and I need to strip edge cells off an array
    (dimension unknown) in an a-priori unknown direction. I implented this as
    follows:

    def el_remove_bcell s(var, axis):
    """ elementary routine to strip the edge cells of an array for the given
    direction.
    """

    if axis == 0:
    return var[1:-1]
    if axis == 1:
    return var[:, 1:-1]
    else:
    return var[:, :,1:-1]

    But this only works for at most 3D arrays. It must be possible to program
    this fragment without the ifs, for arrays of arbitrary dimension. Is there
    a command in the Numerical package I can use for this? It is very important
    that this method is very fast, as my arrays normally are in the order of
    100 Mb.

    TIA, Maarten

    --
    =============== =============== =============== =============== =======
    Maarten van Reeuwijk Thermal and Fluids Sciences
    Phd student dept. of Multiscale Physics
    www.ws.tn.tudelft.nl Delft University of Technology
  • Diez B. Roggisch

    #2
    Re: How to slice range of array along a (a-priori unknown) axis?

    This should work.

    def el_remove_bcell s(var, axis):
    if axis == 0:
         return var[1:-1]
    else:
    return eval("var[%s1:-1]" % ":," * axis)

    --
    Regards,

    Diez B. Roggisch

    Comment

    • Maarten van Reeuwijk

      #3
      Re: How to slice range of array along a (a-priori unknown) axis?

      > def el_remove_bcell s(var, axis):[color=blue]
      > if axis == 0:
      > return var[1:-1]
      > else:
      > return eval("var[%s1:-1]" % ":," * axis)[/color]

      Thanks, that does the trick! I love the fact that you can create commands at
      runtime :). I also found a command called swapaxes, which is very fast, so
      just in case you're interested, here's another way:

      def el_remove_bcell s(var, axis):
      swap = swapaxes(var, 0, axis)
      swap = swap[1:-1]
      return swapaxes(swap, 0, axis)

      --
      =============== =============== =============== =============== =======
      Maarten van Reeuwijk Thermal and Fluids Sciences
      Phd student dept. of Multiscale Physics
      www.ws.tn.tudelft.nl Delft University of Technology

      Comment

      • Peter Otten

        #4
        Re: How to slice range of array along a (a-priori unknown) axis?

        Maarten van Reeuwijk wrote:
        [color=blue]
        > I am using the Numeric package, and I need to strip edge cells off an
        > array (dimension unknown) in an a-priori unknown direction. I implented
        > this as follows:
        >
        > def el_remove_bcell s(var, axis):
        > """ elementary routine to strip the edge cells of an array for the
        > given
        > direction.
        > """
        >
        > if axis == 0:
        > return var[1:-1]
        > if axis == 1:
        > return var[:, 1:-1]
        > else:
        > return var[:, :,1:-1]
        >
        > But this only works for at most 3D arrays. It must be possible to program
        > this fragment without the ifs, for arrays of arbitrary dimension. Is there
        > a command in the Numerical package I can use for this? It is very
        > important that this method is very fast, as my arrays normally are in the
        > order of 100 Mb.[/color]

        Maybe:
        [color=blue][color=green][color=darkred]
        >>> def clipdim(a, n):[/color][/color][/color]
        .... t = [slice(None)] * N.rank(a)
        .... t[n] = slice(1,-1)
        .... return a[t]
        ....[color=blue][color=green][color=darkred]
        >>> a[/color][/color][/color]
        array([[[ 0, 1, 2],
        [ 3, 4, 5],
        [ 6, 7, 8]],
        [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],
        [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])[color=blue][color=green][color=darkred]
        >>> clipdim(a, 0)[/color][/color][/color]
        array([ [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]]])[color=blue][color=green][color=darkred]
        >>> clipdim(a, 1)[/color][/color][/color]
        array([[ [ 3, 4, 5]],
        [ [12, 13, 14]],
        [ [21, 22, 23]]])[color=blue][color=green][color=darkred]
        >>> clipdim(a, 2)[/color][/color][/color]
        array([[[ 1],
        [ 4],
        [ 7]],
        [[10],
        [13],
        [16]],
        [[19],
        [22],
        [25]]])

        Peter

        Comment

        Working...