Skip to main content

C++ port of CMU's Tetrad causal discovery library with Python bindings

Project description

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

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, GrowShrinkTree
  • src/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.

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

tetrad_port-0.3.1.tar.gz (176.9 kB view details)

Uploaded Source

Built Distributions

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

tetrad_port-0.3.1-cp312-abi3-win_amd64.whl (2.2 MB view details)

Uploaded CPython 3.12+Windows x86-64

tetrad_port-0.3.1-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.12+manylinux: glibc 2.17+ x86-64

tetrad_port-0.3.1-cp312-abi3-macosx_11_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.12+macOS 11.0+ ARM64

File details

Details for the file tetrad_port-0.3.1.tar.gz.

File metadata

  • Download URL: tetrad_port-0.3.1.tar.gz
  • Upload date:
  • Size: 176.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tetrad_port-0.3.1.tar.gz
Algorithm Hash digest
SHA256 8d47c6697d5ad117c73c77ecbc2b4ba39c96bd3235eed23cc86ed3d6f333c854
MD5 8110447914ae902c371d9609efc1f489
BLAKE2b-256 4037171e162fe3c04c33216b910414e230101a17b54a78990a3933cb279e19e8

See more details on using hashes here.

Provenance

The following attestation bundles were made for tetrad_port-0.3.1.tar.gz:

Publisher: publish.yml on kelvinlim/tetrad-port

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tetrad_port-0.3.1-cp312-abi3-win_amd64.whl.

File metadata

  • Download URL: tetrad_port-0.3.1-cp312-abi3-win_amd64.whl
  • Upload date:
  • Size: 2.2 MB
  • Tags: CPython 3.12+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for tetrad_port-0.3.1-cp312-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 03c9138cf4d33550ce797314025b968e14257bf734df6db2eb96b1ca5e2c5ba5
MD5 c5c34f3b7cd0fbac7a18ccb111d1fd5b
BLAKE2b-256 2e28e8ce544ec7e8a254dd276133ce9738f24b0e8364f24bc473fb3b6dd798d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for tetrad_port-0.3.1-cp312-abi3-win_amd64.whl:

Publisher: publish.yml on kelvinlim/tetrad-port

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tetrad_port-0.3.1-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for tetrad_port-0.3.1-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e47bbd09be675738f611460d7d48a5572e0fa2328af62dc3586c3ffe7056ab22
MD5 35bff0cc9d1d7923b62324530c7f5172
BLAKE2b-256 1da8d127227f46c6f0b9869fbe40b204eba52c13a0b918c6dfe5e2648f3d4722

See more details on using hashes here.

Provenance

The following attestation bundles were made for tetrad_port-0.3.1-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: publish.yml on kelvinlim/tetrad-port

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tetrad_port-0.3.1-cp312-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for tetrad_port-0.3.1-cp312-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6950df17fdd1cb7164ae10176f55220cb210fc14940af29bb2b93e6802a653e6
MD5 28a5a899b127aaaffe47183e9d569439
BLAKE2b-256 110ef56748a2c06d74799e5df1d972544f14b43d578d62b98b766ede3ba0baf5

See more details on using hashes here.

Provenance

The following attestation bundles were made for tetrad_port-0.3.1-cp312-abi3-macosx_11_0_arm64.whl:

Publisher: publish.yml on kelvinlim/tetrad-port

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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