mopsy package

Submodules

mopsy.adders module

mopsy.adders.append_col(mat, col)[source]

A generic append function for sparse matrices.

Parameters:
Raises:

TypeError – If axis is neither 0 nor 1.

Return type:

spmatrix

Returns:

A new sparse matrix, usually the same type as the input matrix.

mopsy.adders.append_row(mat, row)[source]

A generic append function for sparse matrices.

Parameters:
Raises:

TypeError – If axis is neither 0 nor 1.

Return type:

spmatrix

Returns:

A new sparse matrix, usually the same type as the input matrix.

mopsy.adders.sparse_append(mat, row_or_column, axis)[source]

A generic append function for sparse matrices.

Parameters:
Raises:

TypeError – If axis is neither 0 nor 1.

Return type:

spmatrix

Returns:

A new sparse matrix, usually the same type as the input matrix.

mopsy.checkutils module

mopsy.checkutils.check_axis(axis)[source]

Check if axis has a correct value.

Parameters:

axis (Union[int, bool]) – Axis, 0 for rows, 1 for columns

Raises:

ValueError – If axis is neither 0 nor 1

mopsy.helpers module

mopsy.helpers.apply(func, mat, axis, group=None, non_zero=False)[source]

A generic apply function.

Parameters:
  • func (Callable) – Function to apply over the groups.

  • mat (Union[ndarray, spmatrix]) – Input matrix.

  • group (Sequence) – Group vector, must be the same length as the number of rows or columns depending on the axis. Defaults to None.

  • axis (Union[int, bool]) – 0 for rows, 1 for columns.

  • non_zero (bool) – Whether to filter zero values. Defaults to False.

Returns:

A matrix containing the result of the function.

mopsy.helpers.colmean(mat, group=None, non_zero=False)[source]

Apply column mean.

Parameters:
  • mat (Union[ndarray, spmatrix]) – Input matrix.

  • group (Sequence) – Group vector, must be the same length as the number of columns. Defaults to None.

  • non_zero (bool) – Whether to filter zero values. Defaults to False.

Return type:

ndarray

Returns:

A matrix with the column means.

mopsy.helpers.colmedian(mat, group=None, non_zero=False)[source]

Apply column median.

Parameters:
  • mat (Union[ndarray, spmatrix]) – Input matrix.

  • group (Sequence) – Group vector, must be the same length as the number of columns. Defaults to None.

  • non_zero (bool) – Whether to filter zero values. Defaults to False.

Return type:

ndarray

Returns:

A matrix with the column medians.

mopsy.helpers.colsum(mat, group=None, non_zero=False)[source]

Apply column sum.

Parameters:
  • mat (Union[ndarray, spmatrix]) – Input matrix.

  • group (Sequence) – Group vector, must be the same length as the number of columns. Defaults to None.

  • non_zero (bool) – Whether to filter zero values. Defaults to False.

Return type:

ndarray

Returns:

A matrix with the column sums.

mopsy.helpers.multi_apply(funcs, mat, axis, group=None, non_zero=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 (Union[ndarray, spmatrix]) – Input matrix.

  • group (Sequence) – Group vector, must be the same length as the number of rows or columns depending on the axis. Defaults to None.

  • axis (Union[int, bool]) – 0 for rows, 1 for columns.

  • non_zero (bool) – 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, group=None, non_zero=False)[source]

Apply row mean.

Parameters:
  • mat (Union[ndarray, spmatrix]) – Input matrix.

  • group (Sequence) – Group vector, must be the same length as the number of rows. Defaults to None.

  • non_zero (bool) – Whether to filter zero values. Defaults to False.

Return type:

ndarray

Returns:

A matrix with the row means.

mopsy.helpers.rowmedian(mat, group=None, non_zero=False)[source]

Apply row median.

Parameters:
  • mat (Union[ndarray, spmatrix]) – Input matrix.

  • group (Sequence) – Group vector, must be the same length as the number of rows. Defaults to None.

  • non_zero (bool) – Whether to filter zero values. Defaults to False.

Return type:

ndarray

Returns:

A matrix with the row medians.

mopsy.helpers.rowsum(mat, group=None, non_zero=False)[source]

Apply row sum.

Parameters:
  • mat (Union[ndarray, spmatrix]) – Input matrix.

  • group (Sequence) – Group vector, must be the same length as the number of rows. Defaults to None.

  • non_zero (bool) – Whether to filter zero values. Defaults to False.

Return type:

ndarray

Returns:

A matrix with the row sums.

mopsy.mops module

class mopsy.mops.Mops(mat, non_zero=False)[source]

Bases: object

Base class for all matrix operations.

__init__(mat, non_zero=False)[source]

Intialize the matrix.

Parameters:
  • mat – Input matrix.

  • non_zero (bool) – Whether to filter zero values. Defaults to False.

apply(func, group=None, axis=0)[source]

Apply a function to groups along an axis.

Parameters:
  • func (Callable[[list], Any]) – List of function to apply over the groups.

  • group (Sequence) – Group vector, must be the same length as the number of rows or columns depending on the axis. Defaults to None.

  • axis (Union[int, bool]) – 0 for rows, 1 for columns.

Return type:

Tuple[ndarray, Optional[Sequence]]

Returns:

A tuple of matrix and its labels.

groupby_indices(group)[source]

From a group vector, get the list of indices that map to each group.

Parameters:

group (Sequence) – Group vector, expected to ne the same as the number of rows or column.

Return type:

dict

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, group=None, axis=0)[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 (list) – Group vector, must be the same length as the number of rows or columns depending on the axis. Defaults to None.

  • axis (int) – 0 for rows, 1 for columns.

Return type:

Tuple[ndarray, Optional[Sequence]]

Returns:

A tuple of matrix and its labels.

mopsy.nops module

class mopsy.nops.Nops(mat, non_zero=False)[source]

Bases: Mops

Internal representation for numpy arrays.

__annotations__ = {}
__init__(mat, non_zero=False)[source]

Intialize the matrix.

Parameters:
  • mat (ndarray) – Input matrix.

  • non_zero (bool) – Whether to filter zero values. Defaults to False.

iter(group=None, axis=0)[source]

Iterator over groups and an axis.

Parameters:
  • group (Sequence[str]) – Group variable. Defaults to None.

  • axis (Union[int, bool]) – 0 for rows, 1 for columns. Defaults to 0.

Yields:

A tuple (str, Nops) of group and the submatrix.

Return type:

Iterator[Tuple]

mopsy.sops module

class mopsy.sops.Sops(mat, non_zero=False)[source]

Bases: Mops

Sops, Sparse Matrix Operation Class.

__annotations__ = {}
__init__(mat, non_zero=False)[source]

Initialize the class from a scipy sparse matrix.

Parameters:
  • mat (spmatrix) – Input scipy sparse matrix.

  • non_zero (bool) – Whether to filter zero values. Defaults to False.

apply(func, group=None, axis=0)[source]

Apply a function to groups along an axis.

Parameters:
  • func (Callable[[list], Any]) – List of function to apply over the groups.

  • group (Sequence) – Group vector, must be the same length as the number of rows or columns depending on the axis. Defaults to None.

  • axis (Union[int, bool]) – 0 for rows, 1 for columns.

Raises:

Exception – ApplyFuncError, when a function cannot be applied.

Return type:

Tuple[ndarray, Optional[Sequence]]

Returns:

A tuple of matrix and its labels.

static convert_sparse_type(mat, format)[source]

Convert to a sparse matrix format.

Parameters:
  • mat (spmatrix) – A numpy or scipy matrix.

  • format (str) – 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)[source]

Identify the sparse matrix format.

Parameters:

mat (spmatrix) – Input scipy matrix.

Raises:

TypeError – If matrix is not sparse.

Returns:

An internal matrix representation object

iter(group=None, axis=0)[source]

Iterator over groups and an axis.

Parameters:
  • group (list) – Group variable. Defaults to None.

  • axis (Union[int, bool]) – 0 for rows, 1 for columns. Defaults to 0.

Yields:

a tuple (str, matrix) of group and the submatrix.

Return type:

Iterator[Tuple]

multi_apply(funcs, group=None, axis=0)[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 (list) – Group vector, must be the same length as the number of rows or columns depending on the axis. Defaults to None.

  • axis (Union[int, bool]) – 0 for rows, 1 for columns.

Raises:

Exception – ApplyFuncError, when a function cannot be applied.

Return type:

Tuple[ndarray, Optional[Sequence]]

Returns:

A tuple of matrix and its labels.

mopsy.utils module

mopsy.utils.get_matrix_type(mat, non_zero=False)[source]

Get an internal matrix state.

Parameters:
  • mat (Union[ndarray, spmatrix]) – An input numpy or scipy matrix.

  • non_zero (bool) – Whether to filter zero value. Defaults to False.

Raises:

Exception – TypeNotSupported, when the matrix type is not supported.

Returns:

An internal matrix representation object.

Module contents