Last active
March 21, 2020 14:55
-
-
Save Gordin/797c7ba2832d228cf8da516fe6d58b01 to your computer and use it in GitHub Desktop.
Script to calculate mortality rate of COVID-19 in different countries based on data from https://coronavirus.jhu.edu/map.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Little script to calculate the mortality rate of COVID-19 in different | |
# countries based on data from https://coronavirus.jhu.edu/map.html | |
# I extracted this from firefox dev tools. Returns country data as JSON | |
''' | |
curl 'https://services9.arcgis.com/N9p5hsImWXAccRNI/arcgis/rest/services/Z7biAeD8PAkqgmWhxG2A/FeatureServer/1/query?f=json&where=(Confirmed%20%3E%200)%20AND%20(Deaths%3E0)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Deaths%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true' -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0' -H 'Accept: */*' -H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'Origin: https://www.arcgis.com' -H 'Connection: keep-alive' -H 'Referer: https://www.arcgis.com/apps/opsdashboard/index.html' -H 'TE: Trailers' | |
''' | |
# Build the same request in python | |
from urllib import request | |
url = 'https://services9.arcgis.com/N9p5hsImWXAccRNI/arcgis/rest/services/Z7biAeD8PAkqgmWhxG2A/FeatureServer/1/query?f=json&where=(Confirmed%20%3E%200)%20AND%20(Deaths%3E0)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Deaths%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true' | |
req = request.Request(url) | |
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0') | |
req.add_header('Accept', '*/*') | |
req.add_header('Origin', 'https://www.arcgis.com') | |
req.add_header('Referer', 'https://www.arcgis.com/apps/opsdashboard/index.html') | |
req.add_header('TE', 'Trailers') | |
# Get the data from the site | |
resp = request.urlopen(req) | |
data_string = resp.read().decode('utf-8') | |
# Load JSON data into dict | |
import json | |
data = json.loads(data_string) | |
# See all data for countries | |
# [x['attributes'] for x in data['features']] | |
# Get just the values we need from all states into a list | |
ccds = [(y['Country_Region'], y['Confirmed'], y['Deaths']) for y in (x['attributes'] for x in data['features'])] | |
# make defaultdicts for confirmed cases and deaths | |
# (defaultsdict(int) makes it so you can add to an item that does not exist and | |
# it will treat it as 0 instead of throwing an exception | |
from collections import defaultdict | |
confirmed = defaultdict(int) | |
deaths = defaultdict(int) | |
# Sum up state numbers for the countries | |
for cntry, cnf, dd in ccds: | |
confirmed[cntry] += cnf | |
deaths[cntry] += dd | |
# calculate mortality rate from confirmed and deaths | |
mortality_rate = defaultdict(int) | |
# doesn't matter which dicts keys we use, we just need the country names | |
for cntry in confirmed.keys(): | |
mortality_rate[cntry] = 100.0 *deaths[cntry] / confirmed[cntry] | |
# Turn dict to list to sort it | |
rates = list(mortality_rate.items()) | |
# Print results | |
for y in list(enumerate(sorted(rates, key=lambda x: x[1]), 1)): | |
print(y) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(1, ('Israel', 0.11325028312570781)) | |
(2, ('Thailand', 0.24330900243309003)) | |
(3, ('Austria', 0.2627627627627628)) | |
(4, ('Slovenia', 0.2932551319648094)) | |
(5, ('Bahrain', 0.33557046979865773)) | |
(6, ('Malaysia', 0.33812341504649196)) | |
(7, ('Germany', 0.34774209128229894)) | |
(8, ('Norway', 0.3510531594784353)) | |
(9, ('Russia', 0.3952569169960474)) | |
(10, ('Ireland', 0.43923865300146414)) | |
(11, ('Singapore', 0.46296296296296297)) | |
(12, ('Panama', 0.5)) | |
(13, ('Pakistan', 0.5617977528089888)) | |
(14, ('Croatia', 0.5952380952380952)) | |
(15, ('Serbia', 0.6711409395973155)) | |
(16, ('Slovakia', 0.7299270072992701)) | |
(17, ('Portugal', 0.9375)) | |
(18, ('Switzerland', 0.9487976443644691)) | |
(19, ('Sweden', 0.9656004828002414)) | |
(20, ('Denmark', 0.9803921568627451)) | |
(21, ('Mexico', 0.9852216748768473)) | |
(22, ('Cruise Ship', 1.1235955056179776)) | |
(23, ('Brazil', 1.1258955987717503)) | |
(24, ('Poland', 1.1389521640091116)) | |
(25, ('Korea, South', 1.1592226389362428)) | |
(26, ('Luxembourg', 1.1940298507462686)) | |
(27, ('Canada', 1.2)) | |
(28, ('Taiwan*', 1.3071895424836601)) | |
(29, ('Australia', 1.3307984790874525)) | |
(30, ('Turkey', 1.3432835820895523)) | |
(31, ('India', 1.3605442176870748)) | |
(32, ('US', 1.4183623370247123)) | |
(33, ('United Arab Emirates', 1.4285714285714286)) | |
(34, ('Moldova', 1.5151515151515151)) | |
(35, ('Peru', 1.520912547528517)) | |
(36, ('Ecuador', 1.6431924882629108)) | |
(37, ('Costa Rica', 1.7699115044247788)) | |
(38, ('Tunisia', 1.8518518518518519)) | |
(39, ('Argentina', 1.8987341772151898)) | |
(40, ('Greece', 2.0202020202020203)) | |
(41, ('Bulgaria', 2.112676056338028)) | |
(42, ('Lebanon', 2.1390374331550803)) | |
(43, ('Azerbaijan', 2.272727272727273)) | |
(44, ('Belgium', 2.380106571936057)) | |
(45, ('Albania', 2.6315789473684212)) | |
(46, ('Dominican Republic', 2.7777777777777777)) | |
(47, ('Egypt', 2.807017543859649)) | |
(48, ('Burkina Faso', 3.125)) | |
(49, ('Martinique', 3.125)) | |
(50, ('Japan', 3.4756703078450846)) | |
(51, ('Morocco', 3.488372093023256)) | |
(52, ('Netherlands', 3.570236903570237)) | |
(53, ('France', 3.6118468576932337)) | |
(54, ('Hungary', 3.883495145631068)) | |
(55, ('China', 4.052021037188079)) | |
(56, ('Congo (Kinshasa)', 4.3478260869565215)) | |
(57, ('United Kingdom', 4.465629703963874)) | |
(58, ('Cuba', 4.761904761904762)) | |
(59, ('Spain', 5.418932765823284)) | |
(60, ('Paraguay', 5.555555555555555)) | |
(61, ('Philippines', 6.188925081433225)) | |
(62, ('Jamaica', 6.25)) | |
(63, ('Mauritius', 7.142857142857143)) | |
(64, ('Ukraine', 7.317073170731708)) | |
(65, ('Iran', 7.5497331392527895)) | |
(66, ('Bangladesh', 8.0)) | |
(67, ('Iraq', 8.173076923076923)) | |
(68, ('Guatemala', 8.333333333333334)) | |
(69, ('Indonesia', 8.444444444444445)) | |
(70, ('Italy', 8.574892069500862)) | |
(71, ('San Marino', 9.722222222222221)) | |
(72, ('Algeria', 11.702127659574469)) | |
(73, ('Guyana', 14.285714285714286)) | |
(74, ('Gabon', 25.0)) | |
(75, ('Sudan', 50.0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment