Python API¶
-
recursive_diff.
recursive_diff
(lhs, rhs, *, rel_tol=1e-09, abs_tol=0.0, brief_dims=())¶ Compare two objects and yield all differences. The two objects must any of:
- basic types (str, int, float, bool)
- basic collections (list, tuple, dict, set, frozenset)
- numpy scalar types, e.g.
numpy.float64
numpy.ndarray
pandas.Series
pandas.DataFrame
pandas.Index
xarray.DataArray
xarray.Dataset
- any recursive combination of the above
- any other object (compared with ==)
Special treatment is reserved to different types:
- floats and ints are compared with tolerance, using
math.isclose()
- NaN equals to NaN
- bools are only equal to other bools
- numpy arrays are compared elementwise and with tolerance, also testing the dtype
- pandas and xarray objects are compared elementwise, with tolerance, and without order. Duplicate indices are not supported.
- xarray dimensions and variables are compared without order
- collections (list, tuple, dict, set, frozenset) are recursively descended into
- generic/unknown objects are compared with ==
Custom classes can be registered to benefit from the above behaviour; see
cast()
.Parameters: - lhs – left-hand-side data structure
- rhs – right-hand-side data structure
- rel_tol (float) – relative tolerance when comparing numbers. Applies to floats, integers, and all numpy-based data.
- abs_tol (float) – absolute tolerance when comparing numbers. Applies to floats, integers, and all numpy-based data.
- brief_dims –
One of:
- sequence of strings representing xarray dimensions. If one or more differences are found along one of these dimensions, only one message will be reported, stating the differences count.
- ”all”, to produce one line only for every xarray variable that differs
Omit to output a line for every single different cell.
Yields strings containing difference messages, prepended by the path to the point that differs.
-
recursive_diff.
recursive_eq
(lhs, rhs, rel_tol=1e-09, abs_tol=0.0)¶ Wrapper around
recursive_diff()
. Print out all differences and finally assert that there are none.
-
recursive_diff.
cast
(obj, brief_dims)¶ Helper function of
recursive_diff()
.Cast objects into simpler object types:
- Cast tuple to list
- Cast frozenset to set
- Cast all numpy-based objects to
xarray.DataArray
, as it is the most generic format that can describe all use cases:numpy.ndarray
pandas.Series
pandas.DataFrame
pandas.Index
, exceptpandas.RangeIndex
, which is instead returned unalteredxarray.Dataset
The data will be potentially wrapped by a dict to hold the various attributes and marked so that it doesn’t trigger an infinite recursion.
- Do nothing for any other object types.
Parameters: - obj – complex object that must be simplified
- brief_dims (tuple) – sequence of xarray dimensions that must be compacted.
See documentation on
recursive_diff()
.
Returns: simpler object to compare