This repository has been archived by the owner on Dec 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoronaData.py
166 lines (138 loc) · 5.06 KB
/
CoronaData.py
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
# https://www.mohfw.gov.in/data/datanew.json
# for getting web contents
from requests import get
#from prettytable import PrettyTable
from matplotlib import pyplot as plt
from datetime import datetime
from pprint import pprint
import json
#import Image
# importing the required libraries
import pandas as pd
# change image into url
from imgUpload import imgUrl
import numpy as np
# -------------------------------------------------------------------------
# -------------------------------------------------------------------------
def change_to_int(a):
try:
b = int(a)
except ValueError:
b = 0
return(b)
# --------------------------------------------------------------------------
def retry():
url = "https://www.mohfw.gov.in/data/datanew.json"
web_content = get(url).content
data = json.loads(web_content)
# pprint(data)
# type(data)
# ----------------------------- data preprocessing
new_column = ["State_name" , "Active_cases" , "Positive" , "Cured" , "Death" ]
data_Container = []
name =[]
active = []
Cured = []
death = []
confirm = []
for x in data:
# print(x)
new = [x['state_name'] , x['new_active'] , x['new_positive'] , x['new_cured'] , x['new_death']]
data_Container.append(new)
y = len(data_Container)
# print(y)
del data_Container[y-1]
# print(data_Container)
# [['Andaman and Nicobar Islands', '88', '4875', '4732', '61'],
# ------------------------------------------------------------------------------------
'''
time = datetime.now()
string = str(time)
time = string.replace(':',' ')
# print(time)
with open('Datacorona/COVID19'+time+".txt" , 'w') as file:
file.write('["State_name" , "Active_cases" , "Positive" , "Cured" , "Death" ]' +"\n")
for info in data_Container:
file.write(str(info)+"\n")
'''
# -------------------------------------------------------------
# data distributed into parts
for x in data_Container:
name.append(x[0])
active.append(change_to_int(x[1]))
confirm.append(change_to_int(x[2]))
Cured.append(change_to_int(x[3]))
death.append(change_to_int(x[4]))
name = np.array(name)
active = np.array(active,dtype = "int32")
Cured = np.array(Cured,dtype = "int32")
confirm = np.array(confirm,dtype = "int32")
death = np.array(death,dtype = "int32")
'''
table = PrettyTable()
table.field_names = (new_column)
for i in data_Container:
table.add_row(i)
table.add_row(["Total",
sum(active),
sum(Cured),
sum(death),
sum(confirm)])
# print(table)
'''
# ------------------------------------------------------------------------
# Data Visulalisation
plt.figure(figsize = (15,10))
plt.barh(name,active,align = 'center',color='lightblue',edgecolor='blue',label=' ')
plt.title('Total Active Case StateWise',fontsize = 18)
plt.xlabel('No. of Active cases',fontsize = 18)
plt.ylabel('States/UT',fontsize = 18)
plt.xticks(fontsize = 8)
plt.yticks(fontsize = 7)
for index, value in enumerate(active):
plt.text(value, index, str(value), fontsize = 8)
plt.legend()
plt.savefig('img/active.jpg')
c=(sum(confirm))
r=(sum(Cured))
d=(sum(death))
re=(sum(active))
plt.figure(figsize = (15,10))
plt.barh(name,Cured,align = 'center',color='lightblue',edgecolor='blue',label = ' ')
plt.title('Total Cured Case StateWise',fontsize = 18)
plt.xlabel('No. of Cured cases',fontsize = 18)
plt.ylabel('States/UT',fontsize = 18)
plt.xticks(fontsize = 8)
plt.yticks(fontsize = 7)
for index, value in enumerate(Cured):
plt.text(value, index, str(value), fontsize = 8)
plt.legend()
plt.savefig('img/cured.jpg')
group_size = [c,r,d,re]
group_labels = ['\n\nConfirmed :\n ' + str(c),
'\n\nCured :\n' + str(r),
'\n\n------Death :\n' + str(d),
'\n\nTotal Active Cases \n: ' +str(re)]
custom_colors = ['skyblue','yellowgreen','tomato','orange']
plt.figure(figsize = (5,5))
plt.pie(group_size, labels = group_labels, colors = custom_colors, startangle=60 ,explode=(0.1,0.1,0.1,0.3))
central_circle = plt.Circle((0,0),0.5, color = 'white')
fig = plt.gcf()
fig.gca().add_artist(central_circle)
plt.rc('font', size = 12)
plt.title('Nationwide total Active,\n Confirmed, Cured and Deceased Cases', fontsize = 16)
plt.savefig('img/death.jpg')
#plt.show()
# ----------------------------------------------------------------------------------------------------------------------
# change plot into the form of image
#plt.savefig('img/activeplot.jpg')
#Image.open('testplot.png').save('testplot.jpg','JPEG')
def done():
# retry()
"""_it gives the image url file_
no value entered as parameter
"""
x = imgUrl('img/active.jpg')
y = imgUrl('img/cured.jpg')
z = imgUrl('img/death.jpg')
return [x,y,z]