Skip to content

Commit

Permalink
Merge pull request #551 from davidhassell/dask-data-no-args
Browse files Browse the repository at this point in the history
dask: `Data.__init__` with no arguments
  • Loading branch information
davidhassell authored Jan 12, 2023
2 parents afc7e0b + 8e343bb commit ab6b8cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
14 changes: 6 additions & 8 deletions cf/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,6 @@ def __init__(
>>> d = cf.Data(tuple('fly'))
"""
if array is None and source is None: # don't create no/empty Data
raise ValueError(
"Can't create empty data: some input data or datum must be "
"provided via the 'source' or 'array' parameters."
)

if source is None and isinstance(array, self.__class__):
source = array

Expand Down Expand Up @@ -387,6 +381,7 @@ def __init__(
self.hardmask = hardmask

if array is None:
# No data has been set
return

try:
Expand Down Expand Up @@ -8393,13 +8388,16 @@ def to_dask_array(self, apply_mask_hardness=False):
dask.array<cf_soften_mask, shape=(4,), dtype=int64, chunksize=(4,), chunktype=numpy.ndarray>
"""
if apply_mask_hardness:
if apply_mask_hardness and "dask" in self._custom:
if self.hardmask:
self.harden_mask()
else:
self.soften_mask()

return self._custom["dask"]
try:
return self._custom["dask"]
except KeyError:
raise ValueError(f"{self.__class__.__name__} object has no data")

def datum(self, *index):
"""Return an element of the data array as a standard Python
Expand Down
20 changes: 16 additions & 4 deletions cf/test/test_Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,27 @@ def setUp(self):

def test_Data__init__basic(self):
"""Test basic `__init__` cases for Data."""
# Most __init__ parameters are covered by the various other tests,
# so this is mainly to check trivial cases and especially the edge
# case of 'default' Data i.e. if no parameters are specified.
# Most __init__ parameters are covered by the various other
# tests, so this is mainly to check trivial cases.
cf.Data(0, "s")
cf.Data(array=np.arange(5))
cf.Data(source=self.filename)

d = cf.Data()
with self.assertRaises(ValueError):
cf.Data()
d.ndim

with self.assertRaises(ValueError):
d.get_filenames()

def test_Data__init__no_args(self):
"""Test `__init__` with no arg."""
# Most __init__ parameters are covered by the various other
# tests, so this is mainly to check trivial cases.
cf.Data()
cf.Data(0, "s")
cf.Data(array=np.arange(5))
cf.Data(source=self.filename)

def test_Data_equals(self):
"""Test the equality-testing Data method."""
Expand Down

0 comments on commit ab6b8cb

Please sign in to comment.