cmomy.CentralMomentsData#
- class cmomy.CentralMomentsData(obj, *, mom_ndim=None, mom_axes=None, mom_dims=None, mom_params=None, fastpath=False)[source]#
Bases:
CentralMomentsABC
[DataT
,MomParamsXArray
]Central moments wrapper of
DataArray
orDataset
objects.- Parameters:
mom_ndim (
{1, 2}
, optional) – Value indicates if moments (mom_ndim = 1
) or comoments (mom_ndim=2
). If not specified and data is anxarray
object attempt to infermom_ndim
frommom_dims
. Otherwise, default tomom_ndim = 1
.mom_axes (
int
ortuple
ofint
, optional) – Location of the moment dimensions. Default to(-mom_ndim, -mom_ndim+1, ...)
. If specified andmom_ndim
is None, setmom_ndim
tolen(mom_axes)
. Note that ifmom_axes
is specified, negative values are relative to the end of the array. This is also the case foraxes
ifmom_axes
is specified.mom_dims (hashable or
tuple
of hashable) – Name of moment dimensions. If specified, infermom_ndim
frommom_dims
. If also passmom_ndim
, check thatmom_dims
is consistent withmom_dims
. If not specified, defaults todata.dims[-mom_ndim:]
. This is primarily used ifdata
is aDataset
, or ifmom_dims
are not the last dimensions.mom_params (
cmomy.MomParams
orcmomy.MomParamsDict
ordict
, optional) – Moment parameters. You can set moment parametersaxes
anddims
using this option. For example, passingmom_params={"dim": ("a", "b")}
is equivalent to passingmom_dims=("a", "b")
. You can also pass as acmomy.MomParams
object withmom_params=cmomy.MomParams(dims=("a", "b"))
.fastpath (
bool
) – For internal use.
Notes
Base data has the form
\[\begin{split}{\rm data}[..., i, j] = \begin{cases} \text{weight} & i = j = 0 \\ \langle x \rangle & i = 1, j = 0 \\ \langle (x - \langle x \rangle^i) (y - \langle y \rangle^j) \rangle & i + j > 0 \end{cases}\end{split}\]Methods
as_dict
()Create a wrapped dictionary view of dataset
as_numpy
()Coerces wrapped data and coordinates into numpy arrays.
assign
([variables])Assign new variable to and return new object.
assign_attrs
(*args, **kwargs)Assign attributes to data and return new object.
assign_coords
([coords])Assign coordinates to data and return new object.
assign_moment
([moment, squeeze, copy, ...])Create object with update weight, average, etc.
astype
(dtype, *[, order, casting, subok, copy])Underlying data cast to specified type
chunk
(*args, **kwargs)Interface to
xarray.DataArray.chunk()
andxarray.Dataset.chunk()
cmom
()Central moments.
compute
(**kwargs)Interface to
xarray.DataArray.compute()
andxarray.Dataset.compute()
copy
([deep])Create a new object with copy of data.
cov
()Covariance (or variance if
mom_ndim==1
).cumulative
(*[, axis, dim, out, dtype, ...])Convert to cumulative moments.
drop_vars
(names, *[, errors, _reorder, ...])Interface to
xarray.DataArray.drop_vars()
isel
([indexers, drop, missing_dims, ...])Select subset of data by position.
items
()Dict like items method.
iter
()Need this for proper typing with mypy...
jackknife_and_reduce
(*[, axis, dim, ...])Jackknife resample and reduce
keys
()Dict like keys view
mean
([squeeze])Mean (first moment).
moments_to_comoments
(*, mom[, mom_dims_out, ...])Convert moments (mom_ndim=1) to comoments (mom_ndim=2).
moveaxis
([axis, dest, dim, dest_dim, ...])Generalized moveaxis
new_like
([obj, copy, deep, verify, dtype, ...])Create new object like self, with new data.
pipe
(func_or_method, *args[, _reorder, ...])Apply func_or_method to underlying data and wrap results in new wrapped object.
push_data
(data, *[, scale, casting, ...])Push data object to moments.
push_datas
(datas, *[, axis, dim, mom_axes, ...])Push and reduce multiple average central moments.
push_val
(x, *y[, weight, casting, parallel, ...])Push single sample to central moments.
push_vals
(x, *y[, weight, axis, dim, ...])Push multiple samples to central moments.
reduce
([dim, by, block, use_map, group_dim, ...])Create new object reduce along axis.
rename
([new_name_or_name_dict])Rename object.
resample_and_reduce
(*, sampler[, axis, dim, ...])Bootstrap resample and reduce.
reset_index
(dims_or_levels[, drop, ...])Interface to
xarray.DataArray.reset_index()
.rmom
()Raw moments.
sel
([indexers, method, tolerance, drop, ...])Select subset of data.
select_moment
(name, *[, squeeze, ...])Select specific moments.
set_index
([indexes, append, _reorder, ...])Interface to
xarray.DataArray.set_index()
stack
([dim, _reorder, _copy, _verify, _fastpath])Stack dimensions.
std
([squeeze])Standard deviation (ddof=0).
swap_dims
([dims_dict, _reorder, _copy, ...])Interface to
xarray.DataArray.swap_dims()
.to_array
([copy])Convert to
cmomy.CentralMomentsArray
object if possible.to_c
([copy])Alias to
to_array()
.to_dataarray
([dim, name])Convert wrapped Dataset to DataArray
to_dataset
([dim, name, promote_attrs])Convert to Dataset or wrapped Dataset.
to_numpy
()Coerce wrapped data to
ndarray
if possible.to_raw
(*[, weight])Raw moments accumulation array.
transpose
(*dims[, transpose_coords, ...])Transpose dimensions of data.
unstack
([dim, fill_value, sparse, _reorder, ...])Unstack dimensions.
values
()Dict like values view
var
([squeeze])Variance (second central moment).
weight
()Weight data.
zeros
(*, mom[, val_shape, dtype, order, ...])Create a new object with specified sizes.
Create new empty object like self.
Attributes
Attributes of values.
Coordinates of values.
Dimension names.
DType of wrapped object.
Moments tuple.
Moments dimension names.
Number of moment dimensions.
Moments parameters object.
Shape of moments dimensions.
Name of values.
Total number of dimensions.
Underlying object.
Shape of wrapped object.
Sizes of values.
Names of value (i.e., not moment) dimensions.
Number of values dimensions (
ndim - mom_ndim
).Shape of values dimensions.
Dunder Methods
__add__
(other)Add objects to new object.
__getitem__
(key)Access variables or coordinates of this wrapped dataset as a DataArray or a subset of variables or a indexed dataset.
__iadd__
(other)Self adder.
__imul__
(scale)Inplace multiply.
__isub__
(other)Inplace subtraction.
__mul__
(scale)New object with weight scaled by scale.
__sub__
(other)Subtract objects.
- property mom_dims#
Moments dimension names.
- as_dict()[source]#
Create a wrapped dictionary view of dataset
Note that this dict will only contain variables (DataArrays) which contain
mom_dims
. That is, non central moment arrays will be dropped.
- items()[source]#
Dict like items method.
Note that only DataArrays that contain
mom_dims
are returned. To iterate over all items, useself.obj.items()
- __getitem__(key)[source]#
Access variables or coordinates of this wrapped dataset as a DataArray or a subset of variables or a indexed dataset.
For wrapped
Dataset
, return a subset of variables.The selection is wrapped with
CentralMomentsData
.
- new_like(obj=None, *, copy=None, deep=True, verify=False, dtype=None, fastpath=False)[source]#
Create new object like self, with new data.
- Parameters:
obj (array-like,
DataArray
,Dataset
, or mapping) – Object for new object to wrap. Passed to self.obj.copy.copy (
bool
, optional) – If True, copy the data. If None or False, attempt to use view. Note thatFalse
values will be converted toNone
for numpy versions>2.0
. This will be changed to reflect the new behavior of thecopy
parameter tonumpy.array()
when the minimum numpy version>2.0
.verify (
bool
) – If True, make sure data is c-contiguous.fastpath (
bool
) – Internal variable.
- Returns:
CentralMomentsData
– New CentralMomentsData object with data set to zero.
See also
- copy(deep=True)[source]#
Create a new object with copy of data.
- Parameters:
deep (
bool
) – Parameters toxarray.DataArray.copy()
or xarray.Dataset.copy- Returns:
output (
CentralMomentsData
) – Same type as calling class. Object with same attributes as caller, but with new underlying data.
See also
- astype(dtype, *, order=None, casting=None, subok=None, copy=False)[source]#
Underlying data cast to specified type
- Parameters:
dtype (
dtype
or mapping of hashable todtype
) – Ifself.obj
is aDataset
, passing a mapping will update only certain variables.order (
{"C", "F", "A", "K"}
, optional) – Order argument. Seenumpy.asarray()
.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.
subok (
bool
, optional) – If True, then sub-classes will be passed-through, otherwise the returned array will be forced to be a base-class array.copy (
bool
, optional) – By default, astype always returns a newly allocated array. If this is set to False and the dtype requirement is satisfied, the input array is returned instead of a copy.
See also
Notes
Only
numpy.float32
andnumpy.float64
dtype are supported.
- push_data(data, *, scale=None, casting='same_kind', parallel=False, keep_attrs=True, apply_ufunc_kwargs=None)[source]#
Push data object to moments.
- Parameters:
data (array-like or
DataArray
orDataset
) – Accumulation array conformable toself.obj
.scale (array-like) – Scaling to apply to weights of
data
. Optional.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.
parallel (
bool
, optional) – IfTrue
, use parallel numbanumba.njit
ornumba.guvectorized
code if possible. IfNone
, use a heuristic to determine if should attempt to use parallel method.keep_attrs (
{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}
orbool
, 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 ison_missing_core_dim
, which can take the value"copy"
(the default),"raise"
, or"drop"
and controls what to do with variables of aDataset
missing core dimensions. Other options arejoin
,dataset_join
,dataset_fill_value
, anddask_gufunc_kwargs
. Unlisted options are handled internally.
- Returns:
output (
CentralMomentsData
) – Same object with pushed data.
- push_datas(datas, *, axis=MISSING, dim=MISSING, mom_axes=None, casting='same_kind', parallel=None, keep_attrs=True, apply_ufunc_kwargs=None)[source]#
Push and reduce multiple average central moments.
- Parameters:
datas (array-like or
DataArray
orDataset
) – Collection of accumulation arrays to push ontoself
.axis (
int
, optional) – Axis to reduce/sample along. Note that negative values are relative todata.ndim - mom_ndim
. It is assumed that the last dimensions are for moments. For example, ifdata.shape == (1,2,3)
withmom_ndim=1
,axis = -1 `` would be equivalent to ``axis = 1
. Defaults toaxis=-1
.dim (hashable) – Dimension to reduce/sample along.
mom_axes (
int
ortuple
ofint
, optional) – Location of the moment dimensions. Default to(-mom_ndim, -mom_ndim+1, ...)
. If specified andmom_ndim
is None, setmom_ndim
tolen(mom_axes)
. Note that ifmom_axes
is specified, negative values are relative to the end of the array. This is also the case foraxes
ifmom_axes
is specified.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.
parallel (
bool
, optional) – IfTrue
, use parallel numbanumba.njit
ornumba.guvectorized
code if possible. IfNone
, use a heuristic to determine if should attempt to use parallel method.keep_attrs (
{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}
orbool
, 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 ison_missing_core_dim
, which can take the value"copy"
(the default),"raise"
, or"drop"
and controls what to do with variables of aDataset
missing core dimensions. Other options arejoin
,dataset_join
,dataset_fill_value
, anddask_gufunc_kwargs
. Unlisted options are handled internally.
- Returns:
output (
CentralMomentsData
) – Same object with pushed data.
- push_val(x, *y, weight=None, casting='same_kind', parallel=False, keep_attrs=True, apply_ufunc_kwargs=None)[source]#
Push single sample to central moments.
- Parameters:
x (array-like or
DataArray
orDataset
) – Values to push ontoself
.*y (array-like or
DataArray
orDataset
) – Additional values (needed ifmom_ndim > 1
)weight (
int
,float
, array-like orDataArray
orDataset
) – Weight of each sample. If scalar, broadcast w.shape to x0.shape.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.
parallel (
bool
, optional) – IfTrue
, use parallel numbanumba.njit
ornumba.guvectorized
code if possible. IfNone
, use a heuristic to determine if should attempt to use parallel method.keep_attrs (
{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}
orbool
, 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 ison_missing_core_dim
, which can take the value"copy"
(the default),"raise"
, or"drop"
and controls what to do with variables of aDataset
missing core dimensions. Other options arejoin
,dataset_join
,dataset_fill_value
, anddask_gufunc_kwargs
. Unlisted options are handled internally.
- Returns:
output (
CentralMomentsData
) – Same object with pushed data.
Notes
Array x0 should have same shape as self.val_shape.
- push_vals(x, *y, weight=None, axis=MISSING, dim=MISSING, casting='same_kind', parallel=None, keep_attrs=True, apply_ufunc_kwargs=None)[source]#
Push multiple samples to central moments.
- Parameters:
x (array) – Value to reduce.
*y (array-like) – Additional array (if
self.mom_ndim == 2
).weight (
int
,float
, array-like, optional) – Weight of each sample. If scalar, broadcast to x0.shapeaxis (
int
) – Axis to reduce/sample along.dim (hashable) – Dimension to reduce/sample along.
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.
parallel (
bool
, optional) – IfTrue
, use parallel numbanumba.njit
ornumba.guvectorized
code if possible. IfNone
, use a heuristic to determine if should attempt to use parallel method.keep_attrs (
{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}
orbool
, 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 ison_missing_core_dim
, which can take the value"copy"
(the default),"raise"
, or"drop"
and controls what to do with variables of aDataset
missing core dimensions. Other options arejoin
,dataset_join
,dataset_fill_value
, anddask_gufunc_kwargs
. Unlisted options are handled internally.
- Returns:
output (
CentralMomentsData
) – Same object with pushed data.
- reduce(dim=MISSING, *, by=None, block=None, use_map=None, group_dim=None, groups=None, axis=MISSING, out=None, dtype=None, casting='same_kind', order=None, keepdims=False, parallel=None, axes_to_end=False, coords_policy='first', keep_attrs=None, apply_ufunc_kwargs=None)[source]#
Create new object reduce along axis.
- Parameters:
dim (hashable) – Dimension to reduce/sample along.
by (
ndarray
orDataArray
orstr
or iterable ofstr
, optional) – IfNone
, reduce over entiredim
. Otherwise, reduce by group. Ifndarray
, use the unique values. IfDataArray
, use unique values and rename dimensionby.name
. If str or Iterable of str, Create grouper from these named coordinates.block (
int
, optional) – If specified, perform block average reduction with blocks of this size. Negative values are transformed to all data.use_map (
bool
, optional) – Seereduction.reduce_data()
group_dim (
str
, optional) – Name of the output group dimension. Defaults todim
.groups (sequence, optional) – Sequence of length
by.max() + 1
to assign as coordinates forgroup_dim
.axis (
int
, optional) – Axis to reduce/sample along. Note that negative values are relative todata.ndim - mom_ndim
. It is assumed that the last dimensions are for moments. For example, ifdata.shape == (1,2,3)
withmom_ndim=1
,axis = -1 `` would be equivalent to ``axis = 1
. Defaults toaxis=-1
.out (
ndarray
) – Optional output array. If specified, output will be a reference to this array. Note that if the output if method returns aDataset
, then this option is ignored.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. Seenumpy.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) – IfTrue
, use parallel numbanumba.njit
ornumba.guvectorized
code if possible. IfNone
, use a heuristic to determine if should attempt to use parallel method.axes_to_end (
bool
) – IfTrue
, place sampled dimension (if exists in output) and moment dimensions at end of output. Otherwise, place sampled dimension (if exists in output) at same position as inputaxis
and moment dimensions at same position as input (if input does not contain moment dimensions, place them at end of array).coords_policy (
{'first', 'last', 'group', None}
) –Policy for handling coordinates along
dim
ifby
is specified forDataArray
data. If no coordinates do nothing, otherwise use:’first’: select first value of coordinate for each block.
’last’: select last value of coordinate for each block.
’group’: Assign unique groups from
group_idx
todim
None: drop any coordinates.
Note that if
coords_policy
is one offirst
orlast
, parametergroups
will be ignored.keep_attrs (
{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}
orbool
, 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 ison_missing_core_dim
, which can take the value"copy"
(the default),"raise"
, or"drop"
and controls what to do with variables of aDataset
missing core dimensions. Other options arejoin
,dataset_join
,dataset_fill_value
, anddask_gufunc_kwargs
. Unlisted options are handled internally.
- Returns:
output (
CentralMomentsData
) – Ifby
isNone
, reduce all samples alongaxis
, optionally keepingaxis
with size1
ifkeepdims=True
. Otherwise, reduce for each unique value ofby
. In this case, output will have shape(..., shape[axis-1], ngroup, shape[axis+1], ...)
wherengroups = np.max(by) + 1
is the number of unique positive values inby
.
Notes
This is a new feature, and subject to change. In particular, the current implementation is not smart about the output coordinates and dimensions, and is inconsistent with
xarray.DataArray.groupby()
. It is up the the user to manipulate the dimensions/coordinates. output dimensions and coords simplistically.Examples
>>> import cmomy >>> rng = cmomy.default_rng(0) >>> data = xr.DataArray(rng.random((6, 3)), dims=["rec", "mom"]).assign_coords( ... rec=range(6) ... ) >>> da = CentralMomentsData(data) >>> da <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (rec: 6, mom: 3)> Size: 144B array([[0.637 , 0.2698, 0.041 ], [0.0165, 0.8133, 0.9128], [0.6066, 0.7295, 0.5436], [0.9351, 0.8159, 0.0027], [0.8574, 0.0336, 0.7297], [0.1757, 0.8632, 0.5415]]) Coordinates: * rec (rec) int64 48B 0 1 2 3 4 5 Dimensions without coordinates: mom
Reduce along a single dimension
>>> da.reduce(dim="rec") <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (mom: 3)> Size: 24B array([3.2283, 0.4867, 0.4535]) Dimensions without coordinates: mom
Reduce by group:
>>> by = [0, 0, 0, 1, 1, 1] >>> da.reduce(dim="rec", by=by, coords_policy="first") <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (rec: 2, mom: 3)> Size: 48B array([[1.2601, 0.4982, 0.3478], [1.9681, 0.4793, 0.521 ]]) Coordinates: * rec (rec) int64 16B 0 3 Dimensions without coordinates: mom
Note the coordinate
rec
. Using thecoords_policy="first"
uses the first coordinate a group is observed.>>> da.reduce(dim="rec", by=by, coords_policy="group", groups=["a", "b"]) <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (rec: 2, mom: 3)> Size: 48B array([[1.2601, 0.4982, 0.3478], [1.9681, 0.4793, 0.521 ]]) Coordinates: * rec (rec) <U1 8B 'a' 'b' Dimensions without coordinates: mom
Reduce by coord:
>>> dag = da.assign_coords(group_coord=("rec", by)) >>> dag <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (rec: 6, mom: 3)> Size: 144B array([[0.637 , 0.2698, 0.041 ], [0.0165, 0.8133, 0.9128], [0.6066, 0.7295, 0.5436], [0.9351, 0.8159, 0.0027], [0.8574, 0.0336, 0.7297], [0.1757, 0.8632, 0.5415]]) Coordinates: * rec (rec) int64 48B 0 1 2 3 4 5 group_coord (rec) int64 48B 0 0 0 1 1 1 Dimensions without coordinates: mom
>>> dag.reduce(dim="rec", by="group_coord") <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (rec: 2, mom: 3)> Size: 48B array([[1.2601, 0.4982, 0.3478], [1.9681, 0.4793, 0.521 ]]) Coordinates: * rec (rec) int64 16B 0 3 group_coord (rec) int64 16B 0 1 Dimensions without coordinates: mom
Block averaging:
>>> da.reduce(block=3, dim="rec") <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (rec: 2, mom: 3)> Size: 48B array([[1.2601, 0.4982, 0.3478], [1.9681, 0.4793, 0.521 ]]) Coordinates: * rec (rec) int64 16B 0 3 Dimensions without coordinates: mom
Note that this is the same as the group reduction above. For finer control of how
block
is transformed toby
, seeblock_by()
.
- classmethod zeros(*, mom, val_shape=None, dtype=None, order=None, dims=None, mom_dims=None, attrs=None, coords=None, name=None, template=None)[source]#
Create a new object with specified sizes.
This can only be used to create a wrapped
DataArray
object. To create a new wrappedDataset
, usexarray.zeros_like()
.- Parameters:
mom (
int
ortuple
ofint
) – Order or moments. If integer or length one tuple, then moments are for a single variable. If length 2 tuple, then comoments of two variablesval_shape (
tuple
) – Shape of values part of data. That is, the non-moment dimensions.order (
{"C", "F"}
, optional) – Order argument. Seenumpy.zeros()
.dims (hashable or sequence of hashable) –
Dimension of resulting
xarray.DataArray
.If
len(dims) == self.ndim
, then dims specifies all dimensions.If
len(dims) == self.val_ndim
,dims = dims + mom_dims
Default to
('dim_0', 'dim_1', ...)
attrs (mapping) – Attributes of output
coords (mapping) – Coordinates of output
name (hashable) – Name of output
indexes (
Any
) – indexes attribute. This is ignored.template (
DataArray
) – If present, output will have attributes of template. Overrides other options.
- Returns:
CentralMomentsData
– New instance with zero values.
- to_dataset(dim=None, *, name=None, promote_attrs=False)[source]#
Convert to Dataset or wrapped Dataset.
- Parameters:
dim (
Hashable
, optional) – Name of the dimension on this array along which to split this array into separate variables. If not provided, this array is converted into a Dataset of one variable.name (
Hashable
, optional) – Name to substitute for this array’s name. Only valid ifdim
is not provided.promote_attrs (
bool
, default:False
) – Set to True to shallow copy attrs of DataArray to returned Dataset.
- Returns:
CentralMomentsData
– Object wrapping Dataset.
See also
- to_dataarray(dim='variable', *, name=None)[source]#
Convert wrapped Dataset to DataArray
The data variables of this dataset will be broadcast against each other and stacked along the first axis of the new array. All coordinates of this dataset will remain coordinates.
- Parameters:
dim (
Hashable
, default:"variable"
) – Name of the new dimension.name (
Hashable
orNone
, optional) – Name of the new data array.
- Returns:
CentralMomentsData
– New object wrapping DataArray
See also
- property attrs#
Attributes of values.
- property dims#
Dimension names.
Note that for a wrapped Dataset,
self.obj.dims
is a mapping, whileself.dims
is always a tuple of names.
- property val_dims#
Names of value (i.e., not moment) dimensions.
- property coords#
Coordinates of values.
- property name#
Name of values.
- property sizes#
Sizes of values.
- compute(**kwargs)[source]#
Interface to
xarray.DataArray.compute()
andxarray.Dataset.compute()
- chunk(*args, **kwargs)[source]#
Interface to
xarray.DataArray.chunk()
andxarray.Dataset.chunk()
- assign_coords(coords=None, **coords_kwargs)[source]#
Assign coordinates to data and return new object.
- stack(dim=None, *, _reorder=True, _copy=False, _verify=False, _fastpath=False, **dimensions_kwargs)[source]#
Stack dimensions.
- Returns:
output (
CentralMomentsData
) – With dimensions stacked.
See also
Examples
>>> import cmomy >>> rng = cmomy.default_rng(0) >>> vals = xr.DataArray(rng.random((10, 2, 3)), dims=["rec", "dim_0", "dim_1"]) >>> da = cmomy.wrap_reduce_vals(vals, mom=2, dim="rec") >>> da <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (dim_0: 2, dim_1: 3, mom_0: 3)> Size: 144B array([[[10. , 0.5205, 0.0452], [10. , 0.4438, 0.0734], [10. , 0.5038, 0.1153]], [[10. , 0.5238, 0.1272], [10. , 0.628 , 0.0524], [10. , 0.412 , 0.0865]]]) Dimensions without coordinates: dim_0, dim_1, mom_0 >>> da_stack = da.stack(z=["dim_0", "dim_1"]) >>> da_stack <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (z: 6, mom_0: 3)> Size: 144B array([[10. , 0.5205, 0.0452], [10. , 0.4438, 0.0734], [10. , 0.5038, 0.1153], [10. , 0.5238, 0.1272], [10. , 0.628 , 0.0524], [10. , 0.412 , 0.0865]]) Coordinates: * z (z) object 48B MultiIndex * dim_0 (z) int64 48B 0 0 0 1 1 1 * dim_1 (z) int64 48B 0 1 2 0 1 2 Dimensions without coordinates: mom_0
And unstack
>>> da_stack.unstack("z") <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (dim_0: 2, dim_1: 3, mom_0: 3)> Size: 144B array([[[10. , 0.5205, 0.0452], [10. , 0.4438, 0.0734], [10. , 0.5038, 0.1153]], [[10. , 0.5238, 0.1272], [10. , 0.628 , 0.0524], [10. , 0.412 , 0.0865]]]) Coordinates: * dim_0 (dim_0) int64 16B 0 1 * dim_1 (dim_1) int64 24B 0 1 2 Dimensions without coordinates: mom_0
- unstack(dim=None, fill_value=nan, *, sparse=False, _reorder=True, _copy=False, _verify=False, _fastpath=False)[source]#
Unstack dimensions.
- Returns:
output (
CentralMomentsData
) – With dimensions unstacked
See also
- assign_moment(moment=None, *, squeeze=True, copy=True, dim_combined=None, keep_attrs=None, apply_ufunc_kwargs=None, **moment_kwargs)[source]#
Create object with update weight, average, etc.
- Parameters:
moment (mapping of
str
to array-like) –Mapping from moment name to new value. Allowed moment names are:
"weight"
: weights"ave"
: Averages."var"
: Variance."cov"
: Covariance ifmom_ndim == 2
, or variance ifmom_ndim == 1
."xave"
: Average of first variable."xvar"
: Variance of first variable."yave"
: Average of second variable (ifmom_ndim == 2
)."yvar"
: Variance of second variable (ifmom_ndim == 2
)."xmom_n", "ymom_n"
: All values with first (second) variable moment == n."all"
: All values.
Names
"weight", "xave", "yave", "xvar", "yvar", "cov"
imply shapedata.shape[:-mom_ndim]
. Names"ave", "var"
imply shape(*data.shape[:-mom_ndim], mom_ndim)
, unlessmom_ndim == 1
andsqueeze = True
.squeeze (
bool
, defaultFalse
) – If True, squeeze last dimension ifname
is one ofave
orvar
andmom_ndim == 1
.copy (
bool
, defaultTrue
) – IfTrue
(the default), return new array with updated weights. Otherwise, return the original array with weights updated inplace. Note that a copy is always created for adask
backed object.dim_combined (
str
, optional) – Name of dimensions for multiple values. Must supply if passing in multiple values forname="ave"
etc.mom_dims (hashable or
tuple
of hashable) – Name of moment dimensions. If specified, infermom_ndim
frommom_dims
. If also passmom_ndim
, check thatmom_dims
is consistent withmom_dims
. If not specified, defaults todata.dims[-mom_ndim:]
. This is primarily used ifdata
is aDataset
, or ifmom_dims
are not the last dimensions.keep_attrs (
{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}
orbool
, 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 ison_missing_core_dim
, which can take the value"copy"
(the default),"raise"
, or"drop"
and controls what to do with variables of aDataset
missing core dimensions. Other options arejoin
,dataset_join
,dataset_fill_value
, anddask_gufunc_kwargs
. Unlisted options are handled internally.**moment_kwargs (
Union
[Buffer
,_SupportsArray
[dtype
[Any
]],_NestedSequence
[_SupportsArray
[dtype
[Any
]]],bool
,int
,float
,complex
,str
,bytes
,_NestedSequence
[bool
|int
|float
|complex
|str
|bytes
],DataArray
,Dataset
]) – Keyword argument form ofmoment
. Must provide eithermoment
ormoment_kwargs
.
- Returns:
output (
object
) – Same type asself
with updated data.
See also
- cmom()[source]#
Central moments.
Strict central moments of the form
\[\text{cmom[..., n, m]} = \langle (x - \langle x \rangle)^n (y - \langle y \rangle)^m \rangle\]where
\[\langle x \rangle = \sum_i w_i x_i / \sum_i w_i\]
- cumulative(*, axis=MISSING, dim=MISSING, out=None, dtype=None, casting='same_kind', order=None, parallel=None, axes_to_end=False, keep_attrs=None, apply_ufunc_kwargs=None)[source]#
Convert to cumulative moments.
- Parameters:
axis (
int
, optional) – Axis to reduce/sample along. Note that negative values are relative todata.ndim - mom_ndim
. It is assumed that the last dimensions are for moments. For example, ifdata.shape == (1,2,3)
withmom_ndim=1
,axis = -1 `` would be equivalent to ``axis = 1
. Defaults toaxis=-1
.axes_to_end (
bool
) – IfTrue
, place sampled dimension (if exists in output) and moment dimensions at end of output. Otherwise, place sampled dimension (if exists in output) at same position as inputaxis
and moment dimensions at same position as input (if input does not contain moment dimensions, place them at end of array).out (
ndarray
) – Optional output array. If specified, output will be a reference to this array. Note that if the output if method returns aDataset
, then this option is ignored.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. Seenumpy.asarray()
.parallel (
bool
, optional) – IfTrue
, use parallel numbanumba.njit
ornumba.guvectorized
code if possible. IfNone
, use a heuristic to determine if should attempt to use parallel method.keep_attrs (
{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}
orbool
, 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.
mom_dims (hashable or
tuple
of hashable) – Name of moment dimensions. If specified, infermom_ndim
frommom_dims
. If also passmom_ndim
, check thatmom_dims
is consistent withmom_dims
. If not specified, defaults todata.dims[-mom_ndim:]
. This is primarily used ifdata
is aDataset
, or ifmom_dims
are not the last dimensions.apply_ufunc_kwargs (dict-like) – Extra parameters to
xarray.apply_ufunc()
. One useful option ison_missing_core_dim
, which can take the value"copy"
(the default),"raise"
, or"drop"
and controls what to do with variables of aDataset
missing core dimensions. Other options arejoin
,dataset_join
,dataset_fill_value
, anddask_gufunc_kwargs
. Unlisted options are handled internally.
- Returns:
output (
numpy.ndarray
orxarray.DataArray
orxarray.Dataset
) – Same type asself.obj
, with moments accumulated overaxis
.
See also
- property dtype#
DType of wrapped object.
- jackknife_and_reduce(*, axis=MISSING, dim=MISSING, data_reduced=None, rep_dim='rep', axes_to_end=False, out=None, dtype=None, casting='same_kind', order=None, parallel=None, keep_attrs=None, apply_ufunc_kwargs=None)[source]#
Jackknife resample and reduce
- Parameters:
axis (
int
, optional) – Axis to reduce/sample along. Note that negative values are relative todata.ndim - mom_ndim
. It is assumed that the last dimensions are for moments. For example, ifdata.shape == (1,2,3)
withmom_ndim=1
,axis = -1 `` would be equivalent to ``axis = 1
. Defaults toaxis=-1
.parallel (
bool
, optional) – IfTrue
, use parallel numbanumba.njit
ornumba.guvectorized
code if possible. IfNone
, use a heuristic to determine if should attempt to use parallel method.data_reduced (array or
object
) – Data reduced alongaxis
. Array of same type asself.obj
or same type asself
.rep_dim (hashable) – Name of new ‘replicated’ dimension:
axes_to_end (
bool
) – IfTrue
, place sampled dimension (if exists in output) and moment dimensions at end of output. Otherwise, place sampled dimension (if exists in output) at same position as inputaxis
and moment dimensions at same position as input (if input does not contain moment dimensions, place them at end of array).out (
ndarray
) – Optional output array. If specified, output will be a reference to this array. Note that if the output if method returns aDataset
, then this option is ignored.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. Seenumpy.asarray()
.parallel – If
True
, use parallel numbanumba.njit
ornumba.guvectorized
code if possible. IfNone
, use a heuristic to determine if should attempt to use parallel method.keep_attrs (
{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}
orbool
, 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 ison_missing_core_dim
, which can take the value"copy"
(the default),"raise"
, or"drop"
and controls what to do with variables of aDataset
missing core dimensions. Other options arejoin
,dataset_join
,dataset_fill_value
, anddask_gufunc_kwargs
. Unlisted options are handled internally.
- Returns:
object
– Instance of calling class with jackknife resampling alongaxis
.
See also
- property mom#
Moments tuple.
- property mom_ndim#
Number of moment dimensions.
- property mom_params#
Moments parameters object.
- property mom_shape#
Shape of moments dimensions.
- moments_to_comoments(*, mom, mom_dims_out=None, dtype=None, order=None, keep_attrs=None, apply_ufunc_kwargs=None)[source]#
Convert moments (mom_ndim=1) to comoments (mom_ndim=2).
- Parameters:
Moments for comoments array. Pass a negative value for one of the moments to fill all available moments for that dimensions. For example, if original array has moments m (i.e.,
values.shape=(..., m + 1)
), and pass inmom = (2, -1)
, then this will be transformed tomom = (2, m - 2)
.mom_dims_out (
tuple
ofstr
) – Moments dimensions for output (mom_ndim=2
) data. Defaults to("mom_0", "mom_1")
.order (
{"C", "F"}
, optional) – Order argument. Seenumpy.zeros()
.keep_attrs (
{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}
orbool
, 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 ison_missing_core_dim
, which can take the value"copy"
(the default),"raise"
, or"drop"
and controls what to do with variables of aDataset
missing core dimensions. Other options arejoin
,dataset_join
,dataset_fill_value
, anddask_gufunc_kwargs
. Unlisted options are handled internally.
- Returns:
output (
object
) – Same type asself
withmom_ndim=2
.
See also
- moveaxis(axis=MISSING, dest=MISSING, *, dim=MISSING, dest_dim=MISSING, axes_to_end=False)[source]#
Generalized moveaxis
- Parameters:
- Returns:
output (
object
) – Object with moved axes. This is a view of the original data.
See also
- property ndim#
Total number of dimensions.
- property obj#
Underlying object.
- pipe(func_or_method, *args, _reorder=True, _copy=None, _verify=False, _fastpath=False, **kwargs)[source]#
Apply func_or_method to underlying data and wrap results in new wrapped object.
This is useful for calling any not implemented methods on
numpy.ndarray
orxarray.DataArray
data.Note that a
ValueError
will be raised if last dimension(s) do not have the same shape asself.mom_shape
.- Parameters:
func_or_method (
str
orcallable()
) – If callable, then applyvalues = func_or_method(self.to_values(), *args, **kwargs)
. If string is passed, thenvalues = getattr(self.to_values(), func_or_method)(*args, **kwargs)
.*args (
Any
) – Extra positional arguments to func_or_method_reorder (
bool
, defaultTrue
) – If True, reorder the data such thatmom_dims
are last. Only applicable forDataArray
like underlying data._copy (
bool
, defaultFalse
) – If True, copy the resulting data. Otherwise, try to use a view.**kwargs (
Any
) – Extra keyword arguments to func_or_method
- Returns:
output (
object
) – New object after func_or_method is applies to self.to_values()
Notes
Use leading underscore for (_copy, etc) to avoid possible name clashes with
func_or_method
.
- resample_and_reduce(*, sampler, axis=MISSING, dim=MISSING, rep_dim='rep', axes_to_end=False, out=None, dtype=None, casting='same_kind', order=None, parallel=None, keep_attrs=None, apply_ufunc_kwargs=None)[source]#
Bootstrap resample and reduce.
- Parameters:
sampler (
int
or array-like orIndexSampler
or mapping) – Passed throughresample.factory_sampler()
to create anIndexSampler
. Value can either benrep
(the number of replicates),freq
(frequency array), aIndexSampler
object, or a mapping of parameters. The mapping can have form ofFactoryIndexSamplerKwargs
. Allowable keys arefreq
,indices
,ndat
,nrep
,nsamp
,paired
,rng
,replace
,shuffle
.axis (
int
, optional) – Axis to reduce/sample along. Note that negative values are relative todata.ndim - mom_ndim
. It is assumed that the last dimensions are for moments. For example, ifdata.shape == (1,2,3)
withmom_ndim=1
,axis = -1 `` would be equivalent to ``axis = 1
. Defaults toaxis=-1
.rep_dim (hashable) – Name of new ‘replicated’ dimension:
axes_to_end (
bool
) – IfTrue
, place sampled dimension (if exists in output) and moment dimensions at end of output. Otherwise, place sampled dimension (if exists in output) at same position as inputaxis
and moment dimensions at same position as input (if input does not contain moment dimensions, place them at end of array).out (
ndarray
) – Optional output array. If specified, output will be a reference to this array. Note that if the output if method returns aDataset
, then this option is ignored.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. Seenumpy.asarray()
.parallel (
bool
, optional) – IfTrue
, use parallel numbanumba.njit
ornumba.guvectorized
code if possible. IfNone
, use a heuristic to determine if should attempt to use parallel method.keep_attrs (
{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}
orbool
, 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 ison_missing_core_dim
, which can take the value"copy"
(the default),"raise"
, or"drop"
and controls what to do with variables of aDataset
missing core dimensions. Other options arejoin
,dataset_join
,dataset_fill_value
, anddask_gufunc_kwargs
. Unlisted options are handled internally.
- Returns:
output (
object
) – Instance of calling class. Note that new object will have(...,shape[axis-1], nrep, shape[axis+1], ...)
, wherenrep
is the number of replicates.
Examples
>>> import cmomy >>> rng = cmomy.default_rng(0) >>> da = cmomy.wrap_reduce_vals( ... rng.random((10, 3)), ... mom=3, ... axis=0, ... ).to_x(dims="rec") >>> da <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (rec: 3, mom_0: 4)> Size: 96B array([[ 1.0000e+01, 5.2485e-01, 1.1057e-01, -4.6282e-03], [ 1.0000e+01, 5.6877e-01, 6.8876e-02, -1.2745e-02], [ 1.0000e+01, 5.0944e-01, 1.1978e-01, -1.4644e-02]]) Dimensions without coordinates: rec, mom_0
>>> sampler = cmomy.resample.factory_sampler(data=da.obj, dim="rec", nrep=5) >>> da_resamp = da.resample_and_reduce( ... dim="rec", ... sampler=sampler, ... ) >>> da_resamp <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (rep: 5, mom_0: 4)> Size: 160B array([[ 3.0000e+01, 5.0944e-01, 1.1978e-01, -1.4644e-02], [ 3.0000e+01, 5.3435e-01, 1.0038e-01, -1.2329e-02], [ 3.0000e+01, 5.2922e-01, 1.0360e-01, -1.6009e-02], [ 3.0000e+01, 5.5413e-01, 8.3204e-02, -1.1267e-02], [ 3.0000e+01, 5.4899e-01, 8.6627e-02, -1.5407e-02]]) Dimensions without coordinates: rep, mom_0
- rmom()[source]#
Raw moments.
\[\text{rmom[..., n, m]} = \langle x^n y^m \rangle\]where
\[\langle x \rangle = \sum_i w_i x_i / \sum_i w_i\]See also
- select_moment(name, *, squeeze=True, dim_combined='variable', coords_combined=None, keep_attrs=None, apply_ufunc_kwargs=None)[source]#
Select specific moments.
- Parameters:
moment (
{"weight", "ave", "var", "cov", "xave", "xvar", "yave", "yvar", "xmom_0", "xmom_1", "ymom_0", "ymom_1"}
) –Name of moment(s) to select.
"weight"
: weights"ave"
: Averages."var"
: Variance."cov"
: Covariance ifmom_ndim == 2
, or variance ifmom_ndim == 1
."xave"
: Average of first variable."xvar"
: Variance of first variable."yave"
: Average of second variable (ifmom_ndim == 2
)."yvar"
: Variance of second variable (ifmom_ndim == 2
)."all"
: All values.
Names
"weight", "xave", "yave", "xvar", "yvar", "cov"
imply shapedata.shape[:-mom_ndim]
. Names"ave", "var"
imply shape(*data.shape[:-mom_ndim], mom_ndim)
, unlessmom_ndim == 1
andsqueeze = True
.squeeze (
bool
, defaultFalse
) – If True, squeeze last dimension ifname
is one ofave
orvar
andmom_ndim == 1
.dim_combined (
str
, optional) – Name of dimension for options that produce multiple values (e.g.,name="ave"
).coords_combined (
str
or sequence ofstr
, optional) – Coordinates to assign todim_combined
. Defaults to names of moments dimension(s)keep_attrs (
{"drop", "identical", "no_conflicts", "drop_conflicts", "override"}
orbool
, 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 ison_missing_core_dim
, which can take the value"copy"
(the default),"raise"
, or"drop"
and controls what to do with variables of aDataset
missing core dimensions. Other options arejoin
,dataset_join
,dataset_fill_value
, anddask_gufunc_kwargs
. Unlisted options are handled internally.
- Returns:
output (
ndarray
orDataArray
orDataset
) – Same type asself.obj
. Ifname
isave
orvar
, the last dimensions ofoutput
has shapemom_ndim
with each element corresponding to the ith variable. Ifsqueeze=True
and mom_ndim==1, this last dimension is removed. For all othername
options, output has shape of input with moment dimensions removed.
See also
- set_index(indexes=None, append=False, *, _reorder=True, _copy=False, _verify=False, _fastpath=False, **indexes_kwargs)[source]#
Interface to
xarray.DataArray.set_index()
- Returns:
output (
CentralMomentsData
) – With new index.
See also
- property shape#
Shape of wrapped object.
- to_raw(*, weight=None)[source]#
Raw moments accumulation array.
\[\begin{split}\text{raw[..., n, m]} = \begin{cases} \text{weight} & n = m = 0 \\ \langle x^n y ^m \rangle & \text{otherwise} \end{cases}\end{split}\]where
\[\langle x \rangle = \sum_i w_i x_i / \sum_i w_i\]See also
- property val_ndim#
Number of values dimensions (
ndim - mom_ndim
).
- property val_shape#
Shape of values dimensions.
- zeros_like()[source]#
Create new empty object like self.
- Returns:
output (
object
) – Object with same attributes as caller, but with data set to zero.
See also
- reset_index(dims_or_levels, drop=False, *, _reorder=True, _copy=False, _verify=False, _fastpath=False)[source]#
Interface to
xarray.DataArray.reset_index()
.
- drop_vars(names, *, errors='raise', _reorder=True, _copy=False, _verify=False, _fastpath=False)[source]#
Interface to
xarray.DataArray.drop_vars()
- swap_dims(dims_dict=None, _reorder=True, _copy=False, _verify=False, _fastpath=False, **dims_kwargs)[source]#
Interface to
xarray.DataArray.swap_dims()
.
- sel(indexers=None, method=None, tolerance=None, drop=False, *, _reorder=False, _copy=False, _verify=False, _fastpath=False, **indexers_kws)[source]#
Select subset of data.
- Returns:
output (
CentralMomentsData
) – With dimensions unstacked
See also
Examples
>>> import cmomy >>> rng = cmomy.default_rng(0) >>> da = cmomy.wrap_reduce_vals(rng.random((10, 3)), axis=0, mom=2).to_x( ... dims="x", coords=dict(x=list("abc")) ... ) >>> da <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (x: 3, mom_0: 3)> Size: 72B array([[10. , 0.5248, 0.1106], [10. , 0.5688, 0.0689], [10. , 0.5094, 0.1198]]) Coordinates: * x (x) <U1 12B 'a' 'b' 'c' Dimensions without coordinates: mom_0
Select by value
>>> da.sel(x="a") <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (mom_0: 3)> Size: 24B array([10. , 0.5248, 0.1106]) Coordinates: x <U1 4B 'a' Dimensions without coordinates: mom_0 >>> da.sel(x=["a", "c"]) <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (x: 2, mom_0: 3)> Size: 48B array([[10. , 0.5248, 0.1106], [10. , 0.5094, 0.1198]]) Coordinates: * x (x) <U1 8B 'a' 'c' Dimensions without coordinates: mom_0
Select by position
>>> da.isel(x=0) <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (mom_0: 3)> Size: 24B array([10. , 0.5248, 0.1106]) Coordinates: x <U1 4B 'a' Dimensions without coordinates: mom_0 >>> da.isel(x=[0, 1]) <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (x: 2, mom_0: 3)> Size: 48B array([[10. , 0.5248, 0.1106], [10. , 0.5688, 0.0689]]) Coordinates: * x (x) <U1 8B 'a' 'b' Dimensions without coordinates: mom_0
- isel(indexers=None, drop=False, missing_dims='raise', *, _reorder=False, _copy=False, _verify=False, _fastpath=False, **indexers_kws)[source]#
Select subset of data by position.
- Returns:
output (
CentralMomentsData
) – With dimensions unstacked
See also
- transpose(*dims, transpose_coords=True, missing_dims='raise', _copy=False, _verify=False, _fastpath=False)[source]#
Transpose dimensions of data.
Notes
mom_dims
will always be put at the end of the array.
- to_array(copy=False)[source]#
Convert to
cmomy.CentralMomentsArray
object if possible.
- to_c(copy=False)[source]#
Alias to
to_array()
.