Attempt at a more generic wrapper class
Idea is to Wrap ndarray, xr.DataArray, and xr.Dataset objects…
Classes:
MomParams type variable |
|
Generic array type variable |
|
|
Wrapper to calculate central moments. |
- class cmomy.wrapper._wrapper_abc.MomParamsT#
MomParams type variable
alias of TypeVar(‘MomParamsT’, MomParamsArray, MomParamsXArray)
- class cmomy.wrapper._wrapper_abc.GenArrayT#
Generic array type variable
alias of TypeVar(‘GenArrayT’, NDArray[Any], xr.DataArray, xr.Dataset)
- class cmomy.wrapper._wrapper_abc.CentralMomentsABC(obj, *, mom_params, fastpath=False)[source]#
Bases:
ABC,Generic[GenArrayT,MomParamsT]Wrapper to calculate central moments.
- Parameters:
obj (
numpy.ndarrayorxarray.DataArrayorxarray.Dataset) – Central moments array.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")).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}\]Attributes:
Underlying object.
Moments parameters object.
Number of moment dimensions.
Shape of moments dimensions.
Moments tuple.
DType of wrapped object.
Shape of wrapped object.
Shape of values dimensions.
Total number of dimensions.
Number of values dimensions (
ndim - mom_ndim).Methods:
to_numpy()Coerce wrapped data to
ndarrayif possible.new_like([obj, verify, copy, dtype, order, ...])Create new object like self, with new data.
astype(dtype, *[, order, casting, subok, copy])Underlying data cast to specified type
Create new empty object like self.
copy()Create a new object with copy of data.
push_data(data, *[, casting, parallel, scale])Push data object to moments.
push_datas(datas, *[, axis, 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[, axis, weight, casting, ...])Push multiple samples to central moments.
__iadd__(other)Self adder.
__add__(other)Add objects to new object.
__isub__(other)Inplace subtraction.
__sub__(other)Subtract objects.
__mul__(scale)New object with weight scaled by scale.
__imul__(scale)Inplace multiply.
pipe(func_or_method, *args[, _reorder, ...])Apply func_or_method to underlying data and wrap results in new wrapped object.
moveaxis([axis, dest, dim, dest_dim, ...])Generalized moveaxis
select_moment(name, *[, squeeze, ...])Select specific moments.
assign_moment([moment, squeeze, copy, ...])Create object with update weight, average, etc.
cumulative(*[, axis, dim, out, dtype, ...])Convert to cumulative moments.
moments_to_comoments(*, mom[, mom_dims_out, ...])Convert moments (mom_ndim=1) to comoments (mom_ndim=2).
resample_and_reduce(*, sampler[, axis, dim, ...])Bootstrap resample and reduce.
jackknife_and_reduce(*[, axis, dim, ...])Jackknife resample and reduce
reduce()Create new object reduce along axis.
weight()Weight data.
mean([squeeze])Mean (first moment).
var([squeeze])Variance (second central moment).
std([squeeze])Standard deviation (ddof=0).
cov()Covariance (or variance if
mom_ndim==1).cmom()Central moments.
to_raw(*[, weight])Raw moments accumulation array.
rmom()Raw moments.
zeros(*, mom)Create a new base object.
- property obj#
Underlying object.
- property mom_params#
Moments parameters object.
- property mom_ndim#
Number of moment dimensions.
- property mom_shape#
Shape of moments dimensions.
- property mom#
Moments tuple.
- property dtype#
DType of wrapped object.
- property shape#
Shape of wrapped object.
- property val_shape#
Shape of values dimensions.
- property ndim#
Total number of dimensions.
- property val_ndim#
Number of values dimensions (
ndim - mom_ndim).
- abstractmethod new_like(obj=None, *, verify=False, copy=None, dtype=None, order=None, fastpath=False)[source]#
Create new object like self, with new data.
- Parameters:
obj (
numpy.ndarrayorxarray.DataArrayorxarray.Dataset) – Data for new object. Must be conformable toself.obj.verify (
bool) – If True, make sure data is c-contiguous.copy (
bool, optional) – If True, copy the data. If None or False, attempt to use view. Note thatFalsevalues will be converted toNonefor numpy versions>2.0. This will be changed to reflect the new behavior of thecopyparameter tonumpy.array()when the minimum numpy version>2.0.order (
{"C", "F", "A", "K"}, optional) – Order argument. Seenumpy.asarray().fastpath (
bool) – Internal variable.
- Returns:
object– New object object with data set to zero.
- astype(dtype, *, order=None, casting=None, subok=None, copy=False)[source]#
Underlying data cast to specified type
- Parameters:
dtype (
strordtype) – Type code of data-type to cast the array data. Note that a value of None will cast tonp.float64. This is the same behavior asasarray().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.
Notes
Only
numpy.float32andnumpy.float64dtype are supported.See also
- 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
- copy()[source]#
Create a new object with copy of data.
- Returns:
output (
object) – Same type as calling class. Object with same attributes as caller, but with new underlying data.
See also
- abstractmethod push_data(data, *, casting='same_kind', parallel=False, scale=None)[source]#
Push data object to moments.
- Parameters:
data (array-like or
numpy.ndarrayorxarray.DataArrayorxarray.Dataset) – Accumulation array conformable toself.obj.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.njitornumba.guvectorizedcode if possible. IfNone, use a heuristic to determine if should attempt to use parallel method.scale (array-like) – Scaling to apply to weights of
data. Optional.
- Returns:
output (
object) – Same object with pushed data.
- abstractmethod push_datas(datas, *, axis=-1, mom_axes=None, casting='same_kind', parallel=None)[source]#
Push and reduce multiple average central moments.
- Parameters:
datas (array-like or
numpy.ndarrayorxarray.DataArrayorxarray.Dataset) – Collection of accumulation arrays to push ontoself.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.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.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.njitornumba.guvectorizedcode if possible. IfNone, use a heuristic to determine if should attempt to use parallel method.
- Returns:
output (
object) – Same object with pushed data.
- abstractmethod push_val(x, *y, weight=None, casting='same_kind', parallel=False)[source]#
Push single sample to central moments.
- Parameters:
x (array-like or
numpy.ndarrayorxarray.DataArrayorxarray.Dataset) – Values to push ontoself.*y (array-like or
numpy.ndarrayorxarray.DataArrayorxarray.Dataset) – Additional values (needed ifmom_ndim > 1)weight (
int,float, array-like ornumpy.ndarrayorxarray.DataArrayorxarray.Dataset) – 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.njitornumba.guvectorizedcode if possible. IfNone, use a heuristic to determine if should attempt to use parallel method.
- Returns:
output (
object) – Same object with pushed data.
Notes
Array x0 should have same shape as self.val_shape.
- abstractmethod push_vals(x, *y, axis=-1, weight=None, casting='same_kind', parallel=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.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.njitornumba.guvectorizedcode if possible. IfNone, use a heuristic to determine if should attempt to use parallel method.
- Returns:
output (
object) – Same object with pushed data.
- 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.ndarrayorxarray.DataArraydata.Note that a
ValueErrorwill be raised if last dimension(s) do not have the same shape asself.mom_shape.- Parameters:
func_or_method (
strorcallable()) – 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_dimsare last. Only applicable forDataArraylike 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.
- 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
- 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 == 1andsqueeze = True.squeeze (
bool, defaultFalse) – If True, squeeze last dimension ifnameis one ofaveorvarandmom_ndim == 1.dim_combined (
str, optional) – Name of dimension for options that produce multiple values (e.g.,name="ave").coords_combined (
stror 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 aDatasetmissing core dimensions. Other options arejoin,dataset_join,dataset_fill_value, anddask_gufunc_kwargs. Unlisted options are handled internally.
- Returns:
output (
ndarrayorDataArrayorDataset) – Same type asself.obj. Ifnameisaveorvar, the last dimensions ofoutputhas shapemom_ndimwith each element corresponding to the ith variable. Ifsqueeze=Trueand mom_ndim==1, this last dimension is removed. For all othernameoptions, output has shape of input with moment dimensions removed.
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
strto 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 == 1andsqueeze = True.squeeze (
bool, defaultFalse) – If True, squeeze last dimension ifnameis one ofaveorvarandmom_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 adaskbacked 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
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.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.**moment_kwargs (
Union[Buffer,_SupportsArray[dtype[Any]],_NestedSequence[_SupportsArray[dtype[Any]]],complex,bytes,str,_NestedSequence[complex|bytes|str],DataArray,Dataset]) – Keyword argument form ofmoment. Must provide eithermomentormoment_kwargs.
- Returns:
output (
object) – Same type asselfwith updated data.
See also
- 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 inputaxisand 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.njitornumba.guvectorizedcode 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
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.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 (
numpy.ndarrayorxarray.DataArrayorxarray.Dataset) – Same type asself.obj, with moments accumulated overaxis.
See also
- 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 (
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 (
object) – Same type asselfwithmom_ndim=2.
See also
- 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 (
intor array-like orIndexSampleror mapping) – Passed throughcmomy.resample.factory_sampler()to create anIndexSampler. Value can either benrep(the number of replicates),freq(frequency array), aIndexSamplerobject, 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 inputaxisand 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.njitornumba.guvectorizedcode 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 aDatasetmissing 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], ...), wherenrepis 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
- 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.njitornumba.guvectorizedcode 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.objor 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 inputaxisand 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.njitornumba.guvectorizedcode 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 aDatasetmissing 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
- abstractmethod reduce()[source]#
Create new object reduce along axis.
- 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.by (array-like of
int) – Group by values of same length asdataalong sampled dimension. Negative values indicate no group (i.e., skip this index).block (
int, optional) – If specified, perform block average reduction with blocks of this size. Negative values are transformed to all data.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).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.njitornumba.guvectorizedcode if possible. IfNone, use a heuristic to determine if should attempt to use parallel method.
- Returns:
output (
object) – IfbyisNone, reduce all samples alongaxis, optionally keepingaxiswith size1ifkeepdims=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) + 1is the number of unique positive values inby.
See also
reduce_data,reduce_data_grouped,reduce_data_indexed,block_by
- 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\]
- 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
- 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
- abstractmethod classmethod zeros(*, mom)[source]#
Create a new base object.
- Parameters:
mom (
intortupleofint) – 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- Returns:
output (
object) – New instance with zero values.
See also