Python library to handle stacks of sparse COO arrays efficiently.
Project description
Memory efficient stack of multiple 2D sparse arrays.
Installation
Requirements
Python 3.8 or higher
Pip Install
Simply install using pip: pip install sparsestack
First code example
import numpy as np
from sparsestack import StackedSparseArray
# Create some fake data
scores1 = np.random.random((12, 10))
scores1[scores1 < 0.9] = 0 # make "sparse"
scores2 = np.random.random((12, 10))
scores2[scores2 < 0.75] = 0 # make "sparse"
sparsestack = StackedSparseArray(12, 10)
sparsestack.add_dense_matrix(scores1, "scores_1")
# Add second scores and filter
sparsestack.add_dense_matrix(scores2, "scores_2", join_type="left")
# Scores can be accessed using (limited) slicing capabilities
sparsestack[3, 4] # => scores_1 and scores_2 at position row=3, col=4
sparsestack[3, :] # => tuple with row, col, scores for all entries in row=3
sparsestack[:, 2] # => tuple with row, col, scores for all entries in col=2
sparsestack[3, :, 0] # => tuple with row, col, scores_1 for all entries in row=3
sparsestack[3, :, "scores_1"] # => same as the one before
# Scores can also be converted to a dense numpy array:
scores2_after_merge = sparsestack.to_array("scores_2")
Adding data to a sparsestack
-array
Sparsestack provides three options to add data to a new layer.
.add_dense_matrix(input_array)
Can be used to add all none-zero elements ofinput_array
to the sparsestack. Depending on the chosenjoin_type
either all such values will be added (join_type="outer"
orjoin_type="right"
), or only those which are already present in underlying layers ("left" or "inner" join)..add_sparse_matrix(input_coo_matrix)
This method will expect a COO-style matrix (e.g. scipy) which has attributes .row, .col and .data. The join type can again be specified usingjoin_type
..add_sparse_data(row, col, data)
This essentially does the same as.add_sparse_matrix(input_coo_matrix)
but might in some cases be a bit more flexible because row, col and data are separate input arguments.
Accessing data from sparsestack
-array
The collected sparse data can be accessed in multiple ways.
- Slicing.
sparsestack
allows multiple types of slicing (see also code example above).
sparsestack[3, 4] # => tuple with all scores at position row=3, col=4
sparsestack[3, :] # => tuple with row, col, scores for all entries in row=3
sparsestack[:, 2] # => tuple with row, col, scores for all entries in col=2
sparsestack[3, :, 0] # => tuple with row, col, scores_1 for all entries in row=3
sparsestack[3, :, "scores_1"] # => same as the one before
.to_array()
Creates and returns a dense numpy array of size.shape
. Can also be used to create a dense numpy array of only a single layer when used like.to_array(name="layerX")
.
Carefull: Obviously by converting to a dense array, the sparse nature will be lost and all empty positions in the stack will be filled with zeros..to_coo(name="layerX")
Returns a scipy sparse COO-matrix of the specified layer.
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
sparsestack-0.6.2.tar.gz
(9.8 kB
view details)
Built Distribution
File details
Details for the file sparsestack-0.6.2.tar.gz
.
File metadata
- Download URL: sparsestack-0.6.2.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.11.0 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c9d4694f7500f09165cfa9e128135cff6b8e6b7a69f3bceaf87aa77e2165720 |
|
MD5 | 21de241e92651953da0b589028275b0b |
|
BLAKE2b-256 | 96dda79fa3846d8c676abc77474110046a785a7ae9e8e8cacf54d801e14dce9c |
File details
Details for the file sparsestack-0.6.2-py3-none-any.whl
.
File metadata
- Download URL: sparsestack-0.6.2-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.11.0 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc4bdf97229bc96108753252a5404f2d1dffa56e98074aa3a422dce3f7b047ca |
|
MD5 | 669e7ef0bcabb4db6104137c97ddc2ed |
|
BLAKE2b-256 | bf6c7a9745c5ee4b5b6fdfe09a5a872aae6a6d867ffb58801b36df68ac2daed6 |