Skip to content

Commit 2a1a1b0

Browse files
committed
More improvements.
1 parent a0cf155 commit 2a1a1b0

File tree

6 files changed

+92
-35
lines changed

6 files changed

+92
-35
lines changed

docs/changelog_fragments/68.feat.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ The :class:`ncdata.NcData` objects can be indexed with the ``[]`` operation, or
33
specifed dimensions with the :meth:`~ncdata.NcData.slicer` method.
44
This is based on the new :meth:`~ncdata.utils.index_by_dimensions()` utility method
55
and :class:`~ncdata.utils.Slicer` class.
6-
See: :ref:`indexing_overview`
6+
See: :ref:`utils_indexing`

docs/details/developer_notes.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ For a full docs-build:
4646

4747
.. note::
4848

49-
* the above is just for *local testing*, when required.
49+
* the above is just for **local testing**, when required.
5050
* For PRs (and releases), we also provide *automatic* builds on GitHub,
5151
via ReadTheDocs_.
5252

@@ -56,9 +56,9 @@ Release actions
5656

5757
#. Update the :ref:`change-log page <change_log>` in the details section
5858

59-
#. ensure all major changes + PRs are referenced in the :ref:`change_notes` section.
59+
#. start with ``$ towncrier build``
6060

61-
* The starting point for this is now just : ``$ towncrier build``.
61+
#. ensure all major changes + PRs are referenced in the :ref:`change_notes` section.
6262

6363
#. update the "latest version" stated in the :ref:`development_status` section
6464

docs/userdocs/user_guide/common_operations.rst

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ Example :
5555

5656
>>> dataset.variables["x"].avals["units"] = "m s-1"
5757

58+
59+
There is also an :meth:`~ncdata.NameMap.addall` method, which adds multiple content
60+
objects in one operation.
61+
62+
.. doctest:: python
63+
64+
>>> vars = [NcVariable(name) for name in ("a", "b", "c")]
65+
>>> dataset.variables.addall(vars)
66+
>>> list(dataset.variables)
67+
['x', 'a', 'b', 'c']
68+
5869
.. _operations_rename:
5970

6071
Rename
@@ -69,6 +80,18 @@ Example :
6980

7081
>>> dataset.variables.rename("x", "y")
7182

83+
result:
84+
85+
.. doctest:: python
86+
87+
>>> print(dataset.variables.get("x"))
88+
None
89+
>>> print(dataset.variables.get("y"))
90+
<NcVariable(<no-dtype>): y()
91+
y:units = 'm s-1'
92+
>
93+
94+
7295
.. warning::
7396
Renaming a dimension will not rename references to it (i.e. in variables), which
7497
obviously may cause problems.
@@ -125,17 +148,29 @@ Equality Testing
125148
----------------
126149
We implement equality operations ``==`` / ``!=`` for all the core data objects.
127150

151+
.. doctest::
152+
153+
>>> vA = dataset.variables["a"]
154+
>>> vB = dataset.variables["b"]
155+
>>> vA == vB
156+
False
157+
158+
.. doctest::
159+
160+
>>> dataset == dataset.copy()
161+
True
162+
128163
.. warning::
129-
The equality testing actually calls the :func:`ncdata.utils.dataset_differences` and
130-
:func:`ncdata.utils.variable_differences` utility functions.
164+
Equality testing for :class:`~ncdata.NcData` and :class:`~ncdata.NcVariable` actually
165+
calls the :func:`ncdata.utils.dataset_differences` and
166+
:func:`ncdata.utils.variable_differences` utilities.
131167

132168
This can be very costly if it needs to compare large data arrays.
133169

134-
If you need to avoid comparing large (and possibly lazy) arrays then you should use the
170+
If you need to avoid comparing large (and possibly lazy) arrays then you can use the
135171
:func:`ncdata.utils.dataset_differences` and
136-
:func:`ncdata.utils.variable_differences` utility functions directly.
137-
These enable you to use the provided tolerance options, such as ignoring differences in
138-
data content, or accepting that attributes are present in a different order.
172+
:func:`ncdata.utils.variable_differences` utility functions directly instead.
173+
These provide a ``check_var_data=False`` option, to ignore differences in data content.
139174

140175
See: :ref:`utils_equality`
141176

docs/userdocs/user_guide/data_objects.rst

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ However, for most operations on attributes, it is much easier to use the ``.aval
186186
property instead. This accesses *the same attributes*, but in the form of a simple
187187
"name: value" dictionary.
188188

189-
Thus for example, to fetch an attribute you would usually write just :
189+
Get attribute value
190+
^^^^^^^^^^^^^^^^^^^
191+
For example, to fetch an attribute you would usually write just :
190192

191193
.. testsetup::
192194

@@ -205,23 +207,15 @@ and **not** :
205207

206208
.. doctest:: python
207209

208-
>>> # WRONG: this reads an NcAttribute, not its value
210+
>>> # WRONG: this get the NcAttribute object, not its value
209211
>>> unit = dataset.variables["x"].attributes["units"]
210212

211-
or:
212-
213-
.. doctest:: python
214-
215-
>>> # WRONG: this gets NcAttribute.value as a character array, not a string
213+
>>> # WRONG: this returns a character array, not a string
216214
>>> unit = dataset.variables["x"].attributes["units"].value
217215

218-
or even (which is at least correct):
219-
220-
.. doctest:: python
221-
222-
>>> unit = dataset.variables["x"].attributes["units"].as_python_value()
223-
224216

217+
Set attribute value
218+
^^^^^^^^^^^^^^^^^^^
225219
Likewise, to **set** a value, you would normally just
226220

227221
.. doctest:: python
@@ -236,9 +230,11 @@ and **not**
236230
>>> dataset.variables["x"].attributes["units"].value = "K"
237231

238232

239-
Note also, that as the ``.avals`` is a dictionary, you can use standard dictionary
240-
methods such as ``update`` and ``get`` to perform other operations in a relatively
241-
natural, Pythonic way.
233+
``.avals`` as a dictionary
234+
^^^^^^^^^^^^^^^^^^^^^^^^^^
235+
Note also, that as ``.avals`` is a dictionary, you can use standard dictionary
236+
methods such as ``pop``, ``update`` and ``get`` to perform other operations in a
237+
relatively natural, Pythonic way.
242238

243239
.. doctest:: python
244240

@@ -247,6 +243,12 @@ natural, Pythonic way.
247243

248244
>>> dataset.attributes.update({"experiment": "A407", "expt_run": 704})
249245

246+
.. note::
247+
The new ``.avals`` property effectively replaces the old
248+
:meth:`~ncdata.NcData.get_attrval` and :meth:`~ncdata.NcData.set_attrval` methods,
249+
which are now deprecated and will eventually be removed.
250+
251+
250252
.. _data-constructors:
251253

252254
Core Object Constructors

docs/userdocs/user_guide/utilities.rst

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Dataset Equality Testing
1818
------------------------
1919
The functions :func:`~ncdata.utils.dataset_differences` and
2020
:func:`~ncdata.utils.variable_differences` produce a list of messages detailing all the
21-
ways in which two datasets are different.
21+
ways in which two datasets or variables are different.
2222

2323
For Example:
2424
^^^^^^^^^^^^
@@ -51,19 +51,22 @@ For Example:
5151
Dataset "x" dimension has different "unlimited" status : False != True
5252
Dataset variable "vx" shapes differ : (5,) != (2,)
5353

54-
.. note::
55-
To compare isolated variables, the subsidiary routine
56-
:func:`~ncdata.utils.variable_differences` is also provided.
54+
For a short-form test that two things are the same, you can just check that the
55+
results ``== []``.
56+
57+
By default, these functions compare **everything** about the two arguments.
58+
However, they also have multiple keywords which allow certain *types* of differences to
59+
be ignored, E.G. ``check_dims_order=False``, ``check_var_data=False``.
5760

5861
.. note::
5962
The ``==`` and ``!=`` operations on :class:`ncdata.NcData` and
60-
:class:`ncdata.NcVariable` simply call these utility functions to see if there are
61-
any differences.
63+
:class:`ncdata.NcVariable` use these utility functions to check for differences.
6264

6365
.. warning::
64-
As they lack a keyword interface, these operations have no tolerance options
65-
and check absolutely everything. This includes full data-array comparisons,
66-
which could be very costly in time or space if data arrays are large.
66+
As they lack a keyword interface, these operations provide no tolerance options,
67+
so they always check absolutely everything. Especially, they perform **full
68+
data-array comparisons**, which can have very high performance costs if data
69+
arrays are large.
6770

6871
.. _utils_indexing:
6972

lib/ncdata/utils/_compare_nc_datasets.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ def dataset_differences(
9696
A list of "error" strings, describing differences between the inputs.
9797
If empty, no differences were found.
9898
99+
Examples
100+
--------
101+
.. doctest::
102+
103+
>>> data = NcData(
104+
... name="a",
105+
... variables=[NcVariable("b", data=[1, 2, 3, 4])],
106+
... attributes={"a1": 4}
107+
... )
108+
>>> data2 = data.copy()
109+
>>> data2.avals.update({"a1":3, "v":7})
110+
>>> data2.variables["b"].data = np.array([1, 7, 3, 99]) # must be an array!
111+
>>> print('\n'.join(dataset_differences(data, data2)))
112+
Dataset attribute lists do not match: ['a1'] != ['a1', 'v']
113+
Dataset "a1" attribute values differ : 4 != 3
114+
Dataset variable "b" data contents differ, at 2 points: @INDICES[(1,), (3,)] : LHS=[2, 4], RHS=[7, 99]
115+
99116
See Also
100117
--------
101118
:func:`~ncdata.utils.variable_differences`

0 commit comments

Comments
 (0)