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.
- column(c)[source]¶
Access a 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:
c (
int) – Column to access. This should be non-negative and less thanncol().- Return type:
- Returns:
Contents of column
cfrom the matrix. This is always in double-precision, regardless of the underlying representation.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> ptr.column(0)
- column_maxs(num_threads=1)[source]¶
Convenience method to compute column maxima.
- Parameters:
num_threads (
int) – Number of threads.- Return type:
- Returns:
Double-precision array of column maxima.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> ptr.column_maxs()
- column_medians(num_threads=1)[source]¶
Convenience method to compute column medians.
- Parameters:
num_threads (
int) – Number of threads.- Return type:
- Returns:
Double-precision array of column medians.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> ptr.column_medians()
- column_medians_by_group(group, num_threads=1)[source]¶
Convenience method to compute the column-wise median for each group of row.
- Parameters:
- Return type:
- Returns:
Tuple of (i) a 2-dimensional array where each row represents a group and contains the column-wise medians for that group; and (ii) a list containing the unique levels of
grouprepresented by each row.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> group = ["A", "B"] * 5 >>> ptr.column_medians_by_group(group)
- column_mins(num_threads=1)[source]¶
Convenience method to compute column minima.
- Parameters:
num_threads (
int) – Number of threads.- Return type:
- Returns:
Double-precision array of column minima.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> ptr.column_mins()
- column_nan_counts(num_threads=1)[source]¶
Convenience method to count the number of NaNs on each column.
- Parameters:
num_threads (
int) – Number of threads.- Return type:
- Returns:
Integer array containing the number of NaNs in each 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.column_nan_counts()
- column_ranges(num_threads=1)[source]¶
Convenience method to compute column ranges.
- Parameters:
num_threads (
int) – Number of threads.- Return type:
- Returns:
Tuple of two double-precision arrays. The first contains the column minima and the second contains the column maxima.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> ptr.column_ranges()
- column_sums(num_threads=1)[source]¶
Convenience method to compute column sums.
- Parameters:
num_threads (
int) – Number of threads.- Return type:
- Returns:
Double-precision array of column sums.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> ptr.column_sums()
- column_sums_by_group(group, num_threads=1)[source]¶
Convenience method to compute the column-wise sum for each group of row.
- Parameters:
- Return type:
- Returns:
Tuple of (i) a 2-dimensional array where each row represents a group and contains the column-wise sums for that group; and (ii) a list containing the unique levels of
grouprepresented by each row.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> group = ["A", "B"] * 5 >>> ptr.column_sums_by_group(group)
- column_variances(num_threads=1)[source]¶
Convenience method to compute column variances.
- Parameters:
num_threads (
int) – Number of threads.- Return type:
- Returns:
Double-precision array of column variances.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> ptr.column_variances()
- column_variances_by_group(group, num_threads=1)[source]¶
Convenience method to compute the column-wise variance for each group of row.
- Parameters:
- Return type:
- Returns:
Tuple of (i) a 2-dimensional array where each row represents a group and contains the column-wise variances for that group; and (ii) a list containing the unique levels of
grouprepresented by each row.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> group = ["A", "B"] * 5 >>> ptr.column_variances_by_group(group)
- 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
- 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()
- 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
- row(r)[source]¶
Access a row 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:
r (
int) – Row to access. This should be non-negative and less thannrow().- 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.row(0)
- row_maxs(num_threads=1)[source]¶
Convenience method to compute row maxima.
- Parameters:
num_threads (
int) – Number of threads.- Return type:
- Returns:
Double-precision array of row maxima.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> ptr.row_maxs()
- row_medians(num_threads=1)[source]¶
Convenience method to compute row medians.
- Parameters:
num_threads (
int) – Number of threads.- Return type:
- Returns:
Double-precision array of row medians.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> ptr.row_medians()
- row_medians_by_group(group, num_threads=1)[source]¶
Convenience method to compute the row-wise median for each group of columns.
- Parameters:
- Return type:
- Returns:
Tuple of (i) a 2-dimensional array where each column represents a group and contains the row-wise medians for that group; and (ii) a list containing the unique levels of
grouprepresented by each column.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> group = ["A", "B"] * 10 >>> ptr.row_medians_by_group(group)
- row_mins(num_threads=1)[source]¶
Convenience method to compute row minima.
- Parameters:
num_threads (
int) – Number of threads.- Return type:
- Returns:
Double-precision array of row minima.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> ptr.row_mins()
- row_nan_counts(num_threads=1)[source]¶
Convenience method to count the number of NaNs on each row.
- Parameters:
num_threads (
int) – Number of threads.- Return type:
- Returns:
Integer array containing the number of NaNs in each row.
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.row_nan_counts()
- row_ranges(num_threads=1)[source]¶
Convenience method to compute row ranges.
- Parameters:
num_threads (
int) – Number of threads.- Return type:
- Returns:
Tuple of two double-precision arrays. The first contains the row minima and the second contains the row maxima.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> ptr.row_ranges()
- row_sums(num_threads=1)[source]¶
Convenience method to compute row sums.
- Parameters:
num_threads (
int) – Number of threads.- Return type:
- Returns:
Double-precision array of row sums.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> ptr.row_sums()
- row_sums_by_group(group, num_threads=1)[source]¶
Convenience method to compute the row-wise sum for each group of columns.
- Parameters:
- Return type:
- Returns:
Tuple of (i) a 2-dimensional array where each column represents a group and contains the row-wise sums for that group; and (ii) a list containing the unique levels of
grouprepresented by each column.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> group = ["A", "B"] * 10 >>> ptr.row_sums_by_group(group)
- row_variances(num_threads=1)[source]¶
Convenience method to compute row variances.
- Parameters:
num_threads (
int) – Number of threads.- Return type:
- Returns:
Double-precision array of row variances.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> ptr.row_variances()
- row_variances_by_group(group, num_threads=1)[source]¶
Convenience method to compute the row-wise variance for each group of columns.
- Parameters:
- Return type:
- Returns:
Tuple of (i) a 2-dimensional array where each column represents a group and contains the row-wise variances for that group; and (ii) a list containing the unique levels of
grouprepresented by each column.
Examples
>>> import numpy >>> import mattress >>> mat = numpy.random.rand(10, 20) >>> ptr = mattress.initialize(mat) >>> group = ["A", "B"] * 10 >>> ptr.row_variances_by_group(group)
- 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, **kwargs)
- mattress.initialize(x, **kwargs)
- mattress.initialize(x, **kwargs)
- mattress.initialize(x, **kwargs)
- mattress.initialize(x, **kwargs)
- mattress.initialize(x, **kwargs)
- mattress.initialize(x, _unknown_action='message', **kwargs)
- mattress.initialize(x, **kwargs)
- mattress.initialize(x, **kwargs)
- mattress.initialize(x, **kwargs)
- mattress.initialize(x, **kwargs)
- mattress.initialize(x, **kwargs)
- mattress.initialize(x, **kwargs)
- mattress.initialize(x, **kwargs)
- mattress.initialize(x, **kwargs)
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)