File size: 47,816 Bytes
8b8ed7d
 
 
acded99
2905dfe
3dc5893
60f8433
02342db
560aed3
 
 
83d7066
560aed3
99440dc
a792b29
8b8ed7d
dab3f5c
8b8ed7d
 
f5b856b
8b8ed7d
bf49bf4
dab3f5c
8b8ed7d
 
 
 
3dc5893
93c7bb1
945f855
75a3ee0
93c7bb1
30f2144
f8edaf4
 
65408f0
560aed3
ac6bc3f
 
 
 
 
46ec4ad
1142e29
 
 
 
ac6bc3f
 
 
 
 
 
 
 
 
945f855
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac6bc3f
 
 
 
 
 
 
 
 
 
 
 
 
945f855
 
 
 
 
 
 
 
ac6bc3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1142e29
ac6bc3f
 
 
 
1142e29
ac6bc3f
 
 
 
 
1142e29
ac6bc3f
 
 
 
 
 
 
 
945f855
ac6bc3f
 
 
 
 
 
945f855
 
 
 
 
 
ac6bc3f
 
 
87d95cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68ed8b1
87d95cd
945f855
f3a4834
 
 
 
 
560aed3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2e54bb
 
 
 
 
 
560aed3
 
 
 
 
fc7389c
 
 
 
 
560aed3
 
 
 
 
 
d2e54bb
 
 
 
 
 
560aed3
 
 
 
 
fc7389c
 
 
 
 
560aed3
 
 
 
 
 
6c2f452
d2e54bb
 
 
 
 
 
fc7389c
560aed3
 
 
 
 
fc7389c
 
 
 
 
945f855
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560aed3
 
 
 
 
 
d2e54bb
 
 
 
 
 
fc7389c
 
 
 
 
 
560aed3
fc7389c
 
 
 
 
560aed3
 
945f855
 
 
a792b29
 
6e967dd
 
 
 
 
 
 
87d95cd
68ed8b1
945f855
6e967dd
2ccffa0
6e967dd
 
 
d44d7bd
6e967dd
 
 
 
0690031
7ef7d27
6e967dd
 
 
 
 
 
 
7ef7d27
6e967dd
 
 
 
 
 
 
 
7ef7d27
6e967dd
 
 
 
 
 
 
 
7ef7d27
6e967dd
 
 
 
 
 
 
 
7ef7d27
44b5969
 
87d95cd
44b5969
b5dc2d0
a792b29
 
 
 
 
 
87d95cd
94a59c5
b5dc2d0
 
 
 
 
a792b29
 
 
 
 
 
 
 
 
 
 
 
9a99a31
44b5969
b5dc2d0
560aed3
 
 
 
 
 
d4733d8
560aed3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99440dc
945f855
 
 
99440dc
 
 
 
 
 
 
 
 
945f855
 
 
99440dc
 
 
 
 
 
 
 
 
825590c
a3da059
ac6bc3f
99440dc
ac6bc3f
99440dc
bf83601
99440dc
 
 
 
 
945f855
bf83601
99440dc
 
945f855
 
 
ac6bc3f
 
 
 
945f855
ac6bc3f
 
 
99440dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
034ddc7
99440dc
 
945f855
 
99440dc
 
 
945f855
99440dc
945f855
 
 
 
 
 
 
 
 
99440dc
 
 
945f855
 
99440dc
 
945f855
 
 
 
 
99440dc
 
 
 
 
 
 
 
 
 
945f855
 
 
 
 
 
 
 
99440dc
 
 
 
 
 
 
 
 
 
945f855
 
 
99440dc
 
 
 
 
945f855
 
99440dc
08904b5
 
 
2905dfe
 
 
 
f0830d9
2905dfe
08904b5
 
a6ab2dc
 
 
87d95cd
37f1ab4
560aed3
 
b7b0f67
2a5a9ef
80447c9
b7b0f67
 
 
2a5a9ef
7399f1e
8fd769d
c57f362
8b8ed7d
08904b5
8b8ed7d
 
719f4db
8b8ed7d
08904b5
8b8ed7d
 
719f4db
8b8ed7d
08904b5
8b8ed7d
 
719f4db
8b8ed7d
08904b5
8b8ed7d
 
719f4db
8b8ed7d
 
f48be63
8b8ed7d
560aed3
 
f5b856b
 
 
 
 
af564ea
 
b3f2d1d
 
af564ea
bcf2d35
 
 
 
 
 
b3f2d1d
bcf2d35
 
 
 
 
 
1451f69
b3f2d1d
3dc5893
7351dbd
64046ce
3a44a8f
64046ce
3a44a8f
3ddaf11
40746ea
788d24e
3ddaf11
788d24e
87469a0
b3f2d1d
93c7bb1
1d2022c
23a70c2
24e0d93
1d2022c
dc2990d
93c7bb1
def6532
 
93c7bb1
bf83601
 
def6532
54e8c41
 
945f855
 
8ae3dd5
945f855
 
 
 
54e8c41
b3f2d1d
def6532
acded99
def6532
75a3ee0
945f855
 
 
 
def6532
75a3ee0
def6532
6489a03
1c4c7b5
acded99
6146b94
70ce044
b3f2d1d
b7b0f67
 
8b8ed7d
b3f2d1d
560aed3
8a08aed
560aed3
 
 
 
a7b7df3
328282a
ce38201
9fd8cd1
 
 
c819f7e
ce38201
 
 
 
 
c819f7e
ce38201
c819f7e
ce38201
c819f7e
 
bf83601
560aed3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
945f855
560aed3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
328282a
 
 
8a08aed
 
 
 
 
 
 
 
90e9e57
a456741
7e535bc
 
eb8a83e
4ad564e
a456741
 
 
 
 
0767865
945f855
 
 
 
 
1f55b95
daf5242
 
 
 
 
 
 
 
1f55b95
daf5242
 
 
 
 
 
 
 
 
3838254
 
1410ca4
 
daf5242
 
 
 
1410ca4
daf5242
 
 
1410ca4
daf5242
 
 
 
 
 
 
 
1410ca4
daf5242
 
 
 
 
 
 
 
3fd11f7
1410ca4
 
 
3fd11f7
945f855
46b051d
1410ca4
8a08aed
 
e92c083
8a08aed
1410ca4
7e535bc
945f855
a456741
1410ca4
8a08aed
1189a30
560aed3
945f855
560aed3
cbcf3c2
dc5062c
 
 
 
80447c9
dc5062c
 
560aed3
 
2ee45f0
560aed3
 
 
 
 
dc5062c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
945f855
dc5062c
 
 
 
 
 
 
 
 
 
d5ee515
dc5062c
b7b0f67
21a8d72
dc5062c
 
80447c9
dc5062c
b037ea3
94a59c5
46e9839
 
94a59c5
39469d2
 
ce8656a
 
945f855
ce8656a
46e9839
 
 
87d95cd
 
 
 
 
 
560aed3
87d95cd
 
2aa4fdc
 
69bcace
7ef7d27
945f855
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87d95cd
560aed3
 
6fbc2db
8ae3dd5
68ed8b1
80447c9
cbcf3c2
b7b0f67
 
 
2aa4fdc
80447c9
94a59c5
b5dc2d0
560aed3
 
 
87d95cd
dc5062c
8da622a
 
 
 
 
 
 
560aed3
 
87d95cd
80447c9
560aed3
87d95cd
b7b0f67
 
 
80447c9
87d95cd
 
 
b037ea3
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
import os
import pandas as pd
import datasets
import sys
import pickle
import subprocess
import shutil
from urllib.request import urlretrieve
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder
import numpy as np
from tqdm import tqdm
import yaml
import time
import torch


_DESCRIPTION = """\
Dataset for mimic4 data, by default for the Mortality task.
Available tasks are: Mortality, Length of Stay, Readmission, Phenotype.
The data is extracted from the mimic4 database using this pipeline: 'https://github.com/healthylaife/MIMIC-IV-Data-Pipeline/tree/main'
mimic path should have this form : "path/to/mimic4data/from/username/mimiciv/2.2"
If you choose a Custom task provide a configuration file for the Time series.
"""

_HOMEPAGE = "https://huggingface.co/datasets/thbndi/Mimic4Dataset"
_CITATION = "https://proceedings.mlr.press/v193/gupta22a.html"
_URL = "https://github.com/healthylaife/MIMIC-IV-Data-Pipeline"
_DATA_GEN = 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/data_generation_icu_modify.py'
_DATA_GEN_HOSP= 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/data_generation_modify.py'
_DAY_INT= 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/day_intervals_cohort_v22.py'
_CONFIG_URLS = {'los' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/config/los.config',
                'mortality' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/config/mortality.config',
                'phenotype' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/config/phenotype.config',
                'readmission' : 'https://huggingface.co/datasets/thbndi/Mimic4Dataset/resolve/main/config/readmission.config'
        }



def check_config(task,config_file):
    with open(config_file) as f:
        config = yaml.safe_load(f)

    if task=='Phenotype':
        disease_label =  config['disease_label']
    else :
        disease_label = ""
    time = config['timePrediction']
    label = task
    timeW = config['timeWindow']
    include=int(timeW.split()[1])
    bucket = config['timebucket']
    radimp = config['radimp']
    predW = config['predW']
    disease_filter = config['disease_filter']
    icu_no_icu = config['icu_no_icu']
    groupingDiag = config['groupingDiag']

    assert( icu_no_icu in ['ICU','Non-ICU' ], "Chossen data should be one of the following: ICU, Non-ICU")
    data_icu = icu_no_icu=='ICU'
    
    if data_icu:
        chart_flag = config['chart']
        output_flag = config['output']
        select_chart = config['select_chart']
    else:
        lab_flag =config['lab']
        select_lab = config['select_lab']
        groupingMed = config['groupingMed']
        groupingProc = config['groupingProc']


    diag_flag= config['diagnosis']
    proc_flag = config['proc']
    meds_flag = config['meds']
    select_diag= config['select_diag']
    select_med= config['select_med']
    select_proc= config['select_proc']
    select_out = config['select_out']

    outlier_removal=config['outlier_removal']
    thresh=config['outlier']
    left_thresh=config['left_outlier']
    
    if data_icu:
        assert (isinstance(select_diag,bool) and isinstance(select_med,bool) and isinstance(select_proc,bool) and isinstance(select_out,bool) and isinstance(select_chart,bool), " select_diag, select_chart, select_med, select_proc, select_out should be boolean")
        assert (isinstance(chart_flag,bool) and isinstance(output_flag,bool) and isinstance(diag_flag,bool) and isinstance(proc_flag,bool) and isinstance(meds_flag,bool), "chart_flag, output_flag, diag_flag, proc_flag, meds_flag should be boolean")
    
    else:
        assert (isinstance(select_diag,bool) and isinstance(select_med,bool) and isinstance(select_proc,bool) and isinstance(select_out,bool) and isinstance(select_lab,bool), " select_diag, select_lab, select_med, select_proc, select_out should be boolean")
        assert (isinstance(lab_flag,bool) and isinstance(diag_flag,bool) and isinstance(proc_flag,bool) and isinstance(meds_flag,bool), "lab_flag, diag_flag, proc_flag, meds_flag should be boolean")
    
    if task=='Phenotype':
        if disease_label=='Heart Failure':
            label='Readmission'
            time=30
            disease_label='I50'
        elif disease_label=='CAD':
            label='Readmission'
            time=30
            disease_label='I25'
        elif disease_label=='CKD':
            label='Readmission'
            time=30
            disease_label='N18'
        elif disease_label=='COPD':
            label='Readmission'
            time=30
            disease_label='J44'
        else :
            raise ValueError('Disease label not correct provide one in the list: Heart Failure, CAD, CKD, COPD')
        predW=0
        assert (timeW[0]=='Last' and include<=72 and include>=24, "Time window should be between Last 24 and Last 72")
    
    elif task=='Mortality':
        time=0
        label= 'Mortality'
        assert (predW<=8 and predW>=2, "Prediction window should be between 2 and 8")
        assert (timeW[0]=='Fisrt' and include<=72 and include>=24, "Time window should be between First 24 and First 72")
    
    elif task=='Length of Stay':
        label= 'Length of Stay'
        assert (timeW[0]=='Fisrt' and include<=72 and include>=24, "Time window should be between Fisrt 24 and Fisrt 72")
        assert (time<=10 and time>=1, "Length of stay should be between 1 and 10")
        predW=0
    
    elif task=='Readmission':
        label= 'Readmission'
        assert (timeW[0]=='Last' and include<=72 and include>=24, "Time window should be between Last 24 and Last 72")
        assert (time<=150 and time>=10 and time%10==0, "Readmission window should be between 10 and 150 with a step of 10")
        predW=0
    
    else:
        raise ValueError('Task not correct')
    
    assert( disease_filter in ['Heart Failure','COPD','CKD','CAD',""], "Disease filter should be one of the following: Heart Failure, COPD, CKD, CAD or empty")
    assert( groupingDiag in ['Convert ICD-9 to ICD-10 and group ICD-10 codes','Keep both ICD-9 and ICD-10 codes','Convert ICD-9 to ICD-10 codes'], "Grouping ICD should be one of the following: Convert ICD-9 to ICD-10 and group ICD-10 codes, Keep both ICD-9 and ICD-10 codes, Convert ICD-9 to ICD-10 codes")
    assert (bucket<=6 and bucket>=1 and isinstance(bucket, int), "Time bucket should be between 1 and 6 and an integer")
    assert (radimp in ['No Imputation', 'forward fill and mean','forward fill and median'], "imputation should be one of the following: No Imputation, forward fill and mean, forward fill and median")
    if chart_flag:
        assert (left_thresh>=0 and left_thresh<=10 and isinstance(left_thresh, int), "Left outlier threshold should be between 0 and 10 and an integer")
        assert (thresh>=90 and thresh<=99 and isinstance(thresh, int), "Outlier threshold should be between 90 and 99 and an integer")
        assert (outlier_removal in ['No outlier detection','Impute Outlier (default:98)','Remove outliers (default:98)'], "Outlier removal should be one of the following: No outlier detection, Impute Outlier (default:98), Remove outliers (default:98)")
    if lab_flag:
        assert (left_thresh>=0 and left_thresh<=10 and isinstance(left_thresh, int), "Left outlier threshold should be between 0 and 10 and an integer")
        assert (thresh>=90 and thresh<=99 and isinstance(thresh, int), "Outlier threshold should be between 90 and 99 and an integer")
        assert (outlier_removal in ['No outlier detection','Impute Outlier (default:98)','Remove outliers (default:98)'], "Outlier removal should be one of the following: No outlier detection, Impute Outlier (default:98), Remove outliers (default:98)")
        assert (groupingProc in ['ICD-9 and ICD-10','ICD-10'], "Grouping procedure should be one of the following: ICD-9 and ICD-10, ICD-10")
        assert (groupingMed in ['Yes','No'], "Do you want to group Medication codes to use Non propietary names? : Grouping medication should be one of the following: Yes, No")

    return label, time, disease_label, predW

def create_vocab(file,task):
    with open ('./data/dict/'+task+'/'+file, 'rb') as fp:
        condVocab = pickle.load(fp)
    condVocabDict={}
    condVocabDict[0]=0
    for val in range(len(condVocab)):
        condVocabDict[condVocab[val]]= val+1    

    return condVocabDict

def gender_vocab():
    genderVocabDict={}
    genderVocabDict['<PAD>']=0
    genderVocabDict['M']=1
    genderVocabDict['F']=2

    return genderVocabDict

def vocab(task,diag_flag,proc_flag,out_flag,chart_flag,med_flag,lab_flag):
        condVocabDict={}
        procVocabDict={}
        medVocabDict={}
        outVocabDict={}
        chartVocabDict={}
        labVocabDict={}
        ethVocabDict={}
        ageVocabDict={}
        genderVocabDict={}
        insVocabDict={}
        
        ethVocabDict=create_vocab('ethVocab',task)
        with open('./data/dict/'+task+'/ethVocabDict', 'wb') as fp:
            pickle.dump(ethVocabDict, fp)
            
        ageVocabDict=create_vocab('ageVocab',task)
        with open('./data/dict/'+task+'/ageVocabDict', 'wb') as fp:
            pickle.dump(ageVocabDict, fp)
        
        genderVocabDict=gender_vocab()
        with open('./data/dict/'+task+'/genderVocabDict', 'wb') as fp:
            pickle.dump(genderVocabDict, fp)
            
        insVocabDict=create_vocab('insVocab',task)
        with open('./data/dict/'+task+'/insVocabDict', 'wb') as fp:
            pickle.dump(insVocabDict, fp)
        
        if diag_flag:
            file='condVocab'
            with open ('./data/dict/'+task+'/'+file, 'rb') as fp:
                condVocabDict = pickle.load(fp)
        if proc_flag:
            file='procVocab'
            with open ('./data/dict/'+task+'/'+file, 'rb') as fp:
                procVocabDict = pickle.load(fp)
        if med_flag:
            file='medVocab'
            with open ('./data/dict/'+task+'/'+file, 'rb') as fp:
                medVocabDict = pickle.load(fp)
        if out_flag:
            file='outVocab'
            with open ('./data/dict/'+task+'/'+file, 'rb') as fp:
                outVocabDict = pickle.load(fp)
        if chart_flag:
            file='chartVocab'
            with open ('./data/dict/'+task+'/'+file, 'rb') as fp:
                chartVocabDict = pickle.load(fp)
        if lab_flag:
            file='labsVocab'
            with open ('./data/dict/'+task+'/'+file, 'rb') as fp:
                labVocabDict = pickle.load(fp)
        
        return len(condVocabDict),len(procVocabDict),len(medVocabDict),len(outVocabDict),len(chartVocabDict),len(labVocabDict),ethVocabDict,genderVocabDict,ageVocabDict,insVocabDict

def concat_data(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat_lab):
    meds=data['Med']
    proc = data['Proc']
    out = data['Out']
    chart = data['Chart']
    cond= data['Cond']['fids']

    cond_df=pd.DataFrame()
    proc_df=pd.DataFrame()
    out_df=pd.DataFrame()
    chart_df=pd.DataFrame()
    meds_df=pd.DataFrame()

    #demographic
    demo=pd.DataFrame(columns=['Age','gender','ethnicity','label','insurance'])
    new_row = {'Age': data['age'], 'gender': data['gender'], 'ethnicity': data['ethnicity'], 'label': data['label'], 'insurance': data['insurance']}
    demo = demo.append(new_row, ignore_index=True)

    ##########COND#########
    if (feat_cond):
        #get all conds
        with open("./data/dict/"+task+"/condVocab", 'rb') as fp:
            conDict = pickle.load(fp)
        conds=pd.DataFrame(conDict,columns=['COND'])
        features=pd.DataFrame(np.zeros([1,len(conds)]),columns=conds['COND'])

        #onehot encode
        if(cond ==[]):
            cond_df=pd.DataFrame(np.zeros([1,len(features)]),columns=features['COND'])
            cond_df=cond_df.fillna(0)
        else:
            cond_df=pd.DataFrame(cond,columns=['COND'])
            cond_df['val']=1
            cond_df=(cond_df.drop_duplicates()).pivot(columns='COND',values='val').reset_index(drop=True)
            cond_df=cond_df.fillna(0)
            oneh = cond_df.sum().to_frame().T
            combined_df = pd.concat([features,oneh],ignore_index=True).fillna(0)
            combined_oneh=combined_df.sum().to_frame().T
            cond_df=combined_oneh

    ##########PROC#########
    if (feat_proc):
        with open("./data/dict/"+task+"/procVocab", 'rb') as fp:
            procDic = pickle.load(fp)

        if proc :
            feat=proc.keys()
            proc_val=[proc[key] for key in feat]
            procedures=pd.DataFrame(procDic,columns=['PROC'])
            features=pd.DataFrame(np.zeros([1,len(procedures)]),columns=procedures['PROC'])
            features.columns=pd.MultiIndex.from_product([["PROC"], features.columns])
            procs=pd.DataFrame(columns=feat)
            for p,v in zip(feat,proc_val):
                procs[p]=v
            procs.columns=pd.MultiIndex.from_product([["PROC"], procs.columns])
            proc_df = pd.concat([features,procs],ignore_index=True).fillna(0)
        else:
            procedures=pd.DataFrame(procDic,columns=['PROC'])
            features=pd.DataFrame(np.zeros([1,len(procedures)]),columns=procedures['PROC'])
            features.columns=pd.MultiIndex.from_product([["PROC"], features.columns])
            proc_df=features.fillna(0)

    ##########OUT#########
    if (feat_out):
        with open("./data/dict/"+task+"/outVocab", 'rb') as fp:
            outDic = pickle.load(fp)

        if out :
            feat=out.keys()
            out_val=[out[key] for key in feat]
            outputs=pd.DataFrame(outDic,columns=['OUT'])
            features=pd.DataFrame(np.zeros([1,len(outputs)]),columns=outputs['OUT'])
            features.columns=pd.MultiIndex.from_product([["OUT"], features.columns])
            outs=pd.DataFrame(columns=feat)
            for o,v in zip(feat,out_val):
                outs[o]=v
            outs.columns=pd.MultiIndex.from_product([["OUT"], outs.columns])
            out_df = pd.concat([features,outs],ignore_index=True).fillna(0)
        else:
            outputs=pd.DataFrame(outDic,columns=['OUT'])
            features=pd.DataFrame(np.zeros([1,len(outputs)]),columns=outputs['OUT'])
            features.columns=pd.MultiIndex.from_product([["OUT"], features.columns])
            out_df=features.fillna(0)

    ##########CHART#########
    if (feat_chart):
        with open("./data/dict/"+task+"/chartVocab", 'rb') as fp:
            chartDic = pickle.load(fp)

        if chart:
            charts=chart['val']
            feat=charts.keys()
            chart_val=[charts[key] for key in feat]
            charts=pd.DataFrame(chartDic,columns=['CHART'])
            features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['CHART'])
            features.columns=pd.MultiIndex.from_product([["CHART"], features.columns])
            
            chart=pd.DataFrame(columns=feat)
            for c,v in zip(feat,chart_val):
                chart[c]=v
            chart.columns=pd.MultiIndex.from_product([["CHART"], chart.columns])
            chart_df = pd.concat([features,chart],ignore_index=True).fillna(0)
        else:
            charts=pd.DataFrame(chartDic,columns=['CHART'])
            features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['CHART'])
            features.columns=pd.MultiIndex.from_product([["CHART"], features.columns])
            chart_df=features.fillna(0)
        
        ##########LAB#########
    if (feat_lab):
        with open("./data/dict/"+task+"/labsVocab", 'rb') as fp:
            chartDic = pickle.load(fp)

        if chart:
            charts=chart['val']
            feat=charts.keys()
            chart_val=[charts[key] for key in feat]
            charts=pd.DataFrame(chartDic,columns=['LAB'])
            features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['LAB'])
            features.columns=pd.MultiIndex.from_product([["LAB"], features.columns])
            
            chart=pd.DataFrame(columns=feat)
            for c,v in zip(feat,chart_val):
                chart[c]=v
            chart.columns=pd.MultiIndex.from_product([["LAB"], chart.columns])
            chart_df = pd.concat([features,chart],ignore_index=True).fillna(0)
        else:
            charts=pd.DataFrame(chartDic,columns=['LAB'])
            features=pd.DataFrame(np.zeros([1,len(charts)]),columns=charts['LAB'])
            features.columns=pd.MultiIndex.from_product([["LAB"], features.columns])
            chart_df=features.fillna(0)
    
    ###MEDS
    if (feat_meds):
        with open("./data/dict/"+task+"/medVocab", 'rb') as fp:
                medDic = pickle.load(fp)

        if meds:
            feat=meds['signal'].keys()
            med_val=[meds['amount'][key] for key in feat]
            meds=pd.DataFrame(medDic,columns=['MEDS'])
            features=pd.DataFrame(np.zeros([1,len(meds)]),columns=meds['MEDS'])
            features.columns=pd.MultiIndex.from_product([["MEDS"], features.columns])
            
            med=pd.DataFrame(columns=feat)
            for m,v in zip(feat,med_val):
                med[m]=v
            med.columns=pd.MultiIndex.from_product([["MEDS"], med.columns])
            meds_df = pd.concat([features,med],ignore_index=True).fillna(0)
        else:
            meds=pd.DataFrame(medDic,columns=['MEDS'])
            features=pd.DataFrame(np.zeros([1,len(meds)]),columns=meds['MEDS'])
            features.columns=pd.MultiIndex.from_product([["MEDS"], features.columns])
            meds_df=features.fillna(0)

    dyn_df = pd.concat([meds_df,proc_df,out_df,chart_df], axis=1)
    return dyn_df,cond_df,demo


def getXY_deep(data,task,feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat_lab):
    stat_df = torch.zeros(size=(1,0))
    demo_df = torch.zeros(size=(1,0))
    meds = torch.zeros(size=(0,0))
    charts = torch.zeros(size=(0,0))
    proc = torch.zeros(size=(0,0))
    out = torch.zeros(size=(0,0))
    lab = torch.zeros(size=(0,0))
    stat_df = torch.zeros(size=(1,0))
    demo_df = torch.zeros(size=(1,0))
    
    size_cond, size_proc, size_meds, size_out, size_chart, size_lab, eth_vocab,gender_vocab,age_vocab,ins_vocab=vocab(task.replace(" ","_"),feat_cond,feat_proc,feat_out,feat_chart,feat_meds,False)
    dyn,cond_df,demo=concat_data(data,task.replace(" ","_"),feat_cond,feat_proc,feat_out,feat_chart,feat_meds,feat_lab)

    ###########""
    if feat_chart:
        charts = dyn['CHART']
        charts=charts.to_numpy()
        
        charts = torch.tensor(charts)
        charts = charts.unsqueeze(0)
        charts = torch.tensor(charts)
        charts = charts.type(torch.LongTensor)
        charts=charts.view(charts.shape[1],charts.shape[2])
        
    if feat_meds:
        meds = dyn['MEDS']
        meds=meds.to_numpy()
        meds = torch.tensor(meds)
        meds = meds.unsqueeze(0)
        meds = torch.tensor(meds)
        meds = meds.type(torch.LongTensor)
        meds=meds.view(meds.shape[1],meds.shape[2])

    if feat_proc:
        proc = dyn['PROC']
        proc=proc.to_numpy()
        proc = torch.tensor(proc)
        proc = proc.unsqueeze(0)
        proc = torch.tensor(proc)
        proc = proc.type(torch.LongTensor)
        proc=proc.view(proc.shape[1],proc.shape[2])

    if feat_out:
        out = dyn['OUT']
        out=out.to_numpy()
        out = torch.tensor(out)
        out = out.unsqueeze(0)
        out = torch.tensor(out)
        out = out.type(torch.LongTensor)
        out=out.view(out.shape[1],out.shape[2])

    if feat_lab:
        lab = dyn['LAB']
        lab=lab.to_numpy()
        lab = torch.tensor(lab)
        lab = lab.unsqueeze(0)
        lab = torch.tensor(lab)
        lab = lab.type(torch.LongTensor)
        lab=lab.view(lab.shape[1],lab.shape[2])

####################""
    

    stat=cond_df
    stat = stat.to_numpy()
    stat = torch.tensor(stat)
    if stat_df[0].nelement():
        stat_df = torch.cat((stat_df,stat),0)
    else:
        stat_df = stat 

    y = int(demo['label'])
    demo["gender"].replace(gender_vocab, inplace=True)
    demo["ethnicity"].replace(eth_vocab, inplace=True)
    demo["insurance"].replace(ins_vocab, inplace=True)
    demo["Age"].replace(age_vocab, inplace=True)
    demo=demo[["gender","ethnicity","insurance","Age"]]
    demo = demo.values
    demo = torch.tensor(demo)
    if demo_df[0].nelement():
        demo_df = torch.cat((demo_df,demo),0)
    else:
        demo_df = demo
    stat_df = torch.tensor(stat_df)
    stat_df = stat_df.type(torch.LongTensor)
    demo_df = torch.tensor(demo_df)
    demo_df = demo_df.type(torch.LongTensor)
    y_df = torch.tensor(y)
    y_df = y_df.type(torch.LongTensor)

    return stat_df, demo_df, meds, charts, out, proc, lab, y_df

def getXY(dyn,stat,demo,concat_cols,concat):
    X_df=pd.DataFrame()
    if concat:
        dyna=dyn.copy()
        dyna.columns=dyna.columns.droplevel(0)
        dyna=dyna.to_numpy()
        dyna=np.nan_to_num(dyna, copy=False)
        dyna=dyna.reshape(1,-1)
        dyn_df=pd.DataFrame(data=dyna,columns=concat_cols)
    else:
        dyn_df=pd.DataFrame()
        for key in dyn.columns.levels[0]:     
            dyn_temp=dyn[key]
            if ((key=="CHART") or (key=="MEDS")):
                agg=dyn_temp.aggregate("mean")
                agg=agg.reset_index()
            else:
                agg=dyn_temp.aggregate("max")
                agg=agg.reset_index()

            if dyn_df.empty:
                dyn_df=agg
            else:
                dyn_df=pd.concat([dyn_df,agg],axis=0)
        dyn_df=dyn_df.T
        dyn_df.columns = dyn_df.iloc[0]
        dyn_df=dyn_df.iloc[1:,:]
        
    X_df=pd.concat([dyn_df,stat],axis=1)
    X_df=pd.concat([X_df,demo],axis=1)
    return X_df    






def task_cohort(task, mimic_path, config_path):
    sys.path.append('./preprocessing/day_intervals_preproc')
    sys.path.append('./utils')
    sys.path.append('./preprocessing/hosp_module_preproc')
    sys.path.append('./model')
    import day_intervals_cohort_v22
    import day_intervals_cohort
    import feature_selection_icu
    import data_generation_icu_modify
    import data_generation_modify
    import feature_selection_hosp

    
    root_dir = os.path.dirname(os.path.abspath('UserInterface.ipynb'))
    config_path='./config/'+config_path
    with open(config_path) as f:
        config = yaml.safe_load(f)
    version_path = mimic_path+'/'
    version = mimic_path.split('/')[-1][0]
    start = time.time()
    #----------------------------------------------config----------------------------------------------------
    label, tim, disease_label, predW = check_config(task,config_path)

    timeW = config['timeWindow']
    include=int(timeW.split()[1])
    bucket = config['timebucket']
    radimp = config['radimp']

    diag_flag = config['diagnosis']
    out_flag = config['output']
    chart_flag = config['chart']
    proc_flag= config['proc']
    med_flag = config['meds']
    lab_flag = config['lab']

    disease_filter = config['disease_filter']
    icu_no_icu = config['icu_no_icu']
    groupingDiag = config['groupingDiag']
    groupingMed = config['groupingMed']
    groupingProc = config['groupingProc']

    select_diag= config['select_diag']
    select_med= config['select_med']
    select_proc=  config['select_proc']
    select_lab= config['select_lab']
    select_out= config['select_out']
    select_chart=  config['select_chart']

    # -------------------------------------------------------------------------------------------------------------

    data_icu=icu_no_icu=="ICU"
    data_mort=label=="Mortality"
    data_admn=label=='Readmission'
    data_los=label=='Length of Stay'

    if (disease_filter=="Heart Failure"):
        icd_code='I50'
    elif (disease_filter=="CKD"):
        icd_code='N18'
    elif (disease_filter=="COPD"):
        icd_code='J44'
    elif (disease_filter=="CAD"):
        icd_code='I25'
    else:
        icd_code='No Disease Filter'

    #-----------------------------------------------EXTRACT MIMIC-----------------------------------------------------
    if version == '2':
        cohort_output = day_intervals_cohort_v22.extract_data(icu_no_icu,label,tim,icd_code, root_dir,version_path,disease_label)

    elif version == '1':
        cohort_output = day_intervals_cohort.extract_data(icu_no_icu,label,tim,icd_code, root_dir,version_path,disease_label)
    #----------------------------------------------FEATURES-------------------------------------------------------
    print(data_icu)
    if data_icu :
        feature_selection_icu.feature_icu(cohort_output, version_path,diag_flag,out_flag,chart_flag,proc_flag,med_flag)
    else:
        feature_selection_hosp.feature_nonicu(cohort_output, version_path,diag_flag,lab_flag,proc_flag,med_flag)
    #----------------------------------------------GROUPING-------------------------------------------------------
    if data_icu:
        if diag_flag:
            group_diag=groupingDiag
        feature_selection_icu.preprocess_features_icu(cohort_output, diag_flag, group_diag,False,False,False,0,0)
    
    else:
        if diag_flag:
            group_diag=groupingDiag
        if med_flag:
            group_med=groupingMed
        if proc_flag:
            group_proc=groupingProc
        feature_selection_hosp.preprocess_features_hosp(cohort_output, diag_flag,proc_flag,med_flag,False,group_diag,group_med,group_proc,False,False,0,0)
    #----------------------------------------------SUMMARY-------------------------------------------------------
    if data_icu:
        feature_selection_icu.generate_summary_icu(diag_flag,proc_flag,med_flag,out_flag,chart_flag)
    else:
        feature_selection_hosp.generate_summary_hosp(diag_flag,proc_flag,med_flag,lab_flag)
    #----------------------------------------------FEATURE SELECTION---------------------------------------------

    if data_icu:
        feature_selection_icu.features_selection_icu(cohort_output, diag_flag,proc_flag,med_flag,out_flag, chart_flag,select_diag,select_med,select_proc,select_out,select_chart)
    else:
        feature_selection_hosp.features_selection_hosp(cohort_output, diag_flag,proc_flag,med_flag,lab_flag,select_diag,select_med,select_proc,select_lab)

    #---------------------------------------CLEANING OF FEATURES-----------------------------------------------
    thresh=0
    if data_icu:
        if chart_flag:
            outlier_removal=config['outlier_removal']
            clean_chart=outlier_removal!='No outlier detection'
            impute_outlier_chart=outlier_removal=='Impute Outlier (default:98)'
            thresh=config['outlier']
            left_thresh=config['left_outlier']
        feature_selection_icu.preprocess_features_icu(cohort_output, False, False,chart_flag,clean_chart,impute_outlier_chart,thresh,left_thresh)
    else:
        if lab_flag:
            outlier_removal=config['outlier_removal']
            clean_chart=outlier_removal!='No outlier detection'
            impute_outlier_chart=outlier_removal=='Impute Outlier (default:98)'
            thresh=config['outlier']
            left_thresh=config['left_outlier']
        feature_selection_hosp.preprocess_features_hosp(cohort_output, False,False, False,lab_flag,False,False,False,clean_chart,impute_outlier_chart,thresh,left_thresh)
    # ---------------------------------------tim-Series Representation--------------------------------------------
    if radimp == 'forward fill and mean' :
        impute='Mean'
    elif radimp =='forward fill and median':
        impute = 'Median'
    else :
        impute = False
    
    if data_icu:
        gen=data_generation_icu_modify.Generator(task,cohort_output,data_mort,data_admn,data_los,diag_flag,proc_flag,out_flag,chart_flag,med_flag,impute,include,bucket,predW)
    else:
        gen=data_generation_modify.Generator(cohort_output,data_mort,data_admn,data_los,diag_flag,lab_flag,proc_flag,med_flag,impute,include,bucket,predW)
    
    end = time.time()
    print("Time elapsed : ", round((end - start)/60,2),"mins")
    print("[============TASK COHORT SUCCESSFULLY CREATED============]")




 #############################################DATASET####################################################################   
class Mimic4DatasetConfig(datasets.BuilderConfig):
    """BuilderConfig for Mimic4Dataset."""

    def __init__(
        self,
        **kwargs,
    ):
        super().__init__(**kwargs)
        
class Mimic4Dataset(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")

    def __init__(self, **kwargs):
        self.mimic_path = kwargs.pop("mimic_path", None)
        self.encoding = kwargs.pop("encoding",'raw')
        self.config_path = kwargs.pop("config_path",None)
        self.test_size = kwargs.pop("test_size",0.2)
        self.val_size = kwargs.pop("val_size",0.1)
        self.generate_cohort = kwargs.pop("generate_cohort",True)

        if self.encoding == 'concat':
            self.concat = True
        else:
            self.concat = False
            
        super().__init__(**kwargs)
        
        
    BUILDER_CONFIGS = [
        Mimic4DatasetConfig(
            name="Phenotype",
            version=VERSION,
            description="Dataset for mimic4 Phenotype task"
        ),
        Mimic4DatasetConfig(
            name="Readmission",
            version=VERSION,
            description="Dataset for mimic4 Readmission task"
        ),
        Mimic4DatasetConfig(
            name="Length of Stay",
            version=VERSION,
            description="Dataset for mimic4 Length of Stay task"
        ),
        Mimic4DatasetConfig(
            name="Mortality",
            version=VERSION,
            description="Dataset for mimic4 Mortality task"
        ),
    ]
    
    DEFAULT_CONFIG_NAME = "Mortality"
        
    def create_cohort(self):
        if self.config.name==None:
            if self.config.name == 'Phenotype' : self.config_path = _CONFIG_URLS['phenotype'] 
            if self.config.name == 'Readmission' : self.config_path = _CONFIG_URLS['readmission'] 
            if self.config.name == 'Length of Stay' : self.config_path = _CONFIG_URLS['los'] 
            if self.config.name == 'Mortality' : self.config_path = _CONFIG_URLS['mortality']
        
        version = self.mimic_path.split('/')[-1]
        mimic_folder= self.mimic_path.split('/')[-2]
        mimic_complete_path='/'+mimic_folder+'/'+version
        
        current_directory = os.getcwd()
        if os.path.exists(os.path.dirname(current_directory)+'/MIMIC-IV-Data-Pipeline-main'):
            dir =os.path.dirname(current_directory) 
            os.chdir(dir)
        else:
            #move to parent directory of mimic data
            dir = self.mimic_path.replace(mimic_complete_path,'')
            if dir[-1]!='/':
                dir=dir+'/'
            elif dir=='':
                dir="./"
            parent_dir = os.path.dirname(self.mimic_path)
            os.chdir(parent_dir)

        #####################clone git repo if doesnt exists
        repo_url='https://github.com/healthylaife/MIMIC-IV-Data-Pipeline'
        if os.path.exists('MIMIC-IV-Data-Pipeline-main'):
            path_bench = './MIMIC-IV-Data-Pipeline-main'
        else:
            path_bench ='./MIMIC-IV-Data-Pipeline-main'
            subprocess.run(["git", "clone", repo_url, path_bench])
            os.makedirs(path_bench+'/mimic-iv')
            shutil.move(version,path_bench+'/mimic-iv')

        os.chdir(path_bench)
        self.mimic_path = './mimic-iv/'+version

        ####################Get configurations param
        #download config file if not custom
        if self.config_path[0:4] == 'http':
            c = self.config_path.split('/')[-1]
            file_path, head = urlretrieve(self.config_path,c)
        else :
            file_path = self.config_path

        if not os.path.exists('./config'):
            os.makedirs('config')
        #save config file in config folder
        self.conf='./config/'+file_path.split('/')[-1]
        if not os.path.exists(self.conf):
            shutil.move(file_path,'./config')
        with open(self.conf) as f:
            config = yaml.safe_load(f)

        self.data_icu = config['icu_no_icu']=='ICU'
        print(self.data_icu)
        if self.data_icu:
            self.feat_cond, self.feat_chart, self.feat_proc, self.feat_meds, self.feat_out, self.lab = config['diagnosis'], config['chart'], config['proc'],  config['meds'], config['output'], False
        else:
            self.feat_cond, self.feat_lab, self.feat_proc, self.feat_meds, self.feat_chart, self.out = config['diagnosis'], config['lab'], config['proc'],  config['meds'], False, False

        #####################downloads modules from hub
        if not os.path.exists('./model/data_generation_icu_modify.py'):
            file_path, head = urlretrieve(_DATA_GEN, "data_generation_icu_modify.py")
            shutil.move(file_path, './model')

        if not os.path.exists('./model/data_generation_modify.py'):
            file_path, head = urlretrieve(_DATA_GEN_HOSP, "data_generation_modify.py")
            shutil.move(file_path, './model')

        if not os.path.exists('./preprocessing/day_intervals_preproc/day_intervals_cohort_v22.py'):
            file_path, head = urlretrieve(_DAY_INT, "day_intervals_cohort_v22.py")
            shutil.move(file_path, './preprocessing/day_intervals_preproc')
            
        data_dir = "./data/dict/"+self.config.name.replace(" ","_")+"/dataDic"
        sys.path.append(path_bench)
        config = self.config_path.split('/')[-1]

        #####################create task cohort
        if self.generate_cohort:
            task_cohort(self.config.name.replace(" ","_"),self.mimic_path,config)

        #####################Split data into train, test and val
        with open(data_dir, 'rb') as fp:
            dataDic = pickle.load(fp)
        data = pd.DataFrame.from_dict(dataDic)
       
        data=data.T
        train_data, test_data = train_test_split(data, test_size=self.test_size, random_state=42)
        train_data, val_data = train_test_split(train_data, test_size=self.val_size, random_state=42)
        
        dict_dir = "./data/dict/"+self.config.name.replace(" ","_")
        train_dic = train_data.to_dict('index')
        test_dic = test_data.to_dict('index')
        val_dic = val_data.to_dict('index')

        train_path = dict_dir+'/train_data.pkl'
        test_path = dict_dir+'/test_data.pkl'
        val_path = dict_dir+'/val_data.pkl'
        
        with open(train_path, 'wb') as f:
            pickle.dump(train_dic, f)
        with open(val_path, 'wb') as f:
            pickle.dump(val_dic, f)
        with open(test_path, 'wb') as f:
            pickle.dump(test_dic, f)

        return dict_dir
  
###########################################################RAW##################################################################

    def _info_raw(self):
        features = datasets.Features(
            {
                "label": datasets.ClassLabel(num_classes=2,names=["0", "1"]),
                "gender": datasets.Value("string"),
                "ethnicity": datasets.Value("string"),
                "insurance": datasets.Value("string"),
                "age": datasets.Value("int32"),
                "COND": datasets.Sequence(datasets.Value("string")),
                "MEDS": {
                            "signal": 
                                {
                                    "id": datasets.Sequence(datasets.Value("int32")),
                                    "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
                                }
                            ,
                            "rate": 
                                {
                                    "id": datasets.Sequence(datasets.Value("int32")),
                                    "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
                                }
                            ,
                            "amount": 
                                {
                                    "id": datasets.Sequence(datasets.Value("int32")),
                                    "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
                                }
                            
                        },
                "PROC":  {
                            "id": datasets.Sequence(datasets.Value("int32")),
                            "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
                                },
                "CHART/LAB":
                    {
                        "signal" : {
                            "id": datasets.Sequence(datasets.Value("int32")),
                            "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
                                },
                        "val" : {
                            "id": datasets.Sequence(datasets.Value("int32")),
                            "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
                                },
                    },
                "OUT":  {
                            "id": datasets.Sequence(datasets.Value("int32")),
                            "value": datasets.Sequence(datasets.Sequence(datasets.Value("float32")))
                                },
                
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _generate_examples_raw(self, filepath):
        with open(filepath, 'rb') as fp:
            dataDic = pickle.load(fp)
        for hid, data in dataDic.items():
            proc_features = data['Proc']
            meds_features = data['Med']
            out_features = data['Out']
            cond_features = data['Cond']['fids']
            eth= data['ethnicity']
            age = data['age']
            gender = data['gender']
            label = data['label']
            insurance=data['insurance']
            
            items = list(proc_features.keys())
            values =[proc_features[i] for i in items ]
            procs = {"id" : items,
                  "value": values}
            
            items_outs = list(out_features.keys())
            values_outs =[out_features[i] for i in items_outs ]
            outs = {"id" : items_outs,
                  "value": values_outs}

            if self.data_icu:
                chart_features = data['Chart']
            else:
                chart_features = data['Lab']

            #chart signal
            if ('signal' in chart_features):
                items_chart_sig = list(chart_features['signal'].keys())
                values_chart_sig =[chart_features['signal'][i] for i in items_chart_sig ]
                chart_sig = {"id" : items_chart_sig,
                        "value": values_chart_sig}
            else:
                chart_sig = {"id" : [],
                        "value": []}
            #chart val
            if ('val' in chart_features):
                items_chart_val = list(chart_features['val'].keys())
                values_chart_val =[chart_features['val'][i] for i in items_chart_val ]
                chart_val = {"id" : items_chart_val,
                        "value": values_chart_val}
            else:
                chart_val = {"id" : [],
                        "value": []}
                
            charts = {"signal" : chart_sig,
                    "val" : chart_val}

            #meds signal
            if ('signal' in meds_features):
                items_meds_sig = list(meds_features['signal'].keys())
                values_meds_sig =[meds_features['signal'][i] for i in items_meds_sig ]
                meds_sig = {"id" : items_meds_sig,
                    "value": values_meds_sig}
            else:
                meds_sig = {"id" : [],
                    "value": []}
            #meds rate
            if ('rate' in meds_features):
                items_meds_rate = list(meds_features['rate'].keys())
                values_meds_rate =[meds_features['rate'][i] for i in items_meds_rate ]
                meds_rate = {"id" : items_meds_rate,
                        "value": values_meds_rate}
            else:
                meds_rate = {"id" : [],
                        "value": []}
            #meds amount
            if ('amount' in meds_features):
                items_meds_amount = list(meds_features['amount'].keys())
                values_meds_amount =[meds_features['amount'][i] for i in items_meds_amount ]
                meds_amount = {"id" : items_meds_amount,
                        "value": values_meds_amount}
            else:
                meds_amount = {"id" : [],
                        "value": []}
            
            meds = {"signal" : meds_sig,
                    "rate" : meds_rate,
                    "amount" : meds_amount}
            

            yield int(hid), {
                "label" : label,
                "gender" : gender,
                "ethnicity" : eth,
                "insurance" : insurance,
                "age" : age,
                "COND" : cond_features,
                "PROC" : procs,
                "CHART/LAB" : charts,
                "OUT" : outs,
                "MEDS" : meds
            }



###########################################################ENCODED##################################################################
        
    def _info_encoded(self):
        features = datasets.Features(
            {
                "label": datasets.ClassLabel(num_classes=2,names=["0", "1"]),
                "features" : datasets.Sequence(datasets.Value("float32")),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    def _generate_examples_encoded(self, filepath):
        path= './data/dict/'+self.config.name.replace(" ","_")+'/ethVocab'
        with open(path, 'rb') as fp:
            ethVocab = pickle.load(fp)

        path= './data/dict/'+self.config.name.replace(" ","_")+'/insVocab'
        with open(path, 'rb') as fp:
            insVocab = pickle.load(fp)

        genVocab = ['<PAD>', 'M', 'F']
        gen_encoder = LabelEncoder()
        eth_encoder = LabelEncoder()
        ins_encoder = LabelEncoder()
        gen_encoder.fit(genVocab)
        eth_encoder.fit(ethVocab)
        ins_encoder.fit(insVocab)
        with open(filepath, 'rb') as fp:
            dico = pickle.load(fp)

        df = pd.DataFrame.from_dict(dico, orient='index')
        task=self.config.name.replace(" ","_")
        
        for i, data in df.iterrows():
            concat_cols=[]
            dyn_df,cond_df,demo=concat_data(data,task,self.feat_cond,self.feat_proc,self.feat_out, self.feat_chart, self.feat_meds,self.feat_lab)
            dyn=dyn_df.copy()
            dyn.columns=dyn.columns.droplevel(0)
            cols=dyn.columns
            time=dyn.shape[0]
            for t in range(time):
                cols_t = [str(x) + "_"+str(t) for x in cols]
                concat_cols.extend(cols_t)
            demo['gender']=gen_encoder.transform(demo['gender'])
            demo['ethnicity']=eth_encoder.transform(demo['ethnicity'])
            demo['insurance']=ins_encoder.transform(demo['insurance'])
            label = data['label']
            demo=demo.drop(['label'],axis=1)
            X= getXY(dyn_df,cond_df,demo,concat_cols,self.concat)
            X=X.values.tolist()[0]
            yield int(i), {
                "label": label,
                "features": X,
            }
######################################################DEEP###############################################################
    def _info_deep(self):
        features = datasets.Features(
            {
                "label": datasets.ClassLabel(num_classes=2,names=["0", "1"]),
                "DEMO": datasets.Array2D(shape=(None, 4), dtype="int64"),
                "COND" : datasets.Array2D(shape=(None, 1025), dtype='int64') ,
                "MEDS" : datasets.Array2D(shape=(None, self.size_meds), dtype='int64') ,
                "PROC" : datasets.Array2D(shape=(None, self.size_proc), dtype='int64') ,
                "CHART/LAB" : datasets.Array2D(shape=(None, self.size_chart), dtype='int64') ,
                "OUT" : datasets.Array2D(shape=(None, self.size_out), dtype='int64') ,
                
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            citation=_CITATION,
        )

    
    def _generate_examples_deep(self, filepath):
        with open(filepath, 'rb') as fp:
            dico = pickle.load(fp)
        task=self.config.name.replace(" ","_")
        for key, data in dico.items():   
            stat, demo, meds, chart, out, proc, lab, y = getXY_deep(data, task, self.feat_cond,  self.feat_proc,  self.feat_out,  self.feat_chart, self.feat_meds,self.feat_lab)

            if self.data_icu:
                yield int(key), {
                    'label': y,
                    'DEMO': demo,
                    'COND': stat,
                    'MEDS': meds,
                    'PROC': proc,
                    'CHART/LAB': chart,
                    'OUT': out,
                    }
            else:
                yield int(key), {
                    'label': y,
                    'DEMO': demo,
                    'COND': stat,
                    'MEDS': meds,
                    'PROC': proc,
                    'CHART/LAB': lab,
                    'OUT': out,
                    }
            
    
#############################################################################################################################
    def _info(self):
        self.path = self.create_cohort()
        self.size_cond, self.size_proc, self.size_meds, self.size_out, self.size_chart, self.size_lab, eth_vocab,gender_vocab,age_vocab,ins_vocab=vocab(self.config.name.replace(" ","_"),self.feat_cond,self.feat_proc,self.feat_out,self.feat_chart,self.feat_meds,self.feat_lab)
    
        if self.encoding == 'concat' :
            return self._info_encoded()

        elif self.encoding == 'aggreg' :
            return self._info_encoded()
        
        elif self.encoding == 'tensor' :
            return self._info_deep()
        
        else:
            return self._info_raw()


    def _split_generators(self, dl_manager):
        csv_dir = "./data/dict/"+self.config.name.replace(" ","_")

        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": csv_dir+'/train_data.pkl'}),
            datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"filepath": csv_dir+'/val_data.pkl'}),
            datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": csv_dir+'/test_data.pkl'}),
        ]

    def _generate_examples(self, filepath):
        
        if self.encoding == 'concat' :
            yield from self._generate_examples_encoded(filepath)
        
        elif self.encoding == 'aggreg' :
            yield from self._generate_examples_encoded(filepath)
        
        elif self.encoding == 'tensor' :
            yield from self._generate_examples_deep(filepath)
        else :
            yield from self._generate_examples_raw(filepath)