Skip to content

Commit

Permalink
main dafni ignore cycle option
Browse files Browse the repository at this point in the history
  • Loading branch information
jmafoster1 committed Nov 15, 2024
1 parent da3fb4d commit 9263747
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 17 deletions.
4 changes: 2 additions & 2 deletions causal_testing/json_front/json_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ def set_paths(self, json_path: str, dag_path: str, data_paths: list[str] = None)
data_paths = []
self.input_paths = JsonClassPaths(json_path=json_path, dag_path=dag_path, data_paths=data_paths)

def setup(self, scenario: Scenario, data=None):
def setup(self, scenario: Scenario, data=None, ignore_cycles=False):
"""Function to populate all the necessary parts of the json_class needed to execute tests"""
self.scenario = scenario
self._get_scenario_variables()
self.scenario.setup_treatment_variables()
self.causal_specification = CausalSpecification(
scenario=self.scenario, causal_dag=CausalDAG(self.input_paths.dag_path)
scenario=self.scenario, causal_dag=CausalDAG(self.input_paths.dag_path, ignore_cycles=ignore_cycles)
)
# Parse the JSON test plan
with open(self.input_paths.json_path, encoding="utf-8") as f:
Expand Down
20 changes: 5 additions & 15 deletions dafni/main_dafni.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def get_args(test_args=None) -> argparse.Namespace:
"--tests_path", required=True, help="Input configuration file path " "containing the causal tests (.json)"
)

parser.add_argument(
"-i", "--ignore_cycles", action="store_true", help="Whether to ignore cycles in the DAG.", default=False
)

parser.add_argument(
"--variables_path",
required=True,
Expand Down Expand Up @@ -72,17 +76,14 @@ def get_args(test_args=None) -> argparse.Namespace:
args.tests_path = Path(args.tests_path)

if args.dag_path is not None:

args.dag_path = Path(args.dag_path)

if args.output_path is None:

args.output_path = "./data/outputs/causal_tests_results.json"

Path(args.output_path).parent.mkdir(exist_ok=True)

else:

args.output_path = Path(args.output_path)

args.output_path.parent.mkdir(exist_ok=True)
Expand All @@ -98,13 +99,11 @@ def read_variables(variables_path: Path) -> FileNotFoundError | dict:
- dict - A valid dictionary consisting of the causal tests
"""
if not variables_path.exists() or variables_path.is_dir():

print(f"JSON file not found at the specified location: {variables_path}")

raise FileNotFoundError

with variables_path.open("r") as file:

inputs = json.load(file)

return inputs
Expand All @@ -118,7 +117,6 @@ def validate_variables(data_dict: dict) -> tuple:
- Tuple containing the inputs, outputs and constraints to pass into the modelling scenario
"""
if data_dict["variables"]:

variables = data_dict["variables"]

inputs = [
Expand All @@ -136,12 +134,9 @@ def validate_variables(data_dict: dict) -> tuple:
constraints = set()

for variable, input_var in zip(variables, inputs):

if "constraint" in variable:

constraints.add(input_var.z3 == variable["constraint"])
else:

raise ValidationError("Cannot find the variables defined by the causal tests.")

return inputs, outputs, constraints
Expand All @@ -154,7 +149,6 @@ def main():
args = get_args()

try:

# Step 0: Read in the runtime dataset(s)

data_frame = pd.concat([pd.read_csv(d) for d in args.data_path])
Expand Down Expand Up @@ -190,7 +184,7 @@ def main():
json_utility.set_paths(args.tests_path, args.dag_path, args.data_path)

# Step 6: Sets up all the necessary parts of the json_class needed to execute tests
json_utility.setup(scenario=modelling_scenario, data=data_frame)
json_utility.setup(scenario=modelling_scenario, data=data_frame, ignore_cycles=args.ignore_cycles)

# Step 7: Run the causal tests
test_outcomes = json_utility.run_json_tests(
Expand All @@ -200,7 +194,6 @@ def main():
# Step 8: Update, print and save the final outputs

for test in test_outcomes:

test.pop("estimator")

test["result"] = test["result"].to_dict(json=True)
Expand All @@ -210,17 +203,14 @@ def main():
test["result"].pop("control_value")

with open(args.output_path, "w", encoding="utf-8") as f:

print(json.dumps(test_outcomes, indent=2), file=f)

print(json.dumps(test_outcomes, indent=2))

except ValidationError as ve:

print(f"Cannot validate the specified input configurations: {ve}")

else:

print(f"Execution successful. " f"Output file saved at {Path(args.output_path).parent.resolve()}")


Expand Down

0 comments on commit 9263747

Please sign in to comment.