|
| 1 | +import unittest |
| 2 | +from pathlib import Path |
| 3 | +from statistics import StatisticsError |
| 4 | +import scipy |
| 5 | +import os |
| 6 | + |
| 7 | +from causal_testing.testing.estimators import LinearRegressionEstimator |
| 8 | +from causal_testing.testing.base_test_case import BaseTestCase |
| 9 | +from causal_testing.testing.causal_test_case import CausalTestCase |
| 10 | +from causal_testing.testing.causal_test_suite import CausalTestSuite |
| 11 | +from causal_testing.testing.causal_test_adequacy import DAGAdequacy |
| 12 | +from causal_testing.testing.causal_test_outcome import NoEffect, Positive |
| 13 | +from tests.test_helpers import remove_temp_dir_if_existent |
| 14 | +from causal_testing.json_front.json_class import JsonUtility, CausalVariables |
| 15 | +from causal_testing.specification.variable import Input, Output, Meta |
| 16 | +from causal_testing.specification.scenario import Scenario |
| 17 | +from causal_testing.specification.causal_specification import CausalSpecification |
| 18 | + |
| 19 | + |
| 20 | +class TestJsonClass(unittest.TestCase): |
| 21 | + """Test the JSON frontend for the Causal Testing Framework (CTF) |
| 22 | +
|
| 23 | + The JSON frontend is an alternative interface for the CTF where tests are specified in JSON format and ingested |
| 24 | + with the frontend. Tests involve testing that this correctly interfaces with the framework with some dummy data |
| 25 | + """ |
| 26 | + |
| 27 | + def setUp(self) -> None: |
| 28 | + json_file_name = "tests.json" |
| 29 | + dag_file_name = "dag.dot" |
| 30 | + data_file_name = "data_with_categorical.csv" |
| 31 | + test_data_dir_path = Path("tests/resources/data") |
| 32 | + self.json_path = str(test_data_dir_path / json_file_name) |
| 33 | + self.dag_path = str(test_data_dir_path / dag_file_name) |
| 34 | + self.data_path = [str(test_data_dir_path / data_file_name)] |
| 35 | + self.json_class = JsonUtility("temp_out.txt", True) |
| 36 | + self.example_distribution = scipy.stats.uniform(1, 10) |
| 37 | + self.input_dict_list = [ |
| 38 | + {"name": "test_input", "datatype": float, "distribution": self.example_distribution}, |
| 39 | + {"name": "test_input_no_dist", "datatype": float}, |
| 40 | + ] |
| 41 | + self.output_dict_list = [{"name": "test_output", "datatype": float}] |
| 42 | + variables = CausalVariables(inputs=self.input_dict_list, outputs=self.output_dict_list, metas=[]) |
| 43 | + self.scenario = Scenario(variables=variables, constraints=None) |
| 44 | + self.json_class.set_paths(self.json_path, self.dag_path, self.data_path) |
| 45 | + self.json_class.setup(self.scenario) |
| 46 | + |
| 47 | + def test_data_adequacy_numeric(self): |
| 48 | + example_test = { |
| 49 | + "tests": [ |
| 50 | + { |
| 51 | + "name": "test1", |
| 52 | + "mutations": {"test_input": "Increase"}, |
| 53 | + "estimator": "LinearRegressionEstimator", |
| 54 | + "estimate_type": "coefficient", |
| 55 | + "effect_modifiers": [], |
| 56 | + "expected_effect": {"test_output": "NoEffect"}, |
| 57 | + "coverage": True, |
| 58 | + "skip": False, |
| 59 | + } |
| 60 | + ] |
| 61 | + } |
| 62 | + self.json_class.test_plan = example_test |
| 63 | + effects = {"NoEffect": NoEffect()} |
| 64 | + mutates = { |
| 65 | + "Increase": lambda x: self.json_class.scenario.treatment_variables[x].z3 |
| 66 | + > self.json_class.scenario.variables[x].z3 |
| 67 | + } |
| 68 | + estimators = {"LinearRegressionEstimator": LinearRegressionEstimator} |
| 69 | + |
| 70 | + test_results = self.json_class.run_json_tests( |
| 71 | + effects=effects, estimators=estimators, f_flag=False, mutates=mutates |
| 72 | + ) |
| 73 | + self.assertEqual( |
| 74 | + test_results[0]["result"].adequacy.to_dict(), |
| 75 | + {"kurtosis": {"test_input": 0.0}, "bootstrap_size": 100, "passing": 100}, |
| 76 | + ) |
| 77 | + |
| 78 | + def test_data_adequacy_cateogorical(self): |
| 79 | + example_test = { |
| 80 | + "tests": [ |
| 81 | + { |
| 82 | + "name": "test1", |
| 83 | + "mutations": ["test_input_no_dist"], |
| 84 | + "estimator": "LinearRegressionEstimator", |
| 85 | + "estimate_type": "coefficient", |
| 86 | + "effect_modifiers": [], |
| 87 | + "expected_effect": {"test_output": "NoEffect"}, |
| 88 | + "coverage": True, |
| 89 | + "skip": False, |
| 90 | + } |
| 91 | + ] |
| 92 | + } |
| 93 | + self.json_class.test_plan = example_test |
| 94 | + effects = {"NoEffect": NoEffect()} |
| 95 | + mutates = { |
| 96 | + "Increase": lambda x: self.json_class.scenario.treatment_variables[x].z3 |
| 97 | + > self.json_class.scenario.variables[x].z3 |
| 98 | + } |
| 99 | + estimators = {"LinearRegressionEstimator": LinearRegressionEstimator} |
| 100 | + |
| 101 | + test_results = self.json_class.run_json_tests( |
| 102 | + effects=effects, estimators=estimators, f_flag=False, mutates=mutates |
| 103 | + ) |
| 104 | + print("RESULT") |
| 105 | + print(test_results[0]["result"]) |
| 106 | + self.assertEqual( |
| 107 | + test_results[0]["result"].adequacy.to_dict(), |
| 108 | + {"kurtosis": {"test_input_no_dist[T.b]": 0.0}, "bootstrap_size": 100, "passing": 100}, |
| 109 | + ) |
| 110 | + |
| 111 | + def test_dag_adequacy(self): |
| 112 | + base_test_case = BaseTestCase( |
| 113 | + treatment_variable="test_input", |
| 114 | + outcome_variable="test_output", |
| 115 | + effect=None, |
| 116 | + ) |
| 117 | + causal_test_case = CausalTestCase( |
| 118 | + base_test_case=base_test_case, |
| 119 | + expected_causal_effect=None, |
| 120 | + estimate_type=None, |
| 121 | + ) |
| 122 | + test_suite = CausalTestSuite() |
| 123 | + test_suite.add_test_object(base_test_case, causal_test_case, None, None) |
| 124 | + dag_adequacy = DAGAdequacy(self.json_class.causal_specification.causal_dag, test_suite) |
| 125 | + dag_adequacy.measure_adequacy() |
| 126 | + print(dag_adequacy.to_dict()) |
| 127 | + self.assertEqual( |
| 128 | + dag_adequacy.to_dict(), |
| 129 | + { |
| 130 | + "causal_dag": self.json_class.causal_specification.causal_dag, |
| 131 | + "test_suite": test_suite, |
| 132 | + "tested_pairs": {("test_input", "test_output")}, |
| 133 | + "pairs_to_test": { |
| 134 | + ("test_input_no_dist", "test_input"), |
| 135 | + ("test_input", "B"), |
| 136 | + ("test_input_no_dist", "C"), |
| 137 | + ("test_input_no_dist", "B"), |
| 138 | + ("test_input", "C"), |
| 139 | + ("B", "C"), |
| 140 | + ("test_input_no_dist", "test_output"), |
| 141 | + ("test_input", "test_output"), |
| 142 | + ("C", "test_output"), |
| 143 | + ("B", "test_output"), |
| 144 | + }, |
| 145 | + "untested_edges": { |
| 146 | + ("test_input_no_dist", "test_input"), |
| 147 | + ("test_input", "B"), |
| 148 | + ("test_input_no_dist", "C"), |
| 149 | + ("test_input_no_dist", "B"), |
| 150 | + ("test_input", "C"), |
| 151 | + ("B", "C"), |
| 152 | + ("test_input_no_dist", "test_output"), |
| 153 | + ("C", "test_output"), |
| 154 | + ("B", "test_output"), |
| 155 | + }, |
| 156 | + "dag_adequacy": 0.1, |
| 157 | + }, |
| 158 | + ) |
| 159 | + |
| 160 | + def tearDown(self) -> None: |
| 161 | + remove_temp_dir_if_existent() |
| 162 | + if os.path.exists("temp_out.txt"): |
| 163 | + os.remove("temp_out.txt") |
0 commit comments