Balance and visualize multi-cluster interaction networks
Project description
netbalance
netbalance is a Python library and CLI tool for balancing and visualizing multi-cluster interaction (association) networks.
The core problem it solves is negative sampling: given a set of positive interaction edges between entities, produce a balanced dataset by selecting an appropriate set of negative (non-interacting) edges.
Installation
pip install netbalance
Data format
All inputs are CSVs where the first N columns are entity names (one per cluster) and the last column is a binary label (1 = positive interaction, 0 = negative):
A,B,interaction
A1,B2,1
A1,B4,0
The viz subcommand requires exactly 2 entity columns (bipartite).
CLI usage
# Balance using uniform random negative sampling
netbalance balance data.csv --method balanced
# Balance using simulated annealing for uniform per-entity degree entropy
netbalance balance data.csv --method entity-balanced -o balanced.csv
# Control the ratio of negatives to positives (default: 1.0)
netbalance balance data.csv -m balanced --negative-ratio 2.0
# Read from stdin
cat data.csv | netbalance balance - -m balanced
# Visualize a bipartite network (opens interactive window)
netbalance viz data.csv
# Save the figure
netbalance viz data.csv -o graph.png
netbalance viz data.csv -o graph.pdf --figsize 10 8
balance options
| Flag | Default | Description |
|---|---|---|
-m / --method |
required | balanced or entity-balanced |
--negative-ratio |
1.0 |
Negatives per positive |
--seed |
42 |
Random seed |
--heuristic-init |
on | Initialize SA with degree-guided sampling |
--max-iter |
1000 |
Simulated annealing iterations |
--initial-temp |
10.0 |
SA starting temperature |
--cooling-rate |
0.99 |
SA cooling factor per iteration |
--entropy-track PATH |
— | Save per-iteration entropy to CSV |
viz options
| Flag | Default | Description |
|---|---|---|
-o / --output |
— | Output path (PNG/PDF/SVG/JPG); omit for interactive |
--dpi |
150 |
Resolution for raster output |
--figsize W H |
auto | Figure size in inches |
--pos-color |
#66C2A5 |
Positive edge/ring colour |
--neg-color |
#D53E4F |
Negative edge/ring colour |
Python API
import numpy as np
from netbalance.balance import balanced, degree_guided, entity_balanced, compute_stats
from netbalance.cli import encode, decode
# Load and encode your CSV data
# associations: np.ndarray of shape (n_edges, n_clusters + 1)
# node_names: list of lists of entity name strings
result = balanced(associations, node_names, negative_ratio=1.0, seed=42)
result = degree_guided(associations, node_names, negative_ratio=1.0)
result = entity_balanced(associations, node_names, negative_ratio=1.0, max_iter=2000)
stats = compute_stats(result, node_names)
print(stats["ent"]) # mean entropy across clusters
Balancing strategies
| Function | Description |
|---|---|
balanced |
Uniform random sampling from the negative pool |
degree_guided |
Weighted sampling — negatives sharing more nodes with positives get higher probability |
entity_balanced |
Simulated annealing optimizing for uniform per-entity degree entropy (most expensive) |
License
MIT
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 Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file netbalance-0.1.2.tar.gz.
File metadata
- Download URL: netbalance-0.1.2.tar.gz
- Upload date:
- Size: 259.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4264c6419f5b65ace1d2a47a0c7e727a9f99a2eab21e819490aeddb2d04af5aa
|
|
| MD5 |
5f1c8cfa0d602bc321735328f8447f52
|
|
| BLAKE2b-256 |
fd9fd3cbdda0f090e3097bd3e3f153a58c6ef672d03d43855bdc3074b27adf6f
|
Provenance
The following attestation bundles were made for netbalance-0.1.2.tar.gz:
Publisher:
python-publish.yml on sobhanAhmadian/netbalance
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
netbalance-0.1.2.tar.gz -
Subject digest:
4264c6419f5b65ace1d2a47a0c7e727a9f99a2eab21e819490aeddb2d04af5aa - Sigstore transparency entry: 1629240847
- Sigstore integration time:
-
Permalink:
sobhanAhmadian/netbalance@d71319e66f8a3fddb583d5b292288d6e4d4b5b7b -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/sobhanAhmadian
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d71319e66f8a3fddb583d5b292288d6e4d4b5b7b -
Trigger Event:
release
-
Statement type:
File details
Details for the file netbalance-0.1.2-py3-none-any.whl.
File metadata
- Download URL: netbalance-0.1.2-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8965932a5c237ca435c40a452a7e0bfada4f192246eb45c291a1d6b050cc9d63
|
|
| MD5 |
434cbc0915a8f496677a2d51dbec4987
|
|
| BLAKE2b-256 |
f6050223c7e6d76d63cc39d2fb17ee68892c3c2767b87702427feb569763efef
|
Provenance
The following attestation bundles were made for netbalance-0.1.2-py3-none-any.whl:
Publisher:
python-publish.yml on sobhanAhmadian/netbalance
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
netbalance-0.1.2-py3-none-any.whl -
Subject digest:
8965932a5c237ca435c40a452a7e0bfada4f192246eb45c291a1d6b050cc9d63 - Sigstore transparency entry: 1629240859
- Sigstore integration time:
-
Permalink:
sobhanAhmadian/netbalance@d71319e66f8a3fddb583d5b292288d6e4d4b5b7b -
Branch / Tag:
refs/tags/v0.1.2 - Owner: https://github.com/sobhanAhmadian
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@d71319e66f8a3fddb583d5b292288d6e4d4b5b7b -
Trigger Event:
release
-
Statement type: