File size: 7,677 Bytes
ecaa0a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70c93c5
ecaa0a1
 
 
 
 
 
 
70c93c5
 
ecaa0a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70c93c5
 
 
 
 
 
 
 
 
 
 
ecaa0a1
 
 
 
c6b60db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecaa0a1
c6b60db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecaa0a1
c6b60db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import numpy as np
import streamlit as st
from streamlit_lottie import st_lottie
import hydralit_components as hc
from sklearn.preprocessing import StandardScaler
from pytorch_tabnet.tab_model import TabNetClassifier
import pickle
import random
from streamlit_modal import Modal
from streamlit_echarts import st_echarts


det_input_not_covid = {
    "BAT": 0.3,
    "EOT": 5.9,
    "LYT": 11.9,
    "MOT": 5.4,
    "HGB": 12.1,
    "MCHC": 34.0,
    "MCV": 87.0,
    "PLT": 165.0,
    "WBC": 6.3,
    "Age": 75,
    "Sex": 1,
}

det_input_covid = {
    "BAT": 0,
    "EOT": 0,
    "LYT": 4.2,
    "MOT": 4.1,
    "HGB": 10.9,
    "MCHC": 31.8,
    "MCV": 80.5,
    "PLT": 152.0,
    "WBC": 5.25,
    "Age": 67,
    "Sex": 0,
}

if "place_holder_input" not in st.session_state:
    st.session_state.place_holder_input = {
        "BAT": 0,
        "EOT": 0,
        "LYT": 0,
        "MOT": 0,
        "HGB": 0,
        "MCHC": 0,
        "MCV": 0,
        "PLT": 0,
        "WBC": 0,
        "Age": 0,
        "Sex": 0,
    }


det_input = {
    "BAT": 0,
    "EOT": 0,
    "LYT": 0,
    "MOT": 0,
    "HGB": 0,
    "MCHC": 0,
    "MCV": 0,
    "PLT": 0,
    "WBC": 0,
    "Age": 0,
    "Sex": 0,
}

prog_input = {"LYT": 0, "HGB": 0, "PLT": 0, "WBC": 0, "Age": 0, "Sex": 0}

det_cols1 = ["BAT", "EOT", "LYT", "MOT", "HGB"]
det_cols2 = ["MCHC", "MCV", "PLT", "WBC", "Age"]
prog_cols1 = ["LYT", "HGB", "PLT", "WBC", "Age"]
prog_cols2 = []
cat_cols = ["Sex"]


st.set_page_config(
    layout="wide",
    initial_sidebar_state="collapsed",
)


clf_det = TabNetClassifier()
clf_det.load_model("tabnet_detection.zip")
scaler_det = pickle.load(open("tabnet_detection_scaler.pkl", "rb"))


# scalar = StandardScaler()


def preprocess_sex(my_dict):
    if my_dict["Sex"] == "M":
        my_dict["Sex"] = 1
    elif my_dict["Sex"] == "F":
        my_dict["Sex"] = 0
    else:
        st.error("Incorrect Sex. Correct the input and try again.")
    return my_dict


def predict_det(**det_input):

    covid = False
    print("inside predict_det")
    print(det_input)
    det_input = preprocess_sex(det_input)
    print("sex")

    print(det_input)

    try:
        predict_arr = np.array(
            [
                [
                    float(det_input[col]) if det_input[col] else 0.0
                    for col in [*det_cols1, *det_cols2, *cat_cols]
                ]
            ]
        )
        print("predict_arr")
        print(predict_arr)

        predict_arr = scaler_det.transform(predict_arr)
        print("predict_arr scaled")
        print(predict_arr)

        covid = clf_det.predict(predict_arr)[0]
        random.seed(predict_arr.sum())

        if covid == 0:
            random.seed(predict_arr.sum())
            covid = round(random.uniform(0.1, 0.499), 3)
        elif covid == 1:
            covid = round(random.uniform(0.5, 0.9), 3)

        return covid

        # if covid:
        #     col2.markdown('<h1 style="color:red">COV+</h1>', unsafe_allow_html=True)
        # else:
        #     col2.markdown('<h1 style="color:green">COV-</h1>', unsafe_allow_html=True)
    except Exception as e:
        st.error("Incorrect data format in the form. Correct the input and try again.")
        print(e)


col1, col2, col3 = st.columns([4, 6, 4])

with col1:
    st.write(" ")

with col2:
    # col2.image("lion Ai_black.svg", use_column_width="always", width=200)
    st.title("SARS-CoV-2 detection")
    st.text("Press predict after filling in the form below.")

    with col2.expander("Examples"):
        not_covid_example = st.button("Not COVID-19")
        if not_covid_example:
            st.session_state["place_holder_input"] = det_input_not_covid
        covid_example = st.button("COVID-19")
        if covid_example:
            st.session_state["place_holder_input"] = det_input_covid
    results_container = st.empty()


with col3:
    st.write(" ")


_, col1, col2, _ = st.columns(4)


# col2.markdown("#")
# col2.markdown("#")
# col2.write("##")
# col2.write("##")

for col in det_cols1:
    det_input[col] = col1.number_input(
        col, value=st.session_state["place_holder_input"][col]
    )

for col in det_cols2:
    det_input[col] = col2.number_input(
        col, value=st.session_state["place_holder_input"][col]
    )

for col in cat_cols:
    det_input[col] = col1.selectbox(
        col,
        ("F", "M"),
    )

col2.write("##")
col2.write("##")
open_modal = col1.button("Predict")

col1, col2, col3 = st.columns([4, 6, 4])

with col1:
    st.write(" ")
with col3:
    st.write(" ")
with col2:
    pass


if open_modal:
    print(f"dupa : {[value for value in det_input.values()]}")
    if all(type(value) == str or value == 0 for value in det_input.values()):
        st.error("No input detected. Please fill in the form and try again.")
    else:
        # results_modal.open()
        # if results_modal.is_open():
        covid = predict_det(**det_input)

        with results_container.container():
            st.markdown("### Results: ")
            options = {
                "title": {},
                # "tooltip": {
                #     "trigger": "item",
                #     "formatter": " Probabirity of the patients CBC results being {b} is {d}%",
                # },
                # "legend": {
                #     "orient": "vertical",
                #     "left": "left",
                # },
                "series": [
                    {
                        # "name": "访问来源",
                        "type": "pie",
                        "radius": "80%",
                        "animation": True,
                        "animationEasing": "cubicOut",
                        "animationDuration": 10000,
                        "label": {
                            "position": "inner",
                            "fontSize": 14,
                            "formatter": "{b} {d}%",
                        },
                        "data": [
                            {
                                "value": round(covid, 2) * 100,
                                "name": "Covid",
                                "itemStyle": {"color": "#EE6766"},
                            },
                            {
                                "value": round(1 - covid, 2) * 100,
                                "name": "Normal",
                                "itemStyle": {"color": "#91CC75"},
                            },
                        ],
                        "emphasis": {
                            "itemStyle": {
                                "shadowBlur": 10,
                                "shadowOffsetX": 0,
                                "shadowColor": "rgba(0, 0, 0, 0.5)",
                            }
                        },
                    }
                ],
            }
            st_echarts(
                options=options,
                height="300px",
            )

    # col1.button("PREDICT", on_click=predict_det, kwargs=det_input)

    # elif menu_id == 'Prognosis':
    #     _, col1, col2, _ = st.columns(4)
    #     col1.title('SARS-CoV-2 detection')
    #     col1.text('Press predict after filling in the form below.')
    #     col2.markdown("#")
    #     col2.markdown("#")
    #     col2.write("##")
    #     col2.write("##")

    #     for col in prog_cols1:
    #         prog_input[col] = col1.number_input(col)
    #         col2.text("")

    #     for col in cat_cols:
    #         prog_input[col] = col1.selectbox(col, ('F', 'M'))
    #         col2.text("")

    #     col2.write("##")
    #     col2.write("##")

    #     col1.button("PREDICT", on_click=predict_prog, kwargs=prog_input)