🧠 brainnet-graph
A lightweight, flexible Python package to construct brain connectivity graphs from ROI-level fMRI BOLD signals using multiple correlation-based methods.
🚀 Features
- Supports raw time-series inputs:
.csv,.tsv,.pkl - Build graphs using:
- Pearson correlation
- Partial correlation
- Cosine similarity
- Outputs in
.pt(PyTorch GeometricData) or.csv(edge list) - One-liner CLI usage
- Easily integrable in pipelines or tutorials
- Demo mode for quick testing
- Built-in input validation & flexible formatting
📦 Installation
pip install brainnet-graph
Requires Python 3.7+
🧠 Concept
Given ROI-level fMRI BOLD signal data ( X \in \mathbb{R}^{T \times N} ), where:
- ( T ) = number of time points
- ( N ) = number of brain regions (ROIs)
We construct an undirected, weighted graph ( G = (V, E, W) ) such that:
- Each node ( v_i \in V ) represents a brain region
- Edges ( (v_i, v_j) \in E ) are computed using a similarity or correlation measure between time series ( X_i ) and ( X_j )
🚀 Quick Start
🧪 Methods Supported
The following methods are supported to construct brain connectivity graphs from time-series data:
| Method | Description |
|---|---|
pearson_correlation |
Classic Pearson correlation: linear association(\rho(X_i, X_j)) |
spearman_correlation |
Rank-based Spearman correlation |
kendall_correlation |
Kendall’s tau correlation for ordinal association |
partial_correlation |
Removes the effect of other ROIs using inverse covariance |
cosine_similarity |
Measures angular similarity between ROI vectors |
correlations_correlation |
Second-order correlation: similarity between ROI correlation profiles |
associated_high_order_fc |
High-order FC using correlation of correlation vectors |
euclidean_distance |
Distance-based connectivity (low = more similar) |
knn_graph |
Builds graph using k-nearest neighbors (non-correlation) |
mutual_information |
Nonlinear dependency estimation via mutual information |
cross_correlation |
Temporal lagged correlation |
granger_causality |
Temporal causal inference using Granger's test |
generalised_synchronisation_matrix |
Dynamical systems synchrony —very slow |
patels_conditional_dependence_measures_kappa |
Conditional dependence metric (Patel’s(\kappa)) |
patels_conditional_dependence_measures_tau |
Conditional dependence metric (Patel’s(\tau)) |
lingam |
Causal inference using LiNGAM model —very slow |
📌 Note:
- Some methods like
lingamandgeneralised_synchronisation_matrixare computationally intensive. - All methods return a connectivity matrix which is then converted to a PyTorch Geometric graph object or edge list CSV.
- You can plug in your own method via code if desired (ask in GitHub Issues and we’ll help).
🛠️ From CLI
construct-graph --input bold.csv --output graph.csv --method pearson_correlation
📦 With Demo Data
construct-graph --demo
Save demo as PyTorch Geometric .pt:
construct-graph --demo --format pt
🧰 CLI Arguments
| Flag | Description |
|---|---|
--input, -i |
Path to input BOLD signal (.csv/.tsv/.pkl) |
--output, -o |
Output file path (.csv or .pt) |
--method, -m |
Method to use (see above) |
--format, -f |
Output format:csv or pt |
--demo |
Run using generated toy dataset |
🧬 Python API
from brainnet_graph.construction import load_data, validate_data, construct_graph, save_graph
df = load_data("subject001.csv")
validate_data(df)
graph = construct_graph(df, method_name="pearson_correlation")
save_graph(graph, "subject001_graph.csv", fmt="csv")
📁 Example
Input CSV (ROI x Time):
ROI_1, ROI_2, ROI_3, ...
0.24, 0.42, 0.11
0.30, 0.39, 0.14
...
→ Graph → Edgelist or PyTorch Data object
📚 Output Format
🔹 CSV output
source,target,weight
0,1,0.82
0,2,0.57
...
🔹 PyTorch Geometric .pt
Data(x=[10, 1], edge_index=[2, 45], edge_attr=[45, 1])
📷 Visual Example
Use NetworkX + PyTorch Geometric to visualize the graph:
import torch
import networkx as nx
from torch_geometric.utils import to_networkx
graph = torch.load("demo_graph.pt")
G = to_networkx(graph)
nx.draw(G, with_labels=True)
📖 Docs
Full documentation available at: 👉 https://brainnet-graph.readthedocs.io/
🛡 License
MIT License. See LICENSE for details.
🤝 Contributing
Open to contributions, new methods, bug reports, or real data testing.
🔗 Related
Metadata
Release files for brainnet-graph 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| brainnet_graph-0.2.2.tar.gz | 13.0 kB | Without provenance | Details for brainnet_graph-0.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| brainnet_graph-0.2.2-py3-none-any.whl | Python 3 | none | any | Details for brainnet_graph-0.2.2-py3-none-any.whl |
Total release size: 24.5 kB
Release files / brainnet_graph-0.2.2.tar.gz
| Download URL | brainnet_graph-0.2.2.tar.gz |
|---|---|
| Size | 13.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
151ca8e29616e5bc8d93996f50fee7d07a698cd335700d34cb6590b24767965a
|
|
BLAKE2b-256 checksum How to use checksums |
03e43a4df839bbf084a425f88f8ade250c11cc7a6350ee72fbf2e3baef2d3161
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Release files / brainnet_graph-0.2.2-py3-none-any.whl
| Download URL | brainnet_graph-0.2.2-py3-none-any.whl |
|---|---|
| Size | 11.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
efaca1423a66c23da9f2dce04a9545ad9369ac8b0a701634bc192761148dde1c
|
|
BLAKE2b-256 checksum How to use checksums |
2829d87e3f827281796fbf2bedc50e8dc189e51b98818d4f62c178a4d2f65c4b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.3
|
Security for brainnet-graph 0.2.2
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.