Skip to main content

GitHub PyPI Conda GitHub Workflow Status fair-software.eu

sparsestack logo

Memory efficient stack of multiple 2D sparse arrays.

sparsestack-overview-figure

Installation

Requirements

Python 3.10 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.

  1. .add_dense_matrix(input_array) Can be used to add all none-zero elements of input_array to the sparsestack. Depending on the chosen join_type either all such values will be added (join_type="outer" or join_type="right"), or only those which are already present in underlying layers ("left" or "inner" join).
  2. .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 using join_type.
  3. .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.

  1. 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
  1. .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.
  2. .to_coo(name="layerX") Returns a scipy sparse COO-matrix of the specified layer.

Metadata

Release files for sparsestack 0.7.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sparsestack 0.7.1
File Size Uploaded Provenance Actions
sparsestack-0.7.1.tar.gz 9.4 kB Without provenance Details for sparsestack-0.7.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sparsestack 0.7.1
File Interpreter ABI Platform Actions
sparsestack-0.7.1-py3-none-any.whl Python 3 none any Details for sparsestack-0.7.1-py3-none-any.whl

Total release size: 19.3 kB

Release files / sparsestack-0.7.1.tar.gz

Download URL sparsestack-0.7.1.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
13c2edea1ad8be376e9fa28ce49231ceded9cd114c06d1335a74e0b96a2c6df9
BLAKE2b-256 checksum
How to use checksums
e611a98d17a1852b5fb3eb320049eceb94b975f70f2a58cbfb6c320b143642fa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.11.0 Linux/6.17.0-1015-azure

Release files / sparsestack-0.7.1-py3-none-any.whl

Download URL sparsestack-0.7.1-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
17d588732694873b38fa530b5d5ca8384a03265bfe994373d9a36ffa0d0d6065
BLAKE2b-256 checksum
How to use checksums
a0d7e4ca547cd1ec810f27df4b649369275f2135615e9a11bc643f799eb09c6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.4.1 CPython/3.11.0 Linux/6.17.0-1015-azure

Security for sparsestack 0.7.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page