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

LAMA to Dask: fixes to Query & Data.digitize #427

Merged
Show file tree
Hide file tree
Changes from 13 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
12 changes: 12 additions & 0 deletions cf/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,18 @@ def digitize(
d._set_dask(dx)
d.override_units(_units_None, inplace=True)

# More elegant to handle 'delete_bins' in cf- rather than Dask- space
# i.e. using cf.where with d in-place rather than da.where with dx
# just after the digitize operation above (cf.where already applies
# equivalent logic element-wise).
if delete_bins:
for n, db in enumerate(delete_bins):
db -= n
d.where(d == db, np.ma.masked, None, inplace=True)
# x = d - 1 rather than = d here since there is one fewer bin
# therefore we need to adjust to the new corresponding indices
d.where(d > db, d - 1, None, inplace=True)

if return_bins:
if two_d_bins is None:
two_d_bins = np.empty((bins.size - 1, 2), dtype=bins.dtype)
Expand Down
39 changes: 27 additions & 12 deletions cf/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,32 +857,47 @@ def set_condition_units(self, units):
<CF Query: (ge 3000 m)>

"""
def get_and_set_value_units(v, u):
"""Helper method to simplify logic to set specified units."""
v_units = getattr(v, "Units", None)
if v_units is None: # Value 'v' has no units
v = Data(v, units=u)
else: # Value 'v' already has units
try:
v = v.copy()
v.Units = u
except ValueError:
raise ValueError(
f"Units {u!r} are not equivalent to "
f"query condition units {v_units!r}"
)

return v

units = Units(units)

compound = self._compound
if compound:
for r in compound:
r.set_condition_units(units)

return

value = self._value
if value is None:
return

value_units = getattr(value, "Units", None)
sadielbartholomew marked this conversation as resolved.
Show resolved Hide resolved
if value_units is None:
# Value has no units
value = Data(value, units=units)
if self.operator in ("wi", "wo", "set"):
# Value is a sequence of things that may or may not
# already have units
sadielbartholomew marked this conversation as resolved.
Show resolved Hide resolved
new_values = []
for v in value:
v = get_and_set_value_units(v, units)
new_values.append(v)

value = new_values
else:
# Value already has units
try:
value.Units = units
except ValueError:
raise ValueError(
f"Units {units!r} are not equivalent to "
f"query condition units {value_units!r}"
)
value = get_and_set_value_units(value, units)

self._value = value

Expand Down
20 changes: 8 additions & 12 deletions cf/test/test_Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,18 +942,14 @@ def test_Data_digitize(self):
(np.ma.getmask(e.array) == np.ma.getmask(b)).all()
)

# TODODASK: Reinstate the following test when
# __sub__, minimum, and maximum have
# been daskified

# e.where(
# cf.set([e.minimum(), e.maximum()]),
# cf.masked,
# e - 1,
# inplace=True,
# )
# f = d.digitize(bins, upper=upper)
# self.assertTrue(e.equals(f, verbose=2))
e.where(
cf.set([e.minimum(), e.maximum()]),
cf.masked,
e - 1,
inplace=True,
)
f = d.digitize(bins, upper=upper)
self.assertTrue(e.equals(f, verbose=2))

# Check returned bins
bins = [2, 6, 10, 50, 100]
Expand Down