Code for Measuring Similarity in Causal Graphs: A Framework for Semantic and Structural Analysis (Liu, Yang & Jalali, 2025).
How do you tell whether an LLM-generated causal graph matches a reference graph, when the two may use different variable names and different structure? This repo is a benchmark for that question. We reviewed 40+ semantic and structural similarity metrics, selected 9, and validated them on 2,000 LLM-generated causal loop diagrams (CLDs).
The scorer compares a reference graph and a generated graph on two axes:
| Axis | Metrics | Module |
|---|---|---|
| Semantic (variable names) | BLEU, fuzzy match, sentence-embedding dot product and cosine similarity | nlp_metrics.py |
| Structural (graph shape and polarity) | Pyramid Match, Shortest Path, Subgraph Matching, WL vertex/edge histogram kernels (via GraKeL), graph edit distance | graph_metrics.py |
git clone https://github.com/georgia-max/Causalgraph-comparison.git
cd Causalgraph-comparison
python3.11 -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\Activate
pip install -r requirements.txtnetworkx and pydot are pinned on purpose. Newer versions change the DOT parsing that the scorer relies on.
The input is a CSV with one row per graph pair and two columns of DOT strings:
label_cld: the reference (ground-truth) graphgenerated_cld_DOT: the graph to evaluate
Link polarity is encoded with arrowhead: vee for a positive link, tee for a negative one.
digraph {
"student enrollment" -> "school capacity strain" [arrowhead=vee]
"school capacity strain" -> "student enrollment" [arrowhead=tee]
}Run the scorer on the bundled example:
python get_metric_scores.py \
--input data/methods_example.csv \
--output_folder results/This writes results/metrics_results.xlsx with one column per metric, plus normalized semantic, structural and combined scores (norm_sum_score_nlp, norm_sum_score_graph, norm_sum_score), and prints a summary:
count 14.000000
mean 0.613696
min 0.378250
max 1.000000
Name: norm_sum_score, dtype: float64
Optional flags:
--swap_nodesfirst maps generated variable names onto semantically similar reference names, then rescores. Results before and after the mapping are both kept (columntype).--nlp_metric {cosine,dot,fuzzy}and--nlp_threshold 0.8control that mapping.
generate_synthetic_data.py uses few-shot prompting with an OpenAI model to create many alternative versions of each reference graph. These simulate different people mapping the same system. It needs an API key in a my_key.py file in the repo root, which is not committed:
MY_OPENAI_API_KEY = "sk-..."Then set INPUT_FILE, OUTPUT_FOLDER and NUM_ITERATIONS in run_pipeline.sh (macOS/Linux) or run_pipeline.ps1 (Windows) and run it. You get <input>_synthetic_data.csv and metrics_results.xlsx in the output folder.
get_metric_scores.py # entry point: score a CSV of graph pairs
nlp_metrics.py # semantic similarity metrics
graph_metrics.py # graph kernels and edit distance
my_functions.py # DOT <-> NetworkX conversion, node substitution
generate_synthetic_data.py # LLM-based synthetic CLD generation
synthetic_data_Visualization.py # plots for the synthetic dataset
data/methods_example.csv # small example input
@article{liu2025measuring,
title = {Measuring Similarity in Causal Graphs: A Framework for Semantic and Structural Analysis},
author = {Liu, Ning-Yuan Georgia and Yang, Flower and Jalali, Mohammad S.},
journal = {arXiv preprint arXiv:2503.11046},
year = {2025}
}MIT