|
6 | 6 | import pandas as pd
|
7 | 7 |
|
8 | 8 | from causal_testing.specification.causal_specification import CausalDAG, Node
|
| 9 | +from causal_testing.data_collection.data_collector import ExperimentalDataCollector |
9 | 10 |
|
10 | 11 | @dataclass(order=True)
|
11 | 12 | class MetamorphicRelation:
|
@@ -57,43 +58,91 @@ def generate_follow_up(self,
|
57 | 58 | )
|
58 | 59 | source_test_inputs = source_follow_up_test_inputs[[self.treatment_var]]
|
59 | 60 | follow_up_test_inputs = source_follow_up_test_inputs[[follow_up_input]]
|
60 |
| - follow_up_test_inputs.rename({follow_up_input: self.treatment_var}) |
61 |
| - |
62 |
| - # TODO: Add a metamorphic test dataclass that stores these attributes |
63 |
| - self.tests = list( |
64 |
| - zip( |
65 |
| - source_test_inputs.to_dict(orient="records"), |
66 |
| - follow_up_test_inputs.to_dict(orient="records"), |
67 |
| - test_inputs.to_dict(orient="records") if not test_inputs.empty |
68 |
| - else [{}] * len(source_test_inputs), |
69 |
| - [self.output_var] * len(source_test_inputs), |
70 |
| - [str(self)] * len(source_test_inputs) |
71 |
| - ) |
72 |
| - ) |
| 61 | + follow_up_test_inputs = follow_up_test_inputs.rename(columns={follow_up_input: self.treatment_var}) |
| 62 | + source_test_inputs_record = source_test_inputs.to_dict(orient="records") |
| 63 | + follow_up_test_inputs_record = follow_up_test_inputs.to_dict(orient="records") |
| 64 | + if not test_inputs.empty: |
| 65 | + other_test_inputs_record = test_inputs.to_dict(orient="records") |
| 66 | + else: |
| 67 | + other_test_inputs_record = [{}] * len(source_test_inputs) |
| 68 | + metamorphic_tests = [] |
| 69 | + for i in range(len(source_test_inputs_record)): |
| 70 | + metamorphic_test = MetamorphicTest(source_test_inputs_record[i], |
| 71 | + follow_up_test_inputs_record[i], |
| 72 | + other_test_inputs_record[i], |
| 73 | + self.output_var, |
| 74 | + str(self) |
| 75 | + ) |
| 76 | + metamorphic_tests.append(metamorphic_test) |
| 77 | + self.tests = metamorphic_tests |
| 78 | + |
| 79 | + def execute_tests(self, data_collector: ExperimentalDataCollector): |
| 80 | + """Execute the generated list of metamorphic tests, returning a dictionary of tests that pass and fail. |
| 81 | +
|
| 82 | + :param data_collector: An experimental data collector for the system-under-test. |
| 83 | + """ |
| 84 | + test_results = {"pass": [], "fail": []} |
| 85 | + for metamorphic_test in self.tests: |
| 86 | + # Update the control and treatment configuration to take generated values for source and follow-up tests |
| 87 | + control_input_config = metamorphic_test.source_inputs | metamorphic_test.other_inputs |
| 88 | + treatment_input_config = metamorphic_test.follow_up_inputs | metamorphic_test.other_inputs |
| 89 | + data_collector.control_input_configuration = control_input_config |
| 90 | + data_collector.treatment_input_configuration = treatment_input_config |
| 91 | + metamorphic_test_results_df = data_collector.collect_data() |
| 92 | + print(metamorphic_test_results_df) |
| 93 | + # Compare control and treatment results |
| 94 | + control_output = metamorphic_test_results_df.loc["control_0"][metamorphic_test.output] |
| 95 | + treatment_output = metamorphic_test_results_df.loc["treatment_0"][metamorphic_test.output] |
| 96 | + if not self.assertion(control_output, treatment_output): |
| 97 | + test_results["fail"].append(metamorphic_test) |
| 98 | + else: |
| 99 | + test_results["pass"].append(metamorphic_test) |
| 100 | + return test_results |
73 | 101 |
|
74 | 102 | @abstractmethod
|
75 |
| - def test_oracle(self): |
76 |
| - """A test oracle i.e. a method that checks correctness of a test.""" |
| 103 | + def assertion(self, source_output, follow_up_output): |
| 104 | + """An assertion that should be applied to an individual metamorphic test run.""" |
77 | 105 | ...
|
78 | 106 |
|
79 | 107 | @abstractmethod
|
80 |
| - def execute_test(self): |
81 |
| - """Execute a test for this metamorphic relation.""" |
| 108 | + def test_oracle(self, test_results): |
| 109 | + """A test oracle that assert whether the MR holds or not based on ALL test results. |
| 110 | +
|
| 111 | + This method must raise an assertion, not return a bool.""" |
82 | 112 | ...
|
83 | 113 |
|
84 | 114 |
|
85 | 115 | @dataclass(order=True)
|
86 | 116 | class ShouldCause(MetamorphicRelation):
|
87 | 117 | """Class representing a should cause metamorphic relation."""
|
88 | 118 |
|
89 |
| - def test_oracle(self): |
90 |
| - pass |
| 119 | + def assertion(self, source_output, follow_up_output): |
| 120 | + """If there is a causal effect, the outputs should not be the same.""" |
| 121 | + return source_output != follow_up_output |
| 122 | + |
| 123 | + def test_oracle(self, test_results): |
| 124 | + ... |
91 | 125 |
|
92 |
| - def execute_test(self): |
93 |
| - pass |
94 | 126 |
|
95 | 127 | def __str__(self):
|
96 | 128 | formatted_str = f"{self.treatment_var} --> {self.output_var}"
|
97 | 129 | if self.adjustment_vars:
|
98 | 130 | formatted_str += f" | {self.adjustment_vars}"
|
99 | 131 | return formatted_str
|
| 132 | + |
| 133 | + |
| 134 | +@dataclass(order=True) |
| 135 | +class MetamorphicTest: |
| 136 | + """Class representing a metamorphic test case.""" |
| 137 | + source_inputs: dict |
| 138 | + follow_up_inputs: dict |
| 139 | + other_inputs: dict |
| 140 | + output: str |
| 141 | + relation: str |
| 142 | + |
| 143 | + def __str__(self): |
| 144 | + return f"Source inputs: {self.source_inputs}\n" \ |
| 145 | + f"Follow-up inputs: {self.follow_up_inputs}\n" \ |
| 146 | + f"Other inputs: {self.other_inputs}\n" \ |
| 147 | + f"Output: {self.output}" \ |
| 148 | + f"Metamorphic Relation: {self.relation}" |
0 commit comments