Spaces:
Sleeping
Sleeping
| 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() | |