Skip to content

Commit

Permalink
change chunks to property
Browse files Browse the repository at this point in the history
This avoids accidental mutation and redirects users to the rechunk method

Example
-------

In [1]: import dask.array as da

In [2]: x = da.ones(6, chunks=3)

In [3]: x.chunks = 2
ValueError: Can not set chunks directly

Please use the rechunk method instead:
  x.rechunk(2)

cc @JDWarner
  • Loading branch information
mrocklin committed Jul 11, 2015
1 parent 131d8cf commit b1cbc72
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
18 changes: 14 additions & 4 deletions dask/array/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def map_blocks(func, *arrs, **kwargs):
chunks = tuple([nb * (bs,)
for nb, bs in zip(result.numblocks, chunks)])
if chunks is not None:
result.chunks = chunks
result._chunks = chunks

return result

Expand Down Expand Up @@ -605,13 +605,13 @@ class Array(object):
block sizes along each dimension
"""

__slots__ = 'dask', 'name', 'chunks', '_dtype'
__slots__ = 'dask', 'name', '_chunks', '_dtype'

def __init__(self, dask, name, chunks, dtype=None, shape=None):
self.dask = dask
self.name = name
self.chunks = normalize_chunks(chunks, shape)
if self.chunks is None:
self._chunks = normalize_chunks(chunks, shape)
if self._chunks is None:
raise ValueError(chunks_none_error_message)
if dtype is not None:
dtype = np.dtype(dtype)
Expand All @@ -629,6 +629,16 @@ def numblocks(self):
def shape(self):
return tuple(map(sum, self.chunks))

def _get_chunks(self):
return self._chunks

def _set_chunks(self, chunks):
raise ValueError("Can not set chunks directly\n\n"

This comment has been minimized.

Copy link
@shoyer

shoyer Jul 11, 2015

Contributor

maybe TypeError?

This comment has been minimized.

Copy link
@JDWarner

JDWarner Jul 11, 2015

I'll throw NotImplementedError or AttributeError out there.

This comment has been minimized.

Copy link
@mrocklin

mrocklin via email Jul 12, 2015

Author Owner

This comment has been minimized.

Copy link
@JDWarner

JDWarner Jul 13, 2015

This is a good solution.

"Please use the rechunk method instead:\n"
" x.rechunk(%s)" % str(chunks))

chunks = property(_get_chunks, _set_chunks, "chunks property")

def __len__(self):
return sum(self.chunks[0])

Expand Down
9 changes: 9 additions & 0 deletions dask/array/tests/test_array_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,3 +1092,12 @@ def test_raise_on_no_chunks():
assert "dask.pydata.org" in str(e)

assert raises(ValueError, lambda: da.ones(6))


def test_chunks_is_immutable():
x = da.ones(6, chunks=3)
try:
x.chunks = 2
assert False
except ValueError as e:
assert 'rechunk(2)' in str(e)

0 comments on commit b1cbc72

Please sign in to comment.