|
| 1 | +import pandas as pd |
| 2 | +import numpy as np |
| 3 | +import matplotlib.pyplot as plt |
| 4 | +from causal_testing.specification.causal_dag import CausalDAG |
| 5 | +from causal_testing.specification.scenario import Scenario |
| 6 | +from causal_testing.specification.variable import Input, Output |
| 7 | +from causal_testing.specification.causal_specification import CausalSpecification |
| 8 | +from causal_testing.data_collection.data_collector import ObservationalDataCollector |
| 9 | +from causal_testing.testing.causal_test_case import CausalTestCase |
| 10 | +from causal_testing.testing.causal_test_outcome import Positive, Negative, NoEffect |
| 11 | +from causal_testing.testing.intervention import Intervention |
| 12 | +from causal_testing.testing.causal_test_engine import CausalTestEngine |
| 13 | +from causal_testing.testing.estimators import LinearRegressionEstimator |
| 14 | +from matplotlib.pyplot import rcParams |
| 15 | +OBSERVATIONAL_DATA_PATH = "./data/normalised_results.csv" |
| 16 | + |
| 17 | +rc_fonts = { |
| 18 | + "font.size": 8, |
| 19 | + "figure.figsize": (6, 4), |
| 20 | + "text.usetex": True, |
| 21 | + "font.family": "serif", |
| 22 | + "text.latex.preamble": r"\usepackage{libertine}", |
| 23 | +} |
| 24 | +rcParams.update(rc_fonts) |
| 25 | + |
| 26 | + |
| 27 | +def effects_on_APD90(observational_data_path, treatment_var, control_val, treatment_val, expected_causal_effect): |
| 28 | + """ Perform causal testing for the scenario in which we investigate the causal effect of G_K on APD90. |
| 29 | +
|
| 30 | + :param observational_data_path: Path to observational data containing previous executions of the LR91 model. |
| 31 | + :param treatment_var: The input variable whose effect on APD90 we are interested in. |
| 32 | + :param control_val: The control value for the treatment variable (before intervention). |
| 33 | + :param treatment_val: The treatment value for the treatment variable (after intervention). |
| 34 | + :return: ATE for the effect of G_K on APD90 |
| 35 | + """ |
| 36 | + # 1. Define Causal DAG |
| 37 | + causal_dag = CausalDAG('./dag.dot') |
| 38 | + |
| 39 | + # 2. Specify all inputs |
| 40 | + g_na = Input('G_Na', float) |
| 41 | + g_si = Input('G_si', float) |
| 42 | + g_k = Input('G_K', float) |
| 43 | + g_k1 = Input('G_K1', float) |
| 44 | + g_kp = Input('G_Kp', float) |
| 45 | + g_b = Input('G_b', float) |
| 46 | + |
| 47 | + # 3. Specify all outputs |
| 48 | + max_voltage = Output('max_voltage', float) |
| 49 | + rest_voltage = Output('rest_voltage', float) |
| 50 | + max_voltage_gradient = Output('max_voltage_gradient', float) |
| 51 | + dome_voltage = Output('dome_voltage', float) |
| 52 | + apd50 = Output('APD50', int) |
| 53 | + apd90 = Output('APD90', int) |
| 54 | + |
| 55 | + # 3. Create scenario by applying constraints over a subset of the inputs |
| 56 | + scenario = Scenario( |
| 57 | + variables={g_na, g_si, g_k, g_k1, g_kp, g_b, |
| 58 | + max_voltage, rest_voltage, max_voltage_gradient, dome_voltage, apd50, apd90}, |
| 59 | + constraints=set() |
| 60 | + ) |
| 61 | + |
| 62 | + # 4. Create a causal specification from the scenario and causal DAG |
| 63 | + causal_specification = CausalSpecification(scenario, causal_dag) |
| 64 | + |
| 65 | + # 5. Create a causal test case |
| 66 | + causal_test_case = CausalTestCase(control_input_configuration={treatment_var: control_val}, |
| 67 | + expected_causal_effect=expected_causal_effect, |
| 68 | + outcome_variables={apd90}, |
| 69 | + intervention=Intervention((treatment_var,), (treatment_val,), ), ) |
| 70 | + |
| 71 | + # 6. Create a data collector |
| 72 | + data_collector = ObservationalDataCollector(scenario, observational_data_path) |
| 73 | + |
| 74 | + # 7. Create an instance of the causal test engine |
| 75 | + causal_test_engine = CausalTestEngine(causal_test_case, causal_specification, data_collector) |
| 76 | + |
| 77 | + # 8. Obtain the minimal adjustment set from the causal DAG |
| 78 | + minimal_adjustment_set = causal_test_engine.load_data(index_col=0) |
| 79 | + |
| 80 | + linear_regression_estimator = LinearRegressionEstimator((treatment_var.name,), treatment_val, control_val, |
| 81 | + minimal_adjustment_set, |
| 82 | + ('APD90',), |
| 83 | + ) |
| 84 | + causal_test_result = causal_test_engine.execute_test(linear_regression_estimator, 'ate') |
| 85 | + print(causal_test_result) |
| 86 | + return causal_test_result.ate, causal_test_result.confidence_intervals |
| 87 | + |
| 88 | + |
| 89 | +def plot_ates_with_cis(results_dict, xs, save=True): |
| 90 | + """ |
| 91 | + Plot the average treatment effects for a given treatment against a list of x-values with confidence intervals. |
| 92 | +
|
| 93 | + :param results_dict: A dictionary containing results for sensitivity analysis of each input parameter. |
| 94 | + :param xs: Values to be plotted on the x-axis. |
| 95 | + """ |
| 96 | + fig, axes = plt.subplots() |
| 97 | + for treatment, results in results_dict.items(): |
| 98 | + ates = results['ate'] |
| 99 | + cis = results['cis'] |
| 100 | + before_underscore, after_underscore = treatment.split('_') |
| 101 | + after_underscore_braces = f"{{{after_underscore}}}" |
| 102 | + latex_compatible_treatment_str = rf"${before_underscore}_{after_underscore_braces}$" |
| 103 | + cis_low = [ci[0] for ci in cis] |
| 104 | + cis_high = [ci[1] for ci in cis] |
| 105 | + |
| 106 | + axes.fill_between(xs, cis_low, cis_high, alpha=.3, label=latex_compatible_treatment_str) |
| 107 | + axes.plot(xs, ates, color='black', linewidth=.5) |
| 108 | + axes.plot(xs, [0] * len(xs), color='red', alpha=.5, linestyle='--', linewidth=.5) |
| 109 | + axes.set_ylabel(r"ATE: Change in $APD_{90} (ms)$") |
| 110 | + axes.set_xlabel(r"Treatment value") |
| 111 | + axes.set_ylim(-150, 150) |
| 112 | + axes.set_xlim(min(xs), max(xs)) |
| 113 | + box = axes.get_position() |
| 114 | + axes.set_position([box.x0, box.y0 + box.height * 0.3, |
| 115 | + box.width, box.height * 0.7]) |
| 116 | + plt.legend(loc='upper center', bbox_to_anchor=(0.5, -0.15), fancybox=True, ncol=6, title='Input Parameters') |
| 117 | + if save: |
| 118 | + plt.savefig(f"APD90_sensitivity.pdf", format="pdf") |
| 119 | + plt.show() |
| 120 | + |
| 121 | + |
| 122 | +def normalise_data(df, columns=None): |
| 123 | + """ Normalise the data in the dataframe into the range [0, 1]. """ |
| 124 | + if columns: |
| 125 | + df[columns] = (df[columns] - df[columns].min())/(df[columns].max() - df[columns].min()) |
| 126 | + return df |
| 127 | + else: |
| 128 | + return (df - df.min())/(df.max() - df.min()) |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == '__main__': |
| 132 | + df = pd.read_csv("data/results.csv") |
| 133 | + conductance_means = {'G_K': (.5, Positive), |
| 134 | + 'G_b': (.5, Positive), |
| 135 | + 'G_K1': (.5, Positive), |
| 136 | + 'G_si': (.5, Negative), |
| 137 | + 'G_Na': (.5, NoEffect), |
| 138 | + 'G_Kp': (.5, NoEffect)} |
| 139 | + normalised_df = normalise_data(df, columns=list(conductance_means.keys())) |
| 140 | + normalised_df.to_csv("data/normalised_results.csv") |
| 141 | + |
| 142 | + treatment_values = np.linspace(0, 1, 20) |
| 143 | + results_dict = {'G_K': {}, |
| 144 | + 'G_b': {}, |
| 145 | + 'G_K1': {}, |
| 146 | + 'G_si': {}, |
| 147 | + 'G_Na': {}, |
| 148 | + 'G_Kp': {}} |
| 149 | + for conductance_param, mean_and_oracle in conductance_means.items(): |
| 150 | + average_treatment_effects = [] |
| 151 | + confidence_intervals = [] |
| 152 | + for treatment_value in treatment_values: |
| 153 | + mean, oracle = mean_and_oracle |
| 154 | + conductance_input = Input(conductance_param, float) |
| 155 | + ate, cis = effects_on_APD90(OBSERVATIONAL_DATA_PATH, conductance_input, 0, treatment_value, oracle) |
| 156 | + average_treatment_effects.append(ate) |
| 157 | + confidence_intervals.append(cis) |
| 158 | + results_dict[conductance_param] = {"ate": average_treatment_effects, "cis": confidence_intervals} |
| 159 | + plot_ates_with_cis(results_dict, treatment_values) |
0 commit comments