File size: 75,193 Bytes
18b5fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a03eff
 
18b5fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143a54f
18b5fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0325edd
 
 
 
 
 
 
 
 
 
 
 
18b5fcf
 
0325edd
18b5fcf
 
 
 
 
 
 
 
 
143a54f
18b5fcf
 
 
 
0fa3e2d
 
 
 
18b5fcf
 
 
 
0325edd
 
18b5fcf
0fa3e2d
2950403
0325edd
0fa3e2d
 
 
9f53060
ae5bdaf
 
 
 
 
 
18b5fcf
ae5bdaf
 
 
 
 
 
 
 
 
 
 
 
18b5fcf
ae5bdaf
 
 
 
 
 
18b5fcf
ae5bdaf
 
 
 
 
 
 
 
 
 
9f53060
18b5fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
519db0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18b5fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
519db0a
 
 
18b5fcf
 
 
0fa3e2d
 
18b5fcf
0fa3e2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18b5fcf
0fa3e2d
 
18b5fcf
0fa3e2d
 
 
18b5fcf
0fa3e2d
 
 
 
 
 
18b5fcf
0fa3e2d
 
 
18b5fcf
0fa3e2d
 
 
18b5fcf
0fa3e2d
 
18b5fcf
0fa3e2d
 
 
519db0a
0fa3e2d
 
 
 
18b5fcf
 
7f7384d
 
 
 
 
 
18b5fcf
 
 
7f7384d
 
18b5fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
#pip install stramlit wordcloud
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import plotly.express as px
import plotly.figure_factory as ff
import warnings
warnings.filterwarnings("ignore")
from wordcloud import WordCloud
from sklearn.preprocessing import StandardScaler
import numpy as np
from sklearn.preprocessing import LabelEncoder
from pandasai import SmartDataframe
from pandasai.llm.google_gemini import GoogleGemini
import warnings
from pandasai.responses.response_parser import ResponseParser
# pip install wordcloud
# !pip install kmodes

from sklearn.decomposition import PCA
from sklearn.experimental import enable_iterative_imputer
from sklearn.impute import IterativeImputer
from kmodes.kprototypes import KPrototypes
import plotly.graph_objects as go
import streamlit as st
#pip install google-generativeai


import os
from huggingface_hub import hf_hub_download

repo_id = "Akankshg/ML_DATA"
filename = "EDA_DATA.parquet"

# Access the token
token = os.environ["HUGGING_FACE_HUB_TOKEN"]

# Download the file
local_file = hf_hub_download(repo_id=repo_id, filename=filename, repo_type="dataset",token=token)



class StreamlitResponse(ResponseParser):
    def __init__(self, context) -> None:
        super().__init__(context)

    def format_dataframe(self, result):
        st.dataframe(result["value"])
        return

    def format_plot(self, result):
        st.image(result["value"])
        return


st.set_page_config(page_title="Healthcare Data Analysis", page_icon=":bar_chart:", layout="wide")
st.title(':bar_chart: Healthcare Data Analysis Dashboard')
st.markdown('<style>div.block-container{padding-top:1rem;}</style>',unsafe_allow_html=True)

# Sidebar 1
st.sidebar.title('Dashboard Options')
analysis_option = st.sidebar.selectbox('Select Analysis', ['Data','EDA', 'Machine Learning','Health Care Chat Bot AI'])

## Loading data
@st.cache_data()
def fetch_data():
    data = pd.read_parquet(local_file)
    return data
data = fetch_data()

def funnel_chart(df):
    Patient_visit = df[['PatientID','EncounterDate','LegalSex']].copy()
    Patient_visit['WeekDay'] =  Patient_visit['EncounterDate'].dt.day_name()
    Patient_visit['WeekDay'] = Patient_visit['WeekDay'].astype('string')
    output_df = Patient_visit.groupby(['WeekDay', 'LegalSex']).size().unstack(fill_value=0)
    output_df.reset_index(inplace=True)
    if 'Male' in output_df.columns:
        if 'Female' in  output_df.columns:
            desired_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
            output_df = output_df.set_index('WeekDay').reindex(desired_order).reset_index()
            stages = output_df['WeekDay']
            df_female = pd.DataFrame(dict(number=output_df['Female'], stage=stages))
            df_male = pd.DataFrame(dict(number=output_df['Male'], stage=stages))
            df_female['Gender'] = 'Female'
            df_male['Gender'] = 'Male'
            df_graph = pd.concat([df_male, df_female], axis=0)
            colors = {'Male': '#2986cc', 'Female': '#c90076'}
            fig2 = px.funnel(df_graph, x='number', y='stage', color='Gender', color_discrete_map=colors, title='Patient Visits by Gender and Weekday')
            fig2.update_layout(
                template="plotly_dark",
                xaxis_title='Number of Patients',
                yaxis_title='Weekday',
                height=500, width=250
            )
            return fig2
        else:
            desired_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
            output_df = output_df.set_index('WeekDay').reindex(desired_order).reset_index()
            stages = output_df['WeekDay']
            df_male = pd.DataFrame(dict(number=output_df['Male'], stage=stages))
            df_male['Gender'] = 'Male'
            colors = {'Male': '#2986cc', 'Female': '#c90076'}
            fig2 = px.funnel(df_male, x='number', y='stage', color='Gender', color_discrete_map=colors, title='Patient Visits by Gender and Weekday')
            fig2.update_layout(
                template="plotly_dark",
                xaxis_title='Number of Patients',
                yaxis_title='Weekday',height=500, width=250)
            return fig2 
    else:
        desired_order = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
        output_df = output_df.set_index('WeekDay').reindex(desired_order).reset_index()
        stages = output_df['WeekDay']
        df_female = pd.DataFrame(dict(number=output_df['Female'], stage=stages))
        df_female['Gender'] = 'Female'
        colors = {'Male': '#2986cc', 'Female': '#c90076'}
        fig2 = px.funnel(df_female, x='number', y='stage', color='Gender', color_discrete_map=colors, title='Patient Visits by Gender and Weekday')
        fig2.update_layout(
            template="plotly_dark",
            xaxis_title='Number of Patients',
            yaxis_title='Weekday',height=500, width=250)
        return fig2 

def scatter_man(data):
    Patient_Analysis = data[['PatientID', 'GroupedICD', 'Description', 'Age']].copy()
    patients_diagnosis = Patient_Analysis[Patient_Analysis['GroupedICD'].notna()]
    patients_diagnosis_info = patients_diagnosis[['PatientID', 'GroupedICD', 'Description', 'Age']]
    patients_tests_info = patients_diagnosis_info[patients_diagnosis_info['Age'].notna()]
    patients_tests_df = pd.DataFrame(patients_tests_info)
    
    patients_icd_counts = patients_tests_df.groupby(['Age', 'GroupedICD','Description']).size().reset_index(name='Count')
    patients_icd_counts = patients_icd_counts[patients_icd_counts['Count']> 1000]
    import plotly.express as px
    # sns.set(rc={"axes.facecolor":"#FFF9ED","figure.facecolor":"#FFF9ED"})
    # Scatter plot
    fig5 = px.scatter(patients_icd_counts, y='Age', x='Description', size='Count',
                     hover_name='Age', color='Count', title='Age - ICD Relationship',color_continuous_scale='ylorrd')
    fig5.update_layout(template="plotly_dark",xaxis_title='ICD Code', yaxis_title='Age',coloraxis_colorbar=dict(title='Count'),
                      height=950, width=1400)
    return fig5


def barplot_lab(df):
    df = df[['PatientID','EncounterDate','ComponentName', 'GroupedICD','Description']].copy()
    df.sort_values(by=['EncounterDate'], ascending=True,inplace = True)
    df['DaysSinceLastVisit'] = df.groupby('PatientID')['EncounterDate'].diff().dt.days
    df = df[df['DaysSinceLastVisit'] <= 7]
    lab = df[df['ComponentName'].notna()].copy()
    lab = lab[lab['GroupedICD'].notna()].copy()
    component= lab.groupby(['ComponentName','Description']).size().reset_index(name='Count')
    sss = component.sort_values(by='Count', ascending=False)[:20].copy()
    fig3 = px.bar(sss, x='ComponentName', y='Count',
             hover_data=['ComponentName', 'Count'], color='ComponentName', height=450, title='Lab Test')
    fig3.update_xaxes(tickangle=45)
    return fig3

def scatterplot(df):
    df = df[['PatientID','EncounterDate','ComponentName', 'GroupedICD','Description']].copy()
    df.sort_values(by=['EncounterDate'], ascending=True,inplace = True)
    df['DaysSinceLastVisit'] = df.groupby('PatientID')['EncounterDate'].diff().dt.days
    df = df[df['DaysSinceLastVisit'] <= 7]
    lab = df[df['ComponentName'].notna()].copy()
    lab = lab[lab['GroupedICD'].notna()].copy()
    component= lab.groupby(['ComponentName','Description']).size().reset_index(name='Count')
    component = component[component['Count']> 2000]
    component['Description'].nunique()
    fig = px.scatter(component, y='ComponentName', x='Description', size='Count',
                 hover_name='ComponentName', color='Count', title='Lab Component-ICD Relationship')
    fig.update_layout(template="plotly_dark",xaxis_title='ICD Code', yaxis_title='Component Name', coloraxis_colorbar=dict(title='Count'),
                  height=550, width=500)
    return fig

####################################### EDA ##################################################################
def histplot_6(data):
    disease_data = data[['Age','LegalSex']].copy()
    disease_data = disease_data[disease_data['Age'].notna() & disease_data['LegalSex'].notna()].copy()
    fig = px.histogram(disease_data, 
                   x='Age', 
                   color='LegalSex', 
                   nbins=10, 
                   opacity=0.5,
                   title='Age Distribution by Legal Sex',
                   color_discrete_sequence=px.colors.qualitative.Pastel)

    # Update layout to match your desired style
    fig.update_layout(
        title_font=dict(size=20, color='white'),
        xaxis_title_font=dict(size=16, color='white'),
        yaxis_title_font=dict(size=16, color='white'),
        xaxis=dict(tickfont=dict(size=14, color='white')),
        yaxis=dict(tickfont=dict(size=14, color='white'))
    )

    return fig


def histplot_7(data):
    import plotly.graph_objects as go
    graph3_data = data[['Age','BP Severity']].copy()
    graph3_data = graph3_data[graph3_data['BP Severity'].notna()]
    graph3_data = graph3_data[graph3_data['BP Severity'] != 'Unknown']
    graph3_data = graph3_data[graph3_data['BP Severity'] != 'BP NORMAL']

    severities = graph3_data['BP Severity'].unique()
    lines = []

    for severity in severities:
        severity_data = graph3_data[graph3_data['BP Severity'] == severity]
        age_counts = severity_data['Age'].value_counts().sort_index()
        lines.append(go.Scatter(x=age_counts.index, y=age_counts.values, mode='lines+markers', name=severity))

    fig = go.Figure(data=lines)

    fig.update_layout(
        title='Age Distribution by BP Severity',
        xaxis_title='Age',
        yaxis_title='Count',
        title_font=dict(size=20, color='white')
    )

    return fig


def pie_chart_7(data):
    import plotly.graph_objects as go

    # Prepare data
    graph_4 = data[['Depression Severity']].copy()
    graph_4 = graph_4[graph_4['Depression Severity'] != 'None-minimal']
    graph_4 = graph_4[graph_4['Depression Severity'] != 'Unknown']
    severity_counts = graph_4['Depression Severity'].value_counts()

    # Define colors
    colors_inner = ['#FF5733', '#FFC300', '#36A2EB', '#C71585']

    # Create plotly figure
    fig = go.Figure()

    # Add donut chart
    fig.add_trace(go.Pie(
        labels=severity_counts.index,
        values=severity_counts,
        hole=0.6,  # Hole size for donut chart
        marker=dict(colors=colors_inner),
        textinfo='label+percent',
        textfont=dict(size=10),
        insidetextorientation='radial'
    ))

    # Update layout for title and appearance
    fig.update_layout(
        title_text="Distribution of Patients by Depression",
        title_font_size=20,
        title_font_color='white',
        # paper_bgcolor='black',
        # plot_bgcolor='black',
        autosize=False,
        # width=500,
        # height=450,
    )

    # Show figure
    return fig

def chart_8(data):
    import plotly.graph_objects as go
    graph_5 = data[['BP Severity', 'BMI', 'LegalSex']].copy()
    graph_5 = graph_5.dropna(subset=['BP Severity', 'BMI', 'LegalSex'])
    graph_5 = graph_5[graph_5['BP Severity'] != 'Unknown']
    graph_5 = graph_5[graph_5['BP Severity'] != 'BP NORMAL']

    # Create box plot
    fig = go.Figure()

    # Add box plot traces for each gender
    for gender in graph_5['LegalSex'].unique():
        filtered_data = graph_5[graph_5['LegalSex'] == gender]
        fig.add_trace(go.Box(
            y=filtered_data['BMI'],
            x=filtered_data['BP Severity'],
            name=gender,
            boxmean='sd',  # Show mean and standard deviation
            marker_color='#1f77b4' if gender == 'Male' else '#ff7f0e',  # Different colors for genders
            text=filtered_data['BP Severity'],  # Adding text for tooltips
            hoverinfo='y+name+text'
        ))

    # Update layout with titles, axis labels, and other properties
    fig.update_layout(
        title='BMI by BP Severity and Legal Sex',
        title_font=dict(size=20, color='white'),
        xaxis_title='BP Severity',
        yaxis_title='BMI',
        xaxis=dict(tickfont=dict(size=14, color='white')),
        yaxis=dict(tickfont=dict(size=14, color='white')),
        boxmode='group',  # Group box plots by BP Severity
        height=600,  # Set the height of the figure
        width=800,   # Set the width of the figure
        # paper_bgcolor='#FAF5E6',
        # plot_bgcolor='#FAF5E6'
    )
    
    return fig


def chart_9(data):
    import plotly.graph_objects as go
    disease_data = data.copy()
    disease_data = disease_data.select_dtypes(include=['int64', 'float64'])
    columns_to_drop = ['PatientID']
    disease_data.drop(columns=columns_to_drop, inplace=True)
    
    # Calculate the correlation matrix
    corrmat = disease_data.corr()
    corrmat.fillna(0, inplace=True)
    
    # Create a heatmap using Plotly
    fig = go.Figure(data=go.Heatmap(
        z=corrmat.values,
        x=corrmat.columns,
        y=corrmat.columns,
        colorscale='RdYlGn',
        # colorbar=dict(title='Correlation', tickvals=[-1, 0, 1], ticktext=['-1', '0', '1']),
        text=corrmat.round(2).values,  # Add annotations
        texttemplate="%{text:.2f}",  # Format annotations
        textfont=dict(size=12, color='black')  # Set annotation font size and color
    ))

    # Update layout
    fig.update_layout(
        title='Which Feature is Mainly Involved',
        title_font=dict(size=20, color='white'),
        xaxis_title='Features',
        yaxis_title='Features',
        xaxis=dict(tickfont=dict(size=14, color='white')),
        yaxis=dict(tickfont=dict(size=14, color='white')),
        height=600,  # Set the height of the figure
        width=800    # Set the width of the figure
    )
    
    return fig

def chart_10(data):
    import plotly.express as px
    import plotly.graph_objects as go

    graph_7 = data.copy()
    graph_7 = graph_7[graph_7['Depression Severity'] != 'None-minimal']
    graph_7 = graph_7[graph_7['Depression Severity'] != 'Unknown']
    graph_7['Age'] = pd.to_numeric(graph_7['Age'], errors='coerce')
    graph_7 = graph_7.dropna(subset=['Age','Depression Severity','LegalSex'])

    # Create the violin plot
    fig = go.Figure()

    for sex in graph_7['LegalSex'].unique():
        fig.add_trace(go.Violin(
            x=graph_7['Depression Severity'][graph_7['LegalSex'] == sex],
            y=graph_7['Age'][graph_7['LegalSex'] == sex],
            legendgroup=sex, scalegroup=sex, name=sex, side='negative' if sex == 'Female' else 'positive',
            line_color='blue' if sex == 'Female' else 'orange'
        ))

    # Update the layout
    fig.update_layout(
        title="Age by Depression Severity and Legal Sex",
        xaxis_title="Depression Severity",
        yaxis_title="Age",
        xaxis=dict(tickmode='array', tickvals=graph_7['Depression Severity'].unique(), tickangle=20),
        yaxis=dict(range=[0, 80]),
        violingap=0.2,  # gap between violins
        violingroupgap=0.3,  # gap between groups
        violinmode='overlay',  # plot violins over each other
        font=dict(color='white', size=14),
        title_font=dict(size=20, color='white'),
        xaxis_tickfont=dict(size=14, color='white'),
        yaxis_tickfont=dict(size=14, color='white'),
        paper_bgcolor='rgba(0,0,0,0)',
        plot_bgcolor='rgba(0,0,0,0)',
        showlegend=True
    )

    return fig


def feature_analytics(disease_data):
    corrmat = disease_data.corr( numeric_only = True)
    corr_threshold = 0.7
    selected_features = []
    for column in corrmat.columns[:]:
        correlated_features = corrmat.index[corrmat[column] > corr_threshold].tolist()
        if correlated_features:
            selected_features.extend(correlated_features)
    selected_features = list(set(selected_features))
    values_to_pop = ['Weight', 'DiastolicBP', 'SystolicBP', 'ComponentValue', 'Height', 'Age', 'BMI']
    for value in values_to_pop:
        if value in selected_features:
            selected_features.remove(value)
    values_to_find = ['PeakFlow', 'Temperature', 'Respiration', 'Pulse', 'SPO2']
    found_values = []
    l = []
    m = []
    not_found_values = []
    for i, value in enumerate(selected_features):
        if value in values_to_find:
            found_values.append((i, value))
            l.append(value)
        else:
            not_found_values.append((i, value))
            m.append(value)
    return l,m



def chart_11(disease_data):
    import plotly.express as px
    feature = feature_analytics(disease_data)
    select,featurel = feature
    Top_feature_Lab = select[0]
    graph_8 = disease_data.copy()
    graph_8 = graph_8.dropna(subset=[Top_feature_Lab, 'Age', 'LegalSex'])

    # Create the scatter plot with Plotly
    fig = px.scatter(
        graph_8,
        x=Top_feature_Lab,
        y="Age",
        color="LegalSex",
        color_discrete_sequence=px.colors.qualitative.Set2,
        title=f'Age group: {Top_feature_Lab}',
        labels={Top_feature_Lab: Top_feature_Lab, 'Age': 'Age'},
        size_max=200
    )

    # Add vertical line at the mean
    mean_value = graph_8[Top_feature_Lab].mean()
    fig.add_vline(x=mean_value, line=dict(color='red', dash='dash'))

    # Customize the layout
    fig.update_layout(
        title_font=dict(size=20, color='white'),
        xaxis_title_font=dict(size=16, color='white'),
        yaxis_title_font=dict(size=16, color='white'),
        xaxis=dict(tickangle=20, tickfont=dict(size=14, color='white')),
        yaxis=dict(tickfont=dict(size=14, color='white'), range=[0, 80]),
        plot_bgcolor='black',
        paper_bgcolor='black'
    )

    return fig




def chart_12(filtered_data):
    graph_10 = filtered_data.copy()
    no_nan = graph_10.dropna(subset=['ImmunizationName'])
    immu = list(no_nan['ImmunizationName'])
    filtered_data = [item for item in immu if item and not pd.isna(item)]
    unique_values = set(filtered_data)
    my_string = ' '.join(unique_values)
    lmao = my_string.strip(', ')
    lmao = lmao.replace(',', '')
    title = "Immunization Word Cloud"
    cloud = WordCloud(scale=3,
                      max_words=150,
                      colormap='RdYlGn',
                      mask=None,
                      background_color='white',
                      stopwords=None,
                      collocations=True,
                      contour_color='black',
                      contour_width=1).generate(lmao)
    # axes[2,2].imshow(cloud, interpolation='bilinear')
    # axes[2,2].axis('off')
    # axes[2,2].set_title( f'Immunization',color='white', fontsize=20)
    plt.show()



def mean_of_values(cell_value):
    if pd.isna(cell_value):  # Check if cell value is NaN
        return np.nan
    values = [float(val) for val in cell_value.split(',')]
    return sum(values) / len(values)

def plots(original_data):
    a = original_data.copy()
    st.subheader("Clustering Analysis")
    col1, col2 = st.columns(2)
    ## 1
    cluster_counts = a['cluster'].value_counts().reset_index()
    cluster_counts.columns = ['cluster', 'count']  # Rename columns
    fig_1 = px.bar(cluster_counts, y='cluster', x='count', 
                labels={'cluster': 'Cluster', 'count': 'Count'},
                text_auto=True,  # text_auto=True displays the count on top of the bars
                color='cluster',  # Assign different colors to each bar
                color_continuous_scale='plasma',  # Use the plasma color scale
                category_orders={'cluster': [0, 1, 2, 3, 4]},
                )  # Set the order of clusters

    custom_labels = {0: 'Cluster 0', 1: 'Cluster 1', 2: 'Cluster 2', 3: 'Cluster 3', 4: 'Cluster 4'}
    fig_1.update_yaxes(tickvals=[0, 1, 2, 3, 4], ticktext=list(custom_labels.values()))

    fig_1.update_layout(
                    title={'text': "Count of Data Points per Cluster", 'y': 0.95, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top'},
                    yaxis_title='Cluster', xaxis_title='Count', 
                    xaxis=dict(showline=False, showgrid=False, zeroline=False, tickfont=dict(size=14, color='white')),
                    yaxis=dict(showline=False, showgrid=False, zeroline=False, tickfont=dict(size=14, color='white')),
                    title_font=dict(color='white', size=18),
                    # plot_bgcolor='black',  # Background color
                    # paper_bgcolor='black',  # Paper background color
                    title_x=0.5,  # Center the title
                    legend=dict(
                        font=dict(size=16, color='white'),
                        bgcolor='rgba(0,0,0,0)'
        ))
    col1.plotly_chart(fig_1,use_container_width=True)

    ## 2
    fig_2 = px.scatter(a, x='Age', y='BMI', 
                    color='cluster', 
                    title="Cluster's Profile Based On Age And BMI",
                    color_continuous_scale='plasma')  # Use the plasma color palette

    fig_2.update_layout(
        title={'text': "Cluster's Profile Based On Age And BMI", 'y': 0.95, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top'},
        xaxis=dict(showgrid=False, showticklabels=False, zeroline=False),
        yaxis=dict(showgrid=False, showticklabels=False, zeroline=False),
        # plot_bgcolor='black',  # Background color
        # paper_bgcolor='black',  # Paper background color
        title_font=dict(color='white', size=18),  # Title font color and size
        margin=dict(l=20, r=20, t=40, b=20), # Set margins to make the plot more compact
        legend=dict(
            font=dict(size=16, color='white'),
            bgcolor='rgba(0,0,0,0)'
        )
    )
    fig_2.update_traces(marker=dict(size=12, line=dict(width=2, color='DarkSlateGrey')))

    col2.plotly_chart(fig_2,use_container_width=True)

    col3, col4 = st.columns(2)
    ## 3
    palette = ['#636EFA', '#EF553B']  # Adjust the colors as needed
    fig_3 = go.Figure()
    for sex in a['LegalSex'].unique():
        fig_3.add_trace(go.Box(
            y=a[a['LegalSex'] == sex]['cluster'],
            name=f'Legal Sex: {sex}',
            marker_color=palette.pop(0),  # Pop the first color from the palette
            boxmean=True
        ))
    fig_3.update_layout(
        title={'text':"Clusters Distribution by Legal Sex", 'y': 0.95, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top'},
         title_font=dict(color='white', size=18),
        #  plot_bgcolor='black',
        # paper_bgcolor='black',
        xaxis=dict(showline=False, showgrid=False, zeroline=False, tickfont=dict(size=14, color='white')),
        yaxis=dict(showline=False, showgrid=False, zeroline=False, tickfont=dict(size=14, color='white')),
        # plot_bgcolor='rgba(0,0,0,0)',
        # paper_bgcolor='rgba(0,0,0,0)',
        title_font_color='white',
        showlegend=True,
        legend=dict(
            font=dict(size=16, color='white'),
            bgcolor='rgba(0,0,0,0)'
        )
    )

    col3.plotly_chart(fig_3,use_container_width=True)

    ## 4
    # palette = ['#636EFA', '#EF553B', '#00CC96', '#AB63FA', '#FFA15A']  # Example palette
    fig_4 = px.violin(
        a,
        x="BP Severity",
        y="cluster",
        color="BP Severity",
        color_discrete_sequence=px.colors.qualitative.Vivid,
        box=True,  # Adds a box plot inside the violin plot for more detail
        points="all",  # Shows all data points
        title="Clusters Distribution by BP Severity"
    )
    fig_4.update_layout(
        title={'text':"Clusters Distribution by BP Severity", 'y': 0.95, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top'},
        title_font=dict(color='white', size=18),
        xaxis_title="BP Severity",
        yaxis_title="Cluster",
        #  plot_bgcolor='black',
        # paper_bgcolor='black',
        xaxis_title_font=dict(size=16, color='white'),
        yaxis_title_font=dict(size=16, color='white'),
        xaxis=dict(showline=False, showgrid=False, zeroline=False, tickfont=dict(size=14, color='white')),
        yaxis=dict(showline=False, showgrid=False, zeroline=False, tickfont=dict(size=14, color='white')),
        title_font_color='white',
        legend=dict(
            font=dict(size=16, color='white'),
            bgcolor='rgba(0,0,0,0)'
        )
    )

    fig_4.update_xaxes(tickangle=45)  # Rotate the x-axis labels for better readability
    
    col4.plotly_chart(fig_4,use_container_width=True)

    col5, col6 = st.columns(2)
    ## 5
    fig_5 = px.histogram(a, x="Depression Severity", color="cluster",
                    color_discrete_sequence=px.colors.diverging.RdYlBu,
                    title='Clusters Distribution by Depression Severity')

    # Update layout to make it more attractive
    fig_5.update_layout(
        title={'text':"Clusters Distribution by Depression Severity", 'y': 0.95, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top'},
        title_font=dict(color='white', size=18),
        # plot_bgcolor='black',
        # paper_bgcolor='black',
        title_font_color='white',
        xaxis_title='Depression Severity',
        yaxis_title='Count',
        xaxis_title_font_color='white',
        yaxis_title_font_color='white',
        legend=dict(
            font=dict(size=16, color='white'),
            bgcolor='rgba(0,0,0,0)'
        ),
        xaxis=dict(
            tickfont=dict(color='white', size=14),
            title_font=dict(color='white', size=16),
            showline=False,
            showgrid=False,
            ticks=''
        ),
        yaxis=dict(
            tickfont=dict(color='white', size=14),
            title_font=dict(color='white', size=16),
            showline=False,
            showgrid=False,
            ticks=''
        ),
        coloraxis_colorbar=dict(
            tickfont=dict(color='white')
        )
    )

    # Show the plot
    col5.plotly_chart(fig_5,use_container_width=True)

    ## 6
    fig_6 = px.violin(a, y="cluster", x="Temp_condition", box=True, points="all",
                    color="Temp_condition", color_discrete_sequence=px.colors.diverging.RdYlBu,
                    title='Clusters Distribution by Temp_condition')

    # Update layout to make it more attractive
    fig_6.update_layout(
        title={'text':"Clusters Distribution by Temp_condition", 'y': 0.95, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top'},
        title_font=dict(color='white', size=18),
        # plot_bgcolor='black',
        # paper_bgcolor='black',
        title_font_color='white',
        xaxis_title='Temp_condition',
        yaxis_title='Clusters',
        xaxis_title_font_color='white',
        yaxis_title_font_color='white',
        legend=dict(
            font=dict(size=16, color='white'),
            bgcolor='rgba(0,0,0,0)'
        ),
        xaxis=dict(
            tickfont=dict(color='white', size=14),
            title_font=dict(color='white', size=16),
            showline=False,
            showgrid=False,
            ticks=''
        ),
        yaxis=dict(
            tickfont=dict(color='white', size=14),
            title_font=dict(color='white', size=16),
            showline=False,
            showgrid=False,
            ticks=''
        ),
        coloraxis_colorbar=dict(
            tickfont=dict(color='white')
        )
    )

    # Show the plot
    col6.plotly_chart(fig_6,use_container_width=True)

    col7, col8 = st.columns(2)

    ##7
    # Create the stacked bar chart
    ad = a.groupby(['weight_condition', 'cluster']).size().reset_index(name='count')

    fig_7 = px.bar(ad, 
                x='weight_condition', 
                y='count', 
                color='cluster', 
                title='Clusters Distribution by Weight Condition',
                text='count',
                barmode='stack',
                color_discrete_sequence=px.colors.diverging.RdYlBu)  # Use a color scale or palette of your choice

    # Update layout to make it more attractive and remove axes elements
    fig_7.update_layout(
        title={'text': 'Clusters Distribution by Weight Condition', 'y': 0.95, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top'},
        title_font=dict(color='white', size=18),
        xaxis=dict(
            title='',  # Remove x-axis title
            showline=False,
            showgrid=False,
            zeroline=False,
            tickfont=dict(size=14, color='white'),
            tickangle=45  # Rotate x-axis labels for better readability
        ),
        yaxis=dict(
            title='',  # Remove y-axis title
            showline=False,
            showgrid=False,
            zeroline=False,
            tickfont=dict(size=14, color='white')
        ),
        # plot_bgcolor='black',  # Background color
        # paper_bgcolor='black',  # Paper background color
        margin=dict(l=20, r=20, t=40, b=20),  # Set margins to make the plot more compact
        legend=dict(
            font=dict(size=16, color='white'),
            bgcolor='rgba(0,0,0,0)'
        )
    )

    # Update bar text style
    fig_7.update_traces(texttemplate='%{text:.2s}', textfont_size=14, textposition='inside', marker=dict(line=dict(width=1, color='DarkSlateGrey')))

    # Show the plot
    col7.plotly_chart(fig_7,use_container_width=True)


    ## 8
    fig_8 = px.box(a, 
             x='SPO2_condition', 
             y='Age', 
             points='all',  # Show all points
             title="Clusters Distribution by SPO2_condition",
             color='cluster',
             color_discrete_sequence=px.colors.sequential.Plasma_r)

    # Update layout to remove axes titles, labels, and gridlines, and style the chart
    fig_8.update_layout(
        title={'text': "Clusters Distribution by SPO2_condition", 'y': 0.95, 'x': 0.5, 'xanchor': 'center', 'yanchor': 'top'},
        title_font=dict(color='white', size=18),
        xaxis=dict(showline=False, showgrid=False, zeroline=False, tickfont=dict(size=14, color='white')),
        yaxis=dict(showline=False, showgrid=False, zeroline=False, tickfont=dict(size=14, color='white')),
        # plot_bgcolor='black',  # Background color
        # paper_bgcolor='black',  # Paper background color
        margin=dict(l=20, r=20, t=40, b=20),  # Set margins to make the plot more compact
        legend=dict(
            font=dict(size=16, color='white'),
            bgcolor='rgba(0,0,0,0)'
        )
    )

    # Customize the boxen plot appearance
    fig_8.update_traces(
        boxmean=True,  # Add mean line
        jitter=0.3,  # Spread points along x-axis
        marker=dict(size=10, line=dict(width=2, color='DarkSlateGrey'))
    )

    # Show the plot
    col8.plotly_chart(fig_8,use_container_width=True)

    col_11 = st.columns(1)[0]
    fig_11 = px.scatter_matrix(
    a[['Age', 'SystolicBP', 'Pulse', 'Weight', 'BMI', 'cluster']],
    dimensions=['Age', 'SystolicBP', 'Pulse', 'Weight', 'BMI'],
    color='cluster',
    title="Scatter Matrix of Selected Features by Cluster",
    labels={col: col for col in ['Age', 'SystolicBP', 'Pulse', 'Weight', 'BMI']},
    color_continuous_scale= px.colors.diverging.Spectral
    )

    # Update layout for better visualization
    fig_11.update_traces(diagonal_visible=True)
    fig_11.update_layout(height=700, width=700, showlegend=True)

    # Show the plot
    col_11.plotly_chart(fig_11,use_container_width=True)
    #

    ##### Joint Plot
    st.subheader("Summary")
    meanvalue_columns = [col for col in list(a.columns) if 'meanvalue' in col]
        # Group data by clusters
    grouped_data = a.groupby('cluster')

    # Calculate mean for numerical columns
    numerical_columns = a.select_dtypes(include=['number']).columns
    numerical_summary = grouped_data[numerical_columns].mean()

    # Calculate mode for categorical columns
    categorical_columns = a.select_dtypes(include=['object', 'category','string']).columns
    categorical_summary = grouped_data[categorical_columns].agg(lambda x: x.value_counts().index[0])

    for i in range(len(a['cluster'].value_counts())):
    # Example for Cluster 0
        cluster_traits = {
            "Age": numerical_summary.loc[i, 'Age'],
            "Age_Category": categorical_summary.loc[i,"Age_Category"],
            "SystolicBP": numerical_summary.loc[i, 'SystolicBP'],
            "Depression Severity": categorical_summary.loc[i, 'Depression Severity'],
            "Weight Condition" : categorical_summary.loc[i, 'weight_condition'],
            "BP Severity" : categorical_summary.loc[i, 'BP Severity'],
            "Pulse_condition" : categorical_summary.loc[i, 'Pulse_condition'],
            "Respiration_condition" : categorical_summary.loc[i, 'Respiration_condition'],
            "SPO2_condition" :  categorical_summary.loc[i, 'SPO2_condition'],

        }

        # if numerical_summary.loc[i, 'GLUCOSE_meanvalue'] > 100:
        #     glucose_condition = "High frequency of patients with slightly elevated glucose levels."
        # else:
        #     glucose_condition = "Normal glucose levels."


        
        # Writing the summary
        summary = f"""
        Cluster - {i} Traits
        1. Age: Average age is {round(cluster_traits['Age'])} years.
        2. SystolicBP: Patients tend to have slightly elevated systolic blood pressure, averaging {cluster_traits['SystolicBP']} mmHg.
        3. Depression Severity: Predominantly '{cluster_traits['Depression Severity']}'.
        4. "Weight Condition" : {cluster_traits['Weight Condition']}.
        5. "Respiration_condition" : {cluster_traits['Respiration_condition']}.
        6. "Pulse_condition" : {cluster_traits['Pulse_condition']}.
        7. "SPO2_condition" : {cluster_traits['SPO2_condition']}.

        Trait Summary: Cluster {i} mainly consists of {cluster_traits['Age_Category']} individuals with {cluster_traits['Depression Severity']} depression level, {cluster_traits['BP Severity'].lower()}.
        """

        st.write(summary)
    st.write(round(numerical_summary[meanvalue_columns],2))
    
    st.subheader("Density Contour Plot")    
    with st.container():
    # Loop through the columns and create plots
        for i in meanvalue_columns:
            fig = px.density_contour(
                a,  # Replace 'a' with your actual DataFrame name
                y="Age",
                x=i,
                color="cluster",
                marginal_x="histogram",
                marginal_y="histogram",
                template="simple_white",
                color_discrete_sequence=px.colors.qualitative.Set1
            )

            # Add fill to the contours for a similar effect to kde
            fig.update_traces(bingroup="fill")

            # Update layout for better aesthetics
            fig.update_layout(
                title=f"Joint Density Contour of {i} vs Age by Clusters",
                yaxis_title="Age",
                xaxis_title=i,
                xaxis=dict(
                    title=i,
                    showline=False,
                    showgrid=False,
                    zeroline=False,
                    tickfont=dict(size=14, color='white'),
                    tickangle=45,  # Rotate x-axis labels for better readability
                    titlefont=dict(size=16, color='white')  # Set x-axis title to white
                ),
                yaxis=dict(
                    title='Age',
                    showline=False,
                    showgrid=False,
                    zeroline=False,
                    tickfont=dict(size=14, color='white'),
                    titlefont=dict(size=16, color='white')  # Set y-axis title to white
                ),
                plot_bgcolor='black',
                paper_bgcolor='black',
                title_font_color='white',
                legend_title="Clusters",
                width=1500,  # Adjust width as needed
                height=800   # Increase height to make the plot taller
            )

            # Display the plot using st.plotly_chart within a column
            st.plotly_chart(fig, use_container_width=True)


def ML(filtered_data, scaler):
    man = filtered_data.copy()
    man=man.dropna()
    
    man.drop(columns=['PatientID','VisitID'],inplace=True)
    numerical_columns = list(man.select_dtypes(include=['int', 'float']).columns)
    categorial_columns = list(man.select_dtypes(exclude=['int', 'float','datetime']).columns)
    categorical_indexes = []

    for c in categorial_columns:
        categorical_indexes.append(man.columns.get_loc(c))
    
    t = man.shape
    # st.write(t)
    if 5 < t[0] < 10:
        ki =  3
    elif t[0] <= 4 :
        ki = 1
    else:
        ki = 4
    kproto = KPrototypes(n_clusters= ki, init='Huang', n_init = 25, random_state=42)
    kproto.fit_predict(man, categorical= categorical_indexes)
    cluster_labels = kproto.labels_

    original_numeric_data = scaler.inverse_transform(man[numerical_columns])

    # Convert back to DataFrame and add cluster labels
    original_data = pd.DataFrame(original_numeric_data, columns=numerical_columns)
    original_data["cluster"] = cluster_labels
    original_data["cluster"] = original_data["cluster"].astype('category')

    ## PCA Graph
    pca = PCA(n_components=4)
    pca_df = pca.fit_transform(original_data[numerical_columns])
    d = list(original_data[numerical_columns].columns)
    pca_df = pd.DataFrame(pca_df, columns=d[:4])
    import plotly.graph_objects as go

    st.subheader("PCA")
    fig_9 = go.Figure(
        go.Scatter3d(mode='markers',
                    x = pca_df.iloc[:, 0],
                    y = pca_df.iloc[:, 1],
                    z = pca_df.iloc[:, 2],
                    marker=dict(size = 4, color = original_data['cluster'], colorscale = 'spectral')
                    )
    )
    
    fig_9.update_layout(
        scene=dict(
            xaxis_title=d[0],
            yaxis_title=d[1],
            zaxis_title=d[2],
            # bgcolor='black',  # Background color inside the 3D plot
            xaxis=dict(color='white'),  # Axis label color
            yaxis=dict(color='white'),
            zaxis=dict(color='white')
        ),
        # plot_bgcolor='black',  # Background color outside the 3D plot
        # paper_bgcolor='black'  # Paper (entire plot area) background color
    )
    col9 = st.columns(1)[0]
    col9.plotly_chart(fig_9, use_container_width=True)




    mann = man[categorial_columns].copy()
    orig = original_data.reset_index(drop=True)
    mann = mann.reset_index(drop=True)
    original_data = pd.concat([orig, mann], axis=1)

    return plots(original_data)



def imputer(filtered_data):
    # numeric_columns = filtered_data.select_dtypes(include=['int', 'float'])
    # numeric_columns = numeric_columns.iloc[:,2:].copy()

    # # Setting the random_state argument for reproducibility
    # imputer = IterativeImputer(random_state=42)
    # imputed = imputer.fit_transform(numeric_columns)
    # Imputed_data = pd.DataFrame(imputed, columns=numeric_columns.columns)
    # Imputed_data = round(Imputed_data, 2)
    # columns_drop = Imputed_data.columns
    # filtered_data = filtered_data.drop(columns=columns_drop)
    # Ml_data = pd.concat([filtered_data, Imputed_data], axis=1)
    # unscaled_data = Ml_data.copy()

    ##Scaling
    Ml_data = filtered_data.copy()
    scaled_data = Ml_data.select_dtypes(include=['int', 'float'])
    scaled_data = scaled_data.iloc[:,2:].copy()
    scaler = StandardScaler()
    scaler.fit(scaled_data)
    scaled_data = pd.DataFrame(scaler.transform(scaled_data),columns= scaled_data.columns)
    columns_drop = scaled_data.columns
    Ml_data = Ml_data.drop(columns=columns_drop)
    Ml_data = pd.concat([Ml_data, scaled_data], axis=1)
    Ml_data = Ml_data.convert_dtypes()  # change this to outlier_removed if you want outliwer to be removed
    return ML(Ml_data, scaler)




# @st.cache_data()
# def fetch_data_1():
#     data = pd.read_parquet(local_file_1)
#     return data



if analysis_option == 'Machine Learning':
    f_problem = ['Hypertensive diseases', 'General symptoms', 'Digestive, abdomen symptoms', 'Other dorsopathies', 'Metabolic disorders', 'Health service encounters', 'Diabetes mellitus', 'Chronic lower resp', 'Obesity, hyperalimentation', 'Body mass index (BMI)', 'Communicable hazards', 'Other viral diseases', 'Eyelid, lacrimal disorders', 'Male genital diseases', 'Unclassified pain, Other dorsopathies, Other dorsopathies', 'Circ, resp symptoms', 'Unclassified pain', 'Anticoagulant use', 'Influenza, pneumonia', 'Soft tissue disorders', 'Urinary symptoms', 'Other risk factors', 'Blood exam findings', 'Nutritional disorders', 'Other joint disorders', 'Mycoses', 'Other skin disorders', 'Other health encounters', 'Maternal disorders', 'Reproduction services', 'Other obstetric conditions', 'Gestation weeks', 'Oral, salivary diseases', 'Family, personal hazards', 'Respiratory interstitial diseases', 'Mood disorders', 'Arthrosis', 'Nutritional anaemias', 'Intestinal diseases', 'Med, surg care complications', 'Skin, tissue symptoms', 'Neurotic disorders', 'High-risk pregnancy', 'Female genital disorders', 'Urine exam findings', 'Other joint disorders, Other joint disorders', 'Socioeconomic hazards', 'Maternal care, delivery issues', 'Aplastic anaemias', 'Sexual trans infections', 'Substance use disorders', 'Oesoph, stomach diseases', 'Behavioural syndromes', 'Preg-related disorders', 'Gallbladder, pancreas', 'Pregnancy outcomes', 'Imaging, function findings', 'Episodic disorders', 'Other resp diseases', 'Nervous, musculo symptoms', 'Other dorsopathies, Unclassified pain', 'Spondylopathies', 'External ear diseases', 'Other ear disorders', 'Dermatitis, eczema', 'Polyneuropathies', 'Urinary system diseases', 'Unclassified pain, Other dorsopathies', 'Benign neoplasms', 'Diabetes mellitus, Renal failure', 'Renal failure', 'Thyroid disorders', 'Cognition, emotion symptoms', 'Paralytic syndromes', 'Unclassified pain, Other joint disorders', 'Bone density disorders', 'Other heart diseases', 'Ischaemic heart diseases', 'Delivery', 'Other digestive disorders', 'Viral skin lesions', 'Breast disorders', 'Cerebrovascular diseases', 'Male genital malig', 'Animal force exposures', 'Urolithiasis', 'Erectile dysfunction', 'Other land accidents', 'Visual disturbances', 'Other resp disorders', 'Skin appendage disorders', 'Upper resp infections', 'Benign neoplasms, Benign neoplasms', 'Neurotic disorders, Mood disorders', 'Maternal disorders, Digestive, abdomen symptoms', 'Puerperium complications', 'Childhood disorders', 'Thorax injuries', 'Other eye disorders', 'Conjunctiva disorders, Other resp diseases', 'Conjunctiva disorders', 'Synovium, tendon disorders', 'Renal tubulo diseases', 'Liver diseases', 'Blood alcohol level', 'Labour, delivery complications', 'Family, personal hazards, Family, personal hazards', 'Unspecified trunk injuries', 'Skin infections', 'Pelvic inflammatory diseases', 'Labour complications', 'Unclassified pain, Other joint disorders, Other joint disorders', 'Hernia', 'Vein, lymph disorders', 'Other CNS disorders', 'Blood disorders', 'Renal hypertension', 'Endocrine disorders', 'Infectious agents', 'Specific health procedures', 'Papulosquamous disorders', 'Muscle disorders', 'Schizophrenia disorders', 'Pleura diseases', 'Ocular muscle disorders', 'Other exam findings', 'Intest infect diseases', 'Female genital disorders, Family, personal hazards', 'Middle ear diseases', 'Kidney, ureter disorders', 'Benign neoplasms, Maternal care, delivery issues', 'Pulmonary heart disease', 'Aplastic anaemias, Renal failure', 'Speech, voice symptoms', 'Unspecified behavior neoplasms', 'Soft tissue disorders, Soft tissue disorders', 'Other joint disorders, Unclassified pain, Other joint disorders', 'Viral hepatitis', 'Unclassified pain, Soft tissue disorders', 'In situ neoplasms', 'Chlamydia diseases', 'Unclassified pain, Soft tissue disorders, Soft tissue disorders', 'Haemolytic anaemias', 'Lens disorders', 'Head injuries', 'Nerve disorders', 'Neurotic disorders, Neurotic disorders', 'Other effects', 'Thyroid disorders, Other obstetric conditions', 'Maternal care, delivery issues, Maternal care, delivery issues', 'Polyarthropathies', 'Enteritis, colitis', 'Female genital disorders, Maternal disorders', 'Personality disorders', 'Vitreous, globe disorders', 'Musculoskeletal malformations', 'Abdomen, lumbar injuries', 'Neck injuries', 'Dorsopathies', 'Male genital diseases, Urinary symptoms', 'Glaucoma', 'Knee, lower leg injuries', 'Gestation, growth issues', 'Skin, tissue symptoms, Skin, tissue symptoms', 'Coagulation disorders, Other obstetric conditions', 'Coagulation disorders', 'Immune disorders', 'Diabetes mellitus, Metabolic disorders', 'Circulatory disorders', 'Artery diseases', 'Movement disorders', 'Circulatory malformations', 'Skin malig', 'Maternal disorders, Urinary symptoms', 'General symptoms, General symptoms', 'Breast cancer', 'Urinary malformations', 'Viral hepatitis, Other obstetric conditions', 'Lower resp infections', 'Elbow, forearm injuries', 'Health service encounters, Family, personal hazards', 'Sexual trans infections, Other obstetric conditions', 'CNS malformations', 'Glucose disorders', 'Arthrosis, Arthrosis', 'Digestive organ malig', 'Connective tissue disorders', 'Specified arthritis', 'Genital malformations', 'Myoneural, muscle diseases', 'Other obstetric conditions, Sexual trans infections', 'Sclera, cornea disorders', 'Wrist, hand injuries', 'Renal failure, Diabetes mellitus', 'Hypertensive diseases, Circ, resp symptoms', 'Diabetes mellitus, Other skin disorders', 'Chromosomal abnormalities', 'Unspecified mental disorder', 'Infectious agents, Oesoph, stomach diseases', 'Oesoph, stomach diseases, Oesoph, stomach diseases', 'Respiratory malig', 'Organic mental disorders, Degenerative CNS diseases', 'Med, surg care complications, Family, personal hazards', 'Unclassified pain, Breast disorders', 'Health service encounters, Other health encounters', 'Digestive, abdomen symptoms, Digestive, abdomen symptoms', 'Mechanical force exposures', 'Pregnancy outcomes, Specific health procedures', 'Organic mental disorders, Head injuries', 'Radiation skin disorders']    
    problem = ['Hypertensive diseases', 'General symptoms', 'Digestive, abdomen symptoms', 'Other dorsopathies', 'Metabolic disorders', 'Health service encounters', 'Diabetes mellitus', 'Chronic lower resp', 'Obesity, hyperalimentation', 'Body mass index (BMI)', 'Communicable hazards', 'Other viral diseases', 'Eyelid, lacrimal disorders', 'Male genital diseases', 'Unclassified pain, Other dorsopathies, Other dorsopathies', 'Circ, resp symptoms', 'Unclassified pain', 'Anticoagulant use', 'Influenza, pneumonia', 'Soft tissue disorders', 'Urinary symptoms', 'Other risk factors', 'Blood exam findings', 'Nutritional disorders', 'Other joint disorders', 'Mycoses', 'Other skin disorders', 'Other health encounters', 'Maternal disorders', 'Reproduction services', 'Other obstetric conditions', 'Gestation weeks', 'Oral, salivary diseases', 'Family, personal hazards', 'Respiratory interstitial diseases', 'Mood disorders', 'Arthrosis', 'Nutritional anaemias', 'Intestinal diseases', 'Med, surg care complications', 'Skin, tissue symptoms', 'Neurotic disorders', 'High-risk pregnancy', 'Female genital disorders', 'Urine exam findings', 'Other joint disorders, Other joint disorders', 'Socioeconomic hazards', 'Maternal care, delivery issues', 'Aplastic anaemias', 'Sexual trans infections', 'Substance use disorders', 'Oesoph, stomach diseases', 'Behavioural syndromes', 'Preg-related disorders', 'Gallbladder, pancreas', 'Pregnancy outcomes', 'Imaging, function findings', 'Episodic disorders', 'Other resp diseases', 'Nervous, musculo symptoms', 'Other dorsopathies, Unclassified pain', 'Spondylopathies', 'External ear diseases', 'Other ear disorders', 'Dermatitis, eczema', 'Polyneuropathies', 'Urinary system diseases', 'Unclassified pain, Other dorsopathies', 'Benign neoplasms', 'Diabetes mellitus, Renal failure', 'Renal failure', 'Thyroid disorders', 'Cognition, emotion symptoms', 'Paralytic syndromes', 'Unclassified pain, Other joint disorders', 'Bone density disorders', 'Other heart diseases', 'Ischaemic heart diseases', 'Delivery', 'Other digestive disorders', 'Viral skin lesions', 'Breast disorders', 'Cerebrovascular diseases', 'Male genital malig', 'Animal force exposures', 'Urolithiasis', 'Erectile dysfunction', 'Other land accidents', 'Visual disturbances', 'Other resp disorders', 'Skin appendage disorders', 'Upper resp infections', 'Benign neoplasms, Benign neoplasms', 'Neurotic disorders, Mood disorders', 'Maternal disorders, Digestive, abdomen symptoms', 'Puerperium complications', 'Childhood disorders', 'Thorax injuries', 'Other eye disorders', 'Conjunctiva disorders, Other resp diseases', 'Conjunctiva disorders', 'Synovium, tendon disorders', 'Renal tubulo diseases', 'Liver diseases', 'Blood alcohol level', 'Labour, delivery complications', 'Family, personal hazards, Family, personal hazards', 'Unspecified trunk injuries', 'Skin infections', 'Pelvic inflammatory diseases', 'Labour complications', 'Unclassified pain, Other joint disorders, Other joint disorders', 'Hernia', 'Vein, lymph disorders', 'Other CNS disorders', 'Blood disorders', 'Endocrine disorders', 'Infectious agents', 'Specific health procedures', 'Papulosquamous disorders', 'Muscle disorders', 'Schizophrenia disorders', 'Pleura diseases', 'Ocular muscle disorders', 'Other exam findings', 'Intest infect diseases', 'Middle ear diseases', 'Kidney, ureter disorders', 'Benign neoplasms, Maternal care, delivery issues', 'Pulmonary heart disease', 'Aplastic anaemias, Renal failure', 'Speech, voice symptoms', 'Unspecified behavior neoplasms', 'Soft tissue disorders, Soft tissue disorders', 'Other joint disorders, Unclassified pain, Other joint disorders', 'Viral hepatitis', 'Unclassified pain, Soft tissue disorders', 'Chlamydia diseases', 'Unclassified pain, Soft tissue disorders, Soft tissue disorders', 'Haemolytic anaemias', 'Lens disorders', 'Head injuries', 'Nerve disorders', 'Neurotic disorders, Neurotic disorders', 'Other effects', 'Maternal care, delivery issues, Maternal care, delivery issues', 'Polyarthropathies', 'Enteritis, colitis', 'Female genital disorders, Maternal disorders', 'Personality disorders', 'Vitreous, globe disorders', 'Musculoskeletal malformations', 'Abdomen, lumbar injuries', 'Neck injuries', 'Dorsopathies', 'Male genital diseases, Urinary symptoms', 'Glaucoma', 'Knee, lower leg injuries', 'Gestation, growth issues', 'Skin, tissue symptoms, Skin, tissue symptoms', 'Coagulation disorders, Other obstetric conditions', 'Coagulation disorders', 'Immune disorders', 'Diabetes mellitus, Metabolic disorders', 'Circulatory disorders', 'Artery diseases', 'Movement disorders', 'Circulatory malformations', 'Skin malig', 'General symptoms, General symptoms', 'Breast cancer', 'Urinary malformations', 'Viral hepatitis, Other obstetric conditions', 'Lower resp infections', 'Elbow, forearm injuries', 'Health service encounters, Family, personal hazards', 'Sexual trans infections, Other obstetric conditions', 'CNS malformations', 'Glucose disorders', 'Arthrosis, Arthrosis', 'Connective tissue disorders', 'Specified arthritis', 'Genital malformations', 'Myoneural, muscle diseases', 'Other obstetric conditions, Sexual trans infections', 'Sclera, cornea disorders', 'Wrist, hand injuries', 'Renal failure, Diabetes mellitus', 'Hypertensive diseases, Circ, resp symptoms', 'Diabetes mellitus, Other skin disorders', 'Chromosomal abnormalities', 'Unspecified mental disorder', 'Infectious agents, Oesoph, stomach diseases', 'Oesoph, stomach diseases, Oesoph, stomach diseases', 'Respiratory malig', 'Organic mental disorders, Degenerative CNS diseases', 'Med, surg care complications, Family, personal hazards', 'Unclassified pain, Breast disorders', 'Digestive, abdomen symptoms, Digestive, abdomen symptoms', 'Mechanical force exposures', 'Radiation skin disorders']
    st.subheader("_Select Disease_:sunglasses:")
    health_option = st.selectbox("_Select Disease_:sunglasses:",[*problem], label_visibility="collapsed")
    repo_id = "Akankshg/ML_DATA"
    filename_1 = f'imputed_scaled_data/{health_option}.parquet'
    token = os.environ["HUGGING_FACE_HUB_TOKEN"]
    local_file_1 = hf_hub_download(repo_id=repo_id, filename=filename_1,repo_type="dataset", token=token)
    filtered_data = pd.read_parquet(local_file_1)
    imputer(filtered_data)
    # if filtered_data['key_lab2'].notna().any():
    #     column_list = ['PatientID', 'VisitID', 'GroupedICD'] + list(filtered_data['key_lab2'].iloc[0])
    #     pivot_data = pd.pivot_table(filtered_data, values='ComponentValue', index=['PatientID', 'VisitID', 'GroupedICD'], columns='ComponentName', aggfunc=lambda x: ', '.join(map(str, x)))
    #     pivot_data = pivot_data.reset_index(drop=False)
    #     pivot_data = pivot_data[column_list].copy()
    #     filtered_data = pd.merge(filtered_data, pivot_data, on=['PatientID', 'VisitID','GroupedICD'], how='left')
        
    #     filtered_data.iloc[:, -20:] = filtered_data.iloc[:, -20:].convert_dtypes()
    #     hmm = pd.DataFrame()
    #     # num_columns = 20 
    #     num_columns = len(list(filtered_data['key_lab2'].iloc[0]))
    #     for i in range(1, num_columns+1):
    #         existing_column = filtered_data.columns[-i]
    #         new_column_name = f'{existing_column}_meanvalue'
    #         hmm[new_column_name] = filtered_data[existing_column].apply(mean_of_values)
    #     filtered_data = pd.concat([filtered_data, hmm], axis=1)
    #     column_list = [
    #     ## Necessary columns
    #     'PatientID', 'VisitID', 'GroupedICD',
        
    #     ## Numerical values
    #     'Age', 'SystolicBP',
    #     'DiastolicBP','Temperature',
    #     'Pulse', 'Weight', 'Height', 'BMI', 'Respiration',
    #     'SPO2', 'PHQ_9Score',
    #     # 'PeakFlow'
    
    #     ## Categorial Values
    #     'LegalSex','BPLocation', 'BPPosition', 'PregnancyStatus', 'LactationStatus', 'TemperatureSource',
    #     'Age_Category','BP Severity','Depression Severity','weight_condition', 'Temp_condition', 'Pulse_condition',
    #     'Respiration_condition', 'SPO2_condition', 'PeakF_condition']
    #     # last = list(filtered_data.columns[-20:])
    #     last = list(hmm.columns)
    #     required_columns = column_list + last
    #     filtered_data = filtered_data[required_columns].copy()
    #     filtered_data = filtered_data.drop_duplicates().reset_index(drop=True)
    #     filtered_data = filtered_data.dropna(axis=1, how='all')
        # imputer(filtered_data)





























def word_cloud(data):
    no_nan = data.dropna(subset=['ImmunizationName'])
    immu = list(no_nan['ImmunizationName'])
    filtered_data = [item for item in immu if item and not pd.isna(item)]
    unique_values = set(filtered_data)
    my_string = ' '.join(unique_values)
    lmao = my_string.strip(', ')
    lmao = lmao.replace(',', '')
    
    cloud = WordCloud(
        scale=3,
        max_words=150,
        colormap='RdYlGn',
        mask=None,
        background_color='white',
        stopwords=None,
        collocations=True,
        contour_color='black',
        contour_width=1
    ).generate(lmao)
    
    # Create a Matplotlib figure
    fig, ax = plt.subplots(figsize=(10, 6))
    ax.imshow(cloud, interpolation='bilinear')
    ax.axis('off')  # Remove the axes
    ax.set_title('Immunization Word Cloud', color='black', fontsize=20)
    
    # Return the figure to be used in Streamlit
    return fig

def more_scatter(data):
    component_icd_counts = data.groupby(['ComponentName', 'GroupedICD','Description']).size().reset_index(name='Count')
    component_icd_counts = component_icd_counts[component_icd_counts['Count']> 900]
    import plotly.express as px
    # Scatter plot
    fig16 = px.scatter(component_icd_counts, y='ComponentName', x='Description', size='Count',
                    hover_name='ComponentName', color='Count', title='Lab Component-ICD Relationship',color_continuous_scale='YlOrBr')
    fig16.update_layout(template="plotly_dark",xaxis_title='ICD Code', yaxis_title='Component Name', coloraxis_colorbar=dict(title='Count'))
    return fig16


































if analysis_option == 'Data':
    age_min = int(data['Age'].min())
    age_max = int(data['Age'].max())
    age_range = st.sidebar.slider('Select Age Range', age_min, age_max, (age_min, age_max))
    data = data[(data['Age'] >= age_range[0]) & (data['Age'] <= age_range[1])].copy()
        
    Sex = data.groupby('LegalSex')['PatientID'].nunique().reset_index(name='count')
    st.subheader("Distribution of Patient's by Sex", divider='rainbow')
    col1, col2,col3 = st.columns(3)
    col1.metric(label="Male", value = Sex[Sex['LegalSex']=='Male']['count'][1])
    col2.metric(label="Female", value = Sex[Sex['LegalSex']=='Female']['count'][0])
    col4, col5 = st.columns(2)
    fig2 = funnel_chart(data)
    col4.plotly_chart(fig2, use_container_width=True)
    fig = scatterplot(data)
    col5.plotly_chart(fig, use_container_width=True)
    col6 = st.columns(1)[0]
    fig_man = scatter_man(data)
    col6.plotly_chart(fig_man, use_container_width=True)
    fig16 = more_scatter(data)
    col8 = st.columns(1)[0]
    col8.plotly_chart(fig16, use_container_width=True)

    st.dataframe(data.head(20).style.format({'PatientID': "{:.0f}"}))

if analysis_option == 'EDA':  
    problem = ['Health service encounters', 'Diabetes mellitus', 'Chronic lower resp', 'Obesity, hyperalimentation', 'Body mass index (BMI)', 'Eyelid, lacrimal disorders', 'Communicable hazards', 'Hypertensive diseases', 'Soft tissue disorders', 'Urinary symptoms', 'Metabolic disorders', 'Falls', 'Other risk factors', 'Nutritional disorders', 'General symptoms', 'Blood exam findings', 'Other joint disorders', 'Mycoses', 'Other dorsopathies', 'Other skin disorders', 'Diabetes mellitus, Obesity, hyperalimentation', 'Maternal disorders', 'Reproduction services', 'Circ, resp symptoms', 'Other obstetric conditions', 'Gestation weeks', 'Oral, salivary diseases', 'Family, personal hazards', 'Respiratory interstitial diseases', 'Mood disorders', 'Arthrosis', 'Nutritional anaemias', 'Intestinal diseases', 'High-risk pregnancy', 'Female genital disorders', 'Substance use disorders', 'Aplastic anaemias', 'Behavioural syndromes', 'Preg-related disorders', 'Newborn problems', 'Labor,delivery complications', 'Maternal care, delivery issues', 'Neurotic disorders', 'Imaging, function findings', 'Episodic disorders', 'Other resp diseases', 'Unclassified pain', 'Nervous, musculo symptoms', 'Other dorsopathies, Unclassified pain', 'External ear diseases', 'Other ear disorders', 'Dermatitis, eczema', 'Urinary system diseases', 'Digestive, abdomen symptoms', 'Oesoph, stomach diseases', 'Unclassified pain, Other dorsopathies', 'Benign neoplasms', 'Diabetes mellitus, Renal failure', 'Renal failure', 'Cognition, emotion symptoms', 'Paralytic syndromes', 'Unclassified pain, Other joint disorders', 'Bone density disorders', 'Other heart diseases', 'Anticoagulant use', 'Ischaemic heart diseases', 'Delivery', 'Polyneuropathies', 'Other land accidents', 'Other resp disorders', 'Viral skin lesions', 'Skin appendage disorders', 'Other health encounters', 'Other joint disorders, Other joint disorders', 'Renal tubulo diseases', 'Sexual trans infections', 'Socioeconomic hazards', 'Childhood disorders', 'Malnutrition', 'Blood alcohol level', 'Skin, tissue symptoms', 'Pelvic inflammatory diseases', 'Maternal disorders, Digestive, abdomen symptoms', 'Labour complications', 'Thyroid disorders', 'Pregnancy outcomes', 'Unclassified pain, Other joint disorders, Other joint disorders', 'Hernia', 'Other dorsopathies, Soft tissue disorders', 'Liver diseases', 'Vein, lymph disorders', 'Other CNS disorders', 'Blood disorders', 'Renal hypertension', 'Poisoning', 'Conjunctiva disorders', 'Visual disturbances', 'Infectious agents', 'Muscle disorders', 'Fetal, newborn conditions', 'Ocular muscle disorders', 'Middle ear diseases', 'Developmental disorders', 'Urticaria, erythema', 'Neurotic disorders, Mood disorders', 'Intest infect diseases', 'Acne', 'Imaging, function findings, Family, personal hazards', 'Soft tissue disorders, Soft tissue disorders', 'Spondylopathies', 'Specific health procedures', 'Med, surg care complications', 'Unspecified trunk injuries', 'Vein, lymph disorders, Liver diseases', 'Urine exam findings', 'Infectious agents, Other obstetric conditions', 'Other exam findings', 'In situ neoplasms', 'Chlamydia diseases', 'Lens disorders', 'Head injuries', 'Upper resp infections', 'Viral hepatitis', 'Unclassified pain, Soft tissue disorders, Soft tissue disorders', 'Female genital disorders, Maternal disorders', 'Gallbladder, pancreas, Maternal disorders', 'Gallbladder, pancreas', 'Peritoneal diseases', 'Schizophrenia disorders', 'Mental retardation', 'Other eye disorders', 'Breast disorders', 'Musculoskeletal malformations', 'Multi-region injuries', 'Abdomen, lumbar injuries', 'Endocrine disorders', 'Male genital diseases', 'Puerperium complications', 'Sexual trans infections, Maternal disorders', 'Maternal care, delivery issues, Maternal care, delivery issues', 'Haemolytic anaemias', 'Glaucoma', 'Skin infections', 'Polyarthropathies', 'Nerve disorders', 'Papulosquamous disorders', 'Supplementary factors', 'Hypertensive crisis', 'Cerebrovascular diseases', 'Organic mental disorders', 'Degenerative CNS diseases, Organic mental disorders', 'Degenerative CNS diseases', 'Kidney, ureter disorders', 'Immune disorders', 'Erectile dysfunction', 'Circulatory disorders', 'Pleura diseases', 'Hypertensive diseases, Other heart diseases', 'Infectious agents, Pelvic inflammatory diseases', 'Other digestive disorders', 'Speech, voice symptoms', 'Urolithiasis', 'Pulmonary heart disease', 'Enteritis, colitis', 'Thorax injuries', 'Neck injuries', 'Circulatory malformations', 'Coagulation disorders', 'Other effects', 'Artery diseases', 'Influenza, pneumonia', 'Knee, lower leg injuries', 'Dentofacial anomalies', 'Male genital diseases, Urinary symptoms', 'Coagulation disorders, Other obstetric conditions', 'General symptoms, General symptoms', 'Puerperium complications, General symptoms', 'Other dorsopathies, Other obstetric conditions', 'Breast cancer', 'Other joint disorders, Unclassified pain, Other joint disorders', 'Synovium, tendon disorders', 'Toxic effects', 'Other dorsopathies, Other dorsopathies', 'Shoulder, upper arm injuries', 'Other osteopathies', 'Urinary malformations', 'Skin, tissue symptoms, Skin, tissue symptoms', 'Uncertain neoplasms', 'Lower resp infections', 'Conjunctiva disorders, Other resp diseases', 'Elbow, forearm injuries', 'Hypertensive diseases, Other heart diseases, Renal failure', 'Aplastic anaemias, Renal failure', 'Other exam findings, Other exam findings', 'Dorsopathies', 'Maternal disorders, General symptoms', 'Assault', 'Infestations', 'Inner ear diseases', 'Sexual trans infections, Other obstetric conditions', 'Foreign body effects', 'Diabetes mellitus, Artery diseases', 'Emergency U07.1 use', 'Renal failure, Hypertensive diseases', 'Infectious agents, Other resp diseases', 'Female genital disorders, Family, personal hazards', 'Diabetes mellitus, Metabolic disorders', 'Health service encounters, Family, personal hazards', 'Unclassified pain, Other dorsopathies, Other dorsopathies', 'Urinary tract malig', 'Labour, delivery complications', 'Arthrosis, Arthrosis', 'Animal force exposures', 'Gestation, growth issues', 'Genetic susceptibility to neoplasms, Genetic carrier, Genetic susceptibility to neoplasms, Genetic carrier', 'Digestive organ malig', 'Unspecified malig, Unspecified malig', 'Connective tissue disorders', 'Maternal disorders, Urinary symptoms', 'Nerve disorders, Maternal disorders', 'Infectious agents, Maternal disorders', 'Other viral diseases', 'Movement disorders', 'Viral hepatitis, Other obstetric conditions', 'Family, personal hazards, Family, personal hazards', 'Lymphoid, haematopoietic malig', 'Other obstetric conditions, Sexual trans infections', 'Male sexual dysfunction', 'Eye, ear malformations', 'Choroid, retina disorders', 'Gestation weeks, Maternal care, delivery issues', 'Maternal care, delivery issues, Gestation weeks', 'Female genital disorders, Med, surg care complications', 'Other bact diseases', 'General symptoms, Maternal disorders', 'Specified arthritis', 'Movement disorders, Poisoning', 'Female genital malig', 'Systemic atrophies, CNS malformations, Eye, ear malformations, Other malformations, Liver diseases, Mental retardation', 'Breast disorders, Breast disorders', 'Other resp disorders, Other resp disorders', 'Emergency U09.9 use', 'Sclera, cornea disorders', 'Skin infections, Skin infections', 'Perinatal resp, cardio disorders', 'Oesoph, stomach diseases, Circ, resp symptoms', 'Renal failure, Diabetes mellitus', 'Ankle, foot injuries', 'Other malformations', 'Skin malig', 'Unspecified behavior neoplasms', 'Intestinal diseases, Poisoning', 'Musculoskeletal disorders', 'Helminthiases', 'Childhood disorders, Cognition, emotion symptoms', 'Chondropathies', 'Renal tubulo diseases, Male genital diseases', 'Digestive, abdomen symptoms, Digestive, abdomen symptoms, Digestive, abdomen symptoms', 'Bone density disorders, Bone density disorders', 'HIV disease', 'Tuberculosis', 'Other bact diseases, Labour, delivery complications', 'Estrogen receptor status', 'Breast cancer, Estrogen receptor status', 'Infectious agents, Oesoph, stomach diseases', 'Chromosomal abnormalities', 'Female genital disorders, Female genital disorders', 'CNS inflammatory diseases', 'Blood exam findings, Infectious agents', 'Blood exam findings, Blood exam findings', 'Unspecified mental disorder', 'Behavioural syndromes, Unspecified mental disorder', 'Respiratory malig', 'Respiratory malig, Unspecified malig', 'Organic mental disorders, Degenerative CNS diseases', 'Unknown causes of death', 'General symptoms, Mood disorders', 'Genital malformations', 'Maternal care, delivery issues, Genital malformations', 'Unclassified pain, Soft tissue disorders', 'Wrist, hand injuries', 'Viral skin lesions, Other obstetric conditions', 'Poisoning, Circ, resp symptoms', 'Vitreous, globe disorders', 'Health service encounters, Other risk factors', 'Med, surg care complications, Family, personal hazards', 'Optic nerve disorders', 'Diabetes mellitus, Other CNS disorders', 'Postpartum complications', 'Middle ear diseases, Middle ear diseases', 'CNS malformations', 'Genetic carrier status', 'Diabetes mellitus, Urine exam findings', 'Circ, resp symptoms, General symptoms', 'Arthropod-borne fevers', 'Unclassified pain, Breast disorders', 'Soft tissue disorders, Unclassified pain', 'Appendix diseases', 'Neurotic disorders, Behavioural syndromes', 'Renal failure, Renal failure', 'Glucose disorders', 'Sexual trans infections, Sexual trans infections', 'Cognition, emotion symptoms, General symptoms', 'Diabetes mellitus, Other skin disorders', 'Digestive, abdomen symptoms, Digestive, abdomen symptoms', 'Cerebrovascular diseases, Speech, voice symptoms', 'Neurotic disorders, Socioeconomic hazards', 'Other obstetric conditions, Other dorsopathies', 'Other digestive disorders, Family, personal hazards', 'Neurotic disorders, Neurotic disorders', 'Episodic disorders, Episodic disorders', 'Polyarthropathies, Soft tissue disorders', 'Other bact diseases, Urinary system diseases', 'Demyelinating diseases', 'Personality disorders', 'Hip, thigh injuries', 'Renal failure, Aplastic anaemias', 'Gestation, growth issues, Gestation, growth issues', 'Other obstetric conditions, Mycoses', 'Hazardous exposures', 'Thorax injuries, Thorax injuries', 'Elbow, forearm injuries, Elbow, forearm injuries', 'Other spirochaetal dis', 'Diabetes mellitus, Diabetes mellitus', 'Burns, corrosions', 'Oesoph, stomach diseases, Oesoph, stomach diseases', 'Other joint disorders, Soft tissue disorders', 'Other bact diseases, General symptoms', 'Early trauma complications', 'Mechanical force exposures', 'Male genital malig', 'Ocular muscle disorders, Ocular muscle disorders', 'Skin, tissue symptoms, General symptoms', 'Glomerular diseases', 'Infectious agents, Upper resp infections', 'Urine exam findings, Other obstetric conditions', 'Polyarthropathies, Polyarthropathies', 'Benign neoplasms, Maternal care, delivery issues', 'Thyroid malig', 'Genital malformations, Maternal care, delivery issues', 'Infectious arthropathies', 'Pregnancy outcomes, Specific health procedures', 'Female genital disorders, Maternal care, delivery issues', 'Cleft lip, palate', 'Unspecified malig', 'Cognition, emotion symptoms, Mood disorders', 'Liver diseases, Other skin disorders', 'Jaw diseases', 'Substance use disorders, Other obstetric conditions', 'Other skin disorders, Socioeconomic hazards', 'Organic mental disorders, Head injuries', 'Skin infections, Unspecified trunk injuries', 'Specific health procedures, Family, personal hazards', 'Mycoses, Mycoses, Mycoses', 'Maternal disorders, Circ, resp symptoms', 'Oral, salivary diseases, Family, personal hazards', 'Mycoses, Poisoning', 'Family, personal hazards, Family, personal hazards, Family, personal hazards', 'Pedestrian accidents', 'Lung diseases', 'Sequelae of infections', 'Ocular muscle disorders, Ocular muscle disorders, Ocular muscle disorders', 'Self-harm', 'Hypertensive diseases, Renal failure', 'Infectious agents, Other resp disorders', 'Polyarthropathies, Toxic effects', 'Mycoses, Diabetes mellitus', 'Protozoal diseases', 'Other obstetric conditions, Soft tissue disorders', 'Wrist, hand injuries, Self-harm', 'Hip, thigh injuries, Animal force exposures', 'Chronic rheumatic heart', 'Diabetes mellitus, Vitreous, globe disorders', 'Vein, lymph disorders, Other skin disorders', 'Synovium, tendon disorders, Synovium, tendon disorders', 'Radiation skin disorders', 'Choroid, retina disorders, Family, personal hazards', 'Eyelid, lacrimal disorders, Eyelid, lacrimal disorders', 'Family, personal hazards, Nutritional anaemias, Other digestive disorders', 'Infectious agents, Conjunctiva disorders', 'Neurotic disorders, Nervous, musculo symptoms', 'Glucose regulation', 'Other skin disorders, Vein, lymph disorders', 'Cognition, emotion symptoms, Cognition, emotion symptoms', 'Eye, organ burns', 'Immune disorders, Pulmonary heart disease', 'Digestive, abdomen symptoms, Poisoning', 'Movement disorders, Nutritional anaemias', 'Soft tissue disorders, Soft tissue disorders, Soft tissue disorders, Soft tissue disorders', 'Bicycle accidents', 'Chronic lower resp, Chronic lower resp', 'Digestive malformations', 'Multiple burns', 'External prosthetics', 'Myoneural, muscle diseases', 'Soft tissue disorders, Polyarthropathies', 'Other obstetric conditions, Urine exam findings', 'Thyroid disorders, Other obstetric conditions', 'Thyroid disorders, Thyroid disorders', 'Blood exam findings, Poisoning', 'Other skin disorders, Diabetes mellitus', 'Assault by sharp object', 'Other resp diseases, Other resp diseases', 'Mood disorders, Neurotic disorders', 'Multiple burns, Heat, hot exposures', 'Lower resp conditions']
    st.subheader("_Select Disease_:sunglasses:")
    health_option = st.selectbox("_Select Disease_:sunglasses:",[*problem], label_visibility="collapsed")
    repo_id = "Akankshg/ML_DATA"
    filename = f'disease_data_EDA/{health_option}.parquet'
    
    token = os.environ["HUGGING_FACE_HUB_TOKEN"]
    
    local_file = hf_hub_download(repo_id=repo_id, filename=filename, repo_type="dataset",token=token)
    health_data = pd.read_parquet(local_file)
    
    Sex = health_data.groupby('LegalSex')['PatientID'].nunique().reset_index(name='count')
    st.subheader(f"Patients for '{health_option}' by Sex", divider='rainbow')
    col1, col2, col3 = st.columns(3)
    if 'Male' in Sex['LegalSex'].values:
        col1.metric(label="Male", value=Sex[Sex['LegalSex'] == 'Male']['count'].iloc[0])
    else:
        col1.metric(label="Male", value=0)
    if 'Female' in Sex['LegalSex'].values:
        col2.metric(label="Female", value=Sex[Sex['LegalSex'] == 'Female']['count'].iloc[0])
    else:
        col2.metric(label="Male", value=0)
    col4, col5 = st.columns(2)
    fig2 = funnel_chart(health_data)
    col4.plotly_chart(fig2, use_container_width=True)

    fig3 = barplot_lab(health_data)
    col5.plotly_chart(fig3, use_container_width=True)

    col6, col7 = st.columns(2)
    fig4 = histplot_6(health_data)
    col6.plotly_chart(fig4, use_container_width=True)

    fig5 = histplot_7(health_data)
    col7.plotly_chart(fig5, use_container_width=True)
    
    col8, col9 = st.columns(2)
    fig6 = pie_chart_7(health_data)
    col8.plotly_chart(fig6, use_container_width=True)

    fig7 = chart_8(health_data)
    col9.plotly_chart(fig7, use_container_width=True)
    

    col10, col11 = st.columns(2)
    fig8 = chart_9(health_data)
    col10.plotly_chart(fig8, use_container_width=True)

    fig9 = chart_10(health_data)
    col11.plotly_chart(fig9, use_container_width=True)

    col12, col13 = st.columns(2)
    fig10 = chart_11(health_data)
    col12.plotly_chart(fig10, use_container_width=True)

    fig11 = word_cloud(health_data)
    col13.pyplot(fig11, use_container_width=True)
    
    st.dataframe(health_data.head(20).style.format({'PatientID': "{:.0f}"}))





# Initialize Google Gemini or any other Google API client using the key


if analysis_option == 'Health Care Chat Bot AI':
    ##//////start here just add paitnet + vital information.
    # data = pd.read_parquet('Health-Data-3.parquet')
    google_api_key = os.environ.get("google_key")
    llm = GoogleGemini(api_key=google_api_key)
    pandas_ai = SmartDataframe(data, config={"llm": llm, "response_parser": StreamlitResponse,"verbose": True})
    pandas_ai_2 = SmartDataframe(data, config={"llm": llm,"verbose": True})   ## string
    # Streamlit app title and description
    st.title("AI-Powered Data Analysis App")
    st.write("This application allows you to interact with your dataset using natural language prompts. Just ask a question, and the AI will provide insights based on your data.")

    # Display the dataset
    st.subheader("Dataset Preview")
    st.dataframe(data.head())

    # User input for natural language prompt
    prompt = st.text_input("Enter your prompt:", placeholder="e.g., What are the top diagnoses?")

    # Process the input and display the result
    if st.button("Submit"):
        if 'plot' in prompt or 'graph' in prompt or 'PLOT' in prompt or 'Graph' in prompt:
            try:
                result = pandas_ai.chat(prompt)
                st.subheader("Result")
            except KeyError as e:
                st.error(f"Error: {e}. Unable to retrieve result.")
        elif prompt:
            try:
                result = pandas_ai_2.chat(prompt)
                st.subheader("Result")
                st.write(result)
            except KeyError as e:
                st.error(f"Error: {e}. Unable to retrieve result.")
        else:
            st.warning("Please enter a prompt.")

    # Add a footer
    st.write("Powered by PandasAI and Google Gemini.")