1 #!//usr/bin/env python3
3 # District population numbers as per Wikipedia.
20 # Read infections table path and output type.
22 if len(sys.argv) != 3:
23 print('Expecting infections table file path and output type as only arguments.')
25 infections_table = sys.argv[1]
26 output_type = sys.argv[2]
28 # Read infections table file lines.
29 f = open(infections_table, 'r')
33 # Basic input validation.
35 header_elements = lines[0].split()
36 if set(header_elements) != district_pops.keys() or \
37 len(header_elements) != len(district_pops.keys()):
38 raise Exception('infections table: invalid header')
40 for line in lines[1:]:
43 if len(header_elements) != len(fields) - 1:
44 raise Exception('infections table: too many elements on line %s',
47 datetime.date.fromisoformat(fields[0])
49 raise Exception('infections table: bad ISO date on line %s',
51 for field in fields[1:]:
55 raise Exception('infections table: bad value on line %s',
58 # Parse first table file line for the names and order of districts.
61 for header in lines[0].split():
62 sorted_districts += [header]
65 # Seed DB with daily new infections data per district, per date.
67 for line in lines[1:]:
70 sorted_dates += [date]
71 for i in range(len(sorted_districts)):
72 district = sorted_districts[i]
73 district_data = fields[i + 1]
74 db[district][date] = {'new_infections': int(district_data)}
77 # In LaGeSo's data, the last "district" is actually the sum of all districts /
78 # the whole of Berlin.
80 # Fail on any day where the "sum" district's new infections are not the proper
81 # sum of the individual districts new infections. Yes, sometimes Lageso sends
82 # data that is troubled in this way. It will then have to be fixed manually in
83 # the table file, since we should have a human look at what mistake was
85 for date in sorted_dates:
86 sum_district = sorted_districts[-1]
88 for district in sorted_districts[:-1]:
89 day_sum += db[district][date]['new_infections']
90 if day_sum != db[sum_district][date]['new_infections']:
91 raise Exception('Questionable district infection sum in %s' % date)
93 # Enhance DB with data about weekly sums, averages, incidences per day. Ignore
94 # days that have less than 6 predecessors (we can only know a weekly average if
95 # we have a whole week of data).
96 for i in range(len(sorted_dates)):
99 date = sorted_dates[i]
102 week_dates += [sorted_dates[i - j]]
103 for district in sorted_districts:
104 district_pop = district_pops[district]
106 for week_date in week_dates:
107 week_sum += db[district][week_date]['new_infections']
108 db[district][date]['week_sum'] = week_sum
109 db[district][date]['week_average'] = week_sum / 7
110 db[district][date]['week_incidence'] = (week_sum / district_pop) * 100000
112 # Optimized for web browser viewing.
113 if output_type == 'html':
116 print('table, tr, th, td { border: 1px solid black; }')
120 print('<th>date</th>')
121 for district in sorted_districts:
122 print('<th>%s</th>' % district)
124 sorted_dates.reverse()
125 for date in sorted_dates:
127 print('<td>%s</td>' % date)
128 for district in sorted_districts:
129 district_data = db[district][date]
130 week_sum = week_avg = week_inc = ''
131 new_infections = district_data['new_infections']
132 if 'week_sum' in district_data:
133 week_sum = '%s' % district_data['week_sum']
134 if 'week_average' in district_data:
135 week_avg = '%.1f' % district_data['week_average']
136 if 'week_incidence' in district_data:
137 week_inc = '%.1f' % district_data['week_incidence']
140 print('<tr><th>new</th><td>%s</td></tr>' % new_infections)
141 print('<tr><th>wsum</th><td>%s</td></tr>' % week_sum)
142 print('<tr><th>wavg</th><td>%s</td></tr>' % week_avg)
143 print('<tr><th>winc</th><td>%s</td></tr>' % week_inc)
150 # Optimized for in-terminal curl.
151 elif output_type == 'txt':
153 # Explain what this is.
155 """Table of Berlin's Corona infection number development by districts.
156 Updated daily around 9pm.
158 Abbrevations/explanations:
160 CW: Charlottenburg-Wilmersdorf
161 FK: Friedrichshain-Kreuzberg
163 MH: Marzahn-Hellersdorf
169 SZ: Steglitz-Zehlendorf
170 TS: Tempelhof-Schöneberg
172 sum: sum for all the districts
173 wsum: sum for last 7 days
174 wavg: per-day average of new infections for last 7 days
175 winc: incidence (x per 100k inhabitants) of new infections for last 7 days
177 Source code: https://plomlompom.com/repos/?p=berlin-corona-table
181 # Output table of enhanced daily infection data, newest on top,
182 # separated into 7-day units.
183 sorted_dates.reverse()
185 sum_district = sorted_districts[-1]
186 for date in sorted_dates:
189 if weekday_count == 0:
190 print(' '*11, ' '.join(sorted_districts[:-1]),
191 sorted_districts[-1], 'wsum', ' wavg', 'winc')
192 week_start_date = date
196 for district in sorted_districts:
197 new_infections += [db[district][date]['new_infections']]
198 week_sum = week_avg = week_inc = ''
199 sum_district_data = db[sum_district][date]
200 if 'week_sum' in sum_district_data:
201 week_sum = '%4s' % sum_district_data['week_sum']
202 if 'week_average' in sum_district_data:
203 week_avg = '%5.1f' % sum_district_data['week_average']
204 if 'week_incidence' in sum_district_data:
205 week_inc = '%4.1f' % sum_district_data['week_incidence']
206 print(date, ' '.join(['%3s' % infections
207 for infections in new_infections]),
208 week_sum, week_avg, week_inc)
210 # Maintain 7-day cycle.
212 if weekday_count != 7:
216 # After each 7 days, print summary for individual districts.
220 for district in sorted_districts[:-1]:
221 weekly_sums += [db[district][week_start_date]['week_sum']]
222 weekly_avgs += [db[district][week_start_date]['week_average']]
223 weekly_incs += [db[district][week_start_date]['week_incidence']]
225 print('district stats for week from %s to %s:' % (date, week_start_date))
226 print(' '*7, ' '.join(sorted_districts[:-1]))
227 print('wsum', ' '.join(['%5.1f' % wsum for wsum in weekly_sums]))
228 print('wavg', ' '.join(['%5.1f' % wavg for wavg in weekly_avgs]))
229 print('winc', ' '.join(['%5.1f' % winc for winc in weekly_incs]))