question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposed new regridder save-load API in 0.3.0 (not backward compatible)

See original GitHub issue

I’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 just xesmf.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 like bilinear_25x53_59x87.nc. Welcome other suggestions.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
spencerahillcommented, Nov 19, 2019

Should the reuse_weights=True kwarg be kept for backward-compatibility?

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.

0reactions
huardcommented, May 8, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gavin D. Madakumbura (@GavinDayanga) / Twitter
My latest research out today in Nature Climate Change ( ... Proposed new regridder save-load API in 0.3.0 (not backward compatible) · Issue...
Read more >
What's new — xESMF 0.3.0 documentation
SpatialAverager did not compute the same weights as Regridder when source cell areas were not ... which are preserved for backward compatibility (PR3)....
Read more >
AIP-180: Backwards compatibility - API Improvement Proposals
Guidance. Existing client code must not be broken by a service updating to a new minor or patch release. Old clients must be...
Read more >
Releasing a non-backwards compatible library on PyPI?
I assert: API changes normally fall into two categories. New API B replaces A but A can entirely be implemented using new API...
Read more >
xESMF - bytemeta
Proposed new regridder save-load API in 0.3.0 (not backward compatible) · Previous Next. Make software development more efficient, Also welcome to join our ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found