Skip to content

Commit

Permalink
Fix chunks in uneven step sizes
Browse files Browse the repository at this point in the history
Fixes dask#188
  • Loading branch information
mrocklin committed May 7, 2015
1 parent 27a2dee commit b7492e3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 3 additions & 2 deletions dask/array/slicing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from itertools import count, product
from toolz import merge, first, accumulate
from operator import getitem, add
from math import ceil
from ..compatibility import long
import numpy as np

Expand Down Expand Up @@ -560,7 +561,7 @@ def new_blockdim(dim_shape, lengths, index):
[4]
>>> new_blockdim(100, [20, 10, 20, 10, 40], slice(90, 10, -2))
[15, 5, 10, 5, 4]
[16, 5, 10, 5, 4]
"""
if isinstance(index, list):
return [len(index)]
Expand All @@ -570,7 +571,7 @@ def new_blockdim(dim_shape, lengths, index):
for i, slc in pairs]
if isinstance(index, slice) and index.step and index.step < 0:
slices = slices[::-1]
return [(slc.stop - slc.start) // slc.step for slc in slices]
return [int(ceil((1. * slc.stop - slc.start) / slc.step)) for slc in slices]


def replace_ellipsis(n, index):
Expand Down
10 changes: 9 additions & 1 deletion dask/array/tests/test_slicing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from dask.compatibility import skip
import dask.array as da
from dask.array import Array
from dask.array.slicing import slice_array, _slice_1d, take
from dask.array.slicing import slice_array, _slice_1d, take, new_blockdim
from operator import getitem
import numpy as np
from toolz import merge
Expand Down Expand Up @@ -437,3 +437,11 @@ def test_multiple_list_slicing():
x = np.random.rand(6, 7, 8)
a = da.from_array(x, chunks=(3, 3, 3))
assert eq(x[:, [0, 1, 2]][[0, 1]], a[:, [0, 1, 2]][[0, 1]])


def test_uneven_chunks():
assert da.ones(20, chunks=5)[::2].chunks == ((3, 2, 3, 2),)


def test_new_blockdim():
assert new_blockdim(20, [5, 5, 5, 5], slice(0, None, 2)) == [3, 2, 3, 2]

0 comments on commit b7492e3

Please sign in to comment.