all your matrix representations belong here!
Project description
Python bindings for tatami
Overview
The mattress package implements Python bindings to the tatami C++ library for matrix representations. Downstream packages can use mattress to develop C++ extensions that are interoperable with many different matrix classes, e.g., dense, sparse, delayed or file-backed. It is based on the beachmat Bioconductor package, which does the same thing for R packages.
Instructions
mattress is published to PyPI, so installation is simple:
pip install mattress
mattress is intended for Python package developers writing C++ extensions that operate on matrices.
- Add
mattress.includes()
to theinclude_dirs=
of yourExtension()
definition insetup.py
. This will give you access to the various tatami headers to compile your C++ code. - Add
#include "Mattress.h"
to your C++ source files. This defines aMattress
class where theptr
member is a pointer to a tatami matrix. Python-visible C++ functions should expect to take aMattress*
or equivalent address (e.g.,uintptr_t
), after which theptr
should be extracted for use in tatami-compatible functions. - Call
mattress.tatamize()
on Python matrix objects within each of your functions that call tatami C++ code. This will wrap the Python matrix in a tatami-compatible C++ representation for use in the C++ code. The pointer to the C++ instance can be accessed through theptr
property of the returned object, which can then be passed to C++ code as anuintptr_t
to aMattress
instance.
So, for example, we can write ctypes bindings like:
#include "Mattress.h"
extern "C" {
int do_something_interesting(const void* mat) {
return reinterpret_cast<const Mattress*>(mat)->ptr->nrow();
}
}
Which we can subsequently call like:
import mattress
import ctypes as ct
lib = ct.CDLL("compiled.so")
lib.do_something_interesting.restype = ct.c_int
lib.do_something_interesting.argtypes = [ ct.c_void_p ]
def do_something_interesting(x):
mat = mattress.tatamize(x)
return do_something_interesting(x.ptr)
Supported matrices
Dense numpy matrices of varying numeric type:
import numpy as np
from mattress import tatamize
x = np.random.rand(1000, 100)
tatamat = tatamize(x)
ix = (x * 100).astype(np.uint16)
tatamat2 = tatamize(ix)
Compressed sparse matrices from scipy with varying index/data types:
from scipy import sparse as sp
from mattress import tatamize
xc = sp.random(100, 20, format="csc")
tatamat = tatamize(xc)
xr = sp.random(100, 20, format="csc", dtype=np.uint8)
tatamat2 = tatamize(xr)
To be added:
- File-backed matrices from the FileBackedArray package, including HDF5 and TileDB.
- Delayed arrays equivalent to the DelayedArray Bioconductor package.
- Arbitrary Python matrices?
Utility methods
The TatamiNumericPointer
instance returned by tatamize()
provides a few Python-visible methods for querying the C++ matrix.
tatamat.nrow() // number of rows
tatamat.column(1) // contents of column 1
tatamat.sparse() // whether the matrix is sparse.
These are mostly intended for non-intensive work or testing/debugging. It is expected that any serious computation should be performed by iterating over the matrix in C++.
Developer Notes
First, initialize the git submodules with:
git submodule update --init --recursive
Then, build the shared object file:
python setup.py build_ext --inplace
For testing, we usually do:
python setup.py build_ext --inplace && tox
To rebuild the ctypes bindings with the wrap.py
helper:
wrap.py src/mattress/lib --py src/mattress/cpphelpers.py --cpp src/mattress/lib/bindings.cpp
Note
This project has been set up using PyScaffold 4.5. For details and usage information on PyScaffold see https://pyscaffold.org/.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for mattress-0.0.7-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8973f9cbfcd58abb275d506c82352d4424f13dac80eeb9d88d6df1640c2b6437 |
|
MD5 | 519b716754c19c76decc3ddd7705417d |
|
BLAKE2b-256 | 182a479e2f94ff66e17e1b2a2bb7ce6d1d6b328ec4f9fba803c6d5267bf81a79 |
Hashes for mattress-0.0.7-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 94926f5224a3a789f760421b9430fe80870ae4fe54cfc1a1cb978ddf07d1418c |
|
MD5 | 5fd8d068df661c052db3be44c8183e33 |
|
BLAKE2b-256 | 8955659b3041f462878c7921316d9a8494901370a7c9f936458e8ae0a2043ae4 |
Hashes for mattress-0.0.7-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47fcbc70febaaefe116be6a256cddbfca20255d684e8153f809bb5bfad09342a |
|
MD5 | 868da158747b61df9eead0eee5fdc673 |
|
BLAKE2b-256 | 01ac0941850f60c3ca74be33c9d5865940ef78b7496affad091e76290a3671bf |
Hashes for mattress-0.0.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d69cc2ff91fb5daf970e83377ae1372c502b7a5f10c164d03a57cab0480750ad |
|
MD5 | d1ff718de87f709615997e71d6c89384 |
|
BLAKE2b-256 | 3e45f66933714f5aededb88aeb9ef6eef92ef8d3f18843009b62580ec8ed784a |
Hashes for mattress-0.0.7-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8677d16792d90df9d0341be2581c8337301734e67caefdcd8e0c1aa1ff51c68 |
|
MD5 | 7d620a1eb825953ed645fc466d151a02 |
|
BLAKE2b-256 | 8099529c7f9ed2b33ab3a53a9c8b944db2503d5f1ad1e4d027a87559cd1c4165 |
Hashes for mattress-0.0.7-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8b568a7e31e9fd070ab7c5eb5009492e67b797ed96dba56c4bf4d7a6754c014d |
|
MD5 | a64a711c64e8bfee138cd033cc6277ee |
|
BLAKE2b-256 | 04f08221978a87a3f523b1cb039ca540ebb568b70f967889f22e118e68054a66 |
Hashes for mattress-0.0.7-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97c4eab2ae06ec801c1a9cf72efc60b25a544b54e7665d13845cb1e8e58f7e45 |
|
MD5 | fabc09cedad9f4093365c9b6229c833a |
|
BLAKE2b-256 | ecb7e81b5c76aae81a51e80eb176bf0d64fb21807e92df61e645d13439326b0c |
Hashes for mattress-0.0.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 781ba4236a4ca38faecc3206dee9fedef5b705345371b93617faecaace5fb156 |
|
MD5 | 7c48d7caec1298c948dd4e833ef22d3b |
|
BLAKE2b-256 | 40d0accc8ead9831e45a2e0832d9a00ce507cafa55647e7ac6472e2fc58dac95 |
Hashes for mattress-0.0.7-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 945de1c5ae012c014f563a42236ab2a60e542401ffa06f02d79a17b99e892960 |
|
MD5 | 94dce2bb8f69c016b3d236717ff72c8a |
|
BLAKE2b-256 | fbe84ff1a360e68dffecd2dd368a5fdac3c79256d6609e55a42a2575b00e1bb9 |
Hashes for mattress-0.0.7-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 705d3c327bddd00104a8b95a8e841e46e0a9ccfc205f1929b1feaf547feeae68 |
|
MD5 | 9eae1401eecaf2e68cd74a4fda732dd9 |
|
BLAKE2b-256 | 8b1aeea19e2576518f549b652c0e202d6b61ea5a1882a11398fe3a9b27ff77dc |
Hashes for mattress-0.0.7-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1097b2d63372e24fb20df06f9af7800ba132636fb865baeeb5473d154aad9884 |
|
MD5 | 00a75fa54bd7a3b7ae7fec36c14fef53 |
|
BLAKE2b-256 | 1e3e9d757695b9914f511b0f0c673766e08ec5385ec16bb81c41c036c73e910d |
Hashes for mattress-0.0.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73eaf8c0ae8b0ea6f684bf7a38d6860d97d3f26b15d309477fad2583de9d078f |
|
MD5 | 8f2a2f989bd87e199ce79b6755c24aff |
|
BLAKE2b-256 | 7898b8dff11c4183e4b79c1f27bd2d0e5472cbf3fcfd1215e101b56800dc134f |
Hashes for mattress-0.0.7-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4bdbef6d38a9eb6a1986c12d90f0c067b44e679f9708bede4a4aa3c51634e1a9 |
|
MD5 | 5245ac8f6fbc95b4627a356ec671ce54 |
|
BLAKE2b-256 | 2178f47f454906365f9bd23a3bc47c5243a874ea12dc251a39f2b9a61fa5a46b |
Hashes for mattress-0.0.7-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 41e3f9bfed657003069fab28d969942718b938648c921e1afac3823965125a84 |
|
MD5 | 9eb480509dbd53bca9085b4d22a788ce |
|
BLAKE2b-256 | 1cf595fec8ce1dc9dbddb63d1c8f0ea039cf436229f19e9031ed8d64d6568ce0 |