tetrad-port
C++ port of CMU's Tetrad causal inference library, with Python bindings via nanobind.
Algorithms
| Algorithm | Type | Output | Latent Confounders |
|---|---|---|---|
| PC | Constraint-based (Fisher Z) | CPDAG | No |
| FGES | Score-based (BIC) | CPDAG | No |
| GFCI | Hybrid (FGES + FCI rules) | PAG | Yes |
| BOSS | Permutation-based (BIC) | CPDAG | No |
| BOSS-FCI | BOSS + FCI rules | PAG | Yes |
| GRaSP | Permutation-based (tuck DFS) | CPDAG | No |
| GRaSP-FCI | GRaSP + FCI rules | PAG | Yes |
All algorithms support background knowledge: temporal tiers, forbidden edges, and required edges.
Quick Start
pip install -e ".[dev]"
import pandas as pd
from tetrad_port import TetradPort, Knowledge
tp = TetradPort()
df = pd.read_csv("data.csv")
# Run PC (constraint-based)
results, graph_info = tp.run_pc(df, alpha=0.05)
# Run FGES (score-based, faster for large graphs)
results, graph_info = tp.run_fges(df, penalty_discount=1.0)
# Run GFCI (handles latent confounders)
results, graph_info = tp.run_gfci(df, alpha=0.05)
# Run BOSS (permutation-based, often faster than FGES)
results, graph_info = tp.run_boss(df, penalty_discount=1.0)
# Run GRaSP (permutation-based with DFS tucks)
results, graph_info = tp.run_grasp(df, penalty_discount=1.0)
# Run BOSS-FCI or GRaSP-FCI (latent confounders)
results, graph_info = tp.run_boss_fci(df, alpha=0.05)
results, graph_info = tp.run_grasp_fci(df, alpha=0.05)
# Add background knowledge
k = Knowledge()
k.set_tier(0, ["Age", "Genetics"]) # Cannot be caused by later variables
k.set_tier(1, ["Exercise", "Diet"])
k.set_forbidden("Exercise", "Cholesterol")
k.set_required("Smoking", "BP")
results, graph_info = tp.run_pc(df, alpha=0.05, knowledge=k)
Building from Source
Prerequisites — Windows 11
Building the C++ extension on Windows requires the MSVC compiler and CMake.
Step 1 — Visual Studio Build Tools
Download Build Tools for Visual Studio 2022 (or 2026) from: https://visualstudio.microsoft.com/downloads/ → "Tools for Visual Studio" section
Run the installer and select the "Desktop development with C++" workload. No other workloads are needed (~4 GB installed).
Step 2 — CMake
Download the Windows x64 installer from https://cmake.org/download/ and during install choose "Add CMake to the system PATH for all users".
Step 3 — Verify (open a new terminal after install)
cl
cmake --version
Both commands should print version information. If cl is not found, use the "x64 Native Tools Command Prompt for VS 2022" from the Start menu — it pre-configures the compiler environment. All pip install and cmake commands below work from that prompt.
Python package (recommended)
Option A — install from requirements.txt (pinned environment)
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -r requirements.txt
requirements.txt includes a pinned tetrad_port git URL that is fetched and compiled automatically. This is the easiest path to a reproducible environment.
Option B — editable install from local source (development)
python -m venv .venv
.venv\Scripts\Activate.ps1
pip install -e ".[dev]"
Verify the install:
python -c "import tetrad_port; print(tetrad_port.__version__)"
pytest tests/test_python_bindings.py -v
C++ standalone
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release
.\build\Release\tetrad_tests.exe # Run all C++ tests
.\build\Release\run_pc.exe # Example CLI
On Linux/macOS:
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
./build/tetrad_tests
./build/run_pc
Documentation
- Python API Reference
- Examples
- Causal Discovery Tutorial — PC, FGES, and GFCI comparison
- Knowledge Tutorial — Temporal tiers, forbidden/required edges
Architecture
The C++ core lives in src/ under namespace tetrad:
src/graph/— Node, Edge, Graph (EdgeListGraph with TAIL/ARROW/CIRCLE endpoints)src/data/— DataSet (Eigen matrix wrapper), Knowledge (tiers, forbidden/required edges)src/search/— PC, FAS, MeekRules, FGES, FciOrient, GFCI, BOSS, BOSS-FCI, GRaSP, GRaSP-FCI, IndTestFisherZ, SemBicScore, GrowShrinkTreesrc/util/— ChoiceGenerator, SublistGenerator
Python bindings (bindings/tetrad_bindings.cpp) expose algorithms via nanobind. The TetradPort facade class (python/tetrad_port/__init__.py) provides a pandas-friendly API with SEM fitting helpers.
Dependencies
C++ (auto-fetched via CMake FetchContent):
- Eigen 3.4.0 — linear algebra
- Catch2 v3.5.2 — testing
- nanobind 2.0+ — Python bindings
Python:
- numpy, pandas (required)
- semopy (optional, SEM fitting)
Cross-Platform Result Comparison
To verify that algorithm outputs are consistent between Windows and Linux (or any two machines), use the scripts in tests/:
# On each machine — export canonical edge lists for all datasets
python tests/export_results.py --out tests/gfci_results_windows.json
python tests/export_results.py --out tests/gfci_results_linux.json
# Run multiple algorithms at once
python tests/export_results.py --algo gfci boss_fci grasp_fci --out tests/results_windows.json
# Compare two result files (copy both to the same machine first)
python tests/compare_platforms.py tests/gfci_results_windows.json tests/gfci_results_linux.json
python tests/compare_platforms.py tests/gfci_results_windows.json tests/gfci_results_linux.json --verbose
The comparison reports adjacency Jaccard and edge-type agreement per dataset. An exit code of 0 means all results are identical; 1 means differences were found.
Reference
Ported from Tetrad 7.6.3 (Java). Results validated against the Tetrad 7.6.3 JAR across all algorithms — see JavaCPPComparison.md.
Metadata
Release files for tetrad-port 0.3.1
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 |
|---|---|---|---|---|
| tetrad_port-0.3.1.tar.gz | 176.9 kB | With provenance | Details for tetrad_port-0.3.1.tar.gz |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tetrad_port-0.3.1-cp312-abi3-win_amd64.whl | CPython 3.12 | abi3 | Windows x86-64 | Details for tetrad_port-0.3.1-cp312-abi3-win_amd64.whl |
| tetrad_port-0.3.1-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl | CPython 3.12 | abi3 | Linux glibc 2.17+ x86-64 | Details for tetrad_port-0.3.1-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
| tetrad_port-0.3.1-cp312-abi3-macosx_11_0_arm64.whl | CPython 3.12 | abi3 | macOS 11.0+ ARM64 | Details for tetrad_port-0.3.1-cp312-abi3-macosx_11_0_arm64.whl |
Total release size: 7.0 MB
Release files / tetrad_port-0.3.1.tar.gz
| Download URL | tetrad_port-0.3.1.tar.gz |
|---|---|
| Size | 176.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8d47c6697d5ad117c73c77ecbc2b4ba39c96bd3235eed23cc86ed3d6f333c854
|
|
BLAKE2b-256 checksum How to use checksums |
4037171e162fe3c04c33216b910414e230101a17b54a78990a3933cb279e19e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 20, 2026.
Transparency logRelease files / tetrad_port-0.3.1-cp312-abi3-win_amd64.whl
| Download URL | tetrad_port-0.3.1-cp312-abi3-win_amd64.whl |
|---|---|
| Size | 2.2 MB |
| Tags | CPython 3.12 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
03c9138cf4d33550ce797314025b968e14257bf734df6db2eb96b1ca5e2c5ba5
|
|
BLAKE2b-256 checksum How to use checksums |
2e28e8ce544ec7e8a254dd276133ce9738f24b0e8364f24bc473fb3b6dd798d7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 20, 2026.
Transparency logRelease files / tetrad_port-0.3.1-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | tetrad_port-0.3.1-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 2.4 MB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
e47bbd09be675738f611460d7d48a5572e0fa2328af62dc3586c3ffe7056ab22
|
|
BLAKE2b-256 checksum How to use checksums |
1da8d127227f46c6f0b9869fbe40b204eba52c13a0b918c6dfe5e2648f3d4722
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 20, 2026.
Transparency logRelease files / tetrad_port-0.3.1-cp312-abi3-macosx_11_0_arm64.whl
| Download URL | tetrad_port-0.3.1-cp312-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 2.2 MB |
| Tags | CPython 3.12 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
6950df17fdd1cb7164ae10176f55220cb210fc14940af29bb2b93e6802a653e6
|
|
BLAKE2b-256 checksum How to use checksums |
110ef56748a2c06d74799e5df1d972544f14b43d578d62b98b766ede3ba0baf5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 20, 2026.
Transparency logSecurity for tetrad-port 0.3.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.
Full provenance
All 4 files in this release have been uploaded with provenance metadata.
Attestation for tetrad_port-0.3.1.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 20, 2026.
Transparency logA 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.