-
Notifications
You must be signed in to change notification settings - Fork 1
/
record_cindex.py
203 lines (191 loc) · 5.78 KB
/
record_cindex.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#! /usr/bin/env python
import os
from shutil import copyfile
import time
from datetime import datetime
from warnings import warn
import argparse
import numpy as np
import pandas as pd
from scipy.ndimage.filters import gaussian_filter1d
from mc_model import mc_model
from model_inspection_tools import multiplot, kaplan_meier
desc = "Record the concordnace index of mc_model ADVI inference."
parser = argparse.ArgumentParser(description=desc)
parser.add_argument(
"tag",
metavar="name",
type=str,
nargs="?",
help="tag the result with a name",
default="nameless",
)
parser.add_argument(
"--cohorts",
nargs="+",
metavar="cohort",
help="Cohort appreviations as in the pheno data. "
"Selects for which cohorts survival data should be used, "
"(e.g. ukd1 prad, default=UKDP)",
default=["UKDP"],
)
parser.add_argument(
"--eventType",
help="Type of events to consider "
"in the hazard model. Must be a column with boolean values "
"in the pheno data (default=biochemicalRecurrence).",
type=str,
default="biochemicalRecurrence",
metavar="event_type",
)
parser.add_argument(
"--eventTime",
help="Time of events to consider "
"in the hazard model. Must be a column with float values "
"in the pheno data (default=bcrTime).",
type=str,
default="bcrTime",
metavar="event_type",
)
parser.add_argument(
"--params",
help="Parameter file.hdf5.",
type=str,
default=None,
metavar="file",
)
parser.add_argument(
"--loss", help="Loss history.txt.", type=str, default=None, metavar="file"
)
args = parser.parse_args()
def load_hist(file, smooth=True, loss_padd=None):
hist = np.loadtxt(file)
is_fine = np.isfinite(hist)
if not any(is_fine):
return pd.DataFrame()
iters = np.where(is_fine)[0]
hist = hist[is_fine]
lb = min(hist)
if loss_padd is not None and lb < 0:
hist += loss_padd - lb
lb = loss_padd
ldf = pd.DataFrame({"loss": hist, "iteration": iters})
if smooth is not False:
if lb > 0:
ldf["sloss"] = np.exp(
gaussian_filter1d(np.log(hist), sigma=smooth)
)
else:
ldf["sloss"] = gaussian_filter1d(hist, sigma=smooth)
return ldf
def watch_model(
name,
cohorts,
param_file=None,
loss_file=None,
output_dir="data",
event_type="biochemicalRecurrence",
event_time="bcrTime",
model=None,
):
if model is None:
md = mc_model(
cohort="pcapp",
counter="htseq",
assembly="tophatV04",
filterFile="/mnt/fhgfs_ribdata/user_worktmp/dominik.otto/PCa-2016/featureLists/PCaP2q8.txt",
)
else:
md = model
def now():
return datetime.today().strftime("%Y-%m-%d-%H:%M:%S")
output = os.path.join(output_dir, name)
out_name = "-".join(cohorts) + "_" + event_type + "_" + event_time
out_tsv = os.path.join(output, f"watch_{out_name}.tsv")
print(f"[{now()}] Writing to {out_tsv}.")
try:
df = pd.read_csv(out_tsv, sep="\t")
except OSError:
df = pd.DataFrame(
columns=[
"iteration",
"smooth_loss",
"cohort",
"cindex",
"cox p-value",
"logrank p-value",
]
)
df.to_csv(out_tsv, sep="\t", header=True)
if param_file is None:
param_file = os.path.join(output_dir, f"{name}_params.hdf5")
param_bare = os.path.splitext(os.path.basename(param_file))[0]
if loss_file is None:
loss_file = os.path.join(output_dir, f"{name}_loss_hist.txt")
print(f"[{now()}] Waiting for {loss_file}.")
while not os.path.exists(loss_file):
time.sleep(5)
last_edit = None
log_iteration = 0
while True:
log_iteration += 1
print(f"[{now()}] Starting log iteration {log_iteration}.")
while True:
try:
stats = os.stat(param_file)
except OSError:
print(f"[{now()}] Waiting for {param_file}.")
time.sleep(30)
continue
if last_edit != stats.st_mtime:
break
else:
time.sleep(10)
print(f"[{now()}] Update detected.")
size = stats.st_size
while True:
time.sleep(1)
stats = os.stat(param_file)
if size == stats.st_size:
break
size = stats.st_size
last_edit = stats.st_mtime
print(f"[{now()}] Making statistics.")
cph, logr = kaplan_meier(
param_file,
model=md,
cohorts=cohorts,
event_type=event_type,
event_time=event_time,
no_plot=True,
)
loss = load_hist(loss_file).iloc[-1, :]
model_iteration = loss["iteration"] + 1
sloss = loss["sloss"]
cindex = cph.concordance_index_
df = df.append(
{
"iteration": model_iteration,
"smooth_loss": sloss,
"cohort": cohorts,
"cindex": cindex,
"cox p-value": cph.summary.loc["score", "p"],
"logrank p-value": logr.p_value,
},
ignore_index=True,
)
df.to_csv(out_tsv, sep="\t", header=True)
out_copy = f"{param_bare}_{model_iteration}iter_{sloss}sloss_{cindex}cindex.hdf5"
print(f"[{now()}] Copying parameters with concordance {cindex}.")
copyfile(param_file, os.path.join(output, out_copy))
if __name__ == "__main__":
print("Watching with args:")
print(args)
watch_model(
args.tag,
args.cohorts,
param_file=args.params,
loss_file=args.loss,
event_type=args.eventType,
event_time=args.eventTime,
)