Skip to content

Commit cb762ab

Browse files
add log file logging
1 parent d931392 commit cb762ab

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

causal_testing/json_front/json_class.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
logger = logging.getLogger(__name__)
2121

22-
2322
class JsonUtility(ABC):
2423
"""
2524
The JsonUtility Class provides the functionality to use structured JSON to setup and run causal tests on the
@@ -37,7 +36,7 @@ class JsonUtility(ABC):
3736
:attr {CausalSpecification} causal_specification:
3837
"""
3938

40-
def __init__(self):
39+
def __init__(self, log_path):
4140
self.json_path = None
4241
self.dag_path = None
4342
self.data_path = None
@@ -48,6 +47,7 @@ def __init__(self):
4847
self.test_plan = None
4948
self.modelling_scenario = None
5049
self.causal_specification = None
50+
self.setup_logger(log_path)
5151

5252
def set_path(self, json_path: str, dag_path: str, data_path: str):
5353
"""
@@ -212,3 +212,9 @@ def add_modelling_assumptions(self, estimation_model: Estimator):
212212
:param estimation_model: estimator model instance for the current running test.
213213
"""
214214
return
215+
216+
@staticmethod
217+
def setup_logger(log_path: Path):
218+
logger = logging.getLogger(__name__)
219+
fh = logging.FileHandler(log_path + "logs.txt")
220+
logger.addHandler(fh)

examples/poisson/run_causal_tests.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from causal_testing.testing.estimators import Estimator
1111
from causal_testing.specification.scenario import Scenario
1212
from causal_testing.specification.variable import Input, Output, Meta
13-
13+
import logging
1414
data_path = "data.csv"
1515
dag_path = "dag.dot"
1616
json_path = "causal_tests.json"
@@ -105,11 +105,19 @@ def get_args() -> argparse.Namespace:
105105
"""
106106
parser = argparse.ArgumentParser(
107107
description="A script for parsing json config files for the Causal Testing Framework")
108-
parser.add_argument("-f", help="if included, the script will stop if a test fails",
109-
action="store_true")
108+
parser.add_argument(
109+
"-f", help="if included, the script will stop if a test fails", action="store_true")
110+
parser.add_argument(
111+
"--log_path", help="Specify a directory to change the location of the log file", default="./")
110112
return parser.parse_args()
111113

112114

115+
def logger_setup():
116+
logger = logging.getLogger(__name__)
117+
fh = logging.FileHandler()
118+
logger.addHandler(fh)
119+
120+
113121
inputs = [
114122
{"name": "width", "type": float, "distribution": "uniform"},
115123
{"name": "height", "type": float, "distribution": "uniform"},
@@ -188,7 +196,7 @@ def add_modelling_assumptions(self, estimation_model: Estimator):
188196
if __name__ == "__main__":
189197
args = get_args()
190198

191-
json_utility = MyJsonUtility() # Create an instance of the extended JsonUtility class
199+
json_utility = MyJsonUtility(args.log_path) # Create an instance of the extended JsonUtility class
192200
json_utility.set_path(json_path, dag_path, data_path) # Set the path to the data.csv, dag.dot and causal_tests.json file
193201

194202
# Load the Causal Variables into the JsonUtility class ready to be used in the tests

0 commit comments

Comments
 (0)