mopsy package¶
Submodules¶
mopsy.adders module¶
- mopsy.adders.append_col(mat: spmatrix, col: spmatrix | ndarray) spmatrix [source]¶
A generic append function for sparse matrices.
- Parameters:
mat – Sparse matrix.
col – Columns to append.
- Raises:
TypeError – If axis is neither 0 nor 1.
- Returns:
A new sparse matrix, usually the same type as the input matrix.
- mopsy.adders.append_row(mat: spmatrix, row: spmatrix | ndarray) spmatrix [source]¶
A generic append function for sparse matrices.
- Parameters:
mat – Sparse matrix.
row – Rows to append.
- Raises:
TypeError – If axis is neither 0 nor 1.
- Returns:
A new sparse matrix, usually the same type as the input matrix.
- mopsy.adders.sparse_append(mat: spmatrix, row_or_column: spmatrix | ndarray, axis: int | bool) spmatrix [source]¶
A generic append function for sparse matrices.
- Parameters:
mat – Sparse matrix.
row_or_column – Rows or columns to append.
axis – 0 for rows, 1 for columns.
- Raises:
TypeError – If axis is neither 0 nor 1.
- Returns:
A new sparse matrix, usually the same type as the input matrix.
mopsy.checkutils module¶
- mopsy.checkutils.check_axis(axis: int | bool)[source]¶
Check if axis has a correct value.
- Parameters:
axis – Axis, 0 for rows, 1 for columns
- Raises:
ValueError – If axis is neither 0 nor 1
mopsy.helpers module¶
- mopsy.helpers.apply(func: Callable, mat: ndarray | spmatrix, axis: int | bool, group: Sequence | None = None, non_zero: bool = False)[source]¶
A generic apply function.
- Parameters:
func – Function to apply over the groups.
mat – Input matrix.
group – Group vector, must be the same length as the number of rows or columns depending on the axis. Defaults to None.
axis – 0 for rows, 1 for columns.
non_zero – Whether to filter zero values. Defaults to False.
- Returns:
A matrix containing the result of the function.
- mopsy.helpers.colmean(mat: ndarray | spmatrix, group: Sequence | None = None, non_zero: bool = False) ndarray [source]¶
Apply column mean.
- Parameters:
mat – Input matrix.
group – Group vector, must be the same length as the number of columns. Defaults to None.
non_zero – Whether to filter zero values. Defaults to False.
- Returns:
A matrix with the column means.
- mopsy.helpers.colmedian(mat: ndarray | spmatrix, group: Sequence | None = None, non_zero: bool = False) ndarray [source]¶
Apply column median.
- Parameters:
mat – Input matrix.
group – Group vector, must be the same length as the number of columns. Defaults to None.
non_zero – Whether to filter zero values. Defaults to False.
- Returns:
A matrix with the column medians.
- mopsy.helpers.colsum(mat: ndarray | spmatrix, group: Sequence | None = None, non_zero: bool = False) ndarray [source]¶
Apply column sum.
- Parameters:
mat – Input matrix.
group – Group vector, must be the same length as the number of columns. Defaults to None.
non_zero – Whether to filter zero values. Defaults to False.
- Returns:
A matrix with the column sums.
- mopsy.helpers.multi_apply(funcs: Sequence[Callable], mat: ndarray | spmatrix, axis: int | bool, group: Sequence | None = None, non_zero: bool = False)[source]¶
A generic multi_apply to apply multiple function over the subset matrices.
- Parameters:
func – List of function to apply over the groups.
mat – Input matrix.
group – Group vector, must be the same length as the number of rows or columns depending on the axis. Defaults to None.
axis – 0 for rows, 1 for columns.
non_zero – Whether to filter zero values. Defaults to False.
- Returns:
A list of matrices, in the same order as the functions containing the result of each the function.
- mopsy.helpers.rowmean(mat: ndarray | spmatrix, group: Sequence | None = None, non_zero: bool = False) ndarray [source]¶
Apply row mean.
- Parameters:
mat – Input matrix.
group – Group vector, must be the same length as the number of rows. Defaults to None.
non_zero – Whether to filter zero values. Defaults to False.
- Returns:
A matrix with the row means.
- mopsy.helpers.rowmedian(mat: ndarray | spmatrix, group: Sequence | None = None, non_zero: bool = False) ndarray [source]¶
Apply row median.
- Parameters:
mat – Input matrix.
group – Group vector, must be the same length as the number of rows. Defaults to None.
non_zero – Whether to filter zero values. Defaults to False.
- Returns:
A matrix with the row medians.
- mopsy.helpers.rowsum(mat: ndarray | spmatrix, group: Sequence | None = None, non_zero: bool = False) ndarray [source]¶
Apply row sum.
- Parameters:
mat – Input matrix.
group – Group vector, must be the same length as the number of rows. Defaults to None.
non_zero – Whether to filter zero values. Defaults to False.
- Returns:
A matrix with the row sums.
mopsy.mops module¶
- class mopsy.mops.Mops(mat, non_zero: bool = False)[source]¶
Bases:
object
Base class for all matrix operations.
- apply(func: Callable[[list], Any], group: Sequence | None = None, axis: int | bool = 0) Tuple[ndarray, Sequence | None] [source]¶
Apply a function to groups along an axis.
- Parameters:
func – List of function to apply over the groups.
group – Group vector, must be the same length as the number of rows or columns depending on the axis. Defaults to None.
axis – 0 for rows, 1 for columns.
- Returns:
A tuple of matrix and its labels.
- groupby_indices(group: Sequence) dict [source]¶
From a group vector, get the list of indices that map to each group.
- Parameters:
group – Group vector, expected to ne the same as the number of rows or column.
- Returns:
A dictionary with each group name as the key and the values containing the list of indices that map to it.
- multi_apply(funcs: Sequence[Callable[[list], Any]], group: list | None = None, axis: int = 0) Tuple[ndarray, Sequence | None] [source]¶
Apply multiple functions, the first axis of the ndarray specifies the results of the inputs functions in the same order.
- Parameters:
func – List of function to apply over the groups.
group – Group vector, must be the same length as the number of rows or columns depending on the axis. Defaults to None.
axis – 0 for rows, 1 for columns.
- Returns:
A tuple of matrix and its labels.
mopsy.nops module¶
mopsy.sops module¶
- class mopsy.sops.Sops(mat: spmatrix, non_zero: bool = False)[source]¶
Bases:
Mops
Sops, Sparse Matrix Operation Class.
- apply(func: Callable[[list], Any], group: Sequence | None = None, axis: int | bool = 0) Tuple[ndarray, Sequence | None] [source]¶
Apply a function to groups along an axis.
- Parameters:
func – List of function to apply over the groups.
group – Group vector, must be the same length as the number of rows or columns depending on the axis. Defaults to None.
axis – 0 for rows, 1 for columns.
- Raises:
Exception – ApplyFuncError, when a function cannot be applied.
- Returns:
A tuple of matrix and its labels.
- static convert_sparse_type(mat: spmatrix, format: str)[source]¶
Convert to a sparse matrix format.
- Parameters:
mat – A numpy or scipy matrix.
format – Sparse matrix format, one of identify_sparse_type().
- Raises:
TypeError – If matrix is not sparse.
- Returns:
An internal matrix representation object.
- static identify_sparse_type(mat: spmatrix)[source]¶
Identify the sparse matrix format.
- Parameters:
mat – Input scipy matrix.
- Raises:
TypeError – If matrix is not sparse.
- Returns:
An internal matrix representation object
- iter(group: list | None = None, axis: int | bool = 0) Iterator[Tuple] [source]¶
Iterator over groups and an axis.
- Parameters:
group – Group variable. Defaults to None.
axis – 0 for rows, 1 for columns. Defaults to 0.
- Yields:
a tuple (str, matrix) of group and the submatrix.
- multi_apply(funcs: Sequence[Callable[[list], Any]], group: list | None = None, axis: int | bool = 0) Tuple[ndarray, Sequence | None] [source]¶
Apply multiple functions, the first axis of the ndarray specifies the results of the inputs functions in the same order.
- Parameters:
func – List of function to apply over the groups.
group – Group vector, must be the same length as the number of rows or columns depending on the axis. Defaults to None.
axis – 0 for rows, 1 for columns.
- Raises:
Exception – ApplyFuncError, when a function cannot be applied.
- Returns:
A tuple of matrix and its labels.
mopsy.utils module¶
- mopsy.utils.get_matrix_type(mat: ndarray | spmatrix, non_zero: bool = False)[source]¶
Get an internal matrix state.
- Parameters:
mat – An input numpy or scipy matrix.
non_zero – Whether to filter zero value. Defaults to False.
- Raises:
Exception – TypeNotSupported, when the matrix type is not supported.
- Returns:
An internal matrix representation object.