Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coding standards tweaks to daskified cube code #2416

Merged
merged 1 commit into from
Mar 8, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,6 @@ def __init__(self, data, standard_name=None, long_name=None,
if isinstance(data, six.string_types):
raise TypeError('Invalid data type: {!r}.'.format(data))

self.fill_value = fill_value

if is_lazy_data(data):
self._dask_array = data
self._numpy_array = None
Expand All @@ -727,8 +725,6 @@ def __init__(self, data, standard_name=None, long_name=None,
data = np.asarray(data)
self._numpy_array = data

self._dtype = dtype

#: The "standard name" for the Cube's phenomenon.
self.standard_name = standard_name

Expand All @@ -755,6 +751,10 @@ def __init__(self, data, standard_name=None, long_name=None,
# Cell Measures
self._cell_measures_and_dims = []

self.fill_value = fill_value

self._dtype = dtype

identities = set()
if dim_coords_and_dims:
dims = set()
Expand Down Expand Up @@ -1611,10 +1611,10 @@ def core_data(self):
to be decided: should this be public??

"""
if self._numpy_array is not None:
result = self._numpy_array
else:
if self.has_lazy_data():
result = self._dask_array
else:
result = self._numpy_array
return result

@property
Expand Down Expand Up @@ -1654,14 +1654,14 @@ def lazy_data(self):
A lazy array, representing the Cube data array.

"""
if self._numpy_array is not None:
if self.has_lazy_data():
result = self._dask_array
else:
data = self._numpy_array
if isinstance(data, ma.masked_array):
data = array_masked_to_nans(data)
data = data.data
result = da.from_array(data, chunks=data.shape)
else:
result = self._dask_array
return result

@property
Expand Down Expand Up @@ -1697,7 +1697,7 @@ def data(self):
(10, 20)

"""
if self._numpy_array is None:
if self.has_lazy_data():
try:
data = self._dask_array.compute()
mask = np.isnan(data)
Expand Down Expand Up @@ -1742,7 +1742,7 @@ def data(self, value):
self._numpy_array = value

def has_lazy_data(self):
return True if self._numpy_array is None else False
return self._numpy_array is None

@property
def dim_coords(self):
Expand Down Expand Up @@ -2205,12 +2205,11 @@ def new_cell_measure_dims(cm_):
first_slice = next(slice_gen)
except StopIteration:
first_slice = None
if self._numpy_array is not None:
cube_data = self._numpy_array
elif self._dask_array is not None:

if self.has_lazy_data():
cube_data = self._dask_array
else:
raise ValueError('This cube has no data, slicing is not supported')
cube_data = self._numpy_array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dkillick At this point, we're confident that either self._dask_array or self._numpy_array is populated.

Prior to this change, there seems to be a hint of that might not be the case, hence the ValueError exception being raised. I'd certainly hope that there is a data payload of some kind to process - we need to make sure that internally that is always the case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we end up at this point having a cube with no data we're deeper in the sticky than a ValueError will help with! The rest of the code in the class makes it clear that we can never construct a cube with no data, so if an end user has done something that means their cube no longer has any data then IMO they can dig their own way out of the sticky stuff...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really hoping that there never, ever is any sticky stuff 😉

It's just a note to ourselves really, that we need to ensure the integrity of the cube as best we can


if first_slice is not None:
data = cube_data[first_slice]
Expand Down Expand Up @@ -2839,12 +2838,14 @@ def transpose(self, new_order=None):
Example usage::

# put the second dimension first, followed by the third dimension,
and finally put the first dimension third cube.transpose([1, 2, 0])
and finally put the first dimension third::

>>> cube.transpose([1, 2, 0])

"""
if new_order is None:
# Passing numpy arrays as new_order works in numpy but not in dask,
# docs specify a list, so ensure a list is used.
# Passing NumPy arrays as new_order works in NumPy but not in dask.
# Dask docs specify a list, so ensure a list is used.
new_order = list(np.arange(self.ndim)[::-1])
elif len(new_order) != self.ndim:
raise ValueError('Incorrect number of dimensions.')
Expand Down