Skip to main content

Pure Python SPACE: sparse partial correlation estimation

Project description

space-graph

PyPI version Python 3.10+ License: GPL-3.0-or-later DOI

Discover which variables in your dataset are directly related to each other, even after accounting for all other variables. Given a data matrix, SPACE estimates a sparse network of partial correlations -- connections that remain after removing indirect effects. Designed for settings where the number of variables can far exceed the number of samples (e.g. genomics).

Pure Python implementation of SPACE (Sparse Partial Correlation Estimation) from Peng et al. (2009), with no R or C dependencies.

Paper: Sparse Partial Correlation Estimation for High-Dimensional Data

Install

pip install space-graph

Optional Numba (faster inner jsrm loop):

pip install 'space-graph[numba]'

From GitHub:

pip install git+https://github.com/shahrozeabbas/space-graph.git

Usage

import numpy as np
from space_graph import SPACE

X = np.random.randn(20, 5)

model = SPACE(
    alpha=0.7,
    max_outer_iter=2,
    max_inner_iter=500,
    tol=1e-6,
    weight='uniform',
)

model.fit(X)

print(model.partial_correlation_)

Penalty

alpha is overall regularization strength (like sklearn). gamma (γ) in [0, 1] sets the mix: lam1 = alpha * gamma, lam2 = alpha * (1 - gamma). Default gamma=1 matches R space::space.joint default lam2 = 0.

Options

  • gamma (default 1): mix γ in [0, 1] for lam1 / lam2; 1 matches R default lam2 = 0.
  • tol (default 1e-6): inner coordinate-descent stopping tolerance (and active-set threshold), same scale as the reference C code.
  • weight: default uniform (unit weights). Use equal as an alias. Other modes: sig, degree, or a custom positive vector of length p.
  • backend (default auto): inner JSRM shooting loop. numpy always uses pure NumPy (no Numba import). auto uses Numba when installed (lazy on first fit), otherwise NumPy. numba requires Numba and raises ImportError if it is missing. The first fit with auto or numba may include JIT compilation time.

Tests

pytest

For a quick manual run after an editable install (or with PYTHONPATH=. from the repo root), see test_space_graph.py.

Optional: build libjsrm_test.so from ../space/src/JSRM.c to run the ctypes cross-check in tests/test_space.py.

License

GPL-3.0-or-later (same family as the original space R package).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

space_graph-0.5.0.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

space_graph-0.5.0-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file space_graph-0.5.0.tar.gz.

File metadata

  • Download URL: space_graph-0.5.0.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for space_graph-0.5.0.tar.gz
Algorithm Hash digest
SHA256 8af00db47676a343297294aa76ffbeea78264ad26260567fa952f09f03f1cbc6
MD5 30a492cbb02b8430f7f4e5abff879fb1
BLAKE2b-256 26962fb1ee9c091fc664f8ddd2f2503f44cbba184403eacbb8d3530f5870b14e

See more details on using hashes here.

File details

Details for the file space_graph-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: space_graph-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for space_graph-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f282936e215566f0735629689368defaaeed88a276a58e88f9a9b82feebb7e3
MD5 0a7b649d52b7fa9775dbeea3b1e9d57f
BLAKE2b-256 4ab9bf0105fbf89f4cf156702fa4c4cfbe898018607818b382d57edea37719f5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page