rsatoolbox.util.matrix module¶
Collection of different utility Matrices
- rsatoolbox.util.matrix.centering(size)[source]¶
generates a centering matrix
- Parameters:
size (int) – size of the center matrix
- Returns:
size * size
- Return type:
centering_matrix (numpy.ndarray)
- rsatoolbox.util.matrix.indicator(index_vector, positive=False)[source]¶
Indicator matrix with one column per unique element in vector
- Parameters:
index_vector (numpy.ndarray) – n_row vector to code - discrete values (one dimensional)
positive (bool) – should the function ignore zero negative entries in the index_vector? Default: false
- Returns:
- nrow x nconditions
indicator matrix
- Return type:
indicator_matrix (numpy.ndarray)
- rsatoolbox.util.matrix.pairwise_contrast(index_vector)[source]¶
Contrast matrix with one row per unqiue pairwise contrast
- Parameters:
index_vector (numpy.ndarray) – n_row vector to code discrete values (one dimensional)
- Returns:
indicator_matrix: n_values * (n_values-1)/2 x n_row contrast matrix
- Return type:
numpy.ndarray
- rsatoolbox.util.matrix.pairwise_contrast_sparse(index_vector)[source]¶
Contrast matrix with one row per unqiue pairwise contrast
- Parameters:
index_vector (numpy.ndarray) – n_row vector to code discrete values (one dimensional)
- Returns:
- indicator_matrix:
n_values * (n_values-1)/2 x n_row contrast matrix
- Return type:
scipy.sparse.csr_matrix
- rsatoolbox.util.matrix.row_col_indicator_g(n_cond)[source]¶
generates a row and column indicator matrix for a vectorized second moment matrix. The vectorized version has the off-diagonal elements first (like in an RDM), and then appends the diagnoal. You can vectorize a second momement matrix G by np.diag(row_i@G@col_i.T) = np.sum(col_i*(row_i@G)),axis=1)
- Parameters:
n_cond (int) – Number of conditions underlying the second moment
- Returns:
n_cond (n_cond-1)/2+n_cond * n_cond col_indicator (numpy.ndarray): n_cond (n_cond-1)/2+n_cond * n_cond
- Return type:
row_indicator (numpy.ndarray)
- rsatoolbox.util.matrix.row_col_indicator_rdm(n_cond)[source]¶
generates a row and column indicator matrix for an RDM vector
- Parameters:
n_cond (int) – Number of conditions underlying the RDM
- Returns:
n_cond (n_cond-1)/2 * n_cond col_indicator (numpy.ndarray): n_cond (n_cond-1)/2 * n_cond
- Return type:
row_indicator (numpy.ndarray)
- rsatoolbox.util.matrix.square_between_category_binary_mask(category_1_idxs, category_2_idxs, size)[source]¶
- A square binary matrix indicating between-category links in an adjacency
matrix.
- Parameters:
category_1_idxs (List[int]) – indices of category 1 members in rows/columns.
category_2_idxs (List[int]) – indices of category 2 members in rows/columns.
size (int) – total size of matrix
Usage example:
>>> square_between_category_binary_mask(category_1_idxs=[1, 2], category_2_idxs=[3, 4], size=5) array([[0., 0., 0., 0., 0.], [0., 0., 0., 1., 1.], [0., 0., 0., 1., 1.], [0., 1., 1., 0., 0.], [0., 1., 1., 0., 0.]])
- Returns:
A square binary numpy.array indicating between-category links in an adjacency matrix.
- rsatoolbox.util.matrix.square_category_binary_mask(category_idxs: List[int], size: int)[source]¶
A square binary matrix indicating within-category links in an adjacency matrix.
- Parameters:
category_idxs (List[int]) – indices of category members in rows/columns.
size (int) – total size of matrix.
Usage example:
>>> square_category_binary_mask(category_idxs=[1, 2, 4], size=5) array([[0., 0., 0., 0., 0.], [0., 1., 1., 0., 1.], [0., 1., 1., 0., 1.], [0., 0., 0., 0., 0.], [0., 1., 1., 0., 1.]])
- Returns:
A square binary numpy.array indicating within-category pairs.