-
Notifications
You must be signed in to change notification settings - Fork 168
Add parcels._datasets subpackage and developer documentation
#1972
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d087dfe
Add parcels._datasets namespace
VeckoTheGecko f1df5b5
Add utilities for comparing datasets
VeckoTheGecko ad69428
Update parcels/_datasets/__init__.py
VeckoTheGecko 6d5eb3b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4406a55
Update parcels/_datasets/__init__.py
VeckoTheGecko 4d52884
review feedback
VeckoTheGecko 42b0a84
update wording
VeckoTheGecko 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| """ | ||
| Datasets compatible with Parcels. | ||
|
|
||
| This subpackage uses xarray to generate *idealised* structured and unstructured hydrodynamical datasets that are compatible with Parcels. The goals are three-fold: | ||
|
|
||
| 1. To provide users with documentation for the types of datasets they can expect Parcels to work with. | ||
| 2. To supply our tutorials with hydrodynamical datasets. | ||
| 3. To offer developers datasets for use in test cases. | ||
|
|
||
| Note that this subpackage is part of the private API for Parcels. Users should not rely directly on the functions defined within this module. Instead, if you want to generate your own datasets, copy the functions from this module into your own code. | ||
|
|
||
| Developers, note that you should only add functions that create idealised datasets to this subpackage if they are (a) quick to generate, and (b) only use dependencies already shipped with Parcels. No data files should be added to this subpackage. Real world data files should be added to the `OceanParcels/parcels-data` repository on GitHub. | ||
|
|
||
| Parcels Dataset Philosophy | ||
| ------------------------- | ||
|
|
||
| When adding datasets, there may be a tension between wanting to add a specific dataset or wanting to add machinery to generate completely parameterised datasets (e.g., with different grid resolutions, with different ranges, with different datetimes etc.). There are trade-offs to both approaches: | ||
|
|
||
| Working with specific hardcoded datasets: | ||
|
|
||
| * Pros | ||
| * the example is stable and self-contained | ||
| * easy to see exactly what the dataset is, there is little to no dependency on other functions defined in the same module | ||
| * datasets don't "break" due to changes in other functions (e.g., grid edges becoming out of sync with grid centres) | ||
| * Cons | ||
| * inflexible for use in tests where you want to test a large range of datasets, or you want to test a specific resolution | ||
|
|
||
| Working with generated datasets is the opposite of all the above. | ||
|
|
||
| Most of the time we only want a single dataset. For example, for use in a tutorial, or for testing a specific feature of Parcels - such as (in the case of structured grids) checking that the grid from a certain (ocean) circulation model is correctly parsed, or checking that indexing is correctly picked up. As such, one should often opt for hardcoded datasets. These are more stable and easier to see exactly what the dataset is. We may have specific examples that become the default "go to" dataset for testing when we don't care about the details of the dataset. | ||
|
|
||
| Sometimes we may want to test Parcels against a whole range of datasets varying in a certain way - to ensure Parcels works as expected. For these, we should add machinery to create generated datasets. | ||
|
|
||
| Structure | ||
| -------- | ||
|
|
||
| This subpackage is broken down into structured and unstructured parts. Each of these have common submodules: | ||
|
|
||
| * ``circulation_model`` -> hardcoded datasets with the intention of mimicking datasets from a certain (ocean) circulation model | ||
| * ``generic`` -> hardcoded datasets that are generic, and not tied to a certain (ocean) circulation model. Instead these focus on the fundamental properties of the dataset | ||
| * ``generated`` -> functions to generate datasets with varying properties | ||
| * ``utils`` -> any utility functions necessary related to either generating or validating datasets | ||
|
|
||
| There may be extra submodules than the ones listed above. | ||
|
|
||
| """ |
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 @@ | ||
| """Structured datasets.""" |
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 @@ | ||
| """Unstructured datasets.""" |
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,69 @@ | ||
| from typing import Any | ||
|
|
||
| import numpy as np | ||
| import xarray as xr | ||
|
|
||
| from parcels._compat import add_note | ||
|
|
||
| _SUPPORTED_ATTR_TYPES = int | float | str | np.ndarray | ||
|
|
||
|
|
||
| def _print_mismatched_keys(d1: dict[Any, Any], d2: dict[Any, Any]) -> None: | ||
| k1 = set(d1.keys()) | ||
| k2 = set(d2.keys()) | ||
| if len(k1 ^ k2) == 0: | ||
| return | ||
| print("Mismatched keys:") | ||
| print(f"L: {k1 - k2!r}") | ||
| print(f"R: {k2 - k1!r}") | ||
|
|
||
|
|
||
| def assert_common_attrs_equal( | ||
| xr_attrs_1: dict[str, _SUPPORTED_ATTR_TYPES], xr_attrs_2: dict[str, _SUPPORTED_ATTR_TYPES], *, verbose: bool = True | ||
| ) -> None: | ||
| d1, d2 = xr_attrs_1, xr_attrs_2 | ||
|
|
||
| common_keys = set(d1.keys()) & set(d2.keys()) | ||
| if verbose: | ||
| _print_mismatched_keys(d1, d2) | ||
|
|
||
| for key in common_keys: | ||
| try: | ||
| if isinstance(d1[key], np.ndarray): | ||
| np.testing.assert_array_equal(d1[key], d2[key]) | ||
| else: | ||
| assert d1[key] == d2[key], f"{d1[key]} != {d2[key]}" | ||
| except AssertionError as e: | ||
| add_note(e, f"error on key {key!r}") | ||
| raise | ||
|
|
||
|
|
||
| def assert_common_variables_common_attrs_equal(ds1: xr.Dataset, ds2: xr.Dataset, *, verbose: bool = True) -> None: | ||
| if verbose: | ||
| print("Checking dataset attrs...") | ||
|
|
||
| assert_common_attrs_equal(ds1.attrs, ds2.attrs, verbose=verbose) | ||
|
|
||
| ds1_vars = set(ds1.variables) | ||
| ds2_vars = set(ds2.variables) | ||
|
|
||
| common_variables = ds1_vars & ds2_vars | ||
| if len(ds1_vars ^ ds2_vars) > 0 and verbose: | ||
| print("Mismatched variables:") | ||
| print(f"L: {ds1_vars - ds2_vars}") | ||
| print(f"R: {ds2_vars - ds1_vars}") | ||
|
|
||
| for var in common_variables: | ||
| if verbose: | ||
| print(f"Checking {var!r} attrs") | ||
| assert_common_attrs_equal(ds1[var].attrs, ds2[var].attrs, verbose=verbose) | ||
|
|
||
|
|
||
| def dataset_repr_diff(ds1: xr.Dataset, ds2: xr.Dataset) -> str: | ||
| """Return a text diff of two datasets.""" | ||
| repr1 = repr(ds1) | ||
| repr2 = repr(ds2) | ||
| import difflib | ||
|
|
||
| diff = difflib.ndiff(repr1.splitlines(keepends=True), repr2.splitlines(keepends=True)) | ||
| return "".join(diff) | ||
VeckoTheGecko marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.