def retrieve_hosp_data(): countries = parse_countries(2) hospData = {} # For now, always get the data from yesterday. We could make fancier check if today's data is already available file_name, headers = urlretrieve(URL_Hosp) workbook = xlrd.open_workbook(file_name) worksheet = workbook.sheet_by_name('Data (table)') i = 0 Ix = {} for c in worksheet.row_values(0): Ix[c] = i i += 1 for row_index in range(1, worksheet.nrows): row = worksheet.row_values(row_index) country = row[Ix['COUNTRY_REGION']] if country in countries: country = countries[country] else: #print(f'could not find country {country}') continue hospData[country] = {'hospitalBeds': stoi(row[Ix['VALUE']]), 'srcHospitalBeds': 'WHO Hospital data from https://gateway.euro.who.int/en/indicators/hfa_479-5061-number-of-acute-care-hospital-beds/'} # second data set, for Asia with open('hospital-data/ICU_asia.tsv', 'r') as fd: rdr = csv.reader(fd, delimiter='\t') hdr = next(rdr) for row in rdr: country = row[0].strip() if stoi(row[1]): hospData[country] = {'hospitalBeds': stoi(row[1]), 'srcHospitalBeds': URL_ICU_ASIA} # Eurostat, newer than from WHO (2017 vs 2015) with open('hospital-data/hospital_eurostat.tsv', 'r') as fd: rdr = csv.reader(fd, delimiter='\t') hdr = next(rdr) for row in rdr: country = row[0].strip() if not country in hospData: hospData[country] = {'hospitalBeds': stoi(row[1]), 'srcHospitalBeds': URL_Hosp_Eurostat} else: #print(f'found new data for {country}: old: {hospData[country]["hospitalBeds"]}, new: {stoi(row[1])}') hospData[country] = {'hospitalBeds': stoi(row[1]), 'srcHospitalBeds': URL_Hosp_Eurostat} return hospData
def retrieve_pop_data(): countries = parse_countries(2) countries1 = parse_countries(1) countries['XKX'] = countries['RKS'] countries1['BLM'] = countries1['BL'] popData = {} file_name, headers = urlretrieve(URL_Pop) workbook = xlrd.open_workbook(file_name) worksheet = workbook.sheet_by_index(0) i = 0 Ix = {} for c in worksheet.row_values(0): Ix[c] = i i += 1 for row_index in range(1, worksheet.nrows): row = worksheet.row_values(row_index) popData2018 = stoi(row[Ix['popData2018']]) # replace country name if we have the "official" one in country_codes.csv country = row[Ix['countryterritoryCode']] if country == [] or not country: country = countries1[row[Ix['geoId']]] elif country in countries: country = countries[country] else: #print(f'could not find country {country}') continue popData[country] = {'populationServed': popData2018, 'srcPopulation': 'ECDC'} return popData
def update_cia_facts(hospData, popData): r = get(URL_CIA) if not r.ok: print(f"Failed to fetch {URL}", file=sys.stderr) exit(1) db = json.loads(r.text) countries = parse_countries(2) for country in db["countries"]: country2 = None matches = [r for s,r in zip([x.lower() for x in countries.values()],[x for x in countries.values()]) if country.replace("_"," ") in s] if len(matches) == 0: # manual map to compensate if country in ciaMap: country2 = ciaMap[country] else: #print(f'no match found for {country}') continue elif len(matches) == 1: country2 = matches[0] else: # only few cases, handle manually for n in matches: if country.replace("_"," ") == n.lower(): country2 = n if country == 'united_states': country2 = 'United States of America' elif country == 'virgin_islands': country2 = 'Virgin Islands (U.S.)' if not country2: print(f'{country} matches {matches}') continue if not 'people' in db['countries'][country]['data']: continue population = stoi(db["countries"][country]["data"]["people"]["population"]["total"]) source = db["countries"][country]["metadata"]["source"] if not country2 in hospData and 'hospital_bed_density' in db['countries'][country]['data']['people']: hospData[country2] = {} hospData[country2]['hospitalBeds'] = int(population/1000*float(db['countries'][country]['data']['people']['hospital_bed_density']['beds_per_1000_population'])) if 'date' in db['countries'][country]['data']['people']['hospital_bed_density']: hospData[country2]['srcHospitalBeds'] = f"Computed from {db['countries'][country]['data']['people']['hospital_bed_density']['date']} data in CIA fact book {source}" else: hospData[country2]['srcHospitalBeds'] = f"Computed from data in CIA fact book {source}" if not country2 in popData and 'population' in db['countries'][country]['data']['people']: popData[country2] = {} popData[country2]['populationServed'] = population if 'date' in db['countries'][country]['data']['people']['population']: popData[country2]['srcPopulation'] = f"{db['countries'][country]['data']['people']['population']['date']} data in CIA fact book {source}" else: popData[country2]['srcPopulation'] = f"Data in CIA fact book {source}" return hospData, popData
def retrieve_icu_data(): icuData = {} with open('hospital-data/ICU_capacity.tsv', 'r') as fd: rdr = csv.reader(fd, delimiter='\t') hdr = next(rdr) for row in rdr: country = row[0].strip() icuData[country] = {'ICUBeds': stoi(row[5]), 'srcICUBeds': URL_ICU} # second data set, for Asia with open('hospital-data/ICU_asia.tsv', 'r') as fd: rdr = csv.reader(fd, delimiter='\t') hdr = next(rdr) for row in rdr: country = row[0].strip() icuData[country] = {'ICUBeds': stoi(row[5]), 'srcICUBeds': URL_ICU_ASIA} return icuData
def parse_tsv(tsv, location): rdr = csv.reader(tsv, delimiter='\t') hdr = next(rdr) idx = {} for col in cols: try: idx[col] = hdr.index(col) except: print(col, cols, hdr) return None, False data = defaultdict(list) for row in rdr: data[location].append({c:stoi(row[idx[c]]) if i > 0 else row[idx[c]] for i, c in enumerate(cols)}) return data, True
def get_old_data(): known = {} with open('populationData.tsv', 'r') as fd: rdr = csv.reader(fd, delimiter='\t') hdr = next(rdr) i = 0 Ix = {} for c in hdr: Ix[c] = i i += 1 for row in rdr: name = row[0] if not name in known: known[name] = {} for n in Ix: if n == 'name': continue if 'name' in n or 'ageDistribution' in n or 'hemisphere' in n or 'src' in n: known[name][n] = row[Ix[n]] else: known[name][n] = stoi(row[Ix[n]]) return known