-
Notifications
You must be signed in to change notification settings - Fork 3
Dim slicer #120
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
Dim slicer #120
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
3fc7889
Initial dim slicing: WIP no groups handling, Slicer untested as yet.
pp-mo 8c05e8f
Start of indexer testing (WIP: incomplete).
pp-mo 43b8056
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2eddbab
Small improvements.
pp-mo a407070
Generalise testing + extend to Slicer tests.
pp-mo d01e707
More tests; small fixes; more docs and api-docs.
pp-mo 05b5071
Add whatsnew.
pp-mo f1ce8fe
Add whastnew for the new utilities page.
pp-mo 96f1bde
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 4413ea0
Merge branch 'main' into dim_slicer
pp-mo 7e6f028
Define .slicer for Ncdata; support full == checking on datasets and v…
pp-mo fc7af0f
Simplify usage modes and API of indexing utilities.
pp-mo 5c59ad1
Fix tests for simplified indexing features.
pp-mo e5c7d29
Add tests for new core object methods.
pp-mo 77a2ed9
Fix docstrings + doctests.
pp-mo e4524d6
Replace dataset difference with equality tests in examples.
pp-mo 18728a4
Document relation between equality testing and difference utilities.
pp-mo 73c3733
Added whatsnew for dataset/variable equality support.
pp-mo 28e8518
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 23b39e2
Merge branch 'main' into dim_slicer
pp-mo d4fc389
Don't keep fragments when building html.
pp-mo 6ddfea8
Improved indexing whatsnew.
pp-mo 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 @@ | ||
| Added a `userguide page <userdocs/user_guide/utilities.html>`_ summarising all the utility features in :mod:`ncdata.utils`. |
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 @@ | ||
| Added utilities to extract sub-regions by indexing on dimensions: :func:`~ncdata.utils.index_by_dimensions` and :class:`~ncdata.utils.Slicer`. |
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,148 @@ | ||
| Utilities and Conveniences | ||
| ========================== | ||
| This section provide a short overview of various more involved operations which are | ||
| provided in the :mod:`~ncdata.utils` module. In all cases, more detail is available in | ||
| the `API pages <../../details/api/ncdata.utils.html>`_ | ||
|
|
||
| Rename Dimensions | ||
| ----------------- | ||
| The :func:`~ncdata.utils.rename_dimension` utility does this, in a way which ensures a | ||
| safe and consistent result. | ||
|
|
||
| Dataset Equality Testing | ||
| ------------------------ | ||
| The function :func:`~ncdata.utils.dataset_differences` produces a list of messages | ||
| detailing all the ways in which two datasets are different. | ||
|
|
||
| For Example: | ||
| ^^^^^^^^^^^^ | ||
| .. testsetup:: | ||
|
|
||
| >>> from ncdata import NcData, NcDimension, NcVariable | ||
| >>> from ncdata.utils import dataset_differences | ||
| >>> import numpy as np | ||
|
|
||
| .. doctest:: | ||
|
|
||
| >>> data1 = NcData( | ||
| ... dimensions=[NcDimension("x", 5)], | ||
| ... variables=[NcVariable("vx", dimensions=["x"], data=np.arange(5))] | ||
| ... ) | ||
| >>> data2 = data1.copy() | ||
| >>> print(dataset_differences(data1, data2)) | ||
| [] | ||
|
|
||
| .. doctest:: | ||
|
|
||
| >>> data2.dimensions["x"].unlimited = True | ||
| >>> data2.variables["vx"].data = np.array([1, 3]) # NB must be a *new* array ! | ||
|
|
||
| .. doctest:: | ||
|
|
||
| >>> diffs = dataset_differences(data1, data2) | ||
| >>> for msg in diffs: | ||
| ... print(msg) | ||
| Dataset "x" dimension has different "unlimited" status : False != True | ||
| Dataset variable "vx" shapes differ : (5,) != (2,) | ||
|
|
||
| .. note:: | ||
| To compare isolated variables, a subsidiary routine | ||
| :func:`~ncdata.utils.variable_differences` is also provided. | ||
|
|
||
| Sub-indexing | ||
| ------------ | ||
| A new dataset can be derived by indexing over dimensions, analagous to sub-indexing | ||
| an array. This operation indexes all the variables appropriately, to produce a new | ||
| independent dataset which is complete and self-consistent. | ||
|
|
||
| The function :func:`~ncdata.utils.index_by_dimensions` provides indexing where the | ||
| indices are passed as arguments or keywords for the specific dimensions. | ||
|
|
||
| For example: | ||
|
|
||
| .. testsetup:: | ||
|
|
||
| >>> from ncdata.utils import index_by_dimensions | ||
|
|
||
| .. doctest:: | ||
|
|
||
| >>> data = NcData( | ||
| ... dimensions=[NcDimension("y", 4), NcDimension("x", 10)], | ||
| ... variables=[NcVariable( | ||
| ... "v1", dimensions=["y", "x"], | ||
| ... data=np.arange(40).reshape((4, 10)) | ||
| ... )] | ||
| ... ) | ||
|
|
||
| .. doctest:: | ||
|
|
||
| >>> subdata = index_by_dimensions(data, y=2, x=slice(None, 4)) | ||
| >>> print(subdata) | ||
| <NcData: <'no-name'> | ||
| dimensions: | ||
| x = 4 | ||
| <BLANKLINE> | ||
| variables: | ||
| <NcVariable(int64): v1(x)> | ||
| > | ||
| >>> print(subdata.variables["v1"].data) | ||
| [20 21 22 23] | ||
|
|
||
| Slicing syntax | ||
| ^^^^^^^^^^^^^^ | ||
| The :class:`~ncdata.utils.Slicer` class is provided to enable the same operation to be | ||
| expressed using multi-dimensional slicing syntax. | ||
|
|
||
| So for example, the above is more neatly expressed like this ... | ||
|
|
||
| .. testsetup:: | ||
|
|
||
| >>> from ncdata.utils import Slicer | ||
|
|
||
| .. doctest:: | ||
|
|
||
| >>> data_slicer = Slicer(data, ["y", "x"]) | ||
| >>> subdata2 = data_slicer[2, :4] | ||
|
|
||
| .. doctest:: | ||
|
|
||
| >>> dataset_differences(subdata, subdata2) == [] | ||
| True | ||
|
|
||
|
|
||
| Consistency Checking | ||
| -------------------- | ||
| The :func:`~ncdata.utils.save_errors` function provides a general | ||
| correctness-and-consistency check. | ||
|
|
||
| For example: | ||
|
|
||
| .. testsetup:: | ||
|
|
||
| >>> from ncdata.utils import save_errors | ||
|
|
||
| .. doctest:: | ||
|
|
||
| >>> data_bad = data.copy() | ||
| >>> array = data_bad.variables["v1"].data | ||
| >>> data_bad.variables["v1"].data = array[:2] | ||
| >>> data_bad.variables.add(NcVariable("q", data={"x": 4})) | ||
|
|
||
| .. doctest:: | ||
|
|
||
| >>> for msg in save_errors(data_bad): | ||
| ... print(msg) | ||
| Variable 'v1' data shape = (2, 10), does not match that of its dimensions = (4, 10). | ||
| Variable 'q' has a dtype which cannot be saved to netcdf : dtype('O'). | ||
|
|
||
|
|
||
| See : :ref:`correctness-checks` | ||
|
|
||
|
|
||
| Data Copying | ||
| ------------ | ||
| The :func:`~ncdata.utils.ncdata_copy` makes structural copies of datasets. | ||
| However, this can be easily be accessed as :meth:`ncdata.NcData.copy`, which is the same | ||
| operation. | ||
|
|
||
| See: :ref:`copy_notes` |
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
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.