Skip to content

Commit 7363412

Browse files
committed
Replicate legacy dask behaviour pre dask/dask#9555.
1 parent b5178a9 commit 7363412

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/iris/_data_manager.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
import numpy as np
1414
import numpy.ma as ma
1515

16-
from iris._lazy_data import as_concrete_data, as_lazy_data, is_lazy_data
16+
from iris._lazy_data import (
17+
as_concrete_data,
18+
as_lazy_data,
19+
copy_lazy_data,
20+
is_lazy_data,
21+
)
1722

1823

1924
class DataManager:
@@ -174,7 +179,7 @@ def _deepcopy(self, memo, data=None):
174179
if data is None:
175180
# Copy the managed data.
176181
if self.has_lazy_data():
177-
data = copy.deepcopy(self._lazy_array, memo)
182+
data = copy_lazy_data(self._lazy_array)
178183
else:
179184
data = self._real_array.copy()
180185
else:

lib/iris/_lazy_data.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,27 @@ def map_complete_blocks(src, func, dims, out_sizes):
417417
out_chunks[dim] = size
418418

419419
return data.map_blocks(func, chunks=out_chunks, dtype=src.dtype)
420+
421+
422+
def copy_lazy_data(data: da.Array) -> da.Array:
423+
"""
424+
Copy a lazy data array, ensuring compute() will return a different array to the original.
425+
426+
Replicates legacy Dask behaviour removed in dask#9555.
427+
428+
Parameters
429+
----------
430+
data : Dask Array
431+
The lazy data array to be copied.
432+
433+
Returns
434+
-------
435+
Dask Array
436+
A copy of `data`.
437+
438+
"""
439+
if data.npartitions == 1:
440+
result = data.map_blocks(dask.utils.M.copy)
441+
else:
442+
result = data.copy()
443+
return result

0 commit comments

Comments
 (0)