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 |
- 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 anxarrayobject attempt to infermom_ndimfrommom_dims. Otherwise, default tomom_ndim = 1.mom_axes (
intortupleofint, optional) – Location of the moment dimensions. Default to(-mom_ndim, -mom_ndim+1, ...). If specified andmom_ndimis None, setmom_ndimtolen(mom_axes). Note that ifmom_axesis specified, negative values are relative to the end of the array. This is also the case foraxesifmom_axesis specified.mom_dims (hashable or
tupleof hashable) – Name of moment dimensions. If specified, infermom_ndimfrommom_dims. If also passmom_ndim, check thatmom_dimsis consistent withmom_dims. If not specified, defaults todata.dims[-mom_ndim:]. This is primarily used ifdatais aDataset, or ifmom_dimsare not the last dimensions.mom_params (
MomParamsorMomParamsDictordict, optional) – Moment parameters. You can set moment parametersaxesanddimsusing this option. For example, passingmom_params={"dim": ("a", "b")}is equivalent to passingmom_dims=("a", "b"). You can also pass as aMomParamsobject withmom_params=cmomy.MomParams(dims=("a", "b")).to (
{"raw", "central"}) – The style of thevalues_into 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 inputaxisand 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 aDatasetmissing core dimensions. Other options arejoin,dataset_join,dataset_fill_value, anddask_gufunc_kwargs. Unlisted options are handled internally.
- Returns:
output (
ndarrayorDataArrayorDataset) – Moments array converted frominput_styleto 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 anxarrayobject attempt to infermom_ndimfrommom_dims. Otherwise, default tomom_ndim = 1.mom_axes (
intortupleofint, optional) – Location of the moment dimensions. Default to(-mom_ndim, -mom_ndim+1, ...). If specified andmom_ndimis None, setmom_ndimtolen(mom_axes). Note that ifmom_axesis specified, negative values are relative to the end of the array. This is also the case foraxesifmom_axesis specified.mom_dims (hashable or
tupleof hashable) – Name of moment dimensions. If specified, infermom_ndimfrommom_dims. If also passmom_ndim, check thatmom_dimsis consistent withmom_dims. If not specified, defaults todata.dims[-mom_ndim:]. This is primarily used ifdatais aDataset, or ifmom_dimsare not the last dimensions.mom_params (
MomParamsorMomParamsDictordict, optional) – Moment parameters. You can set moment parametersaxesanddimsusing this option. For example, passingmom_params={"dim": ("a", "b")}is equivalent to passingmom_dims=("a", "b"). You can also pass as aMomParamsobject withmom_params=cmomy.MomParams(dims=("a", "b")).inverse (
bool, optional) – Default is to create a cumulative moments array. Passinverse=Trueto 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.njitornumba.guvectorizedcode 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 inputaxisand 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 aDatasetmissing core dimensions. Other options arejoin,dataset_join,dataset_fill_value, anddask_gufunc_kwargs. Unlisted options are handled internally.
- Returns:
out (
ndarrayorDataArrayorDataset) – 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
DataArrayorDataset) – 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 (
intortupleofint, optional) – Location of the moment dimensions. Default to(-mom_ndim, -mom_ndim+1, ...). If specified andmom_ndimis None, setmom_ndimtolen(mom_axes). Note that ifmom_axesis specified, negative values are relative to the end of the array. This is also the case foraxesifmom_axesis specified.mom_dims (hashable or
tupleof hashable) – Name of moment dimensions. If specified, infermom_ndimfrommom_dims. If also passmom_ndim, check thatmom_dimsis consistent withmom_dims. If not specified, defaults todata.dims[-mom_ndim:]. This is primarily used ifdatais aDataset, or ifmom_dimsare not the last dimensions.mom_params (
MomParamsorMomParamsDictordict, optional) – Moment parameters. You can set moment parametersaxesanddimsusing this option. For example, passingmom_params={"dim": ("a", "b")}is equivalent to passingmom_dims=("a", "b"). You can also pass as aMomParamsobject withmom_params=cmomy.MomParams(dims=("a", "b")).mom_dims_out (
tupleofstr) – 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 aDatasetmissing core dimensions. Other options arejoin,dataset_join,dataset_fill_value, anddask_gufunc_kwargs. Unlisted options are handled internally.
- Returns:
output (
ndarrayorDataArray) – 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
DataArraydata>>> 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
DataArrayorDataset) – Moments array withmom_ndim==1. It is assumed that the last dimension is the moments dimension.mom_axes (
intortupleofint, optional) – Location of the moment dimensions. Default to(-mom_ndim, -mom_ndim+1, ...). If specified andmom_ndimis None, setmom_ndimtolen(mom_axes). Note that ifmom_axesis specified, negative values are relative to the end of the array. This is also the case foraxesifmom_axesis specified.mom_params (
MomParamsorMomParamsDictordict, optional) – Moment parameters. You can set moment parametersaxesanddimsusing this option. For example, passingmom_params={"dim": ("a", "b")}is equivalent to passingmom_dims=("a", "b"). You can also pass as aMomParamsobject withmom_params=cmomy.MomParams(dims=("a", "b")).mom_dims (hashable or
tupleof hashable) – Name of moment dimensions. If specified, infermom_ndimfrommom_dims. If also passmom_ndim, check thatmom_dimsis consistent withmom_dims. If not specified, defaults todata.dims[-mom_ndim:]. This is primarily used ifdatais aDataset, or ifmom_dimsare not the last dimensions.mom_dims_out (
tupleofstr) – 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 aDatasetmissing core dimensions. Other options arejoin,dataset_join,dataset_fill_value, anddask_gufunc_kwargs. Unlisted options are handled internally.
- Returns:
output (
ndarrayorDataArray) – 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])