def write_dataframes(dataframes): for key, df in dataframes.items(): path = c('directories.export.data') + key + '.json' df.to_json(path, orient='records', date_format='iso') key = 'district-hoexter-ages' path = c('directories.export.data') + key + '.json' df.to_json(path, orient='records', date_format='iso')
def write_configuration(): path = c('directories.export.data') + 'configuration.json' with open(path, 'w') as outfile: json.dump(c(''), outfile) idents = c('names.communes') path = c('directories.export.data') + 'ident.json' with open(path, 'w') as outfile: json.dump(idents, outfile)
def concat(dfs): cdfs = {} for district in c('names.districts'): frames = [] keys = [] for commune, parent in c('parents.communes').items(): if parent == district: frames.append(dfs['commune-' + commune].transpose()) keys.append(commune) df = pd.concat(frames, keys=keys) cdfs['district-' + district] = df return cdfs
def run(): for key, name in c('names.districts').items(): df = load(key) df = shorten_df(df) df = process_manual_data(df, 'district', key) write_imported_dataframe(df, 'district-' + key)
def process_disctricts(dfs, cdfs, args): print('Districts argument list:', str(args)) for district in c('names.districts'): map.plot(dfs['district-' + district], cdfs['district-' + district], 'district', 'commune', district) for count in counts: StackedSupervisor(dfs, count, 'district', district).run() ComparisonSupervisor(cdfs, count, 'confirmed', 'district', 'commune', district, 'Absolute Fallzahlen', 'absolute-comparison').run() ComparisonSupervisor(cdfs, count, 'density_of_cases', 'district', 'commune', district, 'Relative Fallzahlen', 'relative-comparison').run() ComparisonSupervisor(cdfs, count, 'density_of_illness', 'district', 'commune', district, 'Infektionsrisiko', 'risc-comparison').run() ComparisonSupervisor(cdfs, count, 'last_weeks_incidence', 'district', 'commune', district, 'Wocheninzidenzen', 'incidence-comparison').run() IncidenceSupervisor(dfs, count, 'district', district).run() VerticalComparisonSupervisor(dfs, count, 'last_weeks_incidence', 'district', district, 'Risikovergleich', # 'Aktiv Infizierte je 100.000 Einwohner', 'Wocheninzidenz', 'vertical-risc-comparison').run()
def process_ages(dfs, args): print('Ages argument List:', str(args)) ages = dfs['district-hoexter-ages'] for key, group in ages.groupby(['Jahr', 'Monat']): AgesPlotter(group).run() name = 'district-hoexter-ages-' \ + str(key[0]) + '-' + str(key[1]) + '.png' path = c('directories.export.plots') + name plt.savefig(path) plt.close()
def process_communes(dfs, args): print('Communes argument List:', str(args)) if len(args) > 0: communes = args else: communes = c('names.communes') for commune in communes: for count in counts: StackedSupervisor(dfs, count, 'commune', commune).run() IncidenceSupervisor(dfs, count, 'commune', commune).run() VerticalComparisonSupervisor(dfs, count, 'last_weeks_incidence', 'commune', commune, 'Risikovergleich', 'Wocheninzidenz', 'vertical-risc-comparison').run()
def export_data(): source = c('directories.export.data') destination = c('directories.published.data') copy(source, destination)
def export_plots(): source = c('directories.export.plots') destination = c('directories.published.plots') copy(source, destination)
def load(district): path = c('files.raw.districts')[district] return pd.read_csv(path, sep=';')
def f(row): r = lakh * row.last_weeks_incidence / c('populations')[p(type)][key] return round(r, 3)
from urllib.request import urlopen import geopandas import pandas as pd from configuration import get as c import pathlib import datetime from lib import write_imported_dataframe cache = c('files.cache.rki_kreis_hoexter') url = "https://services7.arcgis.com/mOBPykOjAyBO2ZKk/arcgis/rest/services" \ "/RKI_COVID19/FeatureServer/0/query?where=IdLandkreis%20%3D%20'05762" \ "'&outFields=IdBundesland,Bundesland,Landkreis,Altersgruppe," \ "Geschlecht,AnzahlFall,AnzahlTodesfall,ObjectId,Meldedatum," \ "IdLandkreis,Datenstand,NeuerFall,NeuerTodesfall,Refdatum,NeuGenesen," \ "AnzahlGenesen,IstErkrankungsbeginn&returnGeometry=false&outSR=4326&f" \ "=json" data_download_interval_in_minutes = 600 def load(): def download(): with urlopen(url) as reader: content = reader.read().decode('utf-8') with open(cache, 'w') as writer: writer.write(content) def read_df(): data = geopandas.read_file(cache) data['Refdatum'] = pd.to_datetime(data['Refdatum'], unit='ms')
def density_of_cases(row): r = lakh * row.confirmed / c('populations')[p(type)][key] return round(r, 3)
def run(): for key, name in c('names.communes').items(): df = load('hoexter', key) df = shorten_df(df) df = process_manual_data(df, 'commune', key) write_imported_dataframe(df, 'commune-' + key)
def load_jhu_data(): df_confirmed = pd.read_csv(c('files.jhu_confirmed')) df_deaths = pd.read_csv(c('files.jhu_deaths')) df_recovered = pd.read_csv(c('files.jhu_recovered')) return df_confirmed, df_deaths, df_recovered
def run(): dfs = load_jhu_data() for key, names in c('names.countries').items(): df = process_jhu_data(*dfs, country=names['jhu'], key=key) write_imported_dataframe(df, 'country-' + key)
def read_children(self): for k, v in c('names')[p(self.child_type)].items(): self.keys.append(k) self.labels.append(v)
def density_of_death(row): r = lakh * row.dead / c('populations')[p(type)][key] return round(r, 3)
def process_countries(dfs, args): print('Countries argument list:', str(args)) for country in c('names.countries'): for count in counts: StackedSupervisor(dfs, count, 'country', country).run() IncidenceSupervisor(dfs, count, 'country', country).run()
def load(district, commune): path = c('directories.raw.districts')[district] path += 'district-' + commune + '.csv' return pd.read_csv(path, sep=';')
def plot(district_df, cdf, type, child_type, key): # load data def get_latest_incidences(): incidences = list() for k, v in c('names')[p(child_type)].items(): incidences.append((v, cdf.loc[(k, 'last_weeks_incidence')][-1])) return incidences df = pd.DataFrame( columns=['commune', 'last_weeks_incidence'], data=get_latest_incidences(), ) df['last_weeks_incidence'] = round(df['last_weeks_incidence'], 1) date = cdf.keys()[-1] # last date meta = district_df[district_df['date'] == date] if meta['STI-LZG'].notnull().bool(): total_incidence = meta['STI-LZG'] total_incidence_source = 'LZG NRW, amtlich' elif meta['STI-RKI'].notnull().bool(): total_incidence = meta['STI-RKI'] total_incidence_source = 'RKI, vorläufig' elif meta['STI-Kreis'].notnull().bool(): total_incidence = meta['STI-Kreis'] total_incidence_source = 'Kreis, vorläufig' else: total_incidence = None total_incidence_source = '' if total_incidence is not None: total_incidence = float(total_incidence) # load map file = c('files.shapefile') communes = geopandas.read_file(file) communes['Name'] = communes['GN'] # create layer dataframes communes = communes.set_index('GN').join(df.set_index('commune')) communes['helper'] = 0 district = communes.dissolve(by='helper') shadow = district.copy() shadow = shadow.translate(xoff=7000, yoff=2000) # create figure objects figure = plt.figure() axes = plt.axes() # start plotting # shadow if total_incidence is not None: shadow.plot( color=get_color(total_incidence), ax=axes, alpha=0.3, linewidth=0, ) shadow.boundary.plot(ax=axes, color='#aaa', linewidth=0.1) # communes district.plot(ax=axes, color='white', linewidth=0) # white background for # alpha communes.plot( norm=Normalize(0, len(colors) - 1), ax=axes, column='last_weeks_incidence', legend=False, scheme="user_defined", cmap=cmap, classification_kwds={'bins': bins}, alpha=0.88, linewidth=0, ) district.boundary.plot(ax=axes, color='#ddd', linewidth=0.3) # style figure.set_size_inches(mapsize) if total_incidence is not None: bbox_props = dict( boxstyle="round,pad=0.45", fc="white", # ec="black", ec=get_color(total_incidence), lw=0.3, alpha=0.7) plt.annotate( ('Kreis Höxter: ' + str(total_incidence).replace('.', ',') + ' (' + total_incidence_source + ')'), xy=(0.65, 0.78), xycoords='figure fraction', ha='center', bbox=bbox_props, size=10.5, color='#666', fontfamily='sans-serif', fontweight='bold', fontstyle='italic', ) bbox_props = dict(boxstyle="round,pad=0.35", fc="#484848", ec="white", lw=0.2, alpha=1) communes.apply( lambda x: axes.annotate( # ugly, but works for now text=x.Name + ': ' + str(x.last_weeks_incidence).replace('.', ','), size=9.5, xy=x.geometry.centroid.coords[0], ha='center', bbox=bbox_props, color='white', fontfamily='sans-serif', fontweight='bold', fontstyle='italic', ), axis=1) plt.suptitle('7-Tage-Inzidenzen im Kreis Höxter', fontsize=18, y=0.93) day = date.strftime("%d.%m.%Y") axes.set_title(label='je 100.000 Einwohnern am ' + day, fontsize=16) plt.axis('off') # go save(type, key, 'last_weeks_incidence-map')
def density_of_illness(row): r = lakh * row.ill / c('populations')[p(type)][key] return round(r, 3)
def get_latest_incidences(): incidences = list() for k, v in c('names')[p(child_type)].items(): incidences.append((v, cdf.loc[(k, 'last_weeks_incidence')][-1])) return incidences
def density_of_recoverance(row): r = lakh * row.recovered / c('populations')[p(type)][key] return round(r, 3)