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

Improve cf.Field.collapse performance by lazily computing reduced axis coordinates #742

Merged
merged 1 commit into from
Mar 21, 2024
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
2 changes: 2 additions & 0 deletions Changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ version NEXT
to regrid the vertical axis in logarithmic coordinates to
`cf.Field.regrids` and `cf.Field.regridc`
(https://github.com/NCAS-CMS/cf-python/issues/715)
* Improve `cf.Field.collapse` performance by lazily computing reduced
axis coordinates (https://github.com/NCAS-CMS/cf-python/issues/741)
* Fix misleading error message when it is not possible to create area
weights requested from `cf.Field.collapse`
(https://github.com/NCAS-CMS/cf-python/issues/731)
Expand Down
41 changes: 30 additions & 11 deletions cf/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -7149,14 +7149,37 @@ def collapse(
if dim is None:
continue

# Create a new dimension coordinate for this axis
# Create new dimension coordinate bounds
if dim.has_bounds():
bounds_data = [dim.bounds.datum(0), dim.bounds.datum(-1)]
b = dim.bounds.data
else:
bounds_data = [dim.datum(0), dim.datum(-1)]
b = dim.data

units = dim.Units
cached_elements = b._get_cached_elements()
try:
# Try to set the new bounds from cached values
bounds_data = Data(
[[cached_elements[0], cached_elements[-1]]],
dtype=b.dtype,
units=b.Units,
)
except KeyError:
# Otherwise create the new bounds lazily
ndim = b.ndim
bounds_data = Data.concatenate(
[
b[(slice(0, 1, 1),) * ndim],
b[(slice(-1, None, 1),) * ndim],
],
axis=-1,
copy=False,
)
if ndim == 1:
bounds_data.insert_dimension(0, inplace=True)

bounds = self._Bounds(data=bounds_data)

# Create a new dimension coordinate value
if coordinate == "min":
coordinate = "minimum"
print(
Expand All @@ -7171,21 +7194,17 @@ def collapse(
)

if coordinate == "mid_range":
data = Data(
[(bounds_data[0] + bounds_data[1]) * 0.5], units=units
)
data = bounds_data.mean(axes=1, weights=None, squeeze=True)
elif coordinate == "minimum":
data = dim.data.min()
data = dim.data.min(squeeze=False)
elif coordinate == "maximum":
data = dim.data.max()
data = dim.data.max(squeeze=False)
else:
raise ValueError(
"Can't collapse: Bad parameter value: "
f"coordinate={coordinate!r}"
)

bounds = self._Bounds(data=Data([bounds_data], units=units))

dim.set_data(data, copy=False)
dim.set_bounds(bounds, copy=False)

Expand Down
Loading