causal_testing.causal_testing_framework
This module implements the CausalTestingFramework class, which is the main interaction point for causal testing.
Module Contents
Classes
Main class for running causal tests. |
Functions
|
Read data into a dataframe. |
Attributes
- causal_testing.causal_testing_framework.logger
- causal_testing.causal_testing_framework.data_readers
- causal_testing.causal_testing_framework.read_dataframe(file_path: str, content: bytes = None, **kwargs: dict) pandas.DataFrame
Read data into a dataframe.
- Parameters:
file_path – The path to the data.
content – The bytes content of the file.
kwargs – Keyword arguments to be passed to the read_ function.
- Returns:
The read-in DataFrame.
- class causal_testing.causal_testing_framework.CausalTestingFramework(dag: causal_testing.specification.causal_dag.CausalDAG = None, test_cases: list[causal_testing.testing.causal_test_case.CausalTestCase] = None, df: pandas.DataFrame = None)
Main class for running causal tests.
- setup(dag_path: str = None, data_paths: list[str] = None, test_cases_path: str = None, ignore_cycles: bool = False, query: str = None, **kwargs: dict)
Shortcut for loading in the DAG, data, and test cases.
- Parameters:
dag_path – Path to the DAG definition file.
data_paths – List of paths to input data files.
test_cases_path – Path to the test configuration file
ignore_cycles – Whether to ignore cycles in the causal graph. NOTE: Setting this to True severely limits the testing that can be performed.
query – Optional pandas query string to filter the loaded data
kwargs – Keyword arguments to be passed to the read_ function.
- load_dag(dag_path: str, ignore_cycles: bool = False)
Load the causal DAG from the specified file path.
- Parameters:
dag_path – Path to the DAG definition file.
ignore_cycles – Whether to ignore cycles in the causal graph. NOTE: Setting this to True severely limits the testing that can be performed.
- load_data(data_paths: list[str], query: str = None, **kwargs: dict)
Load and combine all data sources with optional filtering.
- Parameters:
data_paths – List of paths to input data files.
query – Optional pandas query string to filter the loaded data
kwargs – Keyword arguments to be passed to the read_ function.
- load_test_cases_from_json(test_cases_path: str)
Load and prepare test configurations from JSON file.
- Parameters:
test_cases_path – Path to the test configuration file
- create_causal_test(test: dict) causal_testing.testing.causal_test_case.CausalTestCase
Create causal test case from test configuration and base test.
- Parameters:
test – Dictionary containing test configuration parameters
- Returns:
CausalTestCase object
- Raises:
ValueError if invalid estimator or configuration is provided
- ready_to_run() bool
Test whether framework is ready to run test cases. :returns: True if the DAG, data, and test cases are defined.
- generate_causal_tests()
Automatically generate the suite of causal tests that corresponds to the DAG.
- run_tests(silent: bool = False, adequacy: bool = False, bootstrap_size: int = 100)
Run all test cases and return their results.
- Parameters:
silent – Whether to suppress errors
adequacy – Whether to calculate causal test adequacy (defaults to False)
bootstrap_size – The number of bootstrap samples to use when calculating causal test adequacy (defaults to 100)
- Raises:
ValueError if no tests are loaded
- Raises:
Exception if test execution fails
- evaluate_dag(bootstrap_size: bool = 100, alpha: float = 0.05) pandas.Series
Calculate confidence intervals for how well a causal DAG fits a dataset by repeatedly resampling the dataset and executing the causal tests. Confidence intervals are then calcualted for how many tests pass, fail, or are inestimable.
- Parameters:
bootstrap_size – The number of bootstrap samples to use when calculating causal test adequacy (defaults to 100)
alpha – The significance level to use when calculating confidence intervals. (defaults to 0.05).
- save_results(output_path: str, include_adequacy_results: bool = False)
Save test results to JSON file in the expected format.
- Parameters:
output_path – Path for output file (.json).
include_adequacy_results – Whether to include the effect estimate and test outcome for adequacy bootstraps.
- test_dataframe() pandas.DataFrame
- Returns:
The causal test cases as a dataframe. Nested objects such as results are indexed as, e.g. result.outcome.