1 #!//usr/bin/env python3
3 # District population numbers as per Wikipedia.
20 # Map abbreviations to full names.
22 'CW': 'Charlottenburg-Wilmersdorf',
23 'FK': 'Friedrichshain-Kreuzberg',
25 'MH': 'Marzahn-Hellersdorf',
29 'Re': 'Reinickendorf',
31 'SZ': 'Steglitz-Zehlendorf',
32 'TS': 'Tempelhof-Schöneberg',
33 'TK': 'Treptow-Köpenick',
34 'sum': 'all of Berlin',
35 'wsum': 'sum for last 7 days',
36 'wavg': 'per-day average of new infections for last 7 days',
37 'winc': 'incidence (x per 100k inhabitants) of new infections for last 7 days',
40 # Read infections table path and output type.
42 if len(sys.argv) != 3:
43 print('Expecting infections table file path and output type as only arguments.')
45 infections_table = sys.argv[1]
46 output_type = sys.argv[2]
48 # Read infections table file lines.
49 f = open(infections_table, 'r')
53 # Basic input validation.
55 header_elements = lines[0].split()
56 if set(header_elements) != district_pops.keys() or \
57 len(header_elements) != len(district_pops.keys()):
58 raise Exception('infections table: invalid header')
60 for line in lines[1:]:
63 if len(header_elements) != len(fields) - 1:
64 raise Exception('infections table: too many elements on line %s',
67 datetime.date.fromisoformat(fields[0])
69 raise Exception('infections table: bad ISO date on line %s',
71 for field in fields[1:]:
75 raise Exception('infections table: bad value on line %s',
78 # Parse first table file line for the names and order of districts.
81 for header in lines[0].split():
82 sorted_districts += [header]
85 # Seed DB with daily new infections data per district, per date.
87 for line in lines[1:]:
90 sorted_dates += [date]
91 for i in range(len(sorted_districts)):
92 district = sorted_districts[i]
93 district_data = fields[i + 1]
94 db[district][date] = {'new_infections': int(district_data)}
97 # In LaGeSo's data, the last "district" is actually the sum of all districts /
98 # the whole of Berlin.
100 # Fail on any day where the "sum" district's new infections are not the proper
101 # sum of the individual districts new infections. Yes, sometimes Lageso sends
102 # data that is troubled in this way. It will then have to be fixed manually in
103 # the table file, since we should have a human look at what mistake was
105 for date in sorted_dates:
106 sum_district = sorted_districts[-1]
108 for district in sorted_districts[:-1]:
109 day_sum += db[district][date]['new_infections']
110 if day_sum != db[sum_district][date]['new_infections']:
111 raise Exception('Questionable district infection sum in %s' % date)
113 # Enhance DB with data about weekly sums, averages, incidences per day. Ignore
114 # days that have less than 6 predecessors (we can only know a weekly average if
115 # we have a whole week of data).
116 for i in range(len(sorted_dates)):
119 date = sorted_dates[i]
122 week_dates += [sorted_dates[i - j]]
123 for district in sorted_districts:
124 district_pop = district_pops[district]
126 for week_date in week_dates:
127 week_sum += db[district][week_date]['new_infections']
128 db[district][date]['week_sum'] = week_sum
129 db[district][date]['week_average'] = week_sum / 7
130 db[district][date]['week_incidence'] = (week_sum / district_pop) * 100000
132 # Optimized for web browser viewing.
133 if output_type == 'html':
136 print('table, tr, th, td { border: 1px solid black; }')
137 print('.day_row:nth-child(7n+2) { background-color: yellow; }')
138 print('.district_name { writing-mode: vertical-rl; '
139 'transform: rotate(180deg); }')
143 print('<th>date</th>')
144 sorted_dates.reverse()
145 sum_district = sorted_districts[-1]
146 for district in sorted_districts:
147 long_form = translate[district]
148 if sum_district == district:
149 print('<th>%s</th>' % long_form)
151 print('<th class="district_name">%s</th>' % long_form)
153 for date in sorted_dates:
154 print('<tr class="day_row">')
155 print('<td>%s</td>' % date)
156 long_wsum = translate['wsum']
157 long_wavg = translate['wavg']
158 long_winc = translate['winc']
159 for district in sorted_districts:
160 district_data = db[district][date]
161 week_sum = week_avg = week_inc = ''
162 new_infections = district_data['new_infections']
163 if 'week_sum' in district_data:
164 week_sum = '%s' % district_data['week_sum']
165 if 'week_average' in district_data:
166 week_avg = '%.1f' % district_data['week_average']
167 if 'week_incidence' in district_data:
168 week_inc = '%.1f' % district_data['week_incidence']
170 print(new_infections)
171 if district == sum_district:
172 print('<details open><summary></summary><table>')
174 print('<details><summary></summary><table>')
175 print('<tr><th>%s</th><td>%s</td></tr>' % (long_wsum, week_sum))
176 print('<tr><th>%s</th><td>%s</td></tr>' % (long_wavg, week_avg))
177 print('<tr><th>%s</th><td>%s</td></tr>' % (long_winc, week_inc))
178 print('</table></details>')
184 # Optimized for in-terminal curl.
185 elif output_type == 'txt':
187 # Explain what this is.
189 """Table of Berlin's Corona infection number development by districts.
190 Updated daily around 9pm.
192 Abbrevations/explanations:
195 intro += "%s: %s\n" % (k, translate[k])
197 Source code: https://plomlompom.com/repos/?p=berlin-corona-table
201 # Output table of enhanced daily infection data, newest on top,
202 # separated into 7-day units.
203 sorted_dates.reverse()
205 sum_district = sorted_districts[-1]
206 for date in sorted_dates:
209 if weekday_count == 0:
210 print(' '*11, ' '.join(sorted_districts[:-1]),
211 sorted_districts[-1], 'wsum', ' wavg', 'winc')
212 week_start_date = date
216 for district in sorted_districts:
217 new_infections += [db[district][date]['new_infections']]
218 week_sum = week_avg = week_inc = ''
219 sum_district_data = db[sum_district][date]
220 if 'week_sum' in sum_district_data:
221 week_sum = '%4s' % sum_district_data['week_sum']
222 if 'week_average' in sum_district_data:
223 week_avg = '%5.1f' % sum_district_data['week_average']
224 if 'week_incidence' in sum_district_data:
225 week_inc = '%4.1f' % sum_district_data['week_incidence']
226 print(date, ' '.join(['%3s' % infections
227 for infections in new_infections]),
228 week_sum, week_avg, week_inc)
230 # Maintain 7-day cycle.
232 if weekday_count != 7:
236 # After each 7 days, print summary for individual districts.
240 for district in sorted_districts[:-1]:
241 weekly_sums += [db[district][week_start_date]['week_sum']]
242 weekly_avgs += [db[district][week_start_date]['week_average']]
243 weekly_incs += [db[district][week_start_date]['week_incidence']]
245 print('district stats for week from %s to %s:' % (date, week_start_date))
246 print(' '*7, ' '.join(sorted_districts[:-1]))
247 print('wsum', ' '.join(['%5.1f' % wsum for wsum in weekly_sums]))
248 print('wavg', ' '.join(['%5.1f' % wavg for wavg in weekly_avgs]))
249 print('winc', ' '.join(['%5.1f' % winc for winc in weekly_incs]))