1 #!//usr/bin/env python3
5 print('Expecting infections table file path as only argument.')
7 infections_table = sys.argv[1]
9 # District population numbers as per Wikipedia.
26 f = open(infections_table, 'r')
30 # Basic input validation.
32 header_elements = lines[0].split()
33 if set(header_elements) != district_pops.keys() or \
34 len(header_elements) != len(district_pops.keys()):
35 raise Exception('infections table: invalid header')
37 for line in lines[1:]:
40 if len(header_elements) != len(fields) - 1:
41 raise Exception('infections table: too many elements on line %s',
44 datetime.date.fromisoformat(fields[0])
46 raise Exception('infections table: bad ISO date on line %s',
48 for field in fields[1:]:
52 raise Exception('infections table: bad value on line %s',
55 # Parse first table file line for the names and order of districts.
58 for header in lines[0].split():
59 sorted_districts += [header]
62 # Seed DB with daily new infections data per district, per date.
64 for line in lines[1:]:
67 sorted_dates += [date]
68 for i in range(len(sorted_districts)):
69 district = sorted_districts[i]
70 district_data = fields[i + 1]
71 db[district][date] = {'new_infections': int(district_data)}
74 # In LaGeSo's data, the last "district" is actually the sum of all districts /
75 # the whole of Berlin.
77 # Fail on any day where the "sum" district's new infections are not the proper
78 # sum of the individual districts new infections. Yes, sometimes Lageso sends
79 # data that is troubled in this way. It will then have to be fixed manually in
80 # the table file, since we should have a human look at what mistake was
82 for date in sorted_dates:
83 sum_district = sorted_districts[-1]
85 for district in sorted_districts[:-1]:
86 day_sum += db[district][date]['new_infections']
87 if day_sum != db[sum_district][date]['new_infections']:
88 raise Exception('Questionable district infection sum in %s' % date)
90 # Enhance DB with data about weekly sums, averages, incidences per day. Ignore
91 # days that have less than 6 predecessors (we can only know a weekly average if
92 # we have a whole week of data).
93 for i in range(len(sorted_dates)):
96 date = sorted_dates[i]
99 week_dates += [sorted_dates[i - j]]
100 for district in sorted_districts:
101 district_pop = district_pops[district]
103 for week_date in week_dates:
104 week_sum += db[district][week_date]['new_infections']
105 db[district][date]['week_sum'] = week_sum
106 db[district][date]['week_average'] = week_sum / 7
107 db[district][date]['week_incidence'] = (week_sum / district_pop) * 100000
109 # Explain what this is.
110 intro = """Table of Berlin's Corona infection number development by districts.
111 Updated daily around 9pm.
113 Abbrevations/explanations:
115 CW: Charlottenburg-Wilmersdorf
116 FK: Friedrichshain-Kreuzberg
118 MH: Marzahn-Hellersdorf
124 SZ: Steglitz-Zehlendorf
125 TS: Tempelhof-Schöneberg
127 sum: sum for all the districts
128 wsum: sum for last 7 days
129 wavg: per-day average of new infections for last 7 days
130 winc: incidence (x per 100k inhabitants) of new infections for last 7 days
132 Source code: https://plomlompom.com/repos/?p=berlin-corona-table
136 # Output table of enhanced daily infection data, newest on top, separated into
138 sorted_dates.reverse()
140 for date in sorted_dates:
143 if weekday_count == 0:
144 print(' '*11, ' '.join(sorted_districts[:-1]),
145 sorted_districts[-1], 'wsum', ' wavg', 'winc')
146 week_start_date = date
150 for district in sorted_districts:
151 new_infections += [db[district][date]['new_infections']]
152 week_sum = week_avg = week_inc = ''
153 sum_district = sorted_districts[-1]
154 sum_district_data = db[sum_district][date]
155 if 'week_sum' in sum_district_data:
156 week_sum = '%4s' % sum_district_data['week_sum']
157 if 'week_average' in sum_district_data:
158 week_avg = '%5.1f' % sum_district_data['week_average']
159 if 'week_incidence' in sum_district_data:
160 week_inc = '%4.1f' % sum_district_data['week_incidence']
161 print(date, ' '.join(['%3s' % infections for infections in new_infections]),
162 week_sum, week_avg, week_inc)
164 # Maintain 7-day cycle.
166 if weekday_count != 7:
170 # After each 7 days, print summary for individual districts.
174 for district in sorted_districts[:-1]:
175 weekly_sums += [db[district][week_start_date]['week_sum']]
176 weekly_avgs += [db[district][week_start_date]['week_average']]
177 weekly_incs += [db[district][week_start_date]['week_incidence']]
179 print('district stats for week from %s to %s:' % (date, week_start_date))
180 print(' '*7, ' '.join(sorted_districts[:-1]))
181 print('wsum', ' '.join(['%5.1f' % wsum for wsum in weekly_sums]))
182 print('wavg', ' '.join(['%5.1f' % wavg for wavg in weekly_avgs]))
183 print('winc', ' '.join(['%5.1f' % winc for winc in weekly_incs]))