mth5.timeseries.xarray_helpers
Module containing helper functions for working with xarray objects.
Functions
|
Compute the covariance matrix with numpy.cov. |
|
Returns a 1D xr.DataArray with variable "channel", having values channels named by the input list. |
|
Returns a 2D xr.Dataset with the given variables and coordinates. |
|
Initialize a 3D xarray DataArray with dimensions from coords plus 'variable'. |
|
TODO: consider changing nomenclature from dims=["channel_1", "channel_2"], |
Module Contents
- mth5.timeseries.xarray_helpers.covariance_xr(X: xarray.DataArray, aweights: numpy.ndarray | None | None = None, bias: bool | None = False, rowvar: bool | None = False) xarray.DataArray[source]
Compute the covariance matrix with numpy.cov.
- Parameters:
X (xarray.core.dataarray.DataArray) – Multivariate time series as an xarray
aweights (array_like, optional) – Passthrough param for np.cov. 1-D array of observation vector weights. These relative weights are typically large for observations considered “important” and smaller for observations considered less “important”. If
ddof=0the array of weights can be used to assign probabilities to observation vectors.bias (bool) – Passthrough param for np.cov. Default normalization (False) is by
(N - 1), whereNis the number of observations given (unbiased estimate). If bias is True, then normalization is byN. These values can be overridden by using the keywordddofin numpy versions >= 1.5.rowvar (bool) – Passthrough param for np.cov. If rowvar is True (default), then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations.
- Returns:
S (xarray.DataArray) – The covariance matrix of the data in xarray form.
Development Notes – In case of ValueError: conflicting sizes for dimension ‘channel_1’, this likely means the bool for rowvar should be flipped.
- mth5.timeseries.xarray_helpers.initialize_xrda_1d(channels: list, dtype: type | None = None, value: complex | float | bool | None = 0.0) xarray.DataArray[source]
Returns a 1D xr.DataArray with variable “channel”, having values channels named by the input list.
- Parameters:
channels (list) – The channels in the multivariate array
dtype (type, optional) – The datatype to initialize the array. Common cases are complex, float, and bool
value (Union[complex, float, bool], optional) – The default value to assign the array
- Returns:
xrda – An xarray container for the channels, initialized to zeros.
- Return type:
xarray.core.dataarray.DataArray
- mth5.timeseries.xarray_helpers.initialize_xrds_2d(variables: list, coords: dict, dtype: type | None = complex, value: complex | float | bool | None = 0) xarray.Dataset[source]
Returns a 2D xr.Dataset with the given variables and coordinates.
- Parameters:
variables (list) – List of variable names to create in the dataset
coords (dict) – Dictionary of coordinates for the dataset dimensions
dtype (type, optional) – The datatype to initialize the arrays. Common cases are complex, float, and bool
value (Union[complex, float, bool], optional) – The default value to assign the arrays
- Returns:
xrds – A 2D xarray Dataset with dimensions from coords
- Return type:
xr.Dataset
- mth5.timeseries.xarray_helpers.initialize_xrda_2d(variables: list, coords: dict, dtype: type = complex, value: int | float = 0.0) xarray.Dataset[source]
Initialize a 3D xarray DataArray with dimensions from coords plus ‘variable’.
- Parameters:
variables (list) – List of variable names for the additional dimension.
coords (dict) – Dictionary of coordinates for the dataset dimensions.
dtype (type, optional) – Data type for the array, by default complex.
value (int or float, optional) – Value to initialize the array with, by default 0.
- Returns:
A 3D DataArray with dimensions from coords plus ‘variable’.
- Return type:
xr.DataArray
- mth5.timeseries.xarray_helpers.initialize_xrda_2d_cov(channels, dtype=complex, value: complex | float | bool | None = 0)[source]
TODO: consider changing nomenclature from dims=[“channel_1”, “channel_2”], to dims=[“variable_1”, “variable_2”], to be consistent with initialize_xrda_1d
- Parameters
- channels: list
The channels in the multivariate array. The covariance matrix will be square with dimensions len(channels) x len(channels).
- dtype: type
The datatype to initialize the array. Common cases are complex, float, and bool
- value: Union[complex, float, bool]
The default value to assign the array
- Returns
- xrda: xarray.core.dataarray.DataArray
An xarray container for the channel covariances, initialized to zeros. The matrix is square with dimensions len(channels) x len(channels).