mattress package¶
- class mattress.InitializedMatrix(ptr)[source]¶
Bases:
objectPointer to an object containing a
tatami::Matrix, typically generated byinitialize()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 theptraddress to C++ and casting it to a pointer to amattress::BoundMatrix(see themattress.hheader). AllInitializedMatrixinstances 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 amattress::BoundMatrixinstance. This is typically obtained by passing astd::uintptr_tfrom 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:
- Return type:
- Returns:
Contents of row
rof 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. IfFalse, column-wise medians are computed for each group of rows instead.group (
Sequence) – Sequence of length equal to the number of columns (ifrow = True) or rows (otherwise) of the matrix, containing the group assignment for each column/row.num_threads (
int) – Number of threads.
- Return type:
- 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 thetatami::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 thetatami::Matrix. Each entry contains the column-wise median for a group.
The second tuple element is a list containing the unique levels of
grouprepresented 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. IfFalse, column-wise sums are computed for each group of rows instead.group (
Sequence) – Sequence of length equal to the number of columns (ifrow = True) or rows (otherwise) of the matrix, containing the group assignment for each column/row.num_threads (
int) – Number of threads.
- Return type:
- 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 thetatami::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 thetatami::Matrix. Each entry contains the column-wise sum for a group.
The second tuple element is a list containing the unique levels of
grouprepresented 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. IfFalse, column-wise variances are computed for each group of rows instead.group (
Sequence) – Sequence of length equal to the number of columns (ifrow = True) or rows (otherwise) of the matrix, containing the group assignment for each column/row.num_threads (
int) – Number of threads.
- Return type:
- 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 thetatami::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 thetatami::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
grouprepresented 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:
- 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:
- Return type:
- 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 anotherInitializedMatrixobject.right (
bool) – Whethervalis the right operand of the multiplication. IfFalse, thisInitializedMatrixis on the right andvalis on the left.num_threads (
int) – Number of threads to use.
- Return type:
- Returns:
A NumPy array containing the product. This is 1-dimensional if
valis 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:
- Return type:
- 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:
- 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:
- 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:
- 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::BoundMatrixinstance, to be passed as astd::uintptr_tto C++ - see themattress.hheader. This should not be used after theInitializedMatrixis deleted. This should not be used to construct a newInitializedMatrix.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:
- Return type:
- 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:
- Return type:
- 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:
- Return type:
- 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.hC++ header.- Return type:
- 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
InitializedMatrixfrom a Python matrix representation. This prepares the matrix for use in C++ code that can accept amattress::BoundMatrixinstance.- Parameters:
x (
Any) – Any matrix-like object._unknown_action (
Literal['none','message','warn','error']) – Action to take upon encountering an unknown matrix. If noterror, 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
xand_unknown_action = "error".- Return type:
- Returns:
An
InitializedMatrixcontaining a pointer tomattress::BoundMatrixC++ object.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(1000, 20) >>> ptr = mattress.initialize(mat)