[d9566e]: / scripts / plcom2012 / plcom2012.py

Download this file

124 lines (109 with data), 3.4 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
import math
from tqdm import tqdm
import pickle
class RiskModel(object):
def __init__(self, args):
self.args = args
def forward(self, batch):
x_transformed = {
key: func(batch[key]) for key, func in self.input_transformers.items()
}
x_scaled = self.scale_inputs(x_transformed)
risk = self.model(x_scaled)
return risk
def test(self, data):
results = []
for sample in tqdm(data.dataset):
sample["golds"] = sample["y"]
sample["probs"] = self.forward(sample)
if self.args.save_predictions:
self.save_predictions(data.dataset)
def save_predictions(self, data):
predictions_dict = [
{k: v for k, v in d.items() if k in self.save_keys} for d in data
]
predictions_filename = "{}.{}.predictions".format(
self.args.results_path, self.save_prefix
)
pickle.dump(predictions_dict, open(predictions_filename, "wb"))
@property
def input_coef(self):
pass
@property
def input_transformers(self):
pass
class PLCOm2012(RiskModel):
def __init__(self, args):
super(PLCOm2012, self).__init__(args)
def model(self, x):
return 1 / (1 + math.exp(-x))
def scale_inputs(self, x):
running_sum = -4.532506
for key, beta in self.input_coef.items():
if key == "race":
running_sum += beta[x["race"]]
else:
running_sum += x[key] * beta
return running_sum
@property
def input_coef(self):
coefs = {
"age": 0.0778868,
"race": {
"white": 0,
"black": 0.3944778,
"hispanic": -0.7434744,
"asian": -0.466585,
"native_hawaiian_pacific": 0,
"american_indian_alaskan": 1.027152,
},
"education": -0.0812744,
"bmi": -0.0274194,
"cancer_hx": 0.4589971,
"family_lc_hx": 0.587185,
"copd": 0.3553063,
"is_smoker": 0.2597431,
"smoking_intensity": -1.822606,
"smoking_duration": 0.0317321,
"years_since_quit_smoking": -0.0308572,
}
return coefs
@property
def input_transformers(self):
funcs = {
"age": lambda x: x - 62,
"race": lambda x: x,
"education": lambda x: x - 4,
"bmi": lambda x: x - 27,
"cancer_hx": lambda x: x,
"family_lc_hx": lambda x: x,
"copd": lambda x: x,
"is_smoker": lambda x: x,
"smoking_intensity": lambda x: 10 / x - 0.4021541613,
"smoking_duration": lambda x: x - 27,
"years_since_quit_smoking": lambda x: x - 10,
}
return funcs
@property
def save_keys(self):
return [
"pid",
"age",
"race",
"education",
"bmi",
"cancer_hx",
"family_lc_hx",
"copd",
"is_smoker",
"smoking_intensity",
"smoking_duration",
"years_since_quit_smoking",
"exam",
"golds",
"probs",
"time_at_event",
"y_seq",
"y_mask",
"screen_timepoint",
]