Skip to content

Commit

Permalink
Coding standards tweaks to daskified cube.py. (#2416)
Browse files Browse the repository at this point in the history
  • Loading branch information
DPeterK authored and pp-mo committed Mar 8, 2017
1 parent 1013ad9 commit a334540
Showing 1 changed file with 20 additions and 19 deletions.
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

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

0 comments on commit a334540

Please sign in to comment.