Routines to perform central moments reduction (reduction)

Routines to perform central moments reduction (reduction)#

Functions:

reduce_vals(x, *y, mom[, axis, dim, weight, ...])

Reduce values to central (co)moments.

reduce_data(data, *[, axis, dim, mom_ndim, ...])

Reduce central moments array along axis.

cmomy.reduction.reduce_vals(x, *y, mom, axis=MISSING, dim=MISSING, weight=None, mom_dims=None, mom_params=None, out=None, dtype=None, casting='same_kind', order=None, parallel=None, keep_attrs=None, apply_ufunc_kwargs=None)[source]#

Reduce values to central (co)moments.

Parameters:
  • x (array-like or DataArray or Dataset) – Values to reduce.

  • *y (array-like or DataArray or Dataset) – Additional values (needed if len(mom)==2). y has same type restrictions and broadcasting rules as weight.

  • mom (int or tuple of int) – Order or moments. If integer or length one tuple, then moments are for a single variable. If length 2 tuple, then comoments of two variables

  • axis (int) – Axis to reduce/sample along.

  • dim (hashable) – Dimension to reduce/sample along.

  • weight (array-like or DataArray or Dataset) –

    Optional weight. The type of weight must be “less than” the type of x.

    In the case that weight is array-like, it must broadcast to x using usual broadcasting rules (see numpy.broadcast_to()), with the following exceptions: If weight is a 1d array of length x.shape[axis]], it will be formatted to broadcast along the other dimensions of x. For example, if x has shape (10, 2, 3) and weight has shape (10,), then weight will be converted to the broadcastable shape (10, 1, 1). If weight is a scalar, it will be broadcast to x.shape.

  • mom_dims (hashable or tuple of hashable) – Name of moment dimensions. Defaults to ("mom_0",) for mom_ndim==1 and (mom_0, mom_1) for mom_ndim==2

  • mom_params (cmomy.MomParams or cmomy.MomParamsDict or dict, optional) – Moment parameters. You can set moment parameters axes and dims using this option. For example, passing mom_params={"dim": ("a", "b")} is equivalent to passing mom_dims=("a", "b"). You can also pass as a cmomy.MomParams object with mom_params=cmomy.MomParams(dims=("a", "b")).

  • out (ndarray) – Optional output array. If specified, output will be a reference to this array. Note that if the output if method returns a Dataset, then this option is ignored.

  • dtype (dtype) – Optional dtype for output data.

  • casting ({'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional) –

    Controls what kind of data casting may occur.

    • ’no’ means the data types should not be cast at all.

    • ’equiv’ means only byte-order changes are allowed.

    • ’safe’ means only casts which can preserve values are allowed.

    • ’same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed.

    • ’unsafe’ (default) means any data conversions may be done.

  • order ({"C", "F"}, optional) – Order argument. See numpy.zeros().

  • parallel (bool, optional) – If True, use parallel numba numba.njit or numba.guvectorized code if possible. If None, use a heuristic to determine if should attempt to use parallel method.

  • keep_attrs ({"drop", "identical", "no_conflicts", "drop_conflicts", "override"} or bool, optional) –

    • ‘drop’ or False: empty attrs on returned xarray object.

    • ’identical’: all attrs must be the same on every object.

    • ’no_conflicts’: attrs from all objects are combined, any that have the same name must also have the same value.

    • ’drop_conflicts’: attrs from all objects are combined, any that have the same name but different values are dropped.

    • ’override’ or True: skip comparing and copy attrs from the first object to the result.

  • apply_ufunc_kwargs (dict-like) – Extra parameters to xarray.apply_ufunc(). One useful option is on_missing_core_dim, which can take the value "copy" (the default), "raise", or "drop" and controls what to do with variables of a Dataset missing core dimensions. Other options are join, dataset_join, dataset_fill_value, and dask_gufunc_kwargs. Unlisted options are handled internally.

Returns:

out (ndarray or DataArray or Dataset) – Central moments array of same type as x. out.shape = shape + (mom0, ...) where shape = np.broadcast_shapes(*(a.shape for a in (x_, *y_, weight_)))[:-1] and x_, y_ and weight_ are the input arrays with axis moved to the last axis.

cmomy.reduction.reduce_data(data, *, axis=MISSING, dim=MISSING, mom_ndim=None, mom_dims=None, mom_axes=None, mom_params=None, out=None, dtype=None, casting='same_kind', order=None, keepdims=False, parallel=None, axes_to_end=False, use_map=None, keep_attrs=None, apply_ufunc_kwargs=None)[source]#

Reduce central moments array along axis.

Parameters:
  • data (ndarray or DataArray or Dataset) – Moments array(s). It is assumed moment dimensions are last.

  • axis (int, tuple of int, optional) – Axis(es) to reduce/sample along. Note that negative values are relative to data.ndim - mom_ndim. It is assumed that the last dimensions are for moments. For example, if data.shape == (1,2,3) with mom_ndim=1, axis = -1 `` would be equivalent to ``axis = 1. Defaults to axis=-1. To reduce over multiple dimensions, specify axis = (axis_0, axis_1, …). Passing axis=None reduces over all value dimensions (i.e., all dimensions excluding moment dimensions).

  • dim (hashable or iterable of hashable) – Dimension(s) to reduce along. Value of None implies reduce over all “value” dimensions.

  • mom_ndim ({1, 2}, optional) – Value indicates if moments (mom_ndim = 1) or comoments (mom_ndim=2). If not specified and data is an xarray object attempt to infer mom_ndim from mom_dims. Otherwise, default to mom_ndim = 1.

  • mom_axes (int or tuple of int, optional) – Location of the moment dimensions. Default to (-mom_ndim, -mom_ndim+1, ...). If specified and mom_ndim is None, set mom_ndim to len(mom_axes). Note that if mom_axes is specified, negative values are relative to the end of the array. This is also the case for axes if mom_axes is specified.

  • mom_dims (hashable or tuple of hashable) – Name of moment dimensions. If specified, infer mom_ndim from mom_dims. If also pass mom_ndim, check that mom_dims is consistent with mom_dims. If not specified, defaults to data.dims[-mom_ndim:]. This is primarily used if data is a Dataset, or if mom_dims are not the last dimensions.

  • mom_params (cmomy.MomParams or cmomy.MomParamsDict or dict, optional) – Moment parameters. You can set moment parameters axes and dims using this option. For example, passing mom_params={"dim": ("a", "b")} is equivalent to passing mom_dims=("a", "b"). You can also pass as a cmomy.MomParams object with mom_params=cmomy.MomParams(dims=("a", "b")).

  • out (ndarray) – Optional output array. If specified, output will be a reference to this array. Note that if the output if method returns a Dataset, then this option is ignored.

  • dtype (dtype) – Optional dtype for output data.

  • casting ({'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional) –

    Controls what kind of data casting may occur.

    • ’no’ means the data types should not be cast at all.

    • ’equiv’ means only byte-order changes are allowed.

    • ’safe’ means only casts which can preserve values are allowed.

    • ’same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed.

    • ’unsafe’ (default) means any data conversions may be done.

  • order ({"C", "F", "A", "K"}, optional) – Order argument. See numpy.asarray().

  • keepdims (bool) – If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • parallel (bool, optional) – If True, use parallel numba numba.njit or numba.guvectorized code if possible. If None, use a heuristic to determine if should attempt to use parallel method.

  • use_map (bool, optional) – If not False, use data.map if data is a Dataset and dim is not a single scalar. This will properly handle cases where dim is None or has multiple dimensions. Note that with this option, variables that do not contain dim or mom_dims will be left in the result unchanged.

  • keep_attrs ({"drop", "identical", "no_conflicts", "drop_conflicts", "override"} or bool, optional) –

    • ‘drop’ or False: empty attrs on returned xarray object.

    • ’identical’: all attrs must be the same on every object.

    • ’no_conflicts’: attrs from all objects are combined, any that have the same name must also have the same value.

    • ’drop_conflicts’: attrs from all objects are combined, any that have the same name but different values are dropped.

    • ’override’ or True: skip comparing and copy attrs from the first object to the result.

  • apply_ufunc_kwargs (dict-like) – Extra parameters to xarray.apply_ufunc(). One useful option is on_missing_core_dim, which can take the value "copy" (the default), "raise", or "drop" and controls what to do with variables of a Dataset missing core dimensions. Other options are join, dataset_join, dataset_fill_value, and dask_gufunc_kwargs. Unlisted options are handled internally.

Returns:

out (ndarray or DataArray or Dataset) – Reduced data array with shape data.shape with axis removed. Same type as input data.