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.datum #332

Merged
merged 2 commits into from
Mar 4, 2022
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
15 changes: 7 additions & 8 deletions cf/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10443,6 +10443,7 @@ def in_memory(self):

return True

@daskified(_DASKIFIED_VERBOSE)
def datum(self, *index):
"""Return an element of the data array as a standard Python
scalar.
Expand Down Expand Up @@ -10558,15 +10559,13 @@ def datum(self, *index):
index = tuple(index)
else:
raise ValueError(
"Incorrect number of indices for {} array".format(
self.__class__.__name__
)
f"Incorrect number of indices ({n_index}) for "
f"{self.ndim}-d {self.__class__.__name__} data"
)
elif n_index != self.ndim:
raise ValueError(
"Incorrect number of indices for {} array".format(
self.__class__.__name__
)
f"Incorrect number of indices ({n_index}) for "
f"{self.ndim}-d {self.__class__.__name__} data"
)

array = self[index].array
Expand All @@ -10576,8 +10575,8 @@ def datum(self, *index):

else:
raise ValueError(
"Can only convert a {} array of size 1 to a "
"Python scalar".format(self.__class__.__name__)
f"For size {self.size} data, must provide an index of "
"the element to be converted to a Python scalar"
)

if not np.ma.isMA(array):
Expand Down
21 changes: 15 additions & 6 deletions cf/test/test_Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1771,12 +1771,7 @@ def test_Data_datum(self):
self.assertEqual(d.datum(-1), 3)
for index in d.ndindex():
self.assertEqual(d.datum(index), d.array[index].item())
self.assertEqual(
d.datum(*index),
d.array[index].item(),
"{}, {}".format(d.datum(*index), d.array[index].item()),
)
# --- End: for
self.assertEqual(d.datum(*index), d.array[index].item())

d = cf.Data(5, "metre")
d[()] = cf.masked
Expand All @@ -1795,6 +1790,20 @@ def test_Data_datum(self):
self.assertIs(d.datum([0, -1]), cf.masked)
self.assertIs(d.datum(-1, -1), cf.masked)

d = cf.Data([1, 2])
with self.assertRaises(ValueError):
d.datum()

with self.assertRaises(ValueError):
d.datum(3)

with self.assertRaises(ValueError):
d.datum(0, 0)

d = cf.Data([[1, 2]])
with self.assertRaises(ValueError):
d.datum((0,))

def test_Data_flip(self):
if self.test_only and inspect.stack()[0][3] not in self.test_only:
return
Expand Down