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
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
import pandas as pd
import matplotlib.pyplot as plt
import gradio as gr
import numpy as np

# ----------------------------------------------------------------------------------------------------------------------------
# Helpful function
# ----------------------------------------------------------------------------------------------------------------------------
# Set up the Files
def SetUpFile(flag):
    # print(flag)
    file =0
    if flag:
        file= pd.read_csv('data.csv',index_col=0)
    else:
        file=pd.read_csv('data-3 (2).csv',index_col=0)

    return file
# Convert from week Type to date type and return string
def ConvertWeektoDate(Year_week):
    import datetime
    d = Year_week
    r = datetime.datetime.strptime(d + '-1', "%Y-W%W-%w").date()
    return r.strftime('%Y/%m/%d')
# This function get flag (true for file 1) and country name and colum
# and return kind of list
def GetCleanData(flag,country,colum):
    if flag:
        file1=SetUpFile(flag)
        # print(file1.head())
        return file1.loc[[country],colum]

    else:
        file2=SetUpFile(flag)
        return file2.loc[[country],colum]
def GetDatesFromFile1(country):
    file1=SetUpFile(True)
    date_list = file1.loc[[country],'year_week'].values
    Array_toreturn=[]
    for x in date_list:
        # print(x)
        real_date=ConvertWeektoDate(x)
        # print(real_date)
        Array_toreturn.append(real_date)
    # print(Array_toreturn)
    return Array_toreturn

def TopN(list1, N):
    final_list = []

    for i in range(0, N):
        max1 = 0

        for j in range(len(list1)):
            if list1[j] > max1:
                max1 = list1[j]

        list1.remove(max1)
        final_list.append(max1)

    return(final_list)

def Sum_Of_Var_In_Country(country):
    date_list = GetCleanData(True, country, 'year_week')
    the_lastOne = '2020'
    filtersDate = []
    for x in date_list:
        filtersDate.append(x[0:4])
    UniqList = set(filtersDate)
    UniqList = list(UniqList)
    UniqList.sort()
    # print(UniqList)
    data = GetCleanData(True, country, 'tests_done')
    # print(len(data))
    filedate = sorted(date_list)
    array_ofSum = []
    sum = 0
    for x in range(len(data)):
        # print(x, data.values[x], sum, filedate[x][0:4])
        if x != 0:
            the_lastOne = filedate[x - 1][0:4]
        if the_lastOne == filedate[x][0:4]:
            sum += data.values[x]
        if the_lastOne != filedate[x][0:4] or x == len(data) - 1:
            array_ofSum.append(sum)
            sum = 0

    # print((array_ofSum), (UniqList))
    return ((array_ofSum), (UniqList))


# ----------------------------------------------------------------------------------------------------------------------------
# The Hw
# ----------------------------------------------------------------------------------------------------------------------------




# ----------------------------------------------------------------------------------------------------------------------------
# Part 1
# ----------------------------------------------------------------------------------------------------------------------------
#Q1
def Display_Test_Rate(country):
    data=GetCleanData(True,country,'testing_rate')
    Cyprus_Test_rate_list=data.values.tolist()
    xpoints = list(range(len(Cyprus_Test_rate_list)))
    XDate=GetDatesFromFile1(country)
    plt.plot(xpoints,Cyprus_Test_rate_list)
    plt.title('Testing Rate in '+country)
    plt.ylabel('testing rate')
    plt.xlabel('week number')
    # plt.xticks(rotation=-45)
    plt.show()


#Q2
def Display_postive_rate(country):
    Sweeden=GetCleanData(True,country,'positivity_rate')
    # print(Sweeden.values)
    xpoints = list(range(len(Sweeden.values)))
    plt.plot(xpoints,Sweeden.values)
    plt.title('positivity_rate in '+country)
    plt.ylabel('R ratio')
    plt.xlabel('week number')
    # plt.xticks(rotation=-45)
    plt.axhline(y=2, color='green', linestyle='-')
    plt.text(0,4,'GOOD ratio')
    plt.axhline(y=15, color='orange', linestyle='-')
    plt.text(0, 17, 'satisfy ratio')
    plt.axhline(y=30, color='red', linestyle='-')
    plt.text(0, 32, 'Very BAD ratio')

    plt.show()

#Q3
def Display_SumOfTest(country):
    date_list=GetDatesFromFile1(country)
    the_lastOne=''
    filtersDate=[]
    for x in date_list:
        filtersDate.append(x[0:7])
    UniqList=set(filtersDate)
    UniqList=list(UniqList)
    UniqList.sort()
    # print(UniqList)
    data=GetCleanData(True,country,'tests_done')
    filedate = GetDatesFromFile1(country)
    array_ofSum=[]
    # print(data.values)
    # print(filedate)
    sum=0
    for x in range(len(data)):
        # print(x,data.values[x],filedate[x][0:7],UniqList[x])
        print(x, data.values[x], filedate[x][0:7])
        if x!=0:
            the_lastOne = filedate[x - 1][0:7]
            print(the_lastOne)
        if the_lastOne==filedate[x][0:7]:
            sum+=data.values[x]
            print(sum)
        else:
            array_ofSum.append(sum)
            sum=0
            print('skip')

    # xpoints = list(range(len(Sweeden.values)))
    colors = ['red', 'green']
    plt.bar(UniqList,array_ofSum,color=colors)
    plt.xticks(rotation=-90)
    plt.ylabel('Sum of Test done')
    plt.xlabel('Year/month')
    plt.title('Sum of test done by month in '+country)


    plt.show()

#Q4
def Display_SumOfPostive(country):
    date_list = GetDatesFromFile1(country)
    the_lastOne = ''
    filtersDate = []
    for x in date_list:
        filtersDate.append(x[0:7])
    UniqList = set(filtersDate)
    UniqList = list(UniqList)
    UniqList.sort()
    # print(UniqList)
    data = GetCleanData(True, country, 'new_cases')
    filedate = GetDatesFromFile1(country)
    array_ofSum = []
    # print(data.values)
    # print(filedate)
    sum = 0
    for x in range(len(data)):
        # print(x,data.values[x],filedate[x][0:7],UniqList[x])
        print(x, data.values[x], filedate[x][0:7])
        if x != 0:
            the_lastOne = filedate[x - 1][0:7]
            print(the_lastOne)
        if the_lastOne == filedate[x][0:7]:
            sum += data.values[x]
            print(sum)
        else:
            array_ofSum.append(sum)
            sum = 0
            print('skip')

    # xpoints = list(range(len(Sweeden.values)))
    colors = ['red', 'green']
    plt.bar(UniqList, array_ofSum, color=colors)
    plt.xticks(rotation=-90)
    plt.ylabel('Sum of new cases')
    plt.xlabel('Year/month')
    plt.title('Sum of new cases by month in '+country)

    plt.show()

#Q5
def Display_Death(country):
    dataY=GetCleanData(False,country,'value')
    dataX=GetCleanData(False,country,'date')
    xpoints = list(range(len(dataX.values)))
    plt.plot(xpoints,dataY.values)
    plt.ylabel('Amount of Death')
    plt.xlabel('Index')
    plt.title('Amount of Death in '+country)
    plt.show()

#Q6
def Display_DeathPie(country):

    date_list=GetCleanData(False,country,'date')
    the_lastOne='2020'
    filtersDate=[]
    for x in date_list:
        filtersDate.append(x[0:4])
    UniqList=set(filtersDate)
    UniqList=list(UniqList)
    UniqList.sort()
    # print(UniqList)
    data=GetCleanData(False,country,'value')
    print(len(data))
    filedate = sorted(date_list)
    array_ofSum=[]
    # print(data.values)
    print('sort Date',filedate)
    sum=0
    for x in range(len(data)):
        print(x,data.values[x],sum,filedate[x][0:4])
        # print(x, data.values[x], filedate[x][0:4])
        if x!=0:
            the_lastOne = filedate[x - 1][0:4]
            # print(the_lastOne)
        if the_lastOne==filedate[x][0:4]:
            sum+=data.values[x]
            # print(sum)
        if the_lastOne!=filedate[x][0:4] or x==len(data)-1:
            array_ofSum.append(sum)
            sum=0
            print('skip')

    print((array_ofSum),(UniqList))
    colors=['r','g','b','y']

    plt.pie(array_ofSum, labels=UniqList, colors=colors,
            startangle=90, shadow=True,
            radius=1.2, autopct='%1.1f%%')
    plt.text(-0.5,1.5,'The death in '+country)
    plt.legend()
    plt.show()

#Q7
def Display_TestRatio(country):
    tests_done = GetCleanData(True,country,'tests_done').values
    new_cases = GetCleanData(True,country,'new_cases').values
    Ratio =[]

    # print(len(tests_done),len(new_cases))
    for i in range(len(tests_done)):
        if tests_done[i]!=0:
            Ratio.append(new_cases[i]/tests_done[i])


    xpoints = list(range(len(Ratio)))
    print(xpoints,Ratio)
    plt.plot(xpoints,Ratio,marker='o')
    plt.title('The Ratio new_cases/test_done   in '+country)
    plt.ylabel('ratio')
    plt.xlabel('weeks')
    plt.show()

#Q8
def Display_DeathAvrage(country):
    deathList=GetCleanData(False,country,'value')
    date_list=GetCleanData(False,country,'date')
    the_lastOne=''
    filtersDate=[]
    for x in date_list:
        filtersDate.append(x[0:7])
    UniqList=set(filtersDate)
    UniqList=list(UniqList)
    UniqList.sort()
    # print(UniqList)
    # print(len(deathList))
    filedate = sorted(date_list)
    array_ofSum=[]
    # print(data.values)
    # print('sort Date',filedate)
    count=0
    sum=0
    for x in range(len(deathList)):
        print(x,deathList.values[x],sum,filedate[x][0:7])
        # print(x, data.values[x], filedate[x][0:4])
        if x!=0:
            the_lastOne = filedate[x - 1][0:7]
            # print(the_lastOne)
        if the_lastOne==filedate[x][0:7]:
            count=count+1
            sum+=deathList.values[x]
            # print(sum)
        if the_lastOne!=filedate[x][0:7] or x==len(deathList)-1:
            if count==0:
                continue
            array_ofSum.append(sum/count)
            sum=0
            count=0
            print('skip')

    print(len(array_ofSum),len(UniqList))
    print((array_ofSum),(UniqList))
    colors=['r','g','b']
    plt.bar(UniqList,array_ofSum,color=colors)
    plt.xticks(rotation=-90)
    plt.title('The avarge of death in '+country)
    plt.ylabel('avg of death')
    plt.show()
#Q9
def Display_freqSource(country):
    all=SetUpFile(False)
    print(len(all['source'].values))
    colors=['r','g','b']
    plt.hist(all['source'].values,color='pink',alpha=1,width=0.8)
    plt.xticks(rotation=-45)
    plt.title('The frequency of source data')
    plt.ylabel('frequency')
    plt.show()

#Q10
def Display_NegtiveTest(country):
    tests_done = GetCleanData(True, country, 'tests_done').values
    new_cases = GetCleanData(True, country, 'new_cases').values


    date_list = GetDatesFromFile1(country)
    the_lastOne = '2020'
    filtersDate = []
    for x in date_list:
        filtersDate.append(x[0:4])
    UniqList = set(filtersDate)
    UniqList = list(UniqList)
    UniqList.sort()
    # print(UniqList)
    print(len(tests_done),len(new_cases))
    filedate = sorted(date_list)
    array_ofSum = []
    # print(data.values)
    # print('sort Date', filedate)
    sum = 0
    sumOfTest=0
    for x in range(len(new_cases)):
        print(x, new_cases[x],tests_done[x], sum, filedate[x][0:4])
        # print(x, data.values[x], filedate[x][0:4])
        if x != 0:
            the_lastOne = filedate[x - 1][0:4]
            # print(the_lastOne)
        if the_lastOne == filedate[x][0:4]:
            sum += tests_done[x]-new_cases[x]
            sumOfTest+=tests_done[x]
            # print(sum)
        if the_lastOne != filedate[x][0:4] or x == len(new_cases) - 1:
            array_ofSum.append(sum/sumOfTest)
            sum = 0
            sumOfTest=0
            print('skip')

    print((array_ofSum), (UniqList))
    colors = ['r', 'g', 'b', 'y']
    plt.barh(UniqList,array_ofSum,color='Yellowgreen',height=0.6)
    plt.title('The negtive precentage of tests in '+country)
    plt.show()










# ----------------------------------------------------------------------------------------------------------------------------
# Part2
# ----------------------------------------------------------------------------------------------------------------------------

#guy
def Compare_avg_pr_by_two_countries(year, country1, country2):
    data = pd.read_csv('data.csv', index_col=0)
    file1 = data.loc[country1, ['positivity_rate', 'year_week']]
    file2 = data.loc[country2, ['positivity_rate', 'year_week']]
    sum1 = 0
    sum2 = 0
    filteredData1 = file1[file1['year_week'].str.startswith(str(year))]['positivity_rate'].values
    filteredData2 = file2[file2['year_week'].str.startswith(str(year))]['positivity_rate'].values
    for i in filteredData1:
        sum1+=i
    for i in filteredData2:
        sum2+=i

    colors = ['r', 'g']
    array_ofSum = [sum1, sum2]
    UniqList = [country1, country2]
    plt.pie(array_ofSum, labels=UniqList, colors=colors,
            startangle=90, shadow=True, autopct='%1.1f%%')
    plt.title('Compare positive rate average between ' + country1 + ' and ' + country2 + ' in ' + str(year))
    plt.legend()
    plt.show()
#guy
def Compare_positive_rate(year, country1, country2):
    data = pd.read_csv('data.csv', index_col=0)
    file1 = data.loc[country1, ['positivity_rate', 'year_week']]
    file2 = data.loc[country2, ['positivity_rate', 'year_week']]

    filteredData1 = file1[file1['year_week'].str.startswith(str(year))]['positivity_rate'].values
    filteredData2 = file2[file2['year_week'].str.startswith(str(year))]['positivity_rate'].values

    xpoints = list(range(len(filteredData1)))
    ypoints = list(range(len(filteredData2)))
    plt.bar(ypoints, filteredData2, label=country1,color='g')

    plt.bar(xpoints, filteredData1, label=country1, color='r')

    plt.title('Comparing positivity rate between {} and {} for {}'.format(country1, country2, year))
    plt.xlabel('Week number')
    plt.ylabel('Positivity Rate')
    plt.legend()

    plt.show()

#guy
def Compare_deaths_based_indicator_in_a_given_year(year):
    data = SetUpFile(False)
    data['year'] = data['year_week'].str[:4]
    data_year = data[data['year'] == year]
    deaths_by_indicator = data_year.groupby('indicator')['value'].sum()
    print(deaths_by_indicator.keys())
    colors = ['r', 'g','y','b']

    plt.bar(deaths_by_indicator.keys(), deaths_by_indicator.values, label=deaths_by_indicator.keys(),color=colors)

    plt.title('Compare deaths based on info from each indicator_in '+year)

    plt.legend()
    plt.show()

#dor
def compare_new_cases(country1, country2, year):

    date_list1=GetCleanData(True,country1,'year_week')

    data1=GetCleanData(True,country1,'new_cases')
    print(data1)
    filedate1 = sorted(date_list1)
    sum1=0
    count=0
    for x in range(len(data1)):
        if filedate1[x - 1][0:4]==year:
            # print(the_lastOne,' ', data1.values[x])
            sum1+=data1.values[x-1]
            count+=1

    print(sum1)

    date_list2=GetCleanData(True,country2,'year_week')
    data2=GetCleanData(True,country2,'new_cases')
    filedate2 = sorted(date_list2)
    sum2=0
    for x in range(len(data1)):
        if filedate2[x - 1][0:4]==year:
            the_lastOne = filedate2[x - 1][0:4]
            sum2+=data2.values[x-1]
    print(sum2)
    colors=['r','g']
    array_ofSum=[sum1,sum2]
    UniqList=[country1,country2]
    plt.pie(array_ofSum, labels=UniqList, colors=colors,
            startangle=90, shadow=True,
            radius=1.2, autopct='%1.1f%%')
    plt.text(-1,1.5,'Comparation of new cases between '+country1+ ' and '+ country2+' in '+year)
    plt.legend()
    plt.show()
#Dor
def Display_Tests_Done_Comparation(country1, country2):
    sumCounty1=Sum_Of_Var_In_Country(country1)[0]
    sumCounty2=Sum_Of_Var_In_Country(country2)[0]
    years=Sum_Of_Var_In_Country(country1)[1]
    print(sumCounty1,sumCounty2)
    arrOfSums=(sumCounty1,sumCounty2)
    print(years)

    plt.bar(years,sumCounty2,color='b')
    plt.bar(years,sumCounty1,color='r')
    plt.ylabel('amount of tests')
    plt.xlabel('years')
    plt.title('Comparation of test done between '+country1+' and '+country2)
    plt.show()
#dor
def plot_top_months_with_deaths(year,NumOfMonth):
    data = SetUpFile(False)
    #return the relevent data by wanted year
    data_year = data[data['date'].str[:4] == year]
    #return sum of death for all month
    deaths_by_month = data_year.groupby(data_year['date'].str[5:7])['value'].sum().reset_index()
    #sort month and take 3 largest
    sorted_months = deaths_by_month.sort_values('value', ascending=False).head(NumOfMonth)

    months = sorted_months['date']
    deaths = sorted_months['value']

    plt.bar(months, deaths)
    plt.xlabel('Month')
    plt.ylabel('Number of Deaths')
    plt.title(f'Top Three Months with Highest Deaths in {year}')
    plt.show()


#Q12
def Display_TheMostTestAmount(year,N):
    countries=GetUniqCountry()
    SumOfTestbyYear=[]
    SumOfTestbyYear2=[]
    for country in countries:
        test_Done_per_country=GetCleanData(True,country,'tests_done').values
        Years_per_country = GetCleanData(True, country, 'year_week').values
    # print(len(test_Done_per_country),len(Years_per_country))
        sum=0
        for i in range(len(test_Done_per_country)):
            if Years_per_country[i][:4]==year:
                sum+=test_Done_per_country[i]
        SumOfTestbyYear.append(sum)
        SumOfTestbyYear2.append(sum)

    print(len(SumOfTestbyYear),SumOfTestbyYear)
    temp=SumOfTestbyYear
    topn=TopN(temp,N)
    print(topn)
    print(len(SumOfTestbyYear), SumOfTestbyYear)
    listLabels=[]
    for i in range(len(SumOfTestbyYear2)):
        for j in range(len(topn)):
            if topn[j]==SumOfTestbyYear2[i]:
                listLabels.append(countries[i])
    print(listLabels)

    plt.bar(listLabels,topn,width=0.8,color=['r','g','b'])
    plt.title('Top '+str(N)+' countries with the most test amount')
    plt.ylabel('Test amount (in 10 millions)')
    plt.legend()
    plt.show()
#Q15
def Display_popdeath_ratio(year,N):
    file=SetUpFile(False)
    countries=list(set(file['value'].keys()))
    dates=file['date'].values
    Death = file['value'].values
    print(len(countries),countries)
    ValuesSumsPerCountry=[]
    ValuesSumsPerCountry2=[]

    popList=[]
    popTopList=[]
    for i in range(len(countries)):
        popList.append(GetCleanData(True,countries[i],'population')[0])
        date_country=GetCleanData(False,countries[i],'date').values
        death_country=GetCleanData(False,countries[i],'value').values
        # print(len(date_country), date_country)
        # print(len(death_country), death_country)
        sumDeathPer = 0
        for a in range(len(death_country)):
            if date_country[a][:4]==str(year):
                sumDeathPer+=death_country[a]
        ValuesSumsPerCountry.append(sumDeathPer)
        ValuesSumsPerCountry2.append(sumDeathPer)

    labelList=[]
    newRatioList=[]
    newRatioList2 = []
    andDeathValueTop=[]
    for i in range(len(ValuesSumsPerCountry)):
        newRatioList.append(ValuesSumsPerCountry[i]/popList[i])
        newRatioList2.append(ValuesSumsPerCountry[i] / popList[i])




    topn=TopN(newRatioList,N)
    for i in range(len(ValuesSumsPerCountry2)):
        if topn.__contains__(newRatioList2[i]):
            labelList.append(countries[i])
            popTopList.append(popList[i])
            andDeathValueTop.append(ValuesSumsPerCountry2[i])

    print(len(ValuesSumsPerCountry2),ValuesSumsPerCountry2)
    print(len(popTopList), popTopList)
    print(len(topn), topn)
    print(len(labelList), labelList)

    plt.bar(labelList,popTopList,label='Population amount')
    plt.bar(labelList,andDeathValueTop,label='Death amount')
    plt.title('Top '+str(N)+' countries with the most Death amount vs population amount')
    plt.ylabel('in 10 millions')
    plt.legend()
    plt.show()

#Q18
def DisplayTestRatio(country1,country2,year):
    datesList=GetDatesFromFile1(country1)
    tests_done1=GetCleanData(True,country1,'tests_done').values
    new_cases1=GetCleanData(True,country1,'new_cases').values
    tests_done2=GetCleanData(True,country2,'tests_done').values
    new_cases2=GetCleanData(True,country2,'new_cases').values

    tests_done1F=[]
    new_cases1F=[]
    tests_done2F=[]
    new_cases2F=[]

    for i in range(len(datesList)):
        if datesList[i][:4]==str(year):
            tests_done1F.append(tests_done1[i])
            new_cases1F.append(new_cases1[i])
            tests_done2F.append(tests_done2[i])
            new_cases2F.append(new_cases2[i])

    print(len(tests_done1F),sum(tests_done1F))
    print(len(new_cases1F), sum(new_cases1F))
    print(len(tests_done2F),sum(tests_done2F) )
    print(len(new_cases2F),sum(new_cases2F) )
    plt.pie([sum(tests_done1F),sum(tests_done2F)],labels=['tests done in '+country1,'tests done in '+country2],radius=0.75, center=(1.7,0),colors=['r','b'],  shadow = True ,autopct='%1.1f%%')
    plt.pie([sum(new_cases1F),sum(new_cases2F)],labels=['new cases in '+country1,'new cases in '+country2],radius=0.75, center=(-1.7, 0), colors=['y','g'],  shadow = True,autopct='%1.1f%%')
    plt.pie([1],radius=0.001,center=(0,0))
    plt.title('compare test done vs new cases in '+country1+' vs '+ country2+' ('+year+')')

    plt.show()

#Dor
def compar_Death_between_three_countries(country1,country2,country3):
    countries = [country1, country2, country3]
    data = pd.read_csv('data-3 (2).csv')
    # Convert 'date' column to datetime type
    data['date'] = pd.to_datetime(data['date'], format='%Y-%m-%d')  # Specify the date format if needed

    # Filter the data for the selected countries
    filtered_data = data[data['country'].isin(countries)]

    # Extract year and month from the 'date' column
    filtered_data['year'] = filtered_data['date'].dt.year

    # Group the data by country, year, and month, and calculate the sum of values
    grouped_data = filtered_data.groupby(['country', 'year'])['value'].sum().reset_index()

    # Set up the figure and axis
    fig, ax = plt.subplots(figsize=(10, 6))

    # Get unique years from the data
    years = grouped_data['year'].unique()

    # Plot data for each country
    for country in countries:
        country_data = grouped_data[grouped_data['country'] == country]
        ax.plot(country_data['year'], country_data['value'], label=f"{country} ")


    # Customize the plot
    ax.set_xlabel('Year')
    ax.set_ylabel('Death')
    ax.set_title('Comparison of Death between '+country1+' '+country2+' '+country3+' '+ 'by Year ')
    ax.legend()

    # Show the plot
    plt.show()

# ----------------------------------------------------------------------------------------------------------------------------
# Dashboard
# ----------------------------------------------------------------------------------------------------------------------------

def DeathPieYears(country):
    date_list = GetCleanData(False, country, 'date')
    the_lastOne = '2020'
    filtersDate = []
    for x in date_list:
        filtersDate.append(x[0:4])
    UniqList = set(filtersDate)
    UniqList = list(UniqList)
    UniqList.sort()
    # print(UniqList)
    data = GetCleanData(False, country, 'value')
    print(len(data))
    filedate = sorted(date_list)
    array_ofSum = []
    # print(data.values)
    print('sort Date', filedate)
    sum = 0
    for x in range(len(data)):
        print(x, data.values[x], sum, filedate[x][0:4])
        # print(x, data.values[x], filedate[x][0:4])
        if x != 0:
            the_lastOne = filedate[x - 1][0:4]
            # print(the_lastOne)
        if the_lastOne == filedate[x][0:4]:
            sum += data.values[x]
            # print(sum)
        if the_lastOne != filedate[x][0:4] or x == len(data) - 1:
            array_ofSum.append(sum)
            sum = 0
            print('skip')

    print((array_ofSum), (UniqList))
    return [array_ofSum,UniqList]
def PostiveSumbyMonth(country):
    date_list = GetDatesFromFile1(country)
    the_lastOne = ''
    filtersDate = []
    for x in date_list:
        filtersDate.append(x[0:7])
    UniqList = set(filtersDate)
    UniqList = list(UniqList)
    UniqList.sort()
    # print(UniqList)
    data = GetCleanData(True, country, 'new_cases')
    filedate = GetDatesFromFile1(country)
    array_ofSum = []
    # print(data.values)
    # print(filedate)
    sum = 0
    for x in range(len(data)):
        # print(x,data.values[x],filedate[x][0:7],UniqList[x])
        print(x, data.values[x], filedate[x][0:7])
        if x != 0:
            the_lastOne = filedate[x - 1][0:7]
            print(the_lastOne)
        if the_lastOne == filedate[x][0:7]:
            sum += data.values[x]
            print(sum)
        else:
            array_ofSum.append(sum)
            sum = 0
            print('skip')
    return [array_ofSum,UniqList]

def TestRatioWeekly(country):
    tests_done = GetCleanData(True,country,'tests_done').values
    new_cases = GetCleanData(True,country,'new_cases').values
    Ratio =[]

    # print(len(tests_done),len(new_cases))
    for i in range(len(tests_done)):
        if tests_done[i]!=0:
            Ratio.append(new_cases[i]/tests_done[i])


    xpoints = list(range(len(Ratio)))
    print(xpoints,Ratio)
    return[Ratio,xpoints]

def SumofTestMonthly(country):
    date_list = GetDatesFromFile1(country)
    the_lastOne = ''
    filtersDate = []
    for x in date_list:
        filtersDate.append(x[0:7])
    UniqList = set(filtersDate)
    UniqList = list(UniqList)
    UniqList.sort()
    # print(UniqList)
    data = GetCleanData(True, country, 'tests_done')
    filedate = GetDatesFromFile1(country)
    array_ofSum = []
    # print(data.values)
    # print(filedate)
    sum = 0
    for x in range(len(data)):
        # print(x,data.values[x],filedate[x][0:7],UniqList[x])
        print(x, data.values[x], filedate[x][0:7])
        if x != 0:
            the_lastOne = filedate[x - 1][0:7]
            print(the_lastOne)
        if the_lastOne == filedate[x][0:7]:
            sum += data.values[x]
            print(sum)
        else:
            array_ofSum.append(sum)
            sum = 0
            print('skip')

    # xpoints = list(range(len(Sweeden.values)))
    # colors = ['red', 'green']
    # plt.bar(UniqList, array_ofSum, color=colors)
    return[array_ofSum,UniqList]

def SumOfDeathRatioEurop(country):
    # Death=GetCleanData(False,country,'value')
    AllDeath=SetUpFile(False)
    AllDeath=AllDeath['value']
    print(AllDeath)
    print(len(AllDeath.keys().values))
    print(len(AllDeath.values))
    OurSum=0
    AllEurop=0;
    for i in range(len(AllDeath.values)):
        if AllDeath.keys().values[i]==country:
            OurSum+=AllDeath.values[i]
        AllEurop+=AllDeath.values[i]
    # print(OurSum,AllEurop,round(OurSum/AllEurop,3))
    return[OurSum,AllEurop,round(OurSum/AllEurop,3)*100]

def calculate_country_population_ratio(country):
    data = SetUpFile(True)
    country_population = data.loc[data.index == country, 'population'].sum()
    total_population = data['population'].sum()
    ratio = country_population / total_population
    print(ratio)
    return ratio
def getPopulationBycountry(country):
    population=GetCleanData(True,country,'population')
    return population[0]
def display_Dashborad(country):


    resPie=DeathPieYears(country)
    resPlotMonth=PostiveSumbyMonth(country)
    resplotTestRatio=TestRatioWeekly(country)
    barData=SumofTestMonthly(country)
    resForTextDeath=SumOfDeathRatioEurop(country)
    resPop=getPopulationBycountry(country)
    resPopR=calculate_country_population_ratio(country)
    # Initialise the subplot function using number of rows and columns
    figure, axis = plt.subplots(2, 2)

    # For Sine Function
    axis[0, 0].plot(resPlotMonth[1], resPlotMonth[0],color='r')
    axis[0, 0].tick_params(axis='x', rotation=-90)
    # plt.xticks(rotation=-90)
    axis[0, 0].set_title("Sum of postive cases in "+country+" by month and year")

    # For Cosine Function
    axis[1, 1].plot(resplotTestRatio[1], resplotTestRatio[0],color='y')
    # axis[1, 1].set_title("Ratio test done / new cases in "+country+" weekly")

    # For Tangent Function
    axis[1, 0].pie(resPie[0],labels=resPie[1],autopct='%1.1f%%')
    # axis[0, 0].set_title("Death in "+country+" by years")

    # For Tanh Function
    axis[0, 1].bar(barData[1], barData[0])
    axis[0, 1].set_title("Sum of Test in "+country+" by month")
    axis[0, 1].tick_params(axis='x', rotation=-90)

    # axis[2,2].text(0,0,"This is the Avarge",fontsize=300)
    axis[-1,0].text(-3.95,0.2,"Sum of death in Europ: "+str(round(resForTextDeath[1]))+"\nThe sum of death in "+country+": "+str(round(resForTextDeath[0]))+"\nThe ratio is: "+str(round(resForTextDeath[2],3))+"%",fontsize=14, color='r')
    axis[-1,0].text(-3.95,-0.4,"The population in "+country+": "+str(round(resPop))+"\n The Ratio vs All Europ: "+str(round(resPopR*100,4))+" %",fontsize=14, color='g')
    axis[-1,0].text(4.4,-1.7,"Test Ratio (test done / new cases) in "+country,fontsize=18, color='black')
    axis[-1, 0].text(-1.2, -1.7, "Sum Of Death in "+ country+" By Years", fontsize=18, color='black')


    # Combine all the operations and display
    plt.show()



# ----------------------------------------------------------------------------------------------------------------------------
# Main interFace
# ----------------------------------------------------------------------------------------------------------------------------




def GetUniqCountry():
    keys=SetUpFile(True)
    setkeys=set(keys.index)
    listkeys = list(setkeys)
    return listkeys

def App(fn,country1,country2,country3,year,N):
    if fn == 1:
        Display_Test_Rate(country1)
    elif fn == 2:
        Display_postive_rate(country1)
    elif fn == 3:
        Display_SumOfTest(country1)
    elif fn == 4:
        Display_SumOfPostive(country1)
    elif fn == 5:
        Display_Death(country1)
    elif fn == 6:
        Display_DeathPie(country1)
    elif fn == 7:
        Display_TestRatio(country1)
    elif fn == 8:
        Display_DeathAvrage(country1)
    elif fn == 9:
        Display_freqSource(country1)
    elif fn == 10:
        Display_NegtiveTest(country1)
    elif fn == 11:
        Compare_avg_pr_by_two_countries(year,country1,country2)
    elif fn == 12:
        Compare_positive_rate(year,country1,country2)
    elif fn == 13:
        Compare_deaths_based_indicator_in_a_given_year(year)
    elif fn == 14:
        compare_new_cases(country1,country2,year)
    elif fn == 15:
        Display_Tests_Done_Comparation(country1,country2)
    elif fn == 16:
        plot_top_months_with_deaths(year,N)
    elif fn == 17:
        Display_TheMostTestAmount(year,N)
    elif fn == 18:
        Display_popdeath_ratio(year,N)
    elif fn == 19:
        DisplayTestRatio(country1,country2,year)
    elif fn == 20:
        compar_Death_between_three_countries(country1,country2,country3)
    else:
        display_Dashborad(country1)





def interFaceLaunch():
    demo = gr.Interface(fn=App, inputs=[gr.Dropdown([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,'Dashboard'],label='function number'),gr.Dropdown(GetUniqCountry(),label='Countries1'),gr.Dropdown(GetUniqCountry(),label='Countries2'),gr.Dropdown(GetUniqCountry(),label='Countries3'),gr.Dropdown(['2020','2021','2022','2023'],label='Year'),gr.Dropdown(list(range(len(GetUniqCountry()))),label='N')], outputs="plot")
    demo.launch()

interFaceLaunch()