mattress package

class mattress.InitializedMatrix(ptr)[source]

Bases: object

Pointer to an object containing a tatami::Matrix, typically generated by initialize() for use in C++ code. Instances of this class should only be created by developers and used within package functions; this is done by passing the ptr address to C++ and casting it to a pointer to a mattress::BoundMatrix (see the mattress.h header). All InitializedMatrix instances are expected to be transient within a Python session; they should not be serialized, nor should they be visible to end users. Each instance will automatically free the C++-allocated memory upon garbage collection.

__init__(ptr)[source]

This constructor should only be called by registered methods for initialize().

Parameters:

ptr (int) – Address of a mattress::BoundMatrix instance. This is typically obtained by passing a std::uintptr_t from C++ code.

property dtype: dtype

Type of the matrix, to masquerade as a NumPy-like object. This is always a double-precision type.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(1000, 20)
>>> ptr = mattress.initialize(mat)
>>> ptr.dtype
get(row, i)[source]

Extract a row or column from the tatami matrix. This method is primarily intended for troubleshooting and should not be used to iterate over the matrix in production code. (Do that in C++ instead.)

Parameters:
  • row (bool) – Whether to extract a row. If False, a column is extract instead.

  • i (int) – Row or column to extract. This should be non-negative and less than nrow() (if row = True) or ncol() (otherwise).

Return type:

ndarray

Returns:

Contents of row r of the matrix. This is always in double-precision, regardless of the underlying representation.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(1000, 20)
>>> ptr = mattress.initialize(mat)
>>> ptr.get(True, 0)
group_median(row, group, num_threads=1)[source]

Convenience method to compute the median for each group of rows or columns.

Parameters:
  • row (bool) – Whether to compute the row-wise median for each group of columns. If False, column-wise medians are computed for each group of rows instead.

  • group (Sequence) – Sequence of length equal to the number of columns (if row = True) or rows (otherwise) of the matrix, containing the group assignment for each column/row.

  • num_threads (int) – Number of threads.

Return type:

Tuple[ndarray, list]

Returns:

Tuple of length 2. The first element is a 2-dimensional array:

  • If row = True, each column of the array represents a group, while each row corresponds to a row of the tatami::Matrix. Each entry contains the row-wise median for a group.

  • If row = False, each row of the array represents a group, while each column corresponds to a column of the tatami::Matrix. Each entry contains the column-wise median for a group.

The second tuple element is a list containing the unique levels of group represented by each row/column in the aforementioned array.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(10, 20)
>>> ptr = mattress.initialize(mat)
>>> group = ["A", "B"] * 10
>>> ptr.group_median(True, group)
group_sum(row, group, num_threads=1)[source]

Convenience method to compute the sum for each group of rows or columns.

Parameters:
  • row (bool) – Whether to compute the row-wise sum for each group of columns. If False, column-wise sums are computed for each group of rows instead.

  • group (Sequence) – Sequence of length equal to the number of columns (if row = True) or rows (otherwise) of the matrix, containing the group assignment for each column/row.

  • num_threads (int) – Number of threads.

Return type:

Tuple[ndarray, list]

Returns:

Tuple of length 2. The first element is a 2-dimensional array:

  • If row = True, each column of the array represents a group, while each row corresponds to a row of the tatami::Matrix. Each entry contains the row-wise sum for a group.

  • If row = False, each row of the array represents a group, while each column corresponds to a column of the tatami::Matrix. Each entry contains the column-wise sum for a group.

The second tuple element is a list containing the unique levels of group represented by each row/column in the aforementioned array.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(10, 20)
>>> ptr = mattress.initialize(mat)
>>> group = ["A", "B"] * 10
>>> ptr.group_sum(True, group)
group_variance(row, group, num_threads=1)[source]

Convenience method to compute the variance for each group of rows or columns.

Parameters:
  • row (bool) – Whether to compute the row-wise variance for each group of columns. If False, column-wise variances are computed for each group of rows instead.

  • group (Sequence) – Sequence of length equal to the number of columns (if row = True) or rows (otherwise) of the matrix, containing the group assignment for each column/row.

  • num_threads (int) – Number of threads.

Return type:

Tuple[ndarray, list]

Returns:

Tuple of length 3. The first two elements are 2-dimensional arrays containing the per-group mean and variances, respectively:

  • If row = True, each column of the array represents a group, while each row corresponds to a row of the tatami::Matrix. Each entry contains the row-wise mean or variance for a group.

  • If row = False, each row of the array represents a group, while each column corresponds to a column of the tatami::Matrix. Each entry contains the column-wise mean or variance for a group.

The third tuple element is a list containing the unique levels of group represented by each row/column in the aforementioned arrays.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(10, 20)
>>> ptr = mattress.initialize(mat)
>>> group = ["A", "B"] * 10
>>> ptr.group_variance(True, group)
is_sparse()[source]

Is the matrix sparse?

Return type:

bool

Returns:

True if matrix is sparse.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(1000, 20)
>>> ptr = mattress.initialize(mat)
>>> ptr.is_sparse()
median(row, num_threads=1)[source]

Convenience method to compute row or column medians.

Parameters:
  • row (bool) – Whether to compute row medians. If False, column medians are computed instead.

  • num_threads (int) – Number of threads.

Return type:

ndarray

Returns:

Double-precision array of row or column medians.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(10, 20)
>>> ptr = mattress.initialize(mat)
>>> ptr.median(True)
multiply(val, right=True, num_threads=1)[source]

Compute a matrix-vector or matrix-matrix products.

Parameters:
  • val (ndarray | InitializedMatrix) – A 1-dimensional NumPy array representing a vector; or a 2-dimensional NumPy array representing a collection of vectors in its columns; or another InitializedMatrix object.

  • right (bool) – Whether val is the right operand of the multiplication. If False, this InitializedMatrix is on the right and val is on the left.

  • num_threads (int) – Number of threads to use.

Return type:

ndarray

Returns:

A NumPy array containing the product. This is 1-dimensional if val is 1-dimensional, otherwise it will be a 2-dimensional array.

nan_count(row, num_threads=1)[source]

Convenience method to count the number of NaNs on each row or column.

Parameters:
  • row (bool) – Whether to count NaNs per row. If False, counts per column are computed instead.

  • num_threads (int) – Number of threads.

Return type:

ndarray

Returns:

Integer array containing the number of NaNs in each row/column.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(10, 20)
>>> mat[0,0] = numpy.nan
>>> mat[9,19] = numpy.nan
>>> ptr = mattress.initialize(mat)
>>> ptr.nan_count(True)
ncol()[source]

Get the number of columns in the matrix.

Return type:

int

Returns:

Number of columns.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(1000, 20)
>>> ptr = mattress.initialize(mat)
>>> ptr.ncol()
nrow()[source]

Get the number of rows in the matrix.

Return type:

int

Returns:

Number of rows.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(1000, 20)
>>> ptr = mattress.initialize(mat)
>>> ptr.nrow()
prefer_rows()[source]

Does the matrix prefer row-by-row access, e.g., for row-major representations?

Return type:

bool

Returns:

True if matrix prefers row access.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(1000, 20)
>>> ptr = mattress.initialize(mat)
>>> ptr.prefer_rows()
property ptr: int

Address to a mattress::BoundMatrix instance, to be passed as a std::uintptr_t to C++ - see the mattress.h header. This should not be used after the InitializedMatrix is deleted. This should not be used to construct a new InitializedMatrix.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(1000, 20)
>>> ptr = mattress.initialize(mat)
>>> ptr.ptr
range(row, num_threads=1)[source]

Convenience method to compute row or column ranges.

Parameters:
  • row (bool) – Whether to compute row ranges. If False, column ranges are computed instead.

  • num_threads (int) – Number of threads.

Return type:

Tuple[ndarray, ndarray]

Returns:

Tuple of length 2. The first element is a double-precision array of row or column minima. The second element is a double-precision array of row or column maxima.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(10, 20)
>>> ptr = mattress.initialize(mat)
>>> ptr.range(True)
property shape: Tuple[int, int]

Shape of the matrix, to masquerade as a NumPy-like object.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(1000, 20)
>>> ptr = mattress.initialize(mat)
>>> ptr.shape
sum(row, num_threads=1)[source]

Convenience method to compute row or column sums.

Parameters:
  • row (bool) – Whether to compute row sums. If False, column sums are computed instead.

  • num_threads (int) – Number of threads.

Return type:

ndarray

Returns:

Double-precision array of row or column sums.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(10, 20)
>>> ptr = mattress.initialize(mat)
>>> ptr.sum(True)
variance(row, num_threads=1)[source]

Convenience method to compute row or column variances. This will also return the row/column means as a by-product.

Parameters:
  • row (bool) – Whether to compute row variances. If False, column variances are computed instead.

  • num_threads (int) – Number of threads.

Return type:

Tuple[ndarray, ndarray]

Returns:

Tuple of length 2. The first element is a double-precision array of row or column means. The second element is a double-precision array of row or column variances.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(10, 20)
>>> ptr = mattress.initialize(mat)
>>> ptr.variance(True)
mattress.includes()[source]

Provides access to the mattress.h C++ header.

Return type:

str

Returns:

Path to a directory containing the header.

mattress.initialize(x, _unknown_action='message', **kwargs)[source]
mattress.initialize(x: InitializedMatrix, **kwargs) → InitializedMatrix
mattress.initialize(x: ndarray, **kwargs) → InitializedMatrix
mattress.initialize(x: csr_array, **kwargs) → InitializedMatrix
mattress.initialize(x: csr_matrix, **kwargs) → InitializedMatrix
mattress.initialize(x: csc_array, **kwargs) → InitializedMatrix
mattress.initialize(x: csc_matrix, **kwargs) → InitializedMatrix
mattress.initialize(x: DelayedArray, _unknown_action='message', **kwargs) → InitializedMatrix
mattress.initialize(x: SparseNdarray, **kwargs) → InitializedMatrix
mattress.initialize(x: UnaryIsometricOpSimple, **kwargs) → InitializedMatrix
mattress.initialize(x: UnaryIsometricOpWithArgs, **kwargs) → InitializedMatrix
mattress.initialize(x: Subset, **kwargs) → InitializedMatrix
mattress.initialize(x: Combine, **kwargs) → InitializedMatrix
mattress.initialize(x: Transpose, **kwargs) → InitializedMatrix
mattress.initialize(x: BinaryIsometricOp, **kwargs) → InitializedMatrix
mattress.initialize(x: Round, **kwargs) → InitializedMatrix

Initialize an InitializedMatrix from a Python matrix representation. This prepares the matrix for use in C++ code that can accept a mattress::BoundMatrix instance.

Parameters:
  • x (Any) – Any matrix-like object.

  • _unknown_action (Literal['none', 'message', 'warn', 'error']) – Action to take upon encountering an unknown matrix. If not error, falls back to the unknown matrix handler with a message or warning. Otherwise, raises an error.

  • kwargs – Additional named arguments for individual methods.

Raises:

NotImplementedError – if no method is registered for the class of x and _unknown_action = "error".

Return type:

InitializedMatrix

Returns:

An InitializedMatrix containing a pointer to mattress::BoundMatrix C++ object.

Examples

>>> import numpy
>>> import mattress
>>> mat = numpy.random.rand(1000, 20)
>>> ptr = mattress.initialize(mat)