Skip to content

Commit

Permalink
Merge pull request #332 from davidhassell/dask-datum
Browse files Browse the repository at this point in the history
dask: `Data.datum`
  • Loading branch information
davidhassell authored Mar 4, 2022
2 parents f130048 + adcec58 commit b06171a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
15 changes: 7 additions & 8 deletions cf/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10492,6 +10492,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 @@ -10607,15 +10608,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 @@ -10625,8 +10624,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 @@ -1811,12 +1811,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 @@ -1835,6 +1830,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

0 comments on commit b06171a

Please sign in to comment.