Proposed new regridder save-load API in 0.3.0 (not backward compatible)
See original GitHub issueI’d like to implement a new regridder save-load API to resolve #11. ESMPy 8.0.0 is the prerequisite for this, and is now available on Conda conda-forge/esmpy-feedstock#26
Before implementing this, I’d like users of xESMF to review and comment on the new API, because there will be backward-incompatible changes.
Description of the changes
The current & old API (until 0.2.x) is:
regridder = xesmf.Regridder(
grid_in, grid_out, method, filename=filename
)
regridder_reload = xesmf.Regridder(
grid_in, grid_out, method, filename=filename, reuse_weights=True
)
The first call writes out a NetCDF file containing regridding weights, and reads the file back to memory to construct a SciPy sparse matrix (representing the regridding operation). Such extra I/O exists because ESMPy < 8.0.0 can only dump weights to disk. ESMPy 8.0.0 allows in-memory retrival of weights as numpy arrays, so such disk I/O is no longer needed.
The second call, with reuse_weights=True
, detects that the weight file exists and simply reads it back without re-computation.
filename
is an optional kwarg. If not specified, the regridder file will be given a default name like bilinear_25x53_59x87.nc
, indicating (regrid method, input grid shape, output grid shape)
.
The new API (starting 0.3.0) will be:
regridder = xesmf.Regridder(grid_in, grid_out, method)
regridder.save(filename)
regridder_reload = xesmf.load_regridder(filename)
The first call only constructs the regridder in-memory, and will not write any files to disk.
Instead of implicitly handing the save/load as part of xesmf.Regridder()
call, the new save()
and load_regridder()
do so explicitly. They are similar to save()
/load_model()
in Keras or save
/load()
in PyTorch.
The original reuse_weights
and filename
kwargs will be removed in xesmf.Regridder()
.
Major question on backward-compatibility
Should the reuse_weights=True
kwarg be kept for backward-compatibility? My inclination is no, as there should only be one unambiguous way to load the regridder.
However, one convenient thing about the old xesmf.Regridder(..., reuse_weights=True)
API is that this single line of code works in any cases. If the weight file doesn’t exist on disk, xesmf builds it and writes to disk; if exists, xesmf reads it back without re-computation.
The same logic with the new API would be:
filename = 'your-regridder-name.nc'
if os.path.exists(filename):
regridder = xesmf.load_regridder(filename)
else:
regridder = xesmf.Regridder(grid_in, grid_out, method)
regridder.save(filename)
This is more verbose, but also more explicit, and explicit is better than implicit
Minor questions
- Should it be
xesmf.load_regridder()
or justxesmf.load()
? I prefer the former as it’s more explicit. - Should
regridder.save()
has a default filename? I slightly prefer no, but some people might have a hard time choosing a name 😃 Can still use the current default naming scheme likebilinear_25x53_59x87.nc
. Welcome other suggestions.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:7 (1 by maintainers)
Top GitHub Comments
This new API seems like a nice step forward, and the package is young enough that you shouldn’t worry too much about backwards compatibility IMO. It’s still 0.X after all 😄 Thanks for xESMF! It’s an excellent tool.
The API changes proposed above have not been released, nor merged into master. If you want to take a look at the branch #91 and provide feedback, that would be useful.