Skip to content

Commit

Permalink
Use partial to make cube pickleable (SciTools#4377)
Browse files Browse the repository at this point in the history
* Use partial to make cube pickleable

* Pickle tests for cube with real data, lazy data and deferred unit conversion

* Revert test_Cube.py and add new test to test_pickling.py

* Update whatsnew with this PR and move a previously incorrectly placed entry

* Remove commented out code

* Avoid whatsnew duplication and reference benchmark

* Update docs/src/whatsnew/latest.rst

Co-authored-by: Bill Little <[email protected]>

Co-authored-by: Bill Little <[email protected]>
  • Loading branch information
wjbenfold and bjlittle authored Jan 6, 2022
1 parent 925998e commit a18a57c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
13 changes: 8 additions & 5 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,6 @@ This document explains the changes made to Iris for this release
#. `@rcomer`_ fixed :meth:`~iris.cube.Cube.subset` to alway return ``None`` if
no value match is found. (:pull:`4417`)

#. `@wjbenfold`_ resolved an issue that previously caused regridding with lazy
data to take significantly longer than with real data. Relevant benchmark
shows a time decrease from >10s to 625ms. (:issue:`4280`, :pull:`4400`)

#. `@wjbenfold`_ changed :meth:`iris.util.points_step` to stop it from warning
when applied to a single point (:issue:`4250`, :pull:`4367`)

Expand All @@ -140,6 +136,10 @@ This document explains the changes made to Iris for this release
coordinate bounds using minimum and maximum for unordered coordinates,
fixing :issue:`1528`. (:pull:`4315`)

#. `@wjbenfold`_ changed how a delayed unit conversion is performed on a cube
so that a cube with lazy data awaiting a unit conversion can be pickled.
(:issue:`4354 `, :pull:`4377`)


💣 Incompatible Changes
=======================
Expand All @@ -150,7 +150,10 @@ This document explains the changes made to Iris for this release
🚀 Performance Enhancements
===========================

#. N/A
#. `@wjbenfold`_ resolved an issue that previously caused regridding with lazy
data to take significantly longer than with real data. Benchmark
:class:`benchmarks.HorizontalChunkedRegridding` shows a time decrease
from >10s to 625ms. (:issue:`4280`, :pull:`4400`)


🔥 Deprecations
Expand Down
4 changes: 1 addition & 3 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,9 +1051,7 @@ def convert_units(self, unit):
old_unit = self.units
new_unit = unit

# Define a delayed conversion operation (i.e. a callback).
def pointwise_convert(values):
return old_unit.convert(values, new_unit)
pointwise_convert = partial(old_unit.convert, other=new_unit)

new_data = _lazy.lazy_elementwise(
self.lazy_data(), pointwise_convert
Expand Down
11 changes: 10 additions & 1 deletion lib/iris/tests/test_pickling.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
import pickle

import cf_units
import numpy as np

import iris
from iris._lazy_data import as_concrete_data
from iris._lazy_data import as_concrete_data, as_lazy_data


class TestPickle(tests.IrisTest):
Expand Down Expand Up @@ -76,6 +77,14 @@ def test_cube_with_coord_points(self):
_, recon_cube = next(self.pickle_then_unpickle(cube))
self.assertEqual(recon_cube, cube)

def test_cube_with_deferred_unit_conversion(self):
real_data = np.arange(12.0).reshape((3, 4))
lazy_data = as_lazy_data(real_data)
cube = iris.cube.Cube(lazy_data, units="m")
cube.convert_units("ft")
_, recon_cube = next(self.pickle_then_unpickle(cube))
self.assertEqual(recon_cube, cube)

@tests.skip_data
def test_cubelist_pickle(self):
cubelist = iris.load(
Expand Down

0 comments on commit a18a57c

Please sign in to comment.