obsnode-table2-reproduction / scan_windows_std.py
Ki-Asif's picture
Window/standardization scan vs paper TRUE rows
6875b24 verified
Raw History Blame Contribute Delete
2.67 kB
"""Scan windows and standardization choices against the paper's true ATEs.
True ATE depends only on the committed CF data and the scaling, so any
window/std combination matching the paper's TRUE rows pins down the
evaluation protocol Table 2 used.
"""
import pickle
import torch
CFDIR = "/work/ObsNODE/Code/Counterfactual/CF_data_cancer/dataset_cancer_cf"
TRAIN = "/work/data/data_train_cancer_DoseAI.pkl"
TEST = "/work/data/data_test_cancer_DoseAI.pkl"
PAPER_TRUE_T = [-0.0105, 0.0031, 0.0107, 0.0063, 0.0063]
PAPER_TRUE_W = [-0.0016, 0.0227, 0.0068, 0.0070, 0.0070]
t_train, x_train, _ = pickle.load(open(TRAIN, "rb"))
t_test, x_test, _ = pickle.load(open(TEST, "rb"))
std_train = x_train.std(dim=(0, 1))
std_test_obs = x_test.std(dim=(0, 1))
# CF6 raw test data for CF-test-set std
_, x_cf6, _ = pickle.load(open(f"{CFDIR}/data_test_cancer_gamma_4_CF_6.pkl", "rb"))
std_cf6 = x_cf6[:24].std(dim=(0, 1))
mean_cf6 = x_cf6[:24].mean(dim=(0, 1))
print("std_train :", std_train.tolist())
print("std_test :", std_test_obs.tolist())
print("std_cf6 :", std_cf6.tolist())
print("mean_cf6 :", mean_cf6.tolist())
# Gather raw potential outcomes: x_cf[k] (24, N, 2)
raws = {}
Ns = set()
for cf in range(1, 7):
_, x, _ = pickle.load(open(f"{CFDIR}/data_test_cancer_gamma_4_CF_{cf}.pkl", "rb"))
raws[cf] = x[:24]
Ns.add(x.shape[1])
print("patient counts:", Ns, "| time:", raws[1].shape[0])
# Per-point (19, 2) true ATE per CF in raw units
diffs = {cf: (raws[cf][5:] - raws[6][5:]) for cf in range(1, 6)} # (19, N, 2)
windows = {
"first6": list(range(6)),
"every3": [0, 3, 6, 9, 12, 15],
"last6": list(range(13, 19)),
"all19": list(range(19)),
"pts_s1_6_later": [13, 14, 15, 16, 17, 18],
}
stds = {"train": std_train, "test_obs": std_test_obs, "cf6": std_cf6}
for wname, idx in windows.items():
for sname, std in stds.items():
vals_t, vals_w = [], []
for cf in range(1, 6):
d = diffs[cf][idx].mean(dim=(0, 1)) / std # (2,)
vals_t.append(d[0].item())
vals_w.append(d[1].item())
et = max(abs(a - b) for a, b in zip(vals_t, PAPER_TRUE_T))
ew = max(abs(a - b) for a, b in zip(vals_w, PAPER_TRUE_W))
print(f"{wname:>8} std={sname:>8} tumor={['%.4f' % v for v in vals_t]} "
f"maxerr={et:.4f} | weight={['%.4f' % v for v in vals_w]} maxerr={ew:.4f}")
# Full per-point trajectory of true ATE (raw / train std) for CF5 to inspect shape
print("\nCF5 true-ATE per point (t, tumor, weight) [train std]:")
d5 = diffs[5].mean(dim=(1))
for i in range(19):
print(f" t=5+{i}: {d5[i, 0] / std_train[0]:+.4f} {d5[i, 1] / std_train[1]:+.4f}")