[6ac965]: / src / iterpretability / experiments / treatment_space_sensitivity.py

Download this file

150 lines (130 with data), 5.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from src.iterpretability.experiments.experiments_base import ExperimentBase
from pathlib import Path
import os
import catenets.models as cate_models
import numpy as np
import pandas as pd
import wandb
from PIL import Image
import src.iterpretability.logger as log
from src.plotting import (
plot_results_datasets_compare,
merge_pngs
)
from src.iterpretability.explain import Explainer
from src.iterpretability.datasets.data_loader import load
from src.iterpretability.simulators import (
TYSimulator,
TSimulator
)
from src.iterpretability.utils import (
attribution_accuracy,
)
# For contour plotting
import umap
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import KFold, StratifiedKFold
import matplotlib.pyplot as plt
import matplotlib.tri as tri
import matplotlib.gridspec as gridspec
from matplotlib.colors import Normalize
from matplotlib.ticker import FuncFormatter
import imageio
import torch
import shap
# Hydra for configuration
import hydra
from omegaconf import DictConfig, OmegaConf
class TreatmentSpaceSensitivity(ExperimentBase):
"""
Sensitivity analysis for varying number of treatment options (still only 1 treatment is given, however). This experiment will generate a .csv with the recorded metrics.
It will also compare this for binary and non-binary outcomes.
"""
def __init__(
self, cfg: DictConfig
) -> None:
super().__init__(cfg)
# Experiment specific settings
self.num_Ts = cfg.num_Ts
def run(self) -> None:
"""
Run the experiment.
"""
# Log
log.info(
f"Starting propensity scale sensitivity experiment for dataset {self.cfg.dataset}."
)
# Main Loop
results_data = []
for seed in self.seeds:
for binary_outcome in [True, False]:
self.discrete_outcome = binary_outcome
for num_T in self.num_Ts:
log.info(
f"Running experiment for seed {seed} and num_T: {num_T}."
)
# Overwrite the number of important features
self.cfg.simulator.num_binary_outcome = self.cfg.simulator.dim_Y if binary_outcome else 0
self.cfg.simulator.num_T = num_T
# Initialize the simulator
if self.simulation_type == "TY":
sim = TYSimulator(dim_X = self.X.shape[1], **self.cfg.simulator, seed=seed)
elif self.simulation_type == "T":
raise ValueError("Experimental knobs cannot be tweaked for simulation only simulating treatment.")
# Retrieve important features
self.all_important_features = sim.all_important_features
self.pred_features = sim.predictive_features
self.prog_features = sim.prognostic_features
self.select_features = sim.selective_features
# Simulate outcomes and treatment assignments
sim.simulate(X=self.X, outcomes=self.outcomes)
(
X,
T,
Y,
outcomes,
propensities
) = sim.get_simulated_data()
# Get splits for cross validation
if self.discrete_outcome:
kf = StratifiedKFold(n_splits=self.n_splits)
else:
kf = KFold(n_splits=self.n_splits) # Change n_splits to the number of folds you want
# Repeat everything for each fold
for split_id, (train_index, test_index) in enumerate(kf.split(X, Y)):
# Extract the data and split it into train and test
train_size = len(train_index)
test_size = len(test_index)
X_train, X_test = X[train_index], X[test_index]
T_train, T_test = T[train_index], T[test_index]
Y_train, Y_test = Y[train_index], Y[test_index]
outcomes_train, outcomes_test = outcomes[train_index], outcomes[test_index]
propensities_train, propensities_test = propensities[train_index], propensities[test_index]
log.info(
f"Running experiment for seed {seed} and num_T: {num_T}."
)
metrics_df = self.compute_metrics(
results_data,
sim,
X_train,
Y_train,
T_train,
X_test,
Y_test,
T_test,
outcomes_train,
outcomes_test,
propensities_train,
propensities_test,
num_T,
"Treatment Options",
binary_outcome,
"Binary Outcome",
seed,
split_id
)
# Save results and plot
self.save_results(metrics_df)