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

dask: Data.__init__ with no arguments #551

Merged
merged 3 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
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
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 @@ -8318,13 +8313,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