-
Notifications
You must be signed in to change notification settings - Fork 283
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
Basic functional lazy saving. #5031
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
import warnings | ||
|
||
import cf_units | ||
import dask | ||
import dask.array as da | ||
import netCDF4 | ||
import numpy as np | ||
|
@@ -490,10 +491,51 @@ def __setitem__(self, keys, arr): | |
MESH_ELEMENTS = ("node", "edge", "face") | ||
|
||
|
||
class DeferredSaveWrapper: | ||
""" | ||
An object which mimics the data access of a netCDF4.Variable, and can be written to. | ||
It encapsulates the netcdf file and variable which are actually to be written to. | ||
This opens the file each time, to enable writing the data chunk, then closes it. | ||
TODO: could be improved with a caching scheme, but this just about works. | ||
""" | ||
|
||
def __init__(self, filepath, cf_var): | ||
# Grab useful properties of the variable, including the identifying 'name'. | ||
self.path = filepath | ||
for key in ("shape", "dtype", "ndim", "name"): | ||
setattr(self, key, getattr(cf_var, key)) | ||
|
||
def __setitem__(self, keys, array_data): | ||
# Write to the variable. | ||
# Re-open the file for writing. | ||
dataset = netCDF4.Dataset(self.path, "r+") | ||
try: | ||
var = dataset.variables[self.name] | ||
var[keys] = array_data | ||
finally: | ||
dataset.close() | ||
|
||
def __repr__(self): | ||
fmt = ( | ||
"<{self.__class__.__name__} shape={self.shape}" | ||
" dtype={self.dtype!r} path={self.path!r}" | ||
" name={self.name!r}>" | ||
) | ||
return fmt.format(self=self) | ||
|
||
|
||
@dask.delayed | ||
def combined_delayeds(*args): | ||
"""A delayed function which simply computes all its arguments.""" | ||
# Dask computes the lazy args before passing them here. | ||
# So job done -- we don't need to do anything with them. | ||
pass | ||
|
||
|
||
class Saver: | ||
"""A manager for saving netcdf files.""" | ||
|
||
def __init__(self, filename, netcdf_format): | ||
def __init__(self, filename, netcdf_format, compute=True): | ||
""" | ||
A manager for saving netcdf files. | ||
|
||
|
@@ -506,6 +548,15 @@ def __init__(self, filename, netcdf_format): | |
Underlying netCDF file format, one of 'NETCDF4', 'NETCDF4_CLASSIC', | ||
'NETCDF3_CLASSIC' or 'NETCDF3_64BIT'. Default is 'NETCDF4' format. | ||
|
||
* compute (bool): | ||
If True, the Saver performs normal 'synchronous' data writes, where data | ||
is streamed directly into file variables during the save operation. | ||
If False, the file is created as normal, but computation and streaming of | ||
any lazy array content is instead deferred to :class:`dask.delayed` objects, | ||
which are held in a list in the saver 'delayed_writes' property. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This bit can probably be treated as internal/private detail now. |
||
The relavant file variables are created empty, and the write can | ||
subsequently be completed by computing the 'save.deferred_writes'. | ||
|
||
Returns: | ||
None. | ||
|
||
|
@@ -542,7 +593,14 @@ def __init__(self, filename, netcdf_format): | |
self._mesh_dims = {} | ||
#: A dictionary, mapping formula terms to owner cf variable name | ||
self._formula_terms_cache = {} | ||
#: Whether lazy saving. | ||
self.lazy_saves = not compute | ||
#: A list of deferred writes (if lazy saving) | ||
self.deferred_writes = [] | ||
#: Target filepath | ||
self.filepath = filename | ||
#: NetCDF dataset | ||
self._dataset = None | ||
try: | ||
self._dataset = netCDF4.Dataset( | ||
filename, mode="w", format=netcdf_format | ||
|
@@ -2442,8 +2500,7 @@ def _increment_name(self, varname): | |
|
||
return "{}_{}".format(varname, num) | ||
|
||
@staticmethod | ||
def _lazy_stream_data(data, fill_value, fill_warn, cf_var): | ||
def _lazy_stream_data(self, data, fill_value, fill_warn, cf_var): | ||
if hasattr(data, "shape") and data.shape == (1,) + cf_var.shape: | ||
# (Don't do this check for string data). | ||
# Reduce dimensionality where the data array has an extra dimension | ||
|
@@ -2453,13 +2510,36 @@ def _lazy_stream_data(data, fill_value, fill_warn, cf_var): | |
data = data.squeeze(axis=0) | ||
|
||
if is_lazy_data(data): | ||
if self.lazy_saves: | ||
# deferred lazy streaming | ||
def store(data, cf_var, fill_value): | ||
# Create a data-writeable object that we can stream into, which | ||
# encapsulates the file to be opened + variable to be written. | ||
writeable_var_wrapper = DeferredSaveWrapper( | ||
self.filepath, cf_var | ||
) | ||
# Add a delayed save to our 'deferred_writes' list. | ||
self.deferred_writes.append( | ||
da.store( | ||
[data], [writeable_var_wrapper], compute=False | ||
) | ||
) | ||
# NOTE: in this case, no checking of fill-value violations so just | ||
# return dummy values for this. | ||
# TODO: just for now -- can probably make this work later | ||
is_masked, contains_value = False, False | ||
return is_masked, contains_value | ||
|
||
def store(data, cf_var, fill_value): | ||
# Store lazy data and check whether it is masked and contains | ||
# the fill value | ||
target = _FillValueMaskCheckAndStoreTarget(cf_var, fill_value) | ||
da.store([data], [target]) | ||
return target.is_masked, target.contains_value | ||
else: | ||
# Immediate streaming store : check mask+fill as we go. | ||
def store(data, cf_var, fill_value): | ||
# Store lazy data and check whether it is masked and contains | ||
# the fill value | ||
target = _FillValueMaskCheckAndStoreTarget( | ||
cf_var, fill_value | ||
) | ||
da.store([data], [target]) | ||
return target.is_masked, target.contains_value | ||
|
||
else: | ||
|
||
|
@@ -2526,6 +2606,7 @@ def save( | |
least_significant_digit=None, | ||
packing=None, | ||
fill_value=None, | ||
compute=True, | ||
): | ||
""" | ||
Save cube(s) to a netCDF file, given the cube and the filename. | ||
|
@@ -2648,6 +2729,14 @@ def save( | |
`:class:`iris.cube.CubeList`, or a single element, and each element of | ||
this argument will be applied to each cube separately. | ||
|
||
* compute (bool): | ||
When False, create the output file but defer writing any lazy array content to | ||
its variables, such as (lazy) data and aux-coords points and bounds. | ||
Instead return a class:`dask.delayed` which, when computed, will compute all | ||
the lazy content and stream it to complete the file. | ||
Several such data saves can be performed in parallel, by passing a list of them | ||
into a :func:`dask.compute` call. | ||
|
||
Returns: | ||
None. | ||
|
||
|
@@ -2748,7 +2837,7 @@ def is_valid_packspec(p): | |
raise ValueError(msg) | ||
|
||
# Initialise Manager for saving | ||
with Saver(filename, netcdf_format) as sman: | ||
with Saver(filename, netcdf_format, compute=compute) as sman: | ||
# Iterate through the cubelist. | ||
for cube, packspec, fill_value in zip(cubes, packspecs, fill_values): | ||
sman.write( | ||
|
@@ -2793,3 +2882,10 @@ def is_valid_packspec(p): | |
|
||
# Add conventions attribute. | ||
sman.update_global_attributes(Conventions=conventions) | ||
|
||
if compute: | ||
result = None | ||
else: | ||
# For lazy save, return a single 'delayed' representing all lazy writes. | ||
result = combined_delayeds(sman.deferred_writes) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pp-mo Apart from caching, locking should be considered, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dead right. I had not understood much, which is why it did not work with 'distributed'.
Hopefully that is now fixed @ed654d ...