Skip to content

Commit da471f7

Browse files
committed
Can now generate metamorphic tests from the metamorphic_relation.py file
1 parent cf9db99 commit da471f7

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

causal_testing/specification/metamorphic_relation.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010
import numpy as np
1111
import pandas as pd
1212
import networkx as nx
13+
import argparse
14+
import logging
15+
import json
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:
@@ -244,3 +249,35 @@ def generate_metamorphic_relations(dag: CausalDAG) -> list[MetamorphicRelation]:
244249
metamorphic_relations.append(ShouldCause(v, u, adj_set, dag))
245250

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

0 commit comments

Comments
 (0)