-
Notifications
You must be signed in to change notification settings - Fork 19
Partition MVP #614
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
Merged
Merged
Partition MVP #614
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
11f5df7
Partition MVP
stephenworsley 8ad6983
lint fixes
stephenworsley bc29614
lint fixes
stephenworsley e9fa31a
lint fixes and name changes
stephenworsley e625856
lint fixes
stephenworsley 60fc675
fix import order
stephenworsley decc42f
fix import order
stephenworsley 237cef8
add tests, docstrings
stephenworsley 7a8affa
add multidimensional test
stephenworsley 8abfed2
add multidimensional cube support
stephenworsley 9b3fc78
fix test
stephenworsley 78e4479
fix test
stephenworsley 147abbf
fix test
stephenworsley 36a9b05
fix ESMFNearest behaviour
stephenworsley 1dd92e6
add test, improve docstrings
stephenworsley 45d449c
fix test
stephenworsley 9bc0dc9
add test
stephenworsley 004e895
fix test
stephenworsley f7ce8ca
ruff format
stephenworsley 6f33251
add documentation
stephenworsley c367325
add to docstrings
stephenworsley 9cbb76e
add docstrings and repr to PartialRegridder
stephenworsley 79b56a1
repr testing
stephenworsley 90968b6
ruff fix
stephenworsley 15c1f5a
fix docs
stephenworsley 84c9482
docs grammar
stephenworsley b6aca52
attempt benchmarks slowdown fix
stephenworsley 2f9ba4e
tidy unused code
stephenworsley 750bc30
Merge branch 'main' into partition_mvp
stephenworsley ae5b638
ruff fix
stephenworsley 905bbfe
Update src/esmf_regrid/experimental/partition.py
stephenworsley a9f4397
review actions
stephenworsley 9e5c6aa
ruff fixes
stephenworsley 32e5baf
add bilinear regridding support
stephenworsley 21a4d54
address review comments
stephenworsley ef2883a
ruff format
stephenworsley 598d2d3
more sensible PartialRegridder kwarg management
stephenworsley 2c53d31
make round tripping more thorough
stephenworsley 1c1f513
address review comments
stephenworsley 10f1239
address review comments
stephenworsley bfbea29
fix typo
stephenworsley 503a309
address review comments
stephenworsley 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| """Provides a regridder class compatible with Partition.""" | ||
|
|
||
| import numpy as np | ||
|
|
||
| from esmf_regrid.schemes import ( | ||
| GridRecord, | ||
| MeshRecord, | ||
| _create_cube, | ||
| _ESMFRegridder, | ||
| ) | ||
|
|
||
|
|
||
| class PartialRegridder(_ESMFRegridder): | ||
| """Regridder class designed for use in :class:`~esmf_regrid.experimental.Partition`.""" | ||
|
|
||
| def __init__(self, src, tgt, src_slice, tgt_slice, weights, scheme, **kwargs): | ||
| """Create a regridder instance for a block of :class:`~esmf_regrid.experimental.Partition`. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| src : :class:`iris.cube.Cube` | ||
| The :class:`~iris.cube.Cube` providing the source. | ||
| tgt : :class:`iris.cube.Cube` or :class:`iris.mesh.MeshXY` | ||
| The :class:`~iris.cube.Cube` or :class:`~iris.mesh.MeshXY` providing the target. | ||
| src_slice : tuple | ||
| The upper and lower bounds of the block taken from the original source from which the | ||
| ``src`` was derived. In the form ((x_low, x_high), ...) where x_low and x_high are the | ||
| upper and lower bounds of the slice (in the x dimension) taken from the original source. | ||
| There are as many tuples of upper and lower bounds as there are horizontal dimensions in | ||
| the source cube (currently this is always 2 as Meshes are not yet supported for sources). | ||
| tgt_slice : tuple | ||
| The upper and lower bounds of the block taken from the original target from which the | ||
| ``tgt`` was derived. In the form ((x_low, x_high), ...) where x_low and x_high are the | ||
| upper and lower bounds of the slice (in the x dimension) taken from the original target. | ||
| There are as many tuples of upper and lower bounds as there are horizontal dimensions in | ||
| the target cube. | ||
| weights : :class:`scipy.sparse.spmatrix` | ||
| The weights to use for regridding. | ||
| scheme : :class:`~esmf_regrid.schemes.ESMFAreaWeighted` or :class:`~esmf_regrid.schemes.ESMFBilinear` | ||
| The scheme used to construct the regridder. | ||
| kwargs : dict | ||
| Additional keyword arguments to pass to the `scheme`s regridder method. | ||
| """ | ||
pp-mo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.src_slice = src_slice # this will be tuple-like | ||
pp-mo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| self.tgt_slice = tgt_slice | ||
| self.scheme = scheme | ||
|
|
||
| self._regridder = scheme.regridder( | ||
| src, | ||
| tgt, | ||
| precomputed_weights=weights, | ||
| **kwargs, | ||
| ) | ||
| self.__dict__.update(self._regridder.__dict__) | ||
|
|
||
| def __repr__(self): | ||
| """Return a representation of the class.""" | ||
| result = ( | ||
| f"PartialRegridder(" | ||
| f"src={self._src}, " | ||
| f"tgt_slice={self._tgt}, " | ||
| f"src_slice={self.src_slice}, " | ||
| f"tgt_slice={self.tgt_slice}, " | ||
| f"scheme={self.scheme})" | ||
| ) | ||
pp-mo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return result | ||
|
|
||
| def partial_regrid(self, src): | ||
| """Perform the first half of regridding, generating weights and data.""" | ||
| dims = self._get_cube_dims(src) | ||
| num_dims = len(dims) | ||
| standard_in_dims = [-1, -2][:num_dims] | ||
| data = np.moveaxis(src.data, dims, standard_in_dims) | ||
| result = self.regridder._gen_weights_and_data(data) | ||
| return result | ||
|
|
||
| def finish_regridding(self, src_cube, weights, data, extra): | ||
| """Perform the second half of regridding, combining weights and data. | ||
|
|
||
| This operation is used to process the combined results from all the partial | ||
| regridders in a Partition. | ||
| Since all the combined data is passed in, this operation can be done using | ||
| *any one* of the individual PartialRegridders. | ||
| However, the passed "src_cube" must be the "correct" slice of the source | ||
| data cube, corresponding to the 'tgt_slice' slice params it was created with. | ||
| It is also implicit that the 'extra' arg (additional dimensions) will be the | ||
| same for all partial results. | ||
| The `src_cube` provides coordinates for the non-horizontal dimensions of the | ||
| result cube, matching the dimensions of the `data` array. | ||
| For technical convenience, its *horizontal* coordinates need to match those | ||
| of the 'src' reference cube provided in regridder creation (`self._src`). | ||
| So, it must be the correct "corresponding slice" of the source cube. | ||
| """ | ||
| src_dims = self._get_cube_dims(src_cube) | ||
|
|
||
| result_data = self.regridder._regrid_from_weights_and_data(weights, data, extra) | ||
|
|
||
| num_out_dims = self.regridder.tgt.dims | ||
| num_dims = len(src_dims) | ||
| standard_out_dims = [-1, -2][:num_out_dims] | ||
| if num_dims == 2 and num_out_dims == 1: | ||
| new_dims = [min(src_dims)] | ||
| elif num_dims == 1 and num_out_dims == 2: | ||
| # Note: this code is currently inaccessible since src_cube can't have a Mesh. | ||
| new_dims = [src_dims[0] + 1, src_dims[0]] | ||
| else: | ||
| new_dims = src_dims | ||
|
|
||
| result_data = np.moveaxis(result_data, standard_out_dims, new_dims) | ||
|
|
||
| if isinstance(self._tgt, GridRecord): | ||
| tgt_coords = self._tgt | ||
| out_dims = 2 | ||
| elif isinstance(self._tgt, MeshRecord): | ||
| tgt_coords = self._tgt.mesh.to_MeshCoords(self._tgt.location) | ||
| out_dims = 1 | ||
| else: | ||
| msg = "Unrecognised target information." | ||
| raise TypeError(msg) | ||
|
|
||
| result_cube = _create_cube( | ||
| result_data, src_cube, src_dims, tgt_coords, out_dims | ||
| ) | ||
| return result_cube | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.