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.__getitem__ performance by not re-calculating axis cyclicity #745

Merged
merged 3 commits into from
Mar 23, 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 @@ -11,6 +11,8 @@ version NEXT
(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)
* Improve `cf.Field.__getitem__` performance by not re-calculating
axis cyclicity (https://github.com/NCAS-CMS/cf-python/issues/744)
* 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
17 changes: 17 additions & 0 deletions cf/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,22 @@ def __getitem__(self, indices):
for axis, size in zip(data_axes, new_data.shape):
domain_axes[axis].set_size(size)

# Record which axes were cyclic before the subspace
org_cyclic = [data_axes.index(axis) for axis in new.cyclic()]

# Set the subspaced data
new.set_data(new_data, axes=data_axes, copy=False)

# Update axis cylcicity. Note that this can only entail
# setting an originally cyclic axis to be non-cyclic. Doing
# this now enables us to disable the (possibly very slow)
# automatic check for cyclicity on the 'set_construct' calls
# below.
if org_cyclic:
new_cyclic = new_data.cyclic()
[new.cyclic(i, iscyclic=False) for i in org_cyclic if i not in new_cyclic]


# ------------------------------------------------------------
# Subspace constructs with data
# ------------------------------------------------------------
Expand Down Expand Up @@ -507,6 +523,7 @@ def __getitem__(self, indices):
key=key,
axes=construct_axes,
copy=False,
autocyclic={"no-op": True},
)

new.set_data(new_data, axes=data_axes, copy=False)
Expand Down
7 changes: 6 additions & 1 deletion cf/mixin/fielddomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,10 +1109,15 @@ def autocyclic(self, key=None, coord=None, verbose=None, config={}):

:Returns:

`bool`
`bool` or `None`
`True` if the dimension is cyclic, `False` if it isn't,
or `None` if no checks were done.

"""
noop = config.get("no-op")
if noop:
davidhassell marked this conversation as resolved.
Show resolved Hide resolved
# Don't do anything
return

if "cyclic" in config:
if not config["cyclic"]:
Expand Down
8 changes: 8 additions & 0 deletions cf/test/test_Field.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,14 @@ def test_Field__getitem__(self):
self.assertTrue(np.allclose(f[:, -3:].array, g[:, :3].array))
self.assertTrue(f[:, :4].equals(g[:, 3:]))

# Test setting of axis cyclicity
f.cyclic("grid_longitude", iscyclic=True)
self.assertEqual(f.data.cyclic(), {1})
g = f[0, :]
self.assertEqual(g.data.cyclic(), {1})
g = f[:, 0]
self.assertEqual(g.data.cyclic(), set())

def test_Field__setitem__(self):
f = self.f.copy().squeeze()

Expand Down