Skip to content

Adding roll #2132

Closed
Closed

Description

@jakirkham

Would be nice to have an operation like NumPy's roll for use with Dask arrays.

Activity

  1. mrocklin commented on Mar 29, 2017

    @mrocklin
    Member

    Others might also appreciate a general local stencil operation, which could specialize to roll. cc @rabernat . This would presumably actually be powered by ghost, which, while useful is possibly to low-level for many users.

    I'm curious what APIs could be for this.

  2. jakirkham commented on Mar 29, 2017

    @jakirkham
    MemberAuthor

    Could you elaborate on what kind of operations you have in mind? Initially I was thinking of roll as just being a combination of slicing and concatenating, which seems to be sufficient to recreate its behavior. What other behavior would these other stencil operations need?

  3. mrocklin commented on Mar 29, 2017

    @mrocklin
    Member

    Roll is sometimes used to do finite difference computations like x[1:] - x[:-1]. These can get more involved. If central differencing is [1, 0, -1] then perhaps roll(1) is [0, 1].

    Although I might be prematurely generalizing. You could probably implement a decent and efficient roll operation just by using the slicing syntax and da.concatenate

  4. rabernat commented on Mar 29, 2017

    @rabernat
    Contributor

    It's probably useful to see how roll is implemented in xarray, which has special logic for dask:
    https://github.com/pydata/xarray/blob/master/xarray/core/variable.py#L633

    The function that actually does the difference is a method on xarray.Variable objects:

       def _roll_one_dim(self, dim, count):
            axis = self.get_axis_num(dim)
    
            count %= self.shape[axis]
            if count != 0:
                indices = [slice(-count, None), slice(None, -count)]
            else:
                indices = [slice(None)]
    
            arrays = [self[(slice(None),) * axis + (idx,)].data
                      for idx in indices]
    
            data = ops.concatenate(arrays, axis)
    
            if isinstance(data, dask_array_type):
                # chunked data should come out with the same chunks; this makes
                # it feasible to combine shifted and unshifted data
                # TODO: remove this once dask.array automatically aligns chunks
                data = data.rechunk(self.data.chunks)
    
            return type(self)(self.dims, data, self._attrs, fastpath=True)

    This does exactly what @jakirkham suggests: slicing and concatenating.

    I like the idea of a ghost-backed roll operation in dask, because it would avoid the potentially expensive communication step that occurs when the shifted points (which may lie on different chunks) are added together (as in a finite difference).

  5. mrocklin commented on Mar 29, 2017

    @mrocklin
    Member

    Yeah, I apologize. Roll and stencil computations are probably best thought about separately.

    @jakirkham I think that slicing + concatenation is the way to go. If you have any interest here then I encourage you to submit a PR.

  6. jakirkham commented on Mar 29, 2017

    @jakirkham
    MemberAuthor

    Thanks for the feedback @rabernat. Always good to hear one's idea is not wildly off from what others have thought.

    Certainly would be interested in other filtering operations as well. If you guys open (or have opened) other issues on convolutions, median/rank filters, etc., please feel free to cc me. Using ghosting (or in other literature haloing) should work very nicely with all of these. Would expect we can get a lot of mileage out of a simple convolve function in Dask.

  7. jakirkham commented on Mar 29, 2017

    @jakirkham
    MemberAuthor

    Where do you think this roll function should live (module-wise), @mrocklin?

  8. mrocklin commented on Mar 29, 2017

    @mrocklin
    Member

    Somewhere in dask.array, probably dask.array.core

  9. jakirkham commented on Mar 29, 2017

    @jakirkham
    MemberAuthor

    Please see PR ( #2135 ) for a proposal to add the roll function.

  10. added this to the 0.14.2 milestone on May 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions