Conversion routines (convert
)#
Functions:
|
Convert between central and raw moments type. |
|
Convert between moments array and cumulative moments array. |
|
Convert from moments to comoments data. |
|
Convert comoments of same variable to moments array |
|
Concatenate moments objects. |
- cmomy.convert.moments_type(values_in, *, mom_ndim=None, mom_axes=None, mom_dims=None, mom_params=None, to='central', out=None, dtype=None, casting='same_kind', order=None, axes_to_end=False, keep_attrs=None, apply_ufunc_kwargs=None)[source]#
Convert between central and raw moments type.
- Parameters:
values_in (array-like,
DataArray
, orDataset
) – The moments array to convert from.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"))
.to (
{"raw", "central"}
) – The style of thevalues_in
to convert to. If"raw"
, convert from central to raw. If"central"
convert from raw to central moments.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()
.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).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
) – Moments array converted frominput_style
to opposite format.
Notes
The structure of arrays are as follow. Central moments:
values_in[..., 0]
: weightvalues_in[..., 1]
: meanvalues_in[..., k]
: kth central moment
Central comoments of variables a and b:
values_in[..., 0, 0]
: weightvalues_in[..., 1, 0]
: mean of avalues_in[....,0, 1]
: mean of bvalues_in[..., i, j]
: \(\langle (\delta a)^i (\delta b)^j \rangle\),
where a and b are the variables being considered.
Raw moments array:
values_in[..., 0]
: weightvalues_in[..., k]
: kth moment \(\langle a^k \rangle\)
Raw comoments array of variables a and b:
values_in[..., 0, 0]
: weightvalues_in[..., i, j]
: \(\langle a^i b^j \rangle\),
- cmomy.convert.cumulative(values_in, *, axis=MISSING, dim=MISSING, mom_ndim=None, mom_axes=None, mom_dims=None, mom_params=None, inverse=False, out=None, dtype=None, casting='same_kind', order=None, parallel=None, axes_to_end=False, keep_attrs=None, apply_ufunc_kwargs=None)[source]#
Convert between moments array and cumulative moments array.
- Parameters:
values_in (array-like,
DataArray
, orDataset
)axis (
int
) – Axis to reduce/sample along.dim (hashable) – Dimension to reduce/sample along.
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"))
.inverse (
bool
, optional) – Default is to create a cumulative moments array. Passinverse=True
to convert from cumulative moments array back to normal moments.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.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).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:
out (
ndarray
orDataArray
orDataset
) – Same type asvalues_in
, with moments accumulated overaxis
.
Examples
>>> import cmomy >>> x = cmomy.default_rng(0).random((10, 3)) >>> data = cmomy.reduce_vals(x, mom=2, axis=0) >>> data array([[10. , 0.5248, 0.1106], [10. , 0.5688, 0.0689], [10. , 0.5094, 0.1198]])
>>> cdata = cumulative(data, axis=0, mom_ndim=1) >>> cdata array([[10. , 0.5248, 0.1106], [20. , 0.5468, 0.0902], [30. , 0.5344, 0.1004]])
To get the original data back, pass
inverse=True
>>> cumulative(cdata, axis=0, mom_ndim=1, inverse=True) array([[10. , 0.5248, 0.1106], [10. , 0.5688, 0.0689], [10. , 0.5094, 0.1198]])
- cmomy.convert.moments_to_comoments(data, *, mom, mom_axes=None, mom_dims=None, mom_params=None, mom_dims_out=None, dtype=None, order=None, keep_attrs=None, apply_ufunc_kwargs=None)[source]#
Convert from moments to comoments data.
- Parameters:
data (array-like or
DataArray
orDataset
) – Moments array withmom_ndim==1
. It is assumed that the last dimension is the moments dimension.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_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"))
.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 (
ndarray
orDataArray
) – Co-moments array. Same type asdata
. Note that new moment dimensions are always the last dimensions.
Examples
>>> import cmomy >>> x = cmomy.default_rng(0).random(10) >>> data1 = cmomy.reduce_vals(x, mom=4, axis=0) >>> data1 array([10. , 0.5505, 0.1014, -0.0178, 0.02 ])
>>> moments_to_comoments(data1, mom=(2, -1)) array([[10. , 0.5505, 0.1014], [ 0.5505, 0.1014, -0.0178], [ 0.1014, -0.0178, 0.02 ]])
Note that new moment dimensions are always last.
>>> data = cmomy.default_rng(0).random((6, 1, 2)) >>> a = moments_to_comoments(data, mom_axes=0, mom=(2, -1)) >>> a.shape (1, 2, 3, 4) >>> b = moments_to_comoments(np.moveaxis(data, 0, -1), mom=(2, -1)) >>> np.testing.assert_equal(a, b)
Which is identical to
>>> cmomy.reduction.reduce_vals(x, x, mom=(2, 2), axis=0) array([[10. , 0.5505, 0.1014], [ 0.5505, 0.1014, -0.0178], [ 0.1014, -0.0178, 0.02 ]])
This also works for
DataArray
data>>> xdata = xr.DataArray(data1, dims="mom") >>> xdata <xarray.DataArray (mom: 5)> Size: 40B array([10. , 0.5505, 0.1014, -0.0178, 0.02 ]) Dimensions without coordinates: mom
>>> moments_to_comoments(xdata, mom=(2, -1)) <xarray.DataArray (mom_0: 3, mom_1: 3)> Size: 72B array([[10. , 0.5505, 0.1014], [ 0.5505, 0.1014, -0.0178], [ 0.1014, -0.0178, 0.02 ]]) Dimensions without coordinates: mom_0, mom_1
Note that this also works for raw moments.
- cmomy.convert.comoments_to_moments(data, *, mom_axes=None, mom_dims=None, mom_params=None, mom_dims_out=None, dtype=None, order=None, keep_attrs=None, apply_ufunc_kwargs=None)[source]#
Convert comoments of same variable to moments array
Inverse of
moments_to_comoments()
. Use with caution. This is intended only to use for symmetric comoment arrays (i.e., one created fromcomoments_to_moments()
).- Parameters:
data (array-like or
DataArray
orDataset
) – Moments array withmom_ndim==1
. It is assumed that the last dimension is the moments dimension.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_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"))
.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_dims_out (
tuple
ofstr
) – Moments dimensions for output (mom_ndim=1
) data. Defaults to("mom_0",)
.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 (
ndarray
orDataArray
) – Co-moments array. Same type asdata
. Note that the output moments are in the last dimension.
Examples
>>> import cmomy >>> x = cmomy.default_rng(0).random(10) >>> data2 = cmomy.reduce_vals(x, x, mom=(1, 2), axis=0) >>> data2 array([[10. , 0.5505, 0.1014], [ 0.5505, 0.1014, -0.0178]]) >>> comoments_to_moments(data2) array([10. , 0.5505, 0.1014, -0.0178])
Note that this is identical to the following:
>>> cmomy.reduce_vals(x, mom=3, axis=0) array([10. , 0.5505, 0.1014, -0.0178])
- cmomy.convert.concat(arrays, *, axis=MISSING, dim=MISSING, **kwargs)[source]#
Concatenate moments objects.
- Parameters:
arrays (
Iterable
ofndarray
orDataArray
orCentralMomentsArray
orCentralMomentsData
) – Central moments objects to combine.axis (
int
, optional) – Axis to concatenate along. If specify axis forDataArray
orCentralMomentsData
input objects with outdim
, then determinedim
fromdim = first.dims[axis]
wherefirst
is the first item inarrays
.dim (
str
, optional) – Dimension to concatenate along (used forDataArray
andCentralMomentsData
objects only)**kwargs (
Any
) – Extra arguments tonumpy.concatenate()
orxarray.concat()
.
- Returns:
output (
ndarray
orDataArray
orCentralMomentsArray
orCentralMomentsData
) – Concatenated object. Type is the same as the elements ofarrays
.
Examples
>>> import cmomy >>> shape = (2, 1, 2) >>> x = np.arange(np.prod(shape)).reshape(shape).astype(np.float64) >>> y = -x >>> out = concat((x, y), axis=1) >>> out.shape (2, 2, 2) >>> out array([[[ 0., 1.], [-0., -1.]], [[ 2., 3.], [-2., -3.]]])
>>> dx = xr.DataArray(x, dims=["a", "b", "mom"]) >>> dy = xr.DataArray(y, dims=["a", "b", "mom"]) >>> concat((dx, dy), dim="b") <xarray.DataArray (a: 2, b: 2, mom: 2)> Size: 64B array([[[ 0., 1.], [-0., -1.]], [[ 2., 3.], [-2., -3.]]]) Dimensions without coordinates: a, b, mom
For
DataArray
objects, you can specify a new dimension>>> concat((dx, dy), dim="new") <xarray.DataArray (new: 2, a: 2, b: 1, mom: 2)> Size: 64B array([[[[ 0., 1.]], [[ 2., 3.]]], [[[-0., -1.]], [[-2., -3.]]]]) Dimensions without coordinates: new, a, b, mom
You can also concatenate
CentralMomentsArray
andCentralMomentsData
objects>>> cx = cmomy.CentralMomentsArray(x) >>> cy = cmomy.CentralMomentsArray(y) >>> concat((cx, cy), axis=1) <CentralMomentsArray(mom_ndim=1)> array([[[ 0., 1.], [-0., -1.]], [[ 2., 3.], [-2., -3.]]])
>>> dcx = cmomy.CentralMomentsData(dx) >>> dcy = cmomy.CentralMomentsData(dy) >>> concat((dcx, dcy), dim="new") <CentralMomentsData(mom_ndim=1)> <xarray.DataArray (new: 2, a: 2, b: 1, mom: 2)> Size: 64B array([[[[ 0., 1.]], [[ 2., 3.]]], [[[-0., -1.]], [[-2., -3.]]]]) Dimensions without coordinates: new, a, b, mom