osl_dynamics.utils.array_ops#

Helper functions for manipulating NumPy arrays.

Functions#

get_one_hot(values[, n_states])

Expand a categorical variable to a series of boolean columns (one-hot encoding).

cov2corr(cov)

Covariance to correlation.

cov2std(cov)

Get the standard deviation of batches of covariance matrices.

cov2partialcorr(cov)

Covariance to partial correlation.

cov2partialcov(cov[, use_pinv])

Covariance to partial covariance.

ensure_pos_def(cov[, eps])

Ensure a covariance (or batch of covariances) is positive definite.

sliding_window_view(x, window_shape[, axis, subok, ...])

Create a sliding window over an array in arbitrary dimensions.

validate(array, correct_dimensionality, ...)

Checks if the dimensionality of an array is correct.

check_symmetry(mat[, precision])

Checks if one or more matrices are symmetric.

ezclump(binary_array)

Find the clumps (groups of data with the same values) for a 1D bool array.

slice_length(slice_)

Return the length of a slice.

apply_to_lists(list_of_lists, func[, check_empty])

Apply a function to each list in a list of lists.

list_means(list_of_lists)

Calculate the mean of each list in a list of lists.

list_stds(list_of_lists)

Calculate the standard deviation of each list in a list of lists.

Module Contents#

osl_dynamics.utils.array_ops.get_one_hot(values, n_states=None)[source]#

Expand a categorical variable to a series of boolean columns (one-hot encoding).

Categorical Variable

A

C

D

B

becomes

A

B

C

D

1

0

0

0

0

0

1

0

0

0

0

1

0

1

0

0

Parameters:
  • values (np.ndarray) – 1D array of categorical values with shape (n_samples,). The values should be integers (0, 1, 2, 3, … , n_states - 1). Or 2D array of shape (n_samples, n_states) to be binarized.

  • n_states (int, optional) – Total number of states in values. Must be at least the number of states present in values. Default is the number of unique values in values.

Returns:

one_hot – A 2D array containing the one-hot encoded form of values. Shape is (n_samples, n_states).

Return type:

np.ndarray

osl_dynamics.utils.array_ops.cov2corr(cov)[source]#

Covariance to correlation.

Converts batches of covariance matrices into batches of correlation matrices.

Parameters:

cov (np.ndarray) – Covariance matrices. Shape must be (…, N, N).

Returns:

corr – Correlation matrices. Shape is (…, N, N).

Return type:

np.ndarray

osl_dynamics.utils.array_ops.cov2std(cov)[source]#

Get the standard deviation of batches of covariance matrices.

Parameters:

cov (np.ndarray) – Covariance matrix. Shape must be (…, N, N).

Returns:

std – Standard deviations. Shape is (…, N).

Return type:

np.ndarray

osl_dynamics.utils.array_ops.cov2partialcorr(cov)[source]#

Covariance to partial correlation.

Converts batches of covariance matrices into batches of partial correlation matrices.

Parameters:

cov (np.ndarray) – Covariance matrices. Shape must be (…, N, N).

Returns:

partial_corr – Partial correlation matrices. Shape is (…, N, N).

Return type:

np.ndarray

osl_dynamics.utils.array_ops.cov2partialcov(cov, use_pinv=False)[source]#

Covariance to partial covariance.

Converts batches of covariance matrices into batches of partial covariance matrices.

Parameters:
  • cov (np.ndarray) – Covariance matrix or batch of covariance matrices. Shape (…, N, N).

  • use_pinv (bool, optional) – If True, use np.linalg.pinv to calculate inverse of the covariance. If False, we use np.linalg.inv.

Returns:

partial_cov – Partial covariance matrices. Shape (…, N, N). Off-diagonals: -Omega_ij / (Omega_ii * Omega_jj - Omega_ij^2) Diagonals: 1 / Omega_ii (conditional variances)

Return type:

np.ndarray

osl_dynamics.utils.array_ops.ensure_pos_def(cov, eps=1e-05)[source]#

Ensure a covariance (or batch of covariances) is positive definite.

Parameters:
  • cov (np.ndarray) – Covariances of shape (…, N, N).

  • eps (float, optional) – Small value added to the diagonal.

Returns:

cov – Covariances of shape (…, N, N), symmetrized and regularized.

Return type:

np.ndarray

osl_dynamics.utils.array_ops.sliding_window_view(x, window_shape, axis=None, *, subok=False, writeable=False)[source]#

Create a sliding window over an array in arbitrary dimensions.

Unceremoniously ripped from numpy 1.20, np.lib.stride_tricks.sliding_window_view.

Parameters:
  • x (numpy.ndarray)

  • window_shape (Union[int, Tuple[int, Ellipsis]])

  • axis (Optional[Union[int, Tuple[int, Ellipsis]]])

  • subok (bool)

  • writeable (bool)

Return type:

numpy.ndarray

osl_dynamics.utils.array_ops.validate(array, correct_dimensionality, allow_dimensions, error_message)[source]#

Checks if the dimensionality of an array is correct.

Parameters:
  • array (np.ndarray) – Array to be checked.

  • correct_dimensionality (int) – The desired number of dimensions in the array.

  • allow_dimensions (int) – The number of dimensions that is acceptable for the passed array to have.

  • error_message (str) – Message to print if the array is not valid.

Returns:

array – Array with the correct dimensionality.

Return type:

np.ndarray

osl_dynamics.utils.array_ops.check_symmetry(mat, precision=1e-06)[source]#

Checks if one or more matrices are symmetric.

Parameters:
  • mat (np.ndarray or list of np.ndarray) – Matrices to be checked. Shape of a matrix should be (…, N, N).

  • precision (float, optional) – Precision for comparing values. Corresponds to an absolute tolerance parameter. Default is 1e-6.

Returns:

symmetry – Array indicating whether matrices are symmetric.

Return type:

np.ndarray of bool

osl_dynamics.utils.array_ops.ezclump(binary_array)[source]#

Find the clumps (groups of data with the same values) for a 1D bool array.

Taken wholesale from numpy.ma.extras.ezclump.

Parameters:

binary_array (numpy.ndarray)

Return type:

List[slice]

osl_dynamics.utils.array_ops.slice_length(slice_)[source]#

Return the length of a slice.

Parameters:
  • slice (slice) – Slice.

  • slice_ (slice)

Returns:

length – Length.

Return type:

int

osl_dynamics.utils.array_ops.apply_to_lists(list_of_lists, func, check_empty=True)[source]#

Apply a function to each list in a list of lists.

Parameters:
  • list_of_lists (list of list) – List of lists.

  • func (callable) – Function to apply to each list.

  • check_empty (bool, optional) – Return 0 for empty lists if set as True. If False, the function will be applied to an empty list.

Returns:

result – Numpy array with the function applied to each list.

Return type:

np.ndarray

osl_dynamics.utils.array_ops.list_means(list_of_lists)[source]#

Calculate the mean of each list in a list of lists.

Parameters:

list_of_lists (list of list) – List of lists.

Returns:

result – Numpy array with the mean of each list.

Return type:

np.ndarray

osl_dynamics.utils.array_ops.list_stds(list_of_lists)[source]#

Calculate the standard deviation of each list in a list of lists.

Parameters:

list_of_lists (list of list) – List of lists.

Returns:

result – Numpy array with the standard deviation of each list.

Return type:

np.ndarray