osl_dynamics.array_ops#

Helper functions for manipulating NumPy arrays.

Module Contents#

Functions#

get_one_hot(values[, n_states])

Expand a categorical variable to a series of boolean columns

cov2corr(cov)

Converts batches of covariance matrices into batches of correlation

cov2std(cov)

Get the standard deviation of batches of covariance matrices.

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

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.

osl_dynamics.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.array_ops.cov2corr(cov)[source]#

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.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.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.

osl_dynamics.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.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.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.

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

Return the length of a slice.

Parameters:

slice (slice) – Slice.

Returns:

length – Length.

Return type:

int

osl_dynamics.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.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.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