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 cpptypes:
cpptypes 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.1.2-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d435dc4017272f826eadbb9a2e1a4016a970c06a931427e92bcbae9361220b0 |
|
MD5 | e5b36f88c159144f391368a520c96d1f |
|
BLAKE2b-256 | 4fe47f54fb6a66bb7416e89d1c88bd2a8b061a70393b1c3a2a827fb9f56b6ebf |
Hashes for mattress-0.1.2-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c5f90dfcd26153359faf7da501b1acbda018dd2644001bafad10fe24f9547b8 |
|
MD5 | 8a386e99b26d9a96c13b311a33e64701 |
|
BLAKE2b-256 | aaa189d10902846f667991b5d117c697e7363b62d5d45cfe2d0042fb9181831f |
Hashes for mattress-0.1.2-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79a8c7597a4a2d40128d6e40d594a6ede2fe856fdefd48f5e66ddfd880b9e32c |
|
MD5 | d964d725223b97ee107d6039838ae8f7 |
|
BLAKE2b-256 | 89577bd02fe46a78bbe09f5afa8b2fbe78d9d8ebf620b099957425c67097c8eb |
Hashes for mattress-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf514718eb4f1dee80f565cadd94bb65501a1fc5b70f32adb7c6824ef957bf2b |
|
MD5 | 8afd05e2e2870de63d877aac8f8d3161 |
|
BLAKE2b-256 | 44b5c12fd0056f04f86afab8115aa82aed8e3a56d1e020b2bee61270303b429b |
Hashes for mattress-0.1.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a575d24aefb545a50fdb905a2473942b5381a2d0195526a69bf09dc11405240 |
|
MD5 | 220430e50112ddb1e2404248fa302ac9 |
|
BLAKE2b-256 | 35b3fe8267936ce7199d1f40e998157ee2ff8942bfa537a44f79d09544b7933e |
Hashes for mattress-0.1.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea7f601a0255c5bae660d3cbe20c422298375f40f7898de294cf079e5c6c1003 |
|
MD5 | 0d4aaa8e26884827ad6dbaadfd2f7c56 |
|
BLAKE2b-256 | 21242843a14b9af9afc4b6c162e74e4f0057abac51608b35b46880db4e12930a |
Hashes for mattress-0.1.2-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0f2b0fd8524a9d9352f2762a2d1dc4cdeac3f779abbc45f0618ad3b8da62712 |
|
MD5 | 879a5172635d6d6965888d59b1365de4 |
|
BLAKE2b-256 | 8b3efd1029140263551fd1f547db146604e73cd85fce411d34c43a973f7058cb |
Hashes for mattress-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f7f904ddb900fd04220af5c6fc29a9c8363739d90c6ca83f2f8ffb07e652d4a |
|
MD5 | 9dad5184dce05d6f5d4dd2ac86698bc0 |
|
BLAKE2b-256 | 576fdd86576ac74b4f2882e0b0085fc6c47b385dfccd8db35f3b50befaa560f3 |
Hashes for mattress-0.1.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a497be0dadc679b786ff61af5969f7da74d3b225d9a8b721fb80b6d65df6056 |
|
MD5 | ebdde80c29a86c132c19d1542d33ee24 |
|
BLAKE2b-256 | a6d6b6bdf9b29af5c6c5410a9f698048a62ef010baa10cdf0bf08e1ca179c1ac |
Hashes for mattress-0.1.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1eac5771d81e7041c104158bcbcd6af6d07f64050320d3589bf03bf15598fa4c |
|
MD5 | be00e9794e73a322fa3ffabd89bd0b1e |
|
BLAKE2b-256 | f2394c08c55bc5215169df4a6c0d99c9f0af14f49cdfc77aa4146237c1076b32 |
Hashes for mattress-0.1.2-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8a7c25c539fadbb99d26892e892f7933bc29d418a05a3d35eb951a4bfa46bc3 |
|
MD5 | c798ebae9bcfcfc72edcf632d524a5d8 |
|
BLAKE2b-256 | 32f87a4340187d05e0ffa45635ae4f1e112b1dc9e2c43c5b9bd15cf91e5f0f1d |
Hashes for mattress-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04485ddc9b26cf2e78ca93b2f382c7a2d7d518aaee0b9d0fe316c4e6aa927336 |
|
MD5 | 459a4a6fe65730646e1a9fb6627a8315 |
|
BLAKE2b-256 | 323760e044f06578580e1ce44549456605b9dbc207f70dd4761f21c6047f7db2 |
Hashes for mattress-0.1.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bfbc191a9372c8452f0de72c0e08badf248265a7a43d4567f085768e73f1b3c5 |
|
MD5 | 298d9a6b0fcd8979e419beb97284fa39 |
|
BLAKE2b-256 | e67a5850ce4460df91ee1130a4387ef967279632c606bc719429e0e96ccbdd32 |
Hashes for mattress-0.1.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55d36950d928eee5fb3609ec39246d1422b3fbc210ad47d7220b7ccd379e0dbd |
|
MD5 | e2644738dc29bc56815979596e082580 |
|
BLAKE2b-256 | 48d5889e97920b4f8ad0efe5e583e091b44d69b155ecd6ec6becfc3262b23f31 |