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'.
You can also install it as a package using pip install sep-distances. The test suite is not part of the package on pypi, but it is still available on the git repo.
Below is a short description of the important source files and how to use them.
Files
-
- Contains the
LabelledMixedGraphclass: 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.
- Contains the
-
- 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.
- SHD (Structural Hamming Distance) for DAGs/CPDAGs/MAGs:
- Implements the distance/metric functions comparing two graphs. Major groups of functions:
-
- Unit tests covering behavior of
mixed_graph.pyandmetrics.py. The tests include many focused test cases (CPDAGs, DAGs, mixed graphs, AIDs, SHD, SD, s/c metrics, and related helpers). Run them withpytest(instructions below).
- Unit tests covering behavior of
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
gadjidpackage; install it to use those functions.
Running tests
- Install dependencies (recommended in a virtual environment):
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
- 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.
[1.0.1] - 18-11-2025
Fixed
- Tests included in the package would not work, missing ground truths. Incorporating test suite into package would have required major overhaul of files and project structure. Removed test suite from package because it's not that important. Tests are still available on the official git repo.
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 sep_distances-1.0.1.tar.gz.
File metadata
- Download URL: sep_distances-1.0.1.tar.gz
- Upload date:
- Size: 42.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb55d1873b32dbab86c12401fb8ebbe0ca4b53efe04ce794a85ccb8a942dc317
|
|
| MD5 |
08fda977ae0979c816c06b77c6148211
|
|
| BLAKE2b-256 |
9c95569a99009605ce98efcb318f8f14bfff85cc222194b09f116884bccd3b0a
|
File details
Details for the file sep_distances-1.0.1-py3-none-any.whl.
File metadata
- Download URL: sep_distances-1.0.1-py3-none-any.whl
- Upload date:
- Size: 51.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd3aadb68aba525b02b8504ed6bc6d5cc28ebb970698e8960fe571e5b52fd6ee
|
|
| MD5 |
26c606bf1a3ccf0d649147e3397880a5
|
|
| BLAKE2b-256 |
b6ccd8967c745ac06f657617ccb0e071ddbcd75cd0fa4763491ce135178a399b
|