-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransplantbenefit.py
175 lines (158 loc) · 4.64 KB
/
transplantbenefit.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
from pathlib import Path
from typing import Any
import numpy as np
import pandas as pd
import organsync.models.third_party.tbs.tbs as tbs_impl
module_path = Path(__file__).parent
path = module_path / Path("third_party/tbs/")
def _parse_reg_year(year: int) -> int:
if year <= 2007:
return 0
elif year == 2008:
return 1
elif year == 2009:
return 2
elif year == 2010:
return 3
elif year == 2011:
return 4
else:
return 5
def _parse_cod(cod: int) -> int:
if cod == 10 or cod == 11:
return 0
elif cod >= 20 and cod <= 29:
return 1
return 2
def _parse_dtype(cod: int) -> int:
if cod == 40 or cod == 41 or cod == 49:
return 0
return 1
def _parse_disease_group(disease: int) -> int:
if disease in [441, 442, 443, 444, 445, 447]:
return 1
elif disease in [424]:
return 2
elif disease in [419]:
return 3
elif disease in [413, 436]:
return 4
elif disease in [414]:
return 5
elif disease in [411]:
return 6
elif disease in [412, 417]:
return 7
elif disease in [415, 422, 426, 450, 452, 454, 456, 457, 461, 462, 434]:
return 8
elif disease in [
410,
416,
418,
420,
421,
423,
425,
448,
451,
453,
455,
460,
463,
464,
466,
467,
468,
469,
483,
484,
485,
486,
498,
474,
]:
return 9
return 10
class TBS:
def fit(self, *args: Any, **kwargs: Any) -> "TBS":
return self
def _single_predict(self, data: pd.DataFrame) -> pd.DataFrame:
centre_tbs = 0
rregistration_tbs = _parse_reg_year(data["regyr"])
rinpatient_tbs = int(data["PATIENT_LOCATION"]) - 1
rwaiting_time_tbs = data["rwtime"] + 1 if "rwtime" in data else 1
rage_tbs = data["RAGE"]
rgender_tbs = int(data["SEX"] == 2)
rdisease_primary_tbs = _parse_disease_group(data["PRIMARY_LIVER_DISEASE"])
rdisease_secondary_tbs = 9
rdisease_tertiary_tbs = 9
previous_tx_tbs = (
data["NO_OF_PREVIOUS_LIVER_TX"] if "NO_OF_PREVIOUS_LIVER_TX" in data else 0
)
rprevious_surgery_tbs = (
int(data["PREV_ABDOMINAL_SURGERY"])
if "PREV_ABDOMINAL_SURGERY" in data
else 8
)
rbilirubin_tbs = data["SERUM_BILIRUBIN"]
rinr_tbs = data["INR"]
rcreatinine_tbs = data["SERUM_CREATININE"]
rrenal_tbs = int(data["RENAL_SUPPORT"] != 3)
rsodium_tbs = data["SERUM_SODIUM"]
rpotassium_tbs = data["SERUM_POTASSIUM"] if "SERUM_POTASSIUM" in data else 0
ralbumin_tbs = data["SERUM_ALBUMIN"] if "SERUM_ALBUMIN" in data else 0
rencephalopathy_tbs = (
int(data["ENCEPHALOPATHY_GRADE"] != 0)
if "ENCEPHALOPATHY_GRADE" in data
else 8
)
rascites_tbs = data["CURRENT_ASCITES"] if "CURRENT_ASCITES" in data else 8
rdiabetes_tbs = int(data["DIABETIC"])
dage_tbs = data["DAGE"]
dbmi_tbs = data["DBMI"]
dcause_tbs = _parse_cod(data["DCOD"])
ddiabetes_tbs = int(0)
dtype_tbs = data["DGRP"] == 2
splittable_tbs = 0
bloodgroup_compatible_tbs = 1
rmax_afp_tbs = 5
rtumour_number_tbs = 0
rmax_tumour_size_tbs = 1
output = tbs_impl.fn_tbs(
centre_tbs,
rregistration_tbs,
rinpatient_tbs,
rwaiting_time_tbs,
rage_tbs,
rgender_tbs,
rdisease_primary_tbs,
rdisease_secondary_tbs,
rdisease_tertiary_tbs,
previous_tx_tbs,
rprevious_surgery_tbs,
rbilirubin_tbs,
rinr_tbs,
rcreatinine_tbs,
rrenal_tbs,
rsodium_tbs,
rpotassium_tbs,
ralbumin_tbs,
rencephalopathy_tbs,
rascites_tbs,
rdiabetes_tbs,
rmax_afp_tbs,
rtumour_number_tbs,
rmax_tumour_size_tbs,
dage_tbs,
dcause_tbs,
dbmi_tbs,
ddiabetes_tbs,
dtype_tbs,
splittable_tbs,
bloodgroup_compatible_tbs,
)
return [round(output["tbs"], 1), round(output["m1"], 1), round(output["m2"], 1)]
def predict(self, data: pd.DataFrame) -> pd.DataFrame:
scores = data.apply(lambda row: self._single_predict(row), axis=1)
scores = np.vstack(scores)
return pd.DataFrame(scores, columns=["score", "m1", "m2"])