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

Parallel concatenate #5926

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Next Next commit
Simplify concatenate
  • Loading branch information
bouweandela committed Apr 18, 2024
commit f9e0106bcc3ce41cbb40f1a90a9fae7763048198
22 changes: 12 additions & 10 deletions lib/iris/_concatenate.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to fall back on 'normal equality' when hash equality is False?

Sorry to add more work to this, but I've been having some offline conversations with @bjlittle and @pp-mo about equality in general and we're concerned about Iris' strictness. The changes here would make Iris more strict than it is already.

We are therefore keen to use hashing as a way to confirm equality quickly and efficiently, while still retaining the chance for more lenient comparisons such as:

  • Allowing NaN (example).
  • Potentially allowing for floating point differences in future (thanks to @larsbarring for insights).

If this would harm the performance gains you are looking for then we would be open to configurable behaviour in concatenate().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for providing me with feedback! ✨

I've been having some offline conversations with @bjlittle and @pp-mo about equality in general and we're concerned about Iris' strictness.

I agree with this. In ESMValCore we have implemented many workarounds for this to make the life of our users easier.

The changes here would make Iris more strict than it is already.

As far as I'm aware, this pull request does not make any changes to Iris behaviour.
Would you have an example so I can understand when this happens?

I even made the hash comparison work for arrays of different dtypes because I initially expected that that would be allowed, but it turns out that even that is not allowed by the current implementation of concatenate, so I could take that out again. Or we can keep it in case you would be interested in being more lenient w.r.t. this kind of differences in the future.

Allowing NaN

Arrays containing NaNs compare equal with the hashing implementation, I added a test to demonstrate it in 2540fea.

Would it be possible to fall back on 'normal equality' when hash equality is False?

Yes, it would be easy to add the additional comparison here:

iris/lib/iris/_concatenate.py

Lines 1077 to 1078 in 3bfea80

if get_hashes(coord_a.coord) != get_hashes(coord_b.coord):
return False

however, with the current strict implementation of coordinate comparison, there would be no point in doing so because the result would be the same. I'm not too concerned about the performance impact because in our use case, we expect the input to be compatible enough such that the result of the concatenation is a single cube, so the extra comparison would only happen in exceptional cases when there is something wrong with the input data.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# See LICENSE in the root of the repository for full licensing details.
"""Automatic concatenation of multiple cubes over one or more existing dimensions."""

from collections import defaultdict, namedtuple
from collections import namedtuple
import warnings

import dask.array as da
Expand Down Expand Up @@ -326,15 +326,13 @@ def concatenate(
A :class:`iris.cube.CubeList` of concatenated :class:`iris.cube.Cube` instances.

"""
proto_cubes_by_name = defaultdict(list)
proto_cubes = []
# Initialise the nominated axis (dimension) of concatenation
# which requires to be negotiated.
axis = None

# Register each cube with its appropriate proto-cube.
for cube in cubes:
name = cube.standard_name or cube.long_name
proto_cubes = proto_cubes_by_name[name]
registered = False

# Register cube with an existing proto-cube.
Expand All @@ -360,13 +358,11 @@ def concatenate(
concatenated_cubes = iris.cube.CubeList()

# Emulate Python 2 behaviour.
def _none_sort(item):
return (item is not None, item)
def _none_sort(proto_cube):
return (proto_cube.name is not None, proto_cube.name)

for name in sorted(proto_cubes_by_name, key=_none_sort):
for proto_cube in proto_cubes_by_name[name]:
# Construct the concatenated cube.
concatenated_cubes.append(proto_cube.concatenate())
for proto_cube in sorted(proto_cubes, key=_none_sort):
concatenated_cubes.append(proto_cube.concatenate())

# Perform concatenation until we've reached an equilibrium.
count = len(concatenated_cubes)
Expand Down Expand Up @@ -761,6 +757,12 @@ def axis(self):
"""Return the nominated dimension of concatenation."""
return self._axis

@property
def name(self) -> str | None:
"""Return the standard_name or long name."""
metadata = self._cube_signature.defn
return metadata.standard_name or metadata.long_name

def concatenate(self):
"""Concatenate all the source-cubes registered with the :class:`_ProtoCube`.

Expand Down