File size: 3,207 Bytes
50f6fa5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Recreate the path-patched runner scripts from the author repo.

Run from a clone of JenniferJaschob/ObsNODE at commit 62dd3e0b:
    python make_runners.py /path/to/ObsNODE /path/to/training_pickles_dir

The patches are path-only: hard-coded /Users/jaschob paths replaced, the
os.chdir into the vendored torchdiffeq stubbed around the utils import,
NODE checkpoint path redirected to the committed dim2 location, seed set
to a parameter, and the seed-aggregation tail truncated (aggregation is
reimplemented in analyze_table2.py). Model/data logic is untouched.
"""

import re
import sys

REPO = sys.argv[1] if len(sys.argv) > 1 else "/work/ObsNODE"
DATA = sys.argv[2] if len(sys.argv) > 2 else "/work/data"
SEED = int(sys.argv[3]) if len(sys.argv) > 3 else 1

CFDIR = REPO + "/Code/Counterfactual/CF_data_cancer/dataset_cancer_cf"
OUTDIR = "/work/run"
OBSUTIL = REPO + "/Code/Model_observableNODE"
NODUTIL = REPO + "/Code/Model_NODE"


def patch(src_name, out_name, util_dir, util_module, extra=None):
    src = open(REPO + "/Code/Counterfactual/" + src_name).read()
    stub = (f"import sys\n"
            f"sys.path.insert(0, '{util_dir}/torchdiffeq')\n"
            f"sys.path.insert(0, '{util_dir}')\n")
    s = re.sub(r"os\.chdir\('[^']*'\)", stub, src, count=1)
    s = re.sub(r"path_data = '[^']*'",
               f"path_data = '{CFDIR}/'", s, count=1)
    s = re.sub(r"path_data2 = '[^']*'[^\n]*", f"path_data2 = '{DATA}/'", s, count=1)
    s = re.sub(r"num_seed_i = \d+", f"num_seed_i = {SEED}", s, count=1)
    if extra:
        for old, new in extra:
            s = s.replace(old, new)
    # stub the library's own os.chdir around the utils import
    marker = f"from {util_module} import"
    idx = s.index(marker)
    chdir_stub = (f"_real_chdir = os.chdir\n"
                  f"os.chdir = lambda _p: _real_chdir('{util_dir}/torchdiffeq')\n")
    s = s[:idx] + chdir_stub + s[idx:]
    # truncate at the seed-aggregation loop (needs all 5 seeds)
    s = s.split("for cf in range(1, 6):")[0]
    s = s.split("for num_seed_i in range(1, 6):")[0]
    assert "/Users/" not in s.replace("#", ""), "unreplaced hard-coded path"
    open(out_name, "w").write(s)
    print("wrote", out_name)


patch("Pred_ObsNODE_CF_cancer_0.py", f"{OUTDIR}/Pred_ObsNODE_CF_cancer_1seed.py",
      OBSUTIL, "utils_observableNODE",
      extra=[("/work/ObsNODE/Code/Model_observableNODE/out/out_cancer/"
              "model_state_dict_seed_",
              f"{REPO}/Code/Model_observableNODE/out/out_cancer/"
              f"model_state_dict_seed_"),
             ("'/Users/jaschob/Documents/GitHub/Promotion_Jennifer/"
              "Cancer_data_CF/Pred_CF_cancer/obsNODE/'",
              "'/work/eval_out/obsNODE/'")])
patch("Pred_NODE_CF_cancer_0.py", f"{OUTDIR}/Pred_NODE_CF_cancer_1seed.py",
      NODUTIL, "utils_NODE",
      extra=[("read_path = '/Users/jaschob/Desktop/Out_NODE/"
              "model_state_dict_seed_'",
              f"read_path = '{REPO}/Code/Model_NODE/Out_NODE/cancer/dim2/"
              f"model_state_dict_seed_'"),
             ("'/Users/jaschob/Documents/GitHub/Promotion_Jennifer/"
              "Cancer_data_CF/Pred_CF_cancer/NODE/'",
              "'/work/eval_out/NODE/'")])