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.1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c52610d99d4b1a8c02068847c4786cf40cf6d5594c09d5ac4fddb90accd611f3 |
|
MD5 | eff1db82d951a8e683585c32a7cdeb29 |
|
BLAKE2b-256 | 1648fdac87d7ac79b9f0836d6ec581517417f0583479ab8230c4118700dc53c0 |
Hashes for mattress-0.1.1-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 573c88410ea39db3c9c964218a3abfcf8a3b2cc844657a79a7b5cd8c24c71668 |
|
MD5 | add1ce8137e344c801159985914cbdb6 |
|
BLAKE2b-256 | d50b5f54c61edb0f03f50f149a123e4881805067e6080f300bcee7531526ff72 |
Hashes for mattress-0.1.1-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 349488ce60f85b7c621b6cdba48525896996a7cb38c4bc46d262fcc65dbecbbd |
|
MD5 | 6c567febf0779fe3625bf10b4a98d189 |
|
BLAKE2b-256 | 798da97da9e0df5c79191355453dbe0e7b6ba28b27ff942a21cb47c49fb2258b |
Hashes for mattress-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 01dfda3c77b4019f6ab7a75e56e8975a40732ebe58ce5eb9ca4d800ed063a46a |
|
MD5 | 62286d3389898d4ea8ff9b41f84da02d |
|
BLAKE2b-256 | ba705e46f74d63f41c0d784f7d4f9661fb6d9ca3e100431934ea62804f0ff58f |
Hashes for mattress-0.1.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb7d5149239f7684273332eb0297454dd71251aba57b2693e967a2c7105f87cf |
|
MD5 | ab0e46e06b7e6568def4e9d57111cbe4 |
|
BLAKE2b-256 | 67eb8a1ec7c004d58bc9ea532a81c4adf71acd62930bc70ad397caf344737951 |
Hashes for mattress-0.1.1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 374901303eca11189e5db87618733bed8500a37f7e45918301ca3d31313ce719 |
|
MD5 | 4036d68e753676c06d6fc3628f8a58b5 |
|
BLAKE2b-256 | 3b60de372ec9689475b5c7e5ab82ebc2d4cb8ef677557bf22fdd55785ea17118 |
Hashes for mattress-0.1.1-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ff925fc2b7977d944f31ae0c735c310b4f60350c87109d35e9d5f14872b5cad |
|
MD5 | 5c3ca352a0510af03778081de7396f76 |
|
BLAKE2b-256 | 2f707bf6cfbada1216efdb21ea14285bac00fd54237f0d6bf28c9e3b0eea9ba3 |
Hashes for mattress-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79460ddf6d510722f6c2f0a617b12e437b20a38dda28b5150f8eb22a99b12cb4 |
|
MD5 | 65428c774c47258ab81ef4b35ed1173e |
|
BLAKE2b-256 | 99b03c5730fad327037ab8834e9579a1d553a6ed67b47c87c3ce62bbc47daf8b |
Hashes for mattress-0.1.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2a6fb2db7039b83c04456d9c19f811e9a48c37545791ebc9b535d7820861ab8 |
|
MD5 | 740c3800f10ac64642d72e1fac706143 |
|
BLAKE2b-256 | 35ce9f87af9864112d455a9396f8ec6646e42278b42a579bd318fe6fddfc4d33 |
Hashes for mattress-0.1.1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 843b1459b21335aa96826917f93e9dd44bff23291d5a97d7541404a2c2a64aaa |
|
MD5 | 97196c313786c6b4a51fba0fbb4ffc23 |
|
BLAKE2b-256 | cc1bfafeb444c8efc66eff59a56a0bf602db8ba72c76619cc55877903beada46 |
Hashes for mattress-0.1.1-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 55ec125066a1fd9fcb10d1f59f261d1c248778b82ff6a18a70c5f31936241f11 |
|
MD5 | 78f7056e9b424a45a4a6bc1a63254bd3 |
|
BLAKE2b-256 | ed6234b61b4f2457070e2a1cf351b60d27b5570a4404ae7e9e5522cafb55747f |
Hashes for mattress-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06e12d7f074c8d1c1c5b62c1ced7aedf52fec734638cbc30dcc40cb7748c354f |
|
MD5 | da21de32caaed7c502204ed2cba8e7ff |
|
BLAKE2b-256 | 213fc96071cdae4b0da3723be201561858e3eb2476f2bf7127b28f0dd5b7d022 |
Hashes for mattress-0.1.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4439257524b1f3ba3ad19eff795492311673bcfd88f6a71e6d87b1ba1480861b |
|
MD5 | e0deef18aaa056c3a651d01c72b63e19 |
|
BLAKE2b-256 | d80149fdb8caa1fe3749ca35e0a9f2a41501af02a958bc8c2021167e1517009e |
Hashes for mattress-0.1.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96675c1d218e1306c2240a68517432afe64c4a589c691cc4748d803f1b524149 |
|
MD5 | 0a8cb92af845d6cc625854e5bed7789d |
|
BLAKE2b-256 | 4c72630a38dd36c44f0703762a0b3ce6618e0abc00ee4022eef201f6f35dc960 |