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',
37 # Read infections table path and output type.
39 if len(sys.argv) != 3:
40 print('Expecting infections table file path and output type as only arguments.')
42 infections_table = sys.argv[1]
43 output_type = sys.argv[2]
45 # Read infections table file lines.
46 f = open(infections_table, 'r')
50 # Basic input validation.
52 header_elements = lines[0].split()
53 if set(header_elements) != district_pops.keys() or \
54 len(header_elements) != len(district_pops.keys()):
55 raise Exception('infections table: invalid header')
57 for line in lines[1:]:
60 if len(header_elements) != len(fields) - 1:
61 raise Exception('infections table: too many elements on line %s',
64 datetime.date.fromisoformat(fields[0])
66 raise Exception('infections table: bad ISO date on line %s',
68 for field in fields[1:]:
72 raise Exception('infections table: bad value on line %s',
75 # Parse first table file line for the names and order of districts.
78 for header in lines[0].split():
79 sorted_districts += [header]
82 # Seed DB with daily new infections data per district, per date.
84 for line in lines[1:]:
87 sorted_dates += [date]
88 for i in range(len(sorted_districts)):
89 district = sorted_districts[i]
90 district_data = fields[i + 1]
91 db[district][date] = {'new_infections': int(district_data)}
94 # In LaGeSo's data, the last "district" is actually the sum of all districts /
95 # the whole of Berlin.
97 # Fail on any day where the "sum" district's new infections are not the proper
98 # sum of the individual districts new infections. Yes, sometimes Lageso sends
99 # data that is troubled in this way. It will then have to be fixed manually in
100 # the table file, since we should have a human look at what mistake was
102 for date in sorted_dates:
103 sum_district = sorted_districts[-1]
105 for district in sorted_districts[:-1]:
106 day_sum += db[district][date]['new_infections']
107 if day_sum != db[sum_district][date]['new_infections']:
108 raise Exception('Questionable district infection sum in %s' % date)
110 # Enhance DB with data about weekly sums, averages, incidences per day. Ignore
111 # days that have less than 6 predecessors (we can only know a weekly average if
112 # we have a whole week of data).
113 for i in range(len(sorted_dates)):
116 date = sorted_dates[i]
119 week_dates += [sorted_dates[i - j]]
120 for district in sorted_districts:
121 district_pop = district_pops[district]
123 for week_date in week_dates:
124 week_sum += db[district][week_date]['new_infections']
125 db[district][date]['week_sum'] = week_sum
126 db[district][date]['week_average'] = week_sum / 7
127 db[district][date]['week_incidence'] = (week_sum / district_pop) * 100000
129 # Optimized for web browser viewing.
130 if output_type == 'html':
133 print('table, tr, th, td { border: 1px solid black; }')
137 print('<th>date</th>')
138 for district in sorted_districts:
139 long_form = translate[district]
140 print('<th style="writing-mode: vertical-rl; '
141 'transform: rotate(180deg);">%s</th>' % long_form)
143 sorted_dates.reverse()
144 for date in sorted_dates:
146 print('<td>%s</td>' % date)
147 for district in sorted_districts:
148 district_data = db[district][date]
149 week_sum = week_avg = week_inc = ''
150 new_infections = district_data['new_infections']
151 if 'week_sum' in district_data:
152 week_sum = '%s' % district_data['week_sum']
153 if 'week_average' in district_data:
154 week_avg = '%.1f' % district_data['week_average']
155 if 'week_incidence' in district_data:
156 week_inc = '%.1f' % district_data['week_incidence']
159 print('<tr><th>new</th><td>%s</td></tr>' % new_infections)
160 print('<tr><th>wsum</th><td>%s</td></tr>' % week_sum)
161 print('<tr><th>wavg</th><td>%s</td></tr>' % week_avg)
162 print('<tr><th>winc</th><td>%s</td></tr>' % week_inc)
169 # Optimized for in-terminal curl.
170 elif output_type == 'txt':
172 # Explain what this is.
174 """Table of Berlin's Corona infection number development by districts.
175 Updated daily around 9pm.
177 Abbrevations/explanations:
180 intro += "%s: %s\n" % (k, translate[k])
181 intro += """wsum: sum for last 7 days
182 wavg: per-day average of new infections for last 7 days
183 winc: incidence (x per 100k inhabitants) of new infections for last 7 days
185 Source code: https://plomlompom.com/repos/?p=berlin-corona-table
189 # Output table of enhanced daily infection data, newest on top,
190 # separated into 7-day units.
191 sorted_dates.reverse()
193 sum_district = sorted_districts[-1]
194 for date in sorted_dates:
197 if weekday_count == 0:
198 print(' '*11, ' '.join(sorted_districts[:-1]),
199 sorted_districts[-1], 'wsum', ' wavg', 'winc')
200 week_start_date = date
204 for district in sorted_districts:
205 new_infections += [db[district][date]['new_infections']]
206 week_sum = week_avg = week_inc = ''
207 sum_district_data = db[sum_district][date]
208 if 'week_sum' in sum_district_data:
209 week_sum = '%4s' % sum_district_data['week_sum']
210 if 'week_average' in sum_district_data:
211 week_avg = '%5.1f' % sum_district_data['week_average']
212 if 'week_incidence' in sum_district_data:
213 week_inc = '%4.1f' % sum_district_data['week_incidence']
214 print(date, ' '.join(['%3s' % infections
215 for infections in new_infections]),
216 week_sum, week_avg, week_inc)
218 # Maintain 7-day cycle.
220 if weekday_count != 7:
224 # After each 7 days, print summary for individual districts.
228 for district in sorted_districts[:-1]:
229 weekly_sums += [db[district][week_start_date]['week_sum']]
230 weekly_avgs += [db[district][week_start_date]['week_average']]
231 weekly_incs += [db[district][week_start_date]['week_incidence']]
233 print('district stats for week from %s to %s:' % (date, week_start_date))
234 print(' '*7, ' '.join(sorted_districts[:-1]))
235 print('wsum', ' '.join(['%5.1f' % wsum for wsum in weekly_sums]))
236 print('wavg', ' '.join(['%5.1f' % wavg for wavg in weekly_avgs]))
237 print('winc', ' '.join(['%5.1f' % winc for winc in weekly_incs]))