-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
231 lines (184 loc) · 8.3 KB
/
Copy pathmain.py
File metadata and controls
231 lines (184 loc) · 8.3 KB
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
import pandas as pd
import matplotlib.pyplot as plt
import requests
from datetime import datetime, timedelta
import numpy as np
def load_data():
"""
Fetch COVID-19 data from disease.sh API (Johns Hopkins CSSE data).
Returns a pandas DataFrame with historical data.
Note: Recent data may not include recovery figures as disease.sh API
no longer tracks recoveries for most regions.
"""
print("=" * 60)
print("LOADING COVID-19 DATA FROM API")
print("=" * 60)
countries = ['USA', 'India', 'Brazil', 'UK', 'France', 'Germany', 'Italy', 'Spain', 'Canada', 'Australia']
all_data = []
for country in countries:
try:
url = f"https://disease.sh/v3/covid-19/historical/{country}?lastdays=90"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if 'timeline' not in data:
print(f"✗ No timeline data for {country}")
continue
timeline = data['timeline']
if 'cases' not in timeline or 'deaths' not in timeline:
print(f"✗ Incomplete timeline data for {country}")
continue
for date_str in timeline['cases'].keys():
all_data.append({
'date': date_str,
'region': country,
'cases': timeline['cases'].get(date_str, 0),
'deaths': timeline['deaths'].get(date_str, 0),
'recoveries': timeline.get('recovered', {}).get(date_str, 0)
})
print(f"✓ Loaded data for {country}")
else:
print(f"✗ Failed to load data for {country} (HTTP {response.status_code})")
except Exception as e:
print(f"✗ Error loading {country}: {str(e)}")
df = pd.DataFrame(all_data)
print(f"\n✓ Successfully loaded {len(df)} records from {len(countries)} countries")
return df
def clean_data(df):
"""
Clean the COVID-19 data: handle missing values and convert dates.
"""
print("\n" + "=" * 60)
print("DATA CLEANING")
print("=" * 60)
print("\nMissing values before cleaning:")
missing_values = df.isnull().sum()
print(missing_values)
df['recoveries'] = df['recoveries'].fillna(0)
df['date'] = pd.to_datetime(df['date'], format='%m/%d/%y', errors='coerce')
df = df.sort_values(['region', 'date']).reset_index(drop=True)
print("\nMissing values after cleaning:")
print(df.isnull().sum())
print(f"\n✓ Data cleaned successfully")
print(f" - Date range: {df['date'].min().strftime('%Y-%m-%d')} to {df['date'].max().strftime('%Y-%m-%d')}")
print(f" - Regions: {df['region'].nunique()}")
return df
def analyze_data(df):
"""
Perform data analysis: calculate ratios, group by region, and compute correlations.
"""
print("\n" + "=" * 60)
print("DATA ANALYSIS")
print("=" * 60)
df['recovery_ratio'] = df.apply(lambda row: row['recoveries'] / row['cases'] if row['cases'] > 0 else 0, axis=1)
df['death_rate'] = df.apply(lambda row: row['deaths'] / row['cases'] if row['cases'] > 0 else 0, axis=1)
df_grouped = df.groupby('region').last().reset_index()
regional_summary = df_grouped[['region', 'cases', 'deaths', 'recoveries']].copy()
regional_summary = regional_summary.sort_values('cases', ascending=False)
print("\nREGIONAL SUMMARY (Latest Data):")
print("-" * 60)
print(regional_summary.to_string(index=False))
valid_data = df[(df['cases'] > 0) & (df['recovery_ratio'] > 0) & (df['death_rate'] > 0)]
if len(valid_data) > 0:
correlation = valid_data['recovery_ratio'].corr(valid_data['death_rate'])
print(f"\n✓ Correlation between recovery ratio and death rate: {correlation:.4f}")
else:
print("\n⚠ Not enough valid data to calculate correlation")
correlation = 0
total_cases = regional_summary['cases'].sum()
total_deaths = regional_summary['deaths'].sum()
total_recoveries = regional_summary['recoveries'].sum()
print(f"\nGLOBAL SUMMARY:")
print(f" - Total Cases: {total_cases:,}")
print(f" - Total Deaths: {total_deaths:,}")
print(f" - Total Recoveries: {total_recoveries:,}")
print(f" - Global Death Rate: {(total_deaths/total_cases*100):.2f}%")
return df, regional_summary
def visualize_data(df, regional_summary):
"""
Create visualizations: line plot for cases trend and bar chart for regional cases.
"""
print("\n" + "=" * 60)
print("DATA VISUALIZATION")
print("=" * 60)
plt.style.use('seaborn-v0_8-darkgrid')
fig, ax = plt.subplots(figsize=(12, 6))
for region in df['region'].unique():
region_data = df[df['region'] == region].sort_values('date')
ax.plot(region_data['date'], region_data['cases'], label=region, linewidth=2)
ax.set_xlabel('Date', fontsize=12, fontweight='bold')
ax.set_ylabel('Total Cases', fontsize=12, fontweight='bold')
ax.set_title('COVID-19 Cases Trend Over Time by Country', fontsize=14, fontweight='bold')
ax.legend(loc='upper left', framealpha=0.9)
ax.grid(True, alpha=0.3)
plt.xticks(rotation=45)
plt.tight_layout()
plt.savefig('cases_trend.png', dpi=300, bbox_inches='tight')
plt.close()
print("✓ Saved cases_trend.png")
fig, ax = plt.subplots(figsize=(12, 6))
top_regions = regional_summary.head(10)
bars = ax.bar(range(len(top_regions)), top_regions['cases'], color='steelblue', edgecolor='navy', linewidth=1.5)
ax.set_xlabel('Country', fontsize=12, fontweight='bold')
ax.set_ylabel('Total Cases', fontsize=12, fontweight='bold')
ax.set_title('COVID-19 Total Cases by Country (Top 10)', fontsize=14, fontweight='bold')
ax.set_xticks(range(len(top_regions)))
ax.set_xticklabels(top_regions['region'], rotation=45, ha='right')
ax.grid(True, alpha=0.3, axis='y')
for i, bar in enumerate(bars):
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width()/2., height,
f'{int(height):,}',
ha='center', va='bottom', fontsize=9)
plt.tight_layout()
plt.savefig('regional_cases.png', dpi=300, bbox_inches='tight')
plt.close()
print("✓ Saved regional_cases.png")
print("\n✓ Visualizations created successfully")
def save_to_excel(regional_summary):
"""
Save the regional summary to an Excel file.
"""
print("\n" + "=" * 60)
print("EXCEL EXPORT")
print("=" * 60)
with pd.ExcelWriter('covid_summary.xlsx', engine='openpyxl') as writer:
regional_summary.to_excel(writer, sheet_name='Regional Summary', index=False)
workbook = writer.book
worksheet = writer.sheets['Regional Summary']
for column in worksheet.columns:
max_length = 0
column_letter = column[0].column_letter
for cell in column:
try:
if len(str(cell.value)) > max_length:
max_length = len(str(cell.value))
except:
pass
adjusted_width = min(max_length + 2, 50)
worksheet.column_dimensions[column_letter].width = adjusted_width
print("✓ Saved covid_summary.xlsx")
print("\n✓ Excel export completed successfully")
def main():
"""
Main function to orchestrate the entire data analysis workflow.
"""
print("\n" + "=" * 60)
print("COVID-19 DATA ANALYSIS PROJECT")
print("Data Source: disease.sh API (Johns Hopkins CSSE)")
print("=" * 60)
df = load_data()
df = clean_data(df)
df, regional_summary = analyze_data(df)
visualize_data(df, regional_summary)
save_to_excel(regional_summary)
print("\n" + "=" * 60)
print("ANALYSIS COMPLETE!")
print("=" * 60)
print("\nGenerated Files:")
print(" 1. cases_trend.png - Line chart showing cases over time")
print(" 2. regional_cases.png - Bar chart showing cases by country")
print(" 3. covid_summary.xlsx - Excel file with regional summary")
print("\n" + "=" * 60)
if __name__ == "__main__":
main()