Skip to content

Commit cabc194

Browse files
authored
Merge pull request #195 from CITCOM-project/generate_metamorphic_relations
Can now generate metamorphic tests from the metamorphic_relation.py file
2 parents e09fd6d + 5956941 commit cabc194

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

causal_testing/specification/metamorphic_relation.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77
from abc import abstractmethod
88
from typing import Iterable
99
from itertools import combinations
10-
import numpy as np
11-
import pandas as pd
10+
import argparse
11+
import logging
12+
import json
1213
import networkx as nx
14+
import pandas as pd
15+
import numpy as np
1316

1417
from causal_testing.specification.causal_specification import CausalDAG, Node
1518
from causal_testing.data_collection.data_collector import ExperimentalDataCollector
1619

20+
logger = logging.getLogger(__name__)
21+
1722

1823
@dataclass(order=True)
1924
class MetamorphicRelation:
@@ -246,3 +251,35 @@ def generate_metamorphic_relations(dag: CausalDAG) -> list[MetamorphicRelation]:
246251
metamorphic_relations.append(ShouldCause(v, u, adj_set, dag))
247252

248253
return metamorphic_relations
254+
255+
256+
if __name__ == "__main__": # pragma: no cover
257+
logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO)
258+
parser = argparse.ArgumentParser(
259+
description="A script for generating metamorphic relations to test the causal relationships in a given DAG."
260+
)
261+
parser.add_argument(
262+
"--dag_path",
263+
"-d",
264+
help="Specify path to file containing the DAG, normally a .dot file.",
265+
required=True,
266+
)
267+
parser.add_argument(
268+
"--output_path",
269+
"-o",
270+
help="Specify path where tests should be saved, normally a .json file.",
271+
required=True,
272+
)
273+
args = parser.parse_args()
274+
275+
causal_dag = CausalDAG(args.dag_path)
276+
relations = generate_metamorphic_relations(causal_dag)
277+
tests = [
278+
relation.to_json_stub(skip=False)
279+
for relation in relations
280+
if len(list(causal_dag.graph.predecessors(relation.output_var))) > 0
281+
]
282+
283+
logger.info(f"Generated {len(tests)} tests. Saving to {args.output_path}.")
284+
with open(args.output_path, "w", encoding="utf-8") as f:
285+
json.dump({"tests": tests}, f, indent=2)

tests/specification_tests/test_metamorphic_relations.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ def test_should_not_cause_json_stub(self):
116116
"estimate_type": "coefficient",
117117
"estimator": "LinearRegressionEstimator",
118118
"expected_effect": {"Z": "NoEffect"},
119+
"formula": "Z ~ X1",
119120
"mutations": ["X1"],
120121
"name": "X1 _||_ Z",
121122
"formula": "Z ~ X1",

0 commit comments

Comments
 (0)