1 #!//usr/bin/env python3
3 # District population numbers as per Wikipedia.
20 f = open('daily_infections_table.txt', 'r')
24 # Parse first table file line for the names and order of districts.
27 for header in lines[0].split():
28 sorted_districts += [header]
31 # Seed DB with daily new infections data per district, per date.
33 for line in lines[1:]:
36 sorted_dates += [date]
37 for i in range(len(sorted_districts)):
38 district = sorted_districts[i]
39 district_data = fields[i + 1]
40 db[district][date] = {'new_infections': int(district_data)}
43 # In LaGeSo's data, the last "district" is actually the sum of all districts /
44 # the whole of Berlin.
46 # Fail on any day where the "sum" district's new infections are not the proper
47 # sum of the individual districts new infections. Yes, sometimes Lageso sends
48 # data that is troubled in this way. It will then have to be fixed manually in
49 # the table file, since we should have a human look at what mistake was
51 for date in sorted_dates:
52 sum_district = sorted_districts[-1]
54 for district in sorted_districts[:-1]:
55 day_sum += db[district][date]['new_infections']
56 if day_sum != db[sum_district][date]['new_infections']:
57 raise Exception('Questionable district infection sum in %s' % date)
59 # Enhance DB with data about weekly sums, averages, incidences per day. Ignore
60 # days that have less than 6 predecessors (we can only know a weekly average if
61 # we have a whole week of data).
62 for i in range(len(sorted_dates)):
65 date = sorted_dates[i]
68 week_dates += [sorted_dates[i - j]]
69 for district in sorted_districts:
70 district_pop = district_pops[district]
72 for week_date in week_dates:
73 week_sum += db[district][week_date]['new_infections']
74 db[district][date]['week_sum'] = week_sum
75 db[district][date]['week_average'] = week_sum / 7
76 db[district][date]['week_incidence'] = (week_sum / district_pop) * 100000
78 # Explain what this is.
79 intro = """Table of Berlin's Corona infection number development by districts.
80 Updated daily around 9pm.
82 Abbrevations/explanations:
84 CW: Charlottenburg-Wilmersdorf
85 FK: Friedrichshain-Kreuzberg
87 MH: Marzahn-Hellersdorf
93 SZ: Steglitz-Zehlendorf
94 TS: Tempelhof-Schöneberg
96 sum: sum for all the districts
97 wsum: sum for last 7 days
98 wavg: per-day average of new infections for last 7 days
99 winc: incidence (x per 100k inhabitants) of new infections for last 7 days
101 Source code: https://plomlompom.com/repos/?p=berlin-corona-table
105 # Output table of enhanced daily infection data, newest on top, separated into
107 sorted_dates.reverse()
109 for date in sorted_dates:
112 if weekday_count == 0:
113 print(' '*11, ' '.join(sorted_districts[:-1]),
114 sorted_districts[-1], 'wsum', ' wavg', 'winc')
115 week_start_date = date
119 for district in sorted_districts:
120 new_infections += [db[district][date]['new_infections']]
121 week_sum = week_avg = week_inc = ''
122 sum_district = sorted_districts[-1]
123 sum_district_data = db[sum_district][date]
124 if 'week_sum' in sum_district_data:
125 week_sum = '%4s' % sum_district_data['week_sum']
126 if 'week_average' in sum_district_data:
127 week_avg = '%5.1f' % sum_district_data['week_average']
128 if 'week_incidence' in sum_district_data:
129 week_inc = '%4.1f' % sum_district_data['week_incidence']
130 print(date, ' '.join(['%3s' % infections for infections in new_infections]),
131 week_sum, week_avg, week_inc)
133 # Maintain 7-day cycle.
135 if weekday_count != 7:
139 # After each 7 days, print summary for individual districts.
143 for district in sorted_districts[:-1]:
144 weekly_sums += [db[district][week_start_date]['week_sum']]
145 weekly_avgs += [db[district][week_start_date]['week_average']]
146 weekly_incs += [db[district][week_start_date]['week_incidence']]
148 print('district stats for week from %s to %s:' % (date, week_start_date))
149 print(' '*7, ' '.join(sorted_districts[:-1]))
150 print('wsum', ' '.join(['%5.1f' % wsum for wsum in weekly_sums]))
151 print('wavg', ' '.join(['%5.1f' % wavg for wavg in weekly_avgs]))
152 print('winc', ' '.join(['%5.1f' % winc for winc in weekly_incs]))