Skip to main content

An implementation of separation distances and s/c-metrics for causal graphs as introduced in the paper 'Separation-based distance metrics for causal graphs'.

Project description

This repository provides an implementation of separation distances and s/c-metrics for causal graphs as introduced in the paper 'Separation-based distance metrics for causal graphs'.

Below is a short description of the important source files and how to use them.

Files

  • codebase/mixed_graph.py

    • Contains the LabelledMixedGraph class: a compact representation for directed, undirected, bidirected and semidirected edges. Useful helpers include conversion to/from NetworkX, adding/removing edges, methods to compute skeletons and CPDAGs, finding v-structures, computing Markov blankets, BayesBall, minimal d-separators and several converters (get_canonical_directed_graph, get_acyclification, etc.). This is the main graph data structure used by the metrics.
  • codebase/metrics.py

    • Implements the distance/metric functions comparing two graphs. Major groups of functions:
      • SHD (Structural Hamming Distance) for DAGs/CPDAGs/MAGs: SHD_DAGs, SHD_CPDAGs, SHD_MAGs.
      • AID (Adjustment Identification Distances) wrappers using gadjid (e.g. parent_AID_DAGs).
      • Separation distances (SD) for DAGs, CPDAGs, and mixed graphs: SD_DAGs, SD_CPDAGs, SD_mixed_graphs.
      • s/c-metrics and variants (s-metric, c-metric, sc-metric) for DAGs, CPDAGs, mixed graphs and graphs with cycles (metric_DAGs, metric_CPDAGs, metric_mixed_graphs, metric_directed_cyclic_graphs).
      • Utilities: generate_triples (create separation statements) and several helper wrappers.
  • tests/

    • Unit tests covering behavior of mixed_graph.py and metrics.py. The tests include many focused test cases (CPDAGs, DAGs, mixed graphs, AIDs, SHD, SD, s/c metrics, and related helpers). Run them with pytest (instructions below).

Quick examples

Basic usage pattern in example.py (import, construct graphs, compute a metric):

from codebase import mixed_graph as mg
from codebase import metrics as metrics

# create two simple DAGs
G1 = mg.LabelledMixedGraph(nodes={"A", "B", "C"})
G1.add_directed("A", "B")
G1.add_directed("B", "C")

G2 = mg.LabelledMixedGraph(nodes={"A", "B", "C"})
G2.add_directed("A", "B")
G2.add_directed("A", "C")

# Compute SHD (Structural Hamming Distance) between DAGs
shd = metrics.SHD_DAGs(G1, G2, normalized=True)
print("SHD (normalized):", shd)

# Compute separation distance (SD) using parent separation
sd = metrics.SD_DAGs(G1, G2, type='parent', normalized=True)
print("SD (parent, normalized):", sd)

# Compute an sc-metric between DAGs (default uses all orders)
sc = metrics.metric_DAGs(G1, G2, type='sc', normalized=True)
print("sc-metric:", sc)

Notes:

  • Many metric functions expect the same node set in both graphs. They typically check graph1.nodes == graph2.nodes.
  • For CPDAGs some functions compute or require a representative DAG of the MEC (see get_representative_of_MEC).
  • AID functions rely on the external gadjid package; install it to use those functions.

Running tests

  1. Install dependencies (recommended in a virtual environment):
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
  1. Run the test suite with pytest from the project root:
pytest -q

If you only want to run a single test file, use e.g. pytest -q tests/test_SHD.py.

Currently, the test suite does not have any tests configured. Instead you should use run it once to check if all the requirements are installed correctly and as example usage for the respective metrics.

If you want to run a test file directly, you need to change the way the imports are called. Currently the imports are structured in a way that work well with pytest, but won't work if you run the file directly.

Example change in tests/test_AIDs.py.:

''' Comment out current imports '''
# from codebase import mixed_graph as mixed
# from codebase import metrics

# from ..tests import Graphs_for_testing as G_testing

''' Replace with the following '''
import os
import sys

# add project root to sys.path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from codebase import mixed_graph as mixed
from codebase import metrics

import Graphs_for_testing as G_testing

Installation / requirements

  • Python 3.10+ is required (see setup.cfg).
  • The project depends on packages listed in requirements.txt / setup.cfg. Notable dependencies:
    • networkx, numpy, scipy, gadjid (optional but required for AID functions).

License

This project is released under the GNU General Public License v3 (GPLv3). See LICENSE.txt for the full text.

Contact & attribution

Original research and initial code: Jonas Wahl & Jakob Runge Package implementation and maintenance: Muhammad Haris Owais Ahmed

If you have questions, bug reports, or performance suggestions, please open an issue or contact the maintainers listed in setup.cfg.

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

sep_distances-1.0.0.tar.gz (39.6 kB view details)

Uploaded Source

Built Distribution

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

sep_distances-1.0.0-py3-none-any.whl (46.7 kB view details)

Uploaded Python 3

File details

Details for the file sep_distances-1.0.0.tar.gz.

File metadata

  • Download URL: sep_distances-1.0.0.tar.gz
  • Upload date:
  • Size: 39.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for sep_distances-1.0.0.tar.gz
Algorithm Hash digest
SHA256 af8e1b4b90e781d6cd9ec8560e996ff23f0b76ec80b689c74d16144ae8b2762c
MD5 f74a880b096fc3d86fb9d6598270bfdb
BLAKE2b-256 ff0dab984b4b3bf864c211ed35bc8825a07ef56a4efc3992d82761808504b4c0

See more details on using hashes here.

File details

Details for the file sep_distances-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: sep_distances-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 46.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for sep_distances-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0337e37e2e6ecde2cb8a8815383a221954026207048bbf4bbfc1cbc3cb9b2a7e
MD5 46f87a1c11b676e68dd129aa51e3f4d2
BLAKE2b-256 0a1acfc1e25b6aec37b25b8bf2ed2fa2151b1356daf1128e98a59f3c90b52ab8

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