-
Notifications
You must be signed in to change notification settings - Fork 55
Avoid converting chunked data to Numpy arrays (i.e. .values calls)
#1588
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
Open
cmdupuis3
wants to merge
19
commits into
UXARRAY:main
Choose a base branch
from
cmdupuis3:cmd/devalue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
5ddf672
Devalue: dataarray.py
cmdupuis3 995044f
Devalue: dataset.py
cmdupuis3 7ea51bb
Devalue: aggregation.py (includes dask branches)
cmdupuis3 e4f007d
Devalue: dataarray_accessor.py
cmdupuis3 2fd47f8
Devalue: grid.py
cmdupuis3 d161efa
Devalue: slice.py
cmdupuis3 396f9bf
Devalue: dataarray.py branch on numpy/dask
cmdupuis3 1408c20
Devalue: replace with .data cases
cmdupuis3 06149a9
Devalue: last bits?
cmdupuis3 6cf056a
Devalue: linting
cmdupuis3 8137ee6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 436dab2
Merge branch 'main' into cmd/devalue
cmdupuis3 5f010b2
devalue linting
cmdupuis3 5ee5157
Merge branch 'main' into cmd/devalue
erogluorhan 850d4ac
devalue: import and output type cleanup
cmdupuis3 f89c41c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] af76639
devalue: rename dask/numpy repro tests
cmdupuis3 84c206b
Merge branch 'main' into cmd/devalue
cmdupuis3 8e86ae9
devalue: Move dask imports back inside
cmdupuis3 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -246,14 +246,14 @@ def to_geodataframe( | |
| same name as the ``UxDataArray`` (or named ``var`` if no name exists) | ||
| """ | ||
|
|
||
| if self.values.ndim > 1: | ||
| if self.ndim > 1: | ||
| # data is multidimensional, must be a 1D slice | ||
| raise ValueError( | ||
| f"Data Variable must be 1-dimensional, with shape {self.uxgrid.n_face} " | ||
| f"for face-centered data." | ||
| ) | ||
|
|
||
| if self.values.size == self.uxgrid.n_face: | ||
| if self.size == self.uxgrid.n_face: | ||
|
cmdupuis3 marked this conversation as resolved.
|
||
| gdf, non_nan_polygon_indices = self.uxgrid.to_geodataframe( | ||
| periodic_elements=periodic_elements, | ||
| projection=projection, | ||
|
|
@@ -290,21 +290,21 @@ def to_geodataframe( | |
|
|
||
| gdf[var_name] = _data | ||
|
|
||
| elif self.values.size == self.uxgrid.n_node: | ||
| elif self.size == self.uxgrid.n_node: | ||
| raise ValueError( | ||
| f"Data Variable with size {self.values.size} does not match the number of faces " | ||
| f"Data Variable with size {self.size} does not match the number of faces " | ||
| f"({self.uxgrid.n_face}. Current size matches the number of nodes. Consider running " | ||
| f"``UxDataArray.topological_mean(destination='face') to aggregate the data onto the faces." | ||
| ) | ||
| elif self.values.size == self.uxgrid.n_edge: | ||
| elif self.size == self.uxgrid.n_edge: | ||
| raise ValueError( | ||
| f"Data Variable with size {self.values.size} does not match the number of faces " | ||
| f"Data Variable with size {self.size} does not match the number of faces " | ||
| f"({self.uxgrid.n_face}. Current size matches the number of edges." | ||
| ) | ||
| else: | ||
| # data is not mapped to | ||
| raise ValueError( | ||
| f"Data Variable with size {self.values.size} does not match the number of faces " | ||
| f"Data Variable with size {self.size} does not match the number of faces " | ||
| f"({self.uxgrid.n_face}." | ||
| ) | ||
|
|
||
|
|
@@ -340,7 +340,7 @@ def to_polycollection( | |
| Flag to indicate whether to override a cached PolyCollection, if it exists | ||
| """ | ||
| # data is multidimensional, must be a 1D slice | ||
| if self.values.ndim > 1: | ||
| if self.ndim > 1: | ||
| raise ValueError( | ||
| f"Data Variable must be 1-dimensional, with shape {self.uxgrid.n_face} " | ||
| f"for face-centered data." | ||
|
|
@@ -612,24 +612,29 @@ def integrate( | |
| >>> uxds = ux.open_dataset("grid.ug", "centroid_pressure_data_ug") | ||
| >>> integral = uxds["psi"].integrate() | ||
| """ | ||
| if self.values.shape[-1] == self.uxgrid.n_face: | ||
| face_areas = self.uxgrid.face_areas.values | ||
|
|
||
| # perform dot product between face areas and last dimension of data | ||
| integral = np.einsum("i,...i", face_areas, self.values) | ||
| if self.shape[-1] == self.uxgrid.n_face: | ||
| # dot product between face areas and the face dimension of the data | ||
| if isinstance(self.data, np.ndarray): | ||
| # eager data: a direct einsum avoids xr.dot's per-call overhead | ||
| integral = np.einsum( | ||
| "i,...i", self.uxgrid.face_areas.values, self.values | ||
| ) | ||
| else: | ||
| # dask-backed data: xr.dot keeps the reduction lazy | ||
|
Collaborator
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. Does integration need a regression test which is sensitive to dask versus numpy? Perhaps, assert that the result is a dask array, and that the values equal the numpy version, similar to the tests already added by this PR into test_topological_agg. |
||
| integral = xr.dot(self, self.uxgrid.face_areas, dim="n_face") | ||
|
|
||
| elif self.values.shape[-1] == self.uxgrid.n_node: | ||
| elif self.shape[-1] == self.uxgrid.n_node: | ||
| raise ValueError("Integrating data mapped to each node not yet supported.") | ||
|
|
||
| elif self.values.shape[-1] == self.uxgrid.n_edge: | ||
| elif self.shape[-1] == self.uxgrid.n_edge: | ||
| raise ValueError("Integrating data mapped to each edge not yet supported.") | ||
|
|
||
| else: | ||
| raise ValueError( | ||
| f"The final dimension of the data variable does not match the number of nodes, edges, " | ||
| f"or faces. Expected one of " | ||
| f"{self.uxgrid.n_node}, {self.uxgrid.n_edge}, or {self.uxgrid.n_face}, " | ||
| f"but received {self.values.shape[-1]}" | ||
| f"but received {self.shape[-1]}" | ||
| ) | ||
|
|
||
| # construct a uxda with integrated quantity | ||
|
|
@@ -1653,7 +1658,7 @@ def curl( | |
| ) | ||
|
|
||
| # Compute curl = ∂v/∂x - ∂u/∂y | ||
| curl_values = grad_v_zonal.values - grad_u_meridional.values | ||
| curl_values = grad_v_zonal.data - grad_u_meridional.data | ||
|
|
||
| u_units = self.attrs.get("units", "") | ||
| has_sphere_radius = "sphere_radius" in self.uxgrid._ds.attrs | ||
|
|
@@ -2180,11 +2185,10 @@ def get_dual(self): | |
| # Get correct dimensions for the dual | ||
| dims = [dim_map.get(dim, dim) for dim in self.dims] | ||
|
|
||
| # Get the values from the data array | ||
| data = np.array(self.values) | ||
|
|
||
| # Construct the new data array | ||
| uxda = uxarray.UxDataArray(uxgrid=dual, data=data, dims=dims, name=self.name) | ||
| uxda = uxarray.UxDataArray( | ||
| uxgrid=dual, data=self.data, dims=dims, name=self.name | ||
| ) | ||
|
|
||
| return uxda | ||
|
|
||
|
|
||
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.