@owid-covid.html
001| """OWID COVID Data.
001|
001| Commands:
001| c0 do_chart_0
001| c1 do_chart_1
001| c2 do_chart_2
001| c3 do_chart_3
001| c4 do_chart_4
001| c5 do_chart_5
001| c6 do_chart_6
001| c7 do_chart_7
001| c8 do_chart_8
001| c9 do_chart_9
001|
001| all <all the above>
001|
001| pie do_chart_pie
001| rank do_percentage_ranking
001| list do_owid_list
001|
001| test do_test
001| """
002|
003| from sys import argv, exc_info
004| from traceback import extract_tb
005| from os import path
006| from datetime import date, datetime
007| import matplotlib.pyplot as plt
008| from matplotlib.ticker import NullLocator, MultipleLocator, NullFormatter, FormatStrFormatter
009| from matplotlib.dates import DayLocator, DateFormatter
010| from logger import logger, info, debug, trace
011| from files import linefile, csvfile
012| from goodies import KWARG
013|
014| BasePath = r'C:\CJS\prj\Python\blog\proj'
015| DataPath = path.join(BasePath,'dat')
016| ImgsPath = path.join(BasePath,'img')
017| DataName = r'owid-covid-data.csv'
018| Log = logger('COVID')
019|
020| CMND = ''
021| ARGS = {
022| 'cc1':'USA',
023| 'cc2':'BRA',
024| 'cc3':'IND',
025| 'cc4':'RUS',
026| 'cc5':'MEX',
027| 'cc6':'GBR',
028| 'big':0,
029| 'fpath':BasePath,
030| }
031|
032|
033|
034|
035| DateFirst = date(2020,3,1)
036| DateLast = date(2024,3,1)
037| ChartWidth = 13.2
038| ChartHeight = 6.0
039|
040| Nmbr = lambda s: float(s) if len(s) else 0.0
041|
042| def SafeDivide (a, b):
043| try:
044| return float((float(a) / float(b)))
045| except:
046| return float(0)
047|
048|
049| COLS = [3,4,5,7,8,10,11,13,14,62]
050|
051| ColNames = {
052| 0:'ISO Code', 1:'Continent', 2:'Location', 3:'Date',
053| 4:'Cases (total)' , 5:'Cases (new)' , 6:'Cases (new;smoothed)',
054| 7:'Deaths (total)', 8:'Deaths (new)', 9:'Deaths (new;smoothed)',
055| 10:'Cases/MM (total)' , 11:'Cases/MM (new)' , 12:'Cases/MM (new;smoothed)',
056| 13:'Deaths/MM (total)', 14:'Deaths/MM (new)', 15:'Deaths/MM (new;smoothed)',
057| 62:'Population'
058| }
059|
060| Colors = ['#ff0000', '#0000ff', '#00a000', '#800080', '#808000', '#008080', '#606060']
061|
062|
063| class owidstats (object):
064| def __init__ (self, recd):
065| self.code = recd[0]
066| self.name = recd[2]
067| self.tpop = Nmbr(recd[62])
068| self.tcase = Nmbr(recd[4])
069| self.tdead = Nmbr(recd[7])
070| self.trecs = 0
071| def check (self, recd):
072| pop = Nmbr(recd[62])
073| tc = Nmbr(recd[4])
074| td = Nmbr(recd[7])
075| if self.tpop != pop: Log.warn(recd)
076| if self.tcase < tc: self.tcase = tc
077| if self.tdead < td: self.tdead = td
078| self.trecs += 1
079| def __str__ (self):
080| s = '%s %s %s cases %s dead'
081| t = (self.code, self.name, self.tcase, self.tdead)
082| return s%t
083| def __repr__ (self):
084| s = '{owidstats:{code:"%s", name:"%s", tpop:%s, tcase:%s, tdead:%s, trecs:%s}}'
085| t = (self.code, self.name, self.tpop, self.tcase, self.tdead, self.trecs)
086| return s%t
087|
088|
089| class owid_covid_data (csvfile):
090| def __init__ (self, fname=DataName, fpath=DataPath):
091| super().__init__(path.join(fpath,fname))
092|
093| self.load()
094| Log.info(self)
095|
096| self.stats = {}
097| for t in self.data:
098| iso = t[0]
099| if iso not in self.stats:
100| self.stats[iso] = owidstats(t)
101| self.stats[iso].check(t)
102|
103| self.ccs = list(sorted(self.stats.keys()))
104| Log.info('ISO codes=%d' % len(self.ccs))
105|
106| def cname (self, code='USA'): return self.stats[code].name
107| def tpop (self, code='USA'): return self.stats[code].tpop
108| def tcase (self, code='USA'): return self.stats[code].tcase
109| def tdead (self, code='USA'): return self.stats[code].tdead
110|
111| def country (self, code='OWID_WRL', cols=[]):
112| '''Return a list of records for a given country.'''
113| f = lambda t:([t[cx] for cx in cols] if cols else t)
114| data = [f(t) for t in self.data if t[0]==code]
115| Log.info('%s=%d' % (code,len(data)))
116| return data
117|
118| def codes (self, fobj):
119| '''Dump Country Stats to a linefile object.'''
120| ttl = 'OWID COVID Data Country Codes'
121| fobj.add(ttl)
122| Log.info(ttl)
123| for ix,k in enumerate(sorted(self.stats.keys())):
124| n = self.stats[k].name
125| p = int(self.stats[k].tpop)
126| c = int(self.stats[k].tcase)
127| d = int(self.stats[k].tdead)
128| s = f'{ix:3d}: {k} {n} [{p:,}; {c:,}; {d:,}]'
129| fobj.add(s)
130| Log.info(s)
131| fobj.add()
132| Log.info()
133| return self.ccs
134|
135| def columns (self, fobj, cols=[]):
136| '''Dump Dataset columns to a linefile object.'''
137| ttl = 'OWID COVID Data Columns'
138| fmt = '%3d: %s'
139| hs = [self.hdrs[ix] for ix in cols] if cols else self.hdrs
140| fobj.add(ttl)
141| Log.info(ttl)
142| for ix,h in enumerate(hs):
143| fobj.add(fmt % (ix,h))
144| Log.info(fmt % (ix,h))
145| fobj.add()
146| Log.info()
147| return hs
148|
149|
150|
151| def do_owid_list (owid, **kwargs):
152| Log.info('owid-list:: %s' % str(list(kwargs.keys())))
153| fpath = kwargs['fpath'] if 'fpath' in kwargs else BasePath
154| fname = kwargs['fname'] if 'fname' in kwargs else 'owid-covid.list'
155| Log.info('fpath="%s"' % fpath)
156| Log.info('fname="%s"' % fname)
157| Log.info()
158| fn = path.join(fpath, fname)
159| fo = linefile(fn)
160|
161| owid.columns(fo, cols=COLS)
162| owid.columns(fo)
163| fo.add()
164|
165| owid.codes(fo)
166| fo.add()
167| fo.save()
168|
169| Log.info()
170| return 'Done'
171|
172|
173| def do_percentage_ranking (owid, **kwargs):
174| Log.info('percent-rank:: %s' % str(list(kwargs.keys())))
175| fpath = kwargs['fpath'] if 'fpath' in kwargs else ImgsPath
176| fname = kwargs['fname'] if 'fname' in kwargs else 'percent-rank.list'
177| Log.info('fpath="%s"' % fpath)
178| Log.info('fname="%s"' % fname)
179| Log.info()
180| f3 = lambda r: (100.0*(r.tcase / r.tpop)) if 0 < r.tpop else 0.0
181| f4 = lambda r: (100.0*(r.tdead / r.tpop)) if 0 < r.tpop else 0.0
182|
183| fn = path.join(fpath, fname)
184| fo = linefile(fn)
185|
186| ttl = 'Number of cases:'
187| fo.add(ttl)
188| Log.info(ttl)
189| g = [(cc,owid.stats[cc].tcase) for cc in owid.ccs]
190| g = list(sorted(g, key=lambda x:x[1], reverse=True))
191| for t in g[0:50]:
192| n = owid.cname(t[0])
193| s = f'{n}: {int(t[1]):,}'
194| fo.add(s)
195| Log.info(s)
196| fo.add()
197| Log.info()
198|
199| ttl = 'Number of deaths:'
200| fo.add(ttl)
201| Log.info(ttl)
202| g = [(cc,owid.stats[cc].tdead) for cc in owid.ccs]
203| g = list(sorted(g, key=lambda x:x[1], reverse=True))
204| for t in g[0:50]:
205| n = owid.cname(t[0])
206| s = f'{n}: {int(t[1]):,d}'
207| fo.add(s)
208| Log.info(s)
209| fo.add()
210| Log.info()
211|
212| ttl = 'Percentage of cases:'
213| fo.add(ttl)
214| Log.info(ttl)
215| g = [(cc,f3(owid.stats[cc])) for cc in owid.ccs]
216| g = list(sorted(g, key=lambda x:x[1], reverse=True))
217| for t in g[0:50]:
218| n = owid.cname(t[0])
219| s = f'{n}: {t[1]:.3f}'
220| fo.add(s)
221| Log.info(s)
222| fo.add()
223| Log.info()
224|
225| ttl = 'Percentage of deaths:'
226| fo.add(ttl)
227| Log.info(ttl)
228| g = [(cc,f4(owid.stats[cc])) for cc in owid.ccs]
229| g = list(sorted(g, key=lambda x:x[1], reverse=True))
230| for t in g[0:50]:
231| n = owid.cname(t[0])
232| s = f'{n}: {t[1]:.3f}'
233| fo.add(s)
234| Log.info(s)
235| fo.add()
236| Log.info()
237| fo.save()
238|
239| Log.info()
240| return 'Done'
241|
242|
243| def do_chart_1 (owid, **kwargs):
244| '''COVID Cases: World vs Selected Countries'''
245| fpath = KWARG('fpath', ImgsPath, str, kwargs)
246| fname = KWARG('fname', 'covid-1.png', str, kwargs)
247| cc1 = KWARG('cc1', 'USA', str, kwargs)
248| cc2 = KWARG('cc2', 'BRA', str, kwargs)
249| cc3 = KWARG('cc3', 'IND', str, kwargs)
250| cc4 = KWARG('cc4', 'RUS', str, kwargs)
251| cc5 = KWARG('cc5', 'MEX', str, kwargs)
252| cc6 = KWARG('cc6', 'GBR', str, kwargs)
253| big = KWARG('big', 0, int, kwargs)
254| Log.info('chart-1:: %s' % str(list(kwargs.keys())))
255| Log.info('fpath="%s"' % fpath)
256| Log.info('fname="%s"' % fname)
257| Log.info('cc1="%s"' % cc1)
258| Log.info('cc2="%s"' % cc2)
259| Log.info('cc3="%s"' % cc3)
260| Log.info('cc4="%s"' % cc4)
261| Log.info('cc5="%s"' % cc5)
262| Log.info('cc6="%s"' % cc6)
263| Log.info('big=%d' % big)
264| Log.info()
265|
266| cols = [3,4]
267| ct0 = owid.country('OWID_WRL', cols=cols)
268| ct1 = owid.country(cc1, cols=cols)
269| ct2 = owid.country(cc2, cols=cols)
270| ct3 = owid.country(cc3, cols=cols)
271| ct4 = owid.country(cc4, cols=cols)
272| ct5 = owid.country(cc5, cols=cols)
273| ct6 = owid.country(cc6, cols=cols)
274|
275| xs0 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct0]
276| xs1 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct1]
277| xs2 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct2]
278| xs3 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct3]
279| xs4 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct4]
280| xs5 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct5]
281| xs6 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct6]
282|
283| ys0 = [Nmbr(t[1])/1000000.0 for t in ct0]
284| ys1 = [Nmbr(t[1])/1000000.0 for t in ct1]
285| ys2 = [Nmbr(t[1])/1000000.0 for t in ct2]
286| ys3 = [Nmbr(t[1])/1000000.0 for t in ct3]
287| ys4 = [Nmbr(t[1])/1000000.0 for t in ct4]
288| ys5 = [Nmbr(t[1])/1000000.0 for t in ct5]
289| ys6 = [Nmbr(t[1])/1000000.0 for t in ct6]
290| Log.info('Cases (World): %s' % max(ys0))
291| Log.info('Cases (%s): %s' % (cc1, max(ys1)))
292| Log.info('Cases (%s): %s' % (cc2, max(ys2)))
293| Log.info('Cases (%s): %s' % (cc3, max(ys3)))
294| Log.info('Cases (%s): %s' % (cc4, max(ys4)))
295| Log.info('Cases (%s): %s' % (cc5, max(ys5)))
296| Log.info('Cases (%s): %s' % (cc6, max(ys6)))
297|
298| fig, ax = plt.subplots()
299| fig.suptitle('COVID Cases - World v Selected Countries', fontweight='bold')
300|
301| ax.set_xlabel('Date', fontweight='bold')
302| ax.set_xlim(DateFirst, DateLast)
303| ax.xaxis.set_major_locator(DayLocator(1))
304| ax.xaxis.set_minor_locator(NullLocator())
305| ax.xaxis.set_major_formatter(DateFormatter('%b %y'))
306|
307| for tick in ax.xaxis.get_major_ticks():
308| tick.label1.set_rotation('vertical')
309| tick.label1.set_size(9)
310|
311| ax.set_ylabel('Cases (millions)', fontweight='bold')
312| if big:
313| parts = path.splitext(fname)
314| fname = '%s-big%s' % (parts[0], parts[1])
315| ax.set_ylim(0, 800)
316| ax.yaxis.set_major_locator(MultipleLocator(100))
317| ax.yaxis.set_minor_locator(MultipleLocator(10))
318| else:
319| ax.set_ylim(0, 120)
320| ax.yaxis.set_major_locator(MultipleLocator(10))
321| ax.yaxis.set_minor_locator(MultipleLocator(2.5))
322| ax.yaxis.set_major_formatter(FormatStrFormatter('%d'))
323|
324| ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
325| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
326| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
327| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
328|
329| ax.plot(xs0,ys0, label='World', c=Colors[0], lw=3.3, alpha=0.75, zorder=20)
330| ax.plot(xs1,ys1, label=cc1 , c=Colors[1], lw=3.0, alpha=0.75, zorder=19)
331| ax.plot(xs2,ys2, label=cc2 , c=Colors[2], lw=2.0, alpha=0.75, zorder=18)
332| ax.plot(xs3,ys3, label=cc3 , c=Colors[3], lw=2.0, alpha=0.75, zorder=17)
333| ax.plot(xs4,ys4, label=cc4 , c=Colors[4], lw=2.0, alpha=0.75, zorder=16)
334| ax.plot(xs5,ys5, label=cc5 , c=Colors[5], lw=2.0, alpha=0.75, zorder=15)
335| ax.plot(xs6,ys6, label=cc6 , c=Colors[6], lw=2.0, alpha=0.75, zorder=14)
336| ax.legend(loc='upper left', shadow=True)
337|
338| fig.set_figwidth(ChartWidth)
339| fig.set_figheight(ChartHeight)
340| fig.subplots_adjust(left=0.05, right=0.99, bottom=0.13, top=0.94)
341|
342| fn = path.join(fpath,fname)
343| fig.savefig(fn)
344| Log.info('wrote: %s' % fn)
345| Log.info()
346| return 'Done'
347|
348|
349| def do_chart_2 (owid, **kwargs):
350| '''COVID Cases (% of pop): World vs Selected Countries'''
351| fpath = KWARG('fpath', ImgsPath, str, kwargs)
352| fname = KWARG('fname', 'covid-2.png', str, kwargs)
353| cc1 = KWARG('cc1', 'USA', str, kwargs)
354| cc2 = KWARG('cc2', 'BRA', str, kwargs)
355| cc3 = KWARG('cc3', 'IND', str, kwargs)
356| cc4 = KWARG('cc4', 'RUS', str, kwargs)
357| cc5 = KWARG('cc5', 'MEX', str, kwargs)
358| cc6 = KWARG('cc6', 'GBR', str, kwargs)
359| big = KWARG('big', 0, int, kwargs)
360| Log.info('chart-2:: %s' % str(list(kwargs.keys())))
361| Log.info('fpath="%s"' % fpath)
362| Log.info('fname="%s"' % fname)
363| Log.info('cc1="%s"' % cc1)
364| Log.info('cc2="%s"' % cc2)
365| Log.info('cc3="%s"' % cc3)
366| Log.info('cc4="%s"' % cc4)
367| Log.info('cc5="%s"' % cc5)
368| Log.info('cc6="%s"' % cc6)
369| Log.info('big=%d' % big)
370| Log.info()
371|
372| cols = [3,4,62]
373| ct0 = owid.country('OWID_WRL', cols=cols)
374| ct1 = owid.country(cc1, cols=cols)
375| ct2 = owid.country(cc2, cols=cols)
376| ct3 = owid.country(cc3, cols=cols)
377| ct4 = owid.country(cc4, cols=cols)
378| ct5 = owid.country(cc5, cols=cols)
379| ct6 = owid.country(cc6, cols=cols)
380|
381| xs0 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct0]
382| xs1 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct1]
383| xs2 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct2]
384| xs3 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct3]
385| xs4 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct4]
386| xs5 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct5]
387| xs6 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct6]
388|
389| pp0 = Nmbr(ct0[0][2])
390| pp1 = Nmbr(ct1[0][2])
391| pp2 = Nmbr(ct2[0][2])
392| pp3 = Nmbr(ct3[0][2])
393| pp4 = Nmbr(ct4[0][2])
394| pp5 = Nmbr(ct5[0][2])
395| pp6 = Nmbr(ct6[0][2])
396| Log.info('World pop: %s' % pp0)
397| Log.info('%s pop: %s' % (cc1, pp1))
398| Log.info('%s pop: %s' % (cc2, pp2))
399| Log.info('%s pop: %s' % (cc3, pp3))
400| Log.info('%s pop: %s' % (cc4, pp4))
401| Log.info('%s pop: %s' % (cc5, pp5))
402| Log.info('%s pop: %s' % (cc6, pp6))
403| ys0 = [100.0*(Nmbr(t[1])/pp0) for t in ct0]
404| ys1 = [100.0*(Nmbr(t[1])/pp1) for t in ct1]
405| ys2 = [100.0*(Nmbr(t[1])/pp2) for t in ct2]
406| ys3 = [100.0*(Nmbr(t[1])/pp3) for t in ct3]
407| ys4 = [100.0*(Nmbr(t[1])/pp4) for t in ct4]
408| ys5 = [100.0*(Nmbr(t[1])/pp5) for t in ct5]
409| ys6 = [100.0*(Nmbr(t[1])/pp6) for t in ct6]
410| Log.info('Cases (World): %s' % max(ys0))
411| Log.info('Cases (%s): %s' % (cc1, max(ys1)))
412| Log.info('Cases (%s): %s' % (cc2, max(ys1)))
413| Log.info('Cases (%s): %s' % (cc3, max(ys1)))
414| Log.info('Cases (%s): %s' % (cc4, max(ys1)))
415| Log.info('Cases (%s): %s' % (cc5, max(ys5)))
416| Log.info('Cases (%s): %s' % (cc6, max(ys6)))
417|
418| fig, ax = plt.subplots()
419| fig.suptitle('COVID Cases - World v Selected Countries (%)', fontweight='bold')
420|
421| ax.set_xlabel('Date', fontweight='bold')
422| ax.set_xlim(DateFirst, DateLast)
423| ax.xaxis.set_major_locator(DayLocator(1))
424| ax.xaxis.set_minor_locator(NullLocator())
425| ax.xaxis.set_major_formatter(DateFormatter('%b %y'))
426|
427| for tick in ax.xaxis.get_major_ticks():
428| tick.label1.set_rotation('vertical')
429| tick.label1.set_size(9)
430|
431| ax.set_ylabel('Cases (% of pop)', fontweight='bold')
432| ax.set_ylim(0.0, 40.0)
433| ax.yaxis.set_major_locator(MultipleLocator(10.0))
434| ax.yaxis.set_minor_locator(MultipleLocator(1.00))
435| ax.yaxis.set_major_formatter(FormatStrFormatter('%d'))
436|
437| ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
438| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
439| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
440| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
441|
442| ax.plot(xs0,ys0, label='World', c=Colors[0], lw=3.3, alpha=0.75, zorder=20)
443| ax.plot(xs1,ys1, label=cc1 , c=Colors[1], lw=3.0, alpha=0.75, zorder=19)
444| ax.plot(xs2,ys2, label=cc2 , c=Colors[2], lw=2.0, alpha=0.75, zorder=18)
445| ax.plot(xs3,ys3, label=cc3 , c=Colors[3], lw=2.0, alpha=0.75, zorder=17)
446| ax.plot(xs4,ys4, label=cc4 , c=Colors[4], lw=2.0, alpha=0.75, zorder=16)
447| ax.plot(xs5,ys5, label=cc5 , c=Colors[5], lw=2.0, alpha=0.75, zorder=15)
448| ax.plot(xs6,ys6, label=cc6 , c=Colors[6], lw=2.0, alpha=0.75, zorder=14)
449| ax.legend(loc='upper left', shadow=True)
450|
451| fig.set_figwidth(ChartWidth)
452| fig.set_figheight(ChartHeight)
453| fig.subplots_adjust(left=0.05, right=0.99, bottom=0.13, top=0.94)
454|
455| fn = path.join(fpath,fname)
456| fig.savefig(fn)
457| Log.info('wrote: %s' % fn)
458| Log.info()
459| return 'Done'
460|
461|
462| def do_chart_3 (owid, **kwargs):
463| '''COVID Deaths: World vs Selected Countries'''
464| fpath = KWARG('fpath', ImgsPath, str, kwargs)
465| fname = KWARG('fname', 'covid-3.png', str, kwargs)
466| cc1 = KWARG('cc1', 'USA', str, kwargs)
467| cc2 = KWARG('cc2', 'BRA', str, kwargs)
468| cc3 = KWARG('cc3', 'IND', str, kwargs)
469| cc4 = KWARG('cc4', 'RUS', str, kwargs)
470| cc5 = KWARG('cc5', 'MEX', str, kwargs)
471| cc6 = KWARG('cc6', 'GBR', str, kwargs)
472| big = KWARG('big', 0, int, kwargs)
473| Log.info('chart-3:: %s' % str(list(kwargs.keys())))
474| Log.info('fpath="%s"' % fpath)
475| Log.info('fname="%s"' % fname)
476| Log.info('cc1="%s"' % cc1)
477| Log.info('cc2="%s"' % cc2)
478| Log.info('cc3="%s"' % cc3)
479| Log.info('cc4="%s"' % cc4)
480| Log.info('cc5="%s"' % cc5)
481| Log.info('cc6="%s"' % cc6)
482| Log.info('big=%d' % big)
483| Log.info()
484|
485| cols = [3,7]
486| ct0 = owid.country('OWID_WRL', cols=cols)
487| ct1 = owid.country(cc1, cols=cols)
488| ct2 = owid.country(cc2, cols=cols)
489| ct3 = owid.country(cc3, cols=cols)
490| ct4 = owid.country(cc4, cols=cols)
491| ct5 = owid.country(cc5, cols=cols)
492| ct6 = owid.country(cc6, cols=cols)
493|
494| xs0 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct0]
495| xs1 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct1]
496| xs2 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct2]
497| xs3 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct3]
498| xs4 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct4]
499| xs5 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct5]
500| xs6 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct6]
501|
502| ys0 = [Nmbr(t[1])/1000000.0 for t in ct0]
503| ys1 = [Nmbr(t[1])/1000000.0 for t in ct1]
504| ys2 = [Nmbr(t[1])/1000000.0 for t in ct2]
505| ys3 = [Nmbr(t[1])/1000000.0 for t in ct3]
506| ys4 = [Nmbr(t[1])/1000000.0 for t in ct4]
507| ys5 = [Nmbr(t[1])/1000000.0 for t in ct5]
508| ys6 = [Nmbr(t[1])/1000000.0 for t in ct6]
509| Log.info('Deaths (World): %s' % max(ys0))
510| Log.info('Deaths (%s): %s' % (cc1, max(ys1)))
511| Log.info('Deaths (%s): %s' % (cc2, max(ys2)))
512| Log.info('Deaths (%s): %s' % (cc3, max(ys3)))
513| Log.info('Deaths (%s): %s' % (cc4, max(ys4)))
514| Log.info('Deaths (%s): %s' % (cc5, max(ys5)))
515| Log.info('Deaths (%s): %s' % (cc6, max(ys6)))
516|
517| fig, ax = plt.subplots()
518| fig.suptitle('COVID Deaths - World v Countries', fontweight='bold')
519|
520| ax.set_xlabel('Date', fontweight='bold')
521| ax.set_xlim(DateFirst, DateLast)
522| ax.xaxis.set_major_locator(DayLocator(1))
523| ax.xaxis.set_minor_locator(NullLocator())
524| ax.xaxis.set_major_formatter(DateFormatter('%b %y'))
525|
526| for tick in ax.xaxis.get_major_ticks():
527| tick.label1.set_rotation('vertical')
528| tick.label1.set_size(9)
529|
530| ax.set_ylabel('Deaths (millions)', fontweight='bold')
531| if big:
532| parts = path.splitext(fname)
533| fname = '%s-big%s' % (parts[0], parts[1])
534| ax.set_ylim(0.0, 8.0)
535| ax.yaxis.set_major_locator(MultipleLocator(1.0))
536| ax.yaxis.set_minor_locator(MultipleLocator(0.25))
537| else:
538| ax.set_ylim(0.0, 1.5)
539| ax.yaxis.set_major_locator(MultipleLocator(0.5))
540| ax.yaxis.set_minor_locator(MultipleLocator(0.1))
541| ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
542|
543| ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
544| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
545| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
546| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
547|
548| ax.plot(xs0,ys0, label='World', c=Colors[0], lw=3.3, alpha=0.75, zorder=20)
549| ax.plot(xs1,ys1, label=cc1 , c=Colors[1], lw=2.0, alpha=0.75, zorder=19)
550| ax.plot(xs2,ys2, label=cc2 , c=Colors[2], lw=2.0, alpha=0.75, zorder=18)
551| ax.plot(xs3,ys3, label=cc3 , c=Colors[3], lw=2.0, alpha=0.75, zorder=17)
552| ax.plot(xs4,ys4, label=cc4 , c=Colors[4], lw=2.0, alpha=0.75, zorder=16)
553| ax.plot(xs5,ys5, label=cc5 , c=Colors[5], lw=2.0, alpha=0.75, zorder=15)
554| ax.plot(xs6,ys6, label=cc6 , c=Colors[6], lw=2.0, alpha=0.75, zorder=14)
555| ax.legend(loc='upper left', shadow=True)
556|
557| fig.set_figwidth(ChartWidth)
558| fig.set_figheight(ChartHeight)
559| fig.subplots_adjust(left=0.05, right=0.99, bottom=0.13, top=0.94)
560|
561| fn = path.join(fpath,fname)
562| fig.savefig(fn)
563| Log.info('wrote: %s' % fn)
564| Log.info()
565| return 'Done'
566|
567|
568| def do_chart_4 (owid, **kwargs):
569| '''COVID Deaths (% of pop): World vs Selected Countries'''
570| fpath = KWARG('fpath', ImgsPath, str, kwargs)
571| fname = KWARG('fname', 'covid-4.png', str, kwargs)
572| cc1 = KWARG('cc1', 'USA', str, kwargs)
573| cc2 = KWARG('cc2', 'BRA', str, kwargs)
574| cc3 = KWARG('cc3', 'IND', str, kwargs)
575| cc4 = KWARG('cc4', 'RUS', str, kwargs)
576| cc5 = KWARG('cc5', 'MEX', str, kwargs)
577| cc6 = KWARG('cc6', 'GBR', str, kwargs)
578| big = KWARG('big', 0, int, kwargs)
579| Log.info('chart-4:: %s' % str(list(kwargs.keys())))
580| Log.info('fpath="%s"' % fpath)
581| Log.info('fname="%s"' % fname)
582| Log.info('cc1="%s"' % cc1)
583| Log.info('cc2="%s"' % cc2)
584| Log.info('cc3="%s"' % cc3)
585| Log.info('cc4="%s"' % cc4)
586| Log.info('cc5="%s"' % cc5)
587| Log.info('cc6="%s"' % cc6)
588| Log.info('big=%d' % big)
589| Log.info()
590|
591| cols = [3,7,62]
592| ct0 = owid.country('OWID_WRL', cols=cols)
593| ct1 = owid.country(cc1, cols=cols)
594| ct2 = owid.country(cc2, cols=cols)
595| ct3 = owid.country(cc3, cols=cols)
596| ct4 = owid.country(cc4, cols=cols)
597| ct5 = owid.country(cc5, cols=cols)
598| ct6 = owid.country(cc6, cols=cols)
599|
600| xs0 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct0]
601| xs1 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct1]
602| xs2 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct2]
603| xs3 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct3]
604| xs4 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct4]
605| xs5 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct5]
606| xs6 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct6]
607|
608| pp0 = Nmbr(ct0[0][2])
609| pp1 = Nmbr(ct1[0][2])
610| pp2 = Nmbr(ct2[0][2])
611| pp3 = Nmbr(ct3[0][2])
612| pp4 = Nmbr(ct4[0][2])
613| pp5 = Nmbr(ct5[0][2])
614| pp6 = Nmbr(ct6[0][2])
615| Log.info('World pop: %s' % pp0)
616| Log.info('%s pop: %s' % (cc1, pp1))
617| Log.info('%s pop: %s' % (cc2, pp2))
618| Log.info('%s pop: %s' % (cc3, pp3))
619| Log.info('%s pop: %s' % (cc4, pp4))
620| Log.info('%s pop: %s' % (cc5, pp5))
621| Log.info('%s pop: %s' % (cc6, pp6))
622| ys0 = [100.0*(Nmbr(t[1])/pp0) for t in ct0]
623| ys1 = [100.0*(Nmbr(t[1])/pp1) for t in ct1]
624| ys2 = [100.0*(Nmbr(t[1])/pp2) for t in ct2]
625| ys3 = [100.0*(Nmbr(t[1])/pp3) for t in ct3]
626| ys4 = [100.0*(Nmbr(t[1])/pp4) for t in ct4]
627| ys5 = [100.0*(Nmbr(t[1])/pp5) for t in ct5]
628| ys6 = [100.0*(Nmbr(t[1])/pp6) for t in ct6]
629| Log.info('Deaths (World): %s' % max(ys0))
630| Log.info('Deaths (%s): %s' % (cc1, max(ys1)))
631| Log.info('Deaths (%s): %s' % (cc2, max(ys2)))
632| Log.info('Deaths (%s): %s' % (cc3, max(ys3)))
633| Log.info('Deaths (%s): %s' % (cc4, max(ys4)))
634| Log.info('Deaths (%s): %s' % (cc5, max(ys5)))
635| Log.info('Deaths (%s): %s' % (cc6, max(ys6)))
636|
637| fig, ax = plt.subplots()
638| fig.suptitle('COVID Deaths - World v Countries (%)', fontweight='bold')
639|
640| ax.set_xlabel('Date', fontweight='bold')
641| ax.set_xlim(DateFirst, DateLast)
642| ax.xaxis.set_major_locator(DayLocator(1))
643| ax.xaxis.set_minor_locator(NullLocator())
644| ax.xaxis.set_major_formatter(DateFormatter('%b %y'))
645|
646| for tick in ax.xaxis.get_major_ticks():
647| tick.label1.set_rotation('vertical')
648| tick.label1.set_size(9)
649|
650| ax.set_ylabel('Deaths (% of pop)', fontweight='bold')
651| ax.set_ylim(0.0, 0.4)
652| ax.yaxis.set_major_locator(MultipleLocator(0.1))
653| ax.yaxis.set_minor_locator(MultipleLocator(0.01))
654| ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
655|
656| ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
657| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
658| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
659| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
660|
661| ax.plot(xs0,ys0, label='World', c=Colors[0], lw=3.3, alpha=0.75, zorder=20)
662| ax.plot(xs1,ys1, label=cc1 , c=Colors[1], lw=3.0, alpha=0.75, zorder=19)
663| ax.plot(xs2,ys2, label=cc2 , c=Colors[2], lw=2.0, alpha=0.75, zorder=18)
664| ax.plot(xs3,ys3, label=cc3 , c=Colors[3], lw=2.0, alpha=0.75, zorder=17)
665| ax.plot(xs4,ys4, label=cc4 , c=Colors[4], lw=2.0, alpha=0.75, zorder=16)
666| ax.plot(xs5,ys5, label=cc5 , c=Colors[5], lw=2.0, alpha=0.75, zorder=15)
667| ax.plot(xs6,ys6, label=cc6 , c=Colors[6], lw=2.0, alpha=0.75, zorder=14)
668| ax.legend(loc='upper left', shadow=True)
669|
670| fig.set_figwidth(ChartWidth)
671| fig.set_figheight(ChartHeight)
672| fig.subplots_adjust(left=0.05, right=0.99, bottom=0.13, top=0.94)
673|
674| fn = path.join(fpath,fname)
675| fig.savefig(fn)
676| Log.info('wrote: %s' % fn)
677| Log.info()
678| return 'Done'
679|
680|
681| def do_chart_5 (owid, **kwargs):
682| '''COVID Cases/Deaths: Top 30 Countries'''
683| fpath = KWARG('fpath', ImgsPath, str, kwargs)
684| fname = KWARG('fname', 'covid-5.png', str, kwargs)
685| Log.info('chart-5:: %s' % str(list(kwargs.keys())))
686| Log.info('fpath="%s"' % fpath)
687| Log.info('fname="%s"' % fname)
688| Log.info()
689|
690| n = 30
691| g1 = [(cc,owid.stats[cc].tcase) for cc in owid.ccs if not cc.startswith('OWID')]
692| g1 = list(sorted(g1, key=lambda x:x[1], reverse=True))
693| g1 = g1[0:n]
694| ys1 = [n+1 for n in reversed(range(n))]
695| ns1 = [t[0] for t in g1]
696| xs1 = [t[1]/1000000 for t in g1]
697| cs1 = ['#3366cc' if n=='USA' else '#bf7f4f' for n in ns1]
698|
699| g2 = [(cc,owid.stats[cc].tdead) for cc in owid.ccs if not cc.startswith('OWID')]
700| g2 = list(sorted(g2, key=lambda x:x[1], reverse=True))
701| g2 = g2[0:n]
702| ys2 = [n+1 for n in reversed(range(n))]
703| ns2 = [t[0] for t in g2]
704| xs2 = [t[1]/100000 for t in g2]
705| cs2 = ['#3366cc' if n=='USA' else '#bf5f2f' for n in ns2]
706|
707| fig, axes = plt.subplots(nrows=1, ncols=2, squeeze=True)
708| fig.suptitle('COVID Top %d Countries'%n, fontweight='bold')
709| ax1 = axes[0]
710| ax2 = axes[1]
711|
712| ax1.set_title('Ranked by #Cases')
713| ax1.set_xlabel('Cases (Millions)')
714|
715| ax1.set_xlim(0.0, 110.0)
716| ax1.set_ylim(0, n+1)
717| ax1.xaxis.set_major_locator(MultipleLocator(10.0))
718| ax1.xaxis.set_minor_locator(MultipleLocator(2.0))
719| ax1.grid(which='major', axis='both', color='#7f7f7f', lw=0.8,ls='-',zorder=1, alpha=1.0)
720| ax1.grid(which='minor', axis='both', color='#bfbfbf', lw=0.5,ls='-',zorder=0, alpha=1.0)
721| ax1.tick_params(which='major', axis='both', direction='out', color='#000000')
722| ax1.tick_params(which='minor', axis='both', direction='out', color='#000000')
723| ax1.barh(ys1,xs1, tick_label=ns1, color=cs1, edgecolor='#000000', zorder=10)
724|
725| ax2.set_title('Ranked by #Deaths')
726| ax2.set_xlabel('Deaths (100,000s)')
727|
728| ax2.set_xlim(0.0, 12.0)
729| ax2.set_ylim(0, n+1)
730| ax2.xaxis.set_major_locator(MultipleLocator(1.00))
731| ax2.xaxis.set_minor_locator(MultipleLocator(0.25))
732| ax2.grid(which='major', axis='both', color='#7f7f7f', lw=0.8,ls='-',zorder=1, alpha=1.0)
733| ax2.grid(which='minor', axis='both', color='#bfbfbf', lw=0.5,ls='-',zorder=0, alpha=1.0)
734| ax2.tick_params(which='major', axis='both', direction='out', color='#000000')
735| ax2.tick_params(which='minor', axis='both', direction='out', color='#000000')
736| ax2.barh(ys2,xs2, tick_label=ns2, color=cs2, edgecolor='#000000', zorder=10)
737|
738| fig.set_figwidth(ChartWidth)
739| fig.set_figheight(7.0)
740| fig.subplots_adjust(left=0.05, right=0.98, bottom=0.08, top=0.92)
741|
742| fn = path.join(fpath,fname)
743| fig.savefig(fn)
744| Log.info('wrote: %s' % fn)
745| Log.info()
746| return 'Done'
747|
748|
749| def do_chart_6 (owid, **kwargs):
750| '''COVID Cases/Deaths (% of pop): Top 30 Countries'''
751| fpath = KWARG('fpath', ImgsPath, str, kwargs)
752| fname = KWARG('fname', 'covid-6.png', str, kwargs)
753| Log.info('chart-6:: %s' % str(list(kwargs.keys())))
754| Log.info('fpath="%s"' % fpath)
755| Log.info('fname="%s"' % fname)
756| Log.info()
757|
758| n = 30
759| f1 = lambda r: (100.0*(r.tcase/r.tpop)) if 0 < r.tpop else 0.0
760| g1 = [(cc,f1(owid.stats[cc])) for cc in owid.ccs if not cc.startswith('OWID')]
761| g1 = list(sorted(g1, key=lambda x:x[1], reverse=True))
762| g1 = g1[0:n]
763| ys1 = [n+1 for n in reversed(range(n))]
764| ns1 = [t[0] for t in g1]
765| xs1 = [t[1] for t in g1]
766| cs1 = ['#3366cc' if n=='USA' else '#bf7f4f' for n in ns1]
767|
768| f2 = lambda r: (100.0*(r.tdead/r.tpop)) if 0 < r.tpop else 0.0
769| g2 = [(cc,f2(owid.stats[cc])) for cc in owid.ccs if not cc.startswith('OWID')]
770| g2 = list(sorted(g2, key=lambda x:x[1], reverse=True))
771| g2 = g2[0:n]
772| ys2 = [n+1 for n in reversed(range(n))]
773| ns2 = [t[0] for t in g2]
774| xs2 = [t[1] for t in g2]
775| cs2 = ['#3366cc' if n=='USA' else '#bf5f2f' for n in ns2]
776|
777| fig, axes = plt.subplots(nrows=1, ncols=2, squeeze=True)
778| fig.suptitle('COVID Top %d Countries'%n, fontweight='bold')
779| ax1 = axes[0]
780| ax2 = axes[1]
781|
782| ax1.set_title('Ranked by % of Cases')
783| ax1.set_xlabel('Cases (% of pop)')
784|
785| ax1.set_xlim(0.0, 80.0)
786| ax1.set_ylim(0, n+1)
787| ax1.xaxis.set_major_locator(MultipleLocator(10.0))
788| ax1.xaxis.set_minor_locator(MultipleLocator(1.0))
789| ax1.xaxis.set_major_formatter(FormatStrFormatter('%d'))
790| ax1.grid(which='major', axis='both', color='#7f7f7f', lw=0.8,ls='-',zorder=1, alpha=1.0)
791| ax1.grid(which='minor', axis='both', color='#bfbfbf', lw=0.5,ls='-',zorder=0, alpha=1.0)
792| ax1.tick_params(which='major', axis='both', direction='out', color='#000000')
793| ax1.tick_params(which='minor', axis='both', direction='out', color='#000000')
794| ax1.barh(ys1,xs1, tick_label=ns1, color=cs1, edgecolor='#000000', zorder=10)
795|
796| ax2.set_title('Ranked by % of Deaths')
797| ax2.set_xlabel('Deaths (% of pop)')
798|
799| ax2.set_xlim(0.0, 0.70)
800| ax2.set_ylim(0, n+1)
801| ax2.xaxis.set_major_locator(MultipleLocator(0.10))
802| ax2.xaxis.set_minor_locator(MultipleLocator(0.02))
803| ax2.xaxis.set_major_formatter(FormatStrFormatter('%.1f'))
804| ax2.grid(which='major', axis='both', color='#7f7f7f', lw=0.8,ls='-',zorder=1, alpha=1.0)
805| ax2.grid(which='minor', axis='both', color='#bfbfbf', lw=0.5,ls='-',zorder=0, alpha=1.0)
806| ax2.tick_params(which='major', axis='both', direction='out', color='#000000')
807| ax2.tick_params(which='minor', axis='both', direction='out', color='#000000')
808| ax2.barh(ys2,xs2, tick_label=ns2, color=cs2, edgecolor='#000000', zorder=10)
809|
810| fig.set_figwidth(ChartWidth)
811| fig.set_figheight(7.0)
812| fig.subplots_adjust(left=0.05, right=0.98, bottom=0.08, top=0.92)
813|
814| fn = path.join(fpath,fname)
815| fig.savefig(fn)
816| Log.info('wrote: %s' % fn)
817| Log.info()
818| return 'Done'
819|
820|
821| def do_chart_7 (owid, **kwargs):
822| '''COVID Cases USA: Daily/Smooth comparison.'''
823| fpath = KWARG('fpath', ImgsPath, str, kwargs)
824| fname = KWARG('fname', 'covid-1.png', str, kwargs)
825| cc0 = KWARG('cc0', 'USA', str, kwargs)
826| ymax = KWARG('ymax', 10.0, float, kwargs)
827| ytick0 = KWARG('yt0' , 1.0, float, kwargs)
828| ytick1 = KWARG('yt1' , 0.25, float, kwargs)
829| Log.info('chart-7:: %s' % str(list(kwargs.keys())))
830| fpath = kwargs['fpath'] if 'fpath' in kwargs else ImgsPath
831| fname = kwargs['fname'] if 'fname' in kwargs else 'covid-7.png'
832| Log.info('fpath="%s"' % fpath)
833| Log.info('fname="%s"' % fname)
834| Log.info('cc0="%s"' % cc0)
835| Log.info('ymax=%f' % ymax)
836| Log.info('ytick0=%f' % ytick0)
837| Log.info('ytick1=%f' % ytick1)
838| Log.info()
839|
840| cols = [3,5,6]
841| ct0 = owid.country(cc0, cols=cols)
842| xs0 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct0]
843| ys0 = [Nmbr(t[1])/100000.0 for t in ct0]
844| ys1 = [Nmbr(t[2])/100000.0 for t in ct0]
845|
846| fig, ax = plt.subplots()
847| fig.suptitle('COVID Cases %s' % cc0, fontweight='bold')
848|
849| ax.set_xlabel('Date', fontweight='bold')
850| ax.set_xlim(DateFirst, DateLast)
851| ax.xaxis.set_major_locator(DayLocator(1))
852| ax.xaxis.set_minor_locator(NullLocator())
853| ax.xaxis.set_major_formatter(DateFormatter('%b %y'))
854|
855| for tick in ax.xaxis.get_major_ticks():
856| tick.label1.set_rotation('vertical')
857| tick.label1.set_size(9)
858|
859| ax.set_ylabel('New Cases (100,000s)', fontweight='bold')
860| ax.set_ylim(0.0, ymax)
861| ax.yaxis.set_major_locator(MultipleLocator(ytick0))
862| ax.yaxis.set_minor_locator(MultipleLocator(ytick1))
863| ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
864|
865| ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
866| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
867| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
868| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
869|
870| ax.plot(xs0,ys0, label='Daily' , c='#ff0000', lw=1.5, alpha=0.50, zorder=20)
871| ax.plot(xs0,ys1, label='Smooth', c='#0000ff', lw=3.5, alpha=1.00, zorder=19)
872| ax.legend(loc='upper right', shadow=True)
873|
874| fig.set_figwidth(ChartWidth)
875| fig.set_figheight(ChartHeight)
876| fig.subplots_adjust(left=0.05, right=0.99, bottom=0.13, top=0.94)
877|
878| fn = path.join(fpath,fname)
879| fig.savefig(fn)
880| Log.info('wrote: %s' % fn)
881| Log.info()
882| Log.info()
883| return 'Done'
884|
885|
886| def do_chart_8 (owid, **kwargs):
887| '''COVID Deaths USA: Daily/Smooth comparison.'''
888| fpath = KWARG('fpath', ImgsPath, str, kwargs)
889| fname = KWARG('fname', 'covid-8.png', str, kwargs)
890| cc0 = KWARG('cc0', 'USA', str, kwargs)
891| ymax = KWARG('ymax', 5.0, float, kwargs)
892| ytick0 = KWARG('yt0' , 1.0, float, kwargs)
893| ytick1 = KWARG('yt1' , 0.1, float, kwargs)
894| Log.info('chart-8:: %s' % str(list(kwargs.keys())))
895| Log.info('fpath="%s"' % fpath)
896| Log.info('fname="%s"' % fname)
897| Log.info('cc0="%s"' % cc0)
898| Log.info('ymax=%f' % ymax)
899| Log.info('ytick0=%f' % ytick0)
900| Log.info('ytick1=%f' % ytick1)
901| Log.info()
902|
903| cols = [3,8,9]
904| ct0 = owid.country(cc0, cols=cols)
905| xs0 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct0]
906| ys0 = [Nmbr(t[1])/1000.0 for t in ct0]
907| ys1 = [Nmbr(t[2])/1000.0 for t in ct0]
908|
909| fig, ax = plt.subplots()
910| fig.suptitle('COVID Deaths %s' % cc0, fontweight='bold')
911|
912| ax.set_xlabel('Date', fontweight='bold')
913| ax.set_xlim(DateFirst, DateLast)
914| ax.xaxis.set_major_locator(DayLocator(1))
915| ax.xaxis.set_minor_locator(NullLocator())
916| ax.xaxis.set_major_formatter(DateFormatter('%b %y'))
917|
918| for tick in ax.xaxis.get_major_ticks():
919| tick.label1.set_rotation('vertical')
920| tick.label1.set_size(9)
921|
922| ax.set_ylabel('New Deaths (1000s)', fontweight='bold')
923| ax.set_ylim(0.0, ymax)
924| ax.yaxis.set_major_locator(MultipleLocator(ytick0))
925| ax.yaxis.set_minor_locator(MultipleLocator(ytick1))
926| ax.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
927|
928| ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
929| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
930| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
931| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
932|
933| ax.plot(xs0,ys0, label='Daily' , c='#ff0000', lw=1.5, alpha=0.50, zorder=20)
934| ax.plot(xs0,ys1, label='Smooth', c='#0000ff', lw=3.5, alpha=1.00, zorder=19)
935| ax.legend(loc='upper right', shadow=True)
936|
937| fig.set_figwidth(ChartWidth)
938| fig.set_figheight(ChartHeight)
939| fig.subplots_adjust(left=0.05, right=0.99, bottom=0.13, top=0.94)
940|
941| fn = path.join(fpath,fname)
942| fig.savefig(fn)
943| Log.info('wrote: %s' % fn)
944| Log.info()
945| Log.info()
946| return 'Done'
947|
948|
949| def do_chart_9 (owid, **kwargs):
950| '''COVID Deaths per Cases'''
951| fpath = KWARG('fpath', ImgsPath, str, kwargs)
952| fname = KWARG('fname', 'covid-9.png', str, kwargs)
953| cc1 = KWARG('cc1', 'USA', str, kwargs)
954| cc2 = KWARG('cc2', 'BRA', str, kwargs)
955| cc3 = KWARG('cc3', 'IND', str, kwargs)
956| cc4 = KWARG('cc4', 'RUS', str, kwargs)
957| cc5 = KWARG('cc5', 'MEX', str, kwargs)
958| cc6 = KWARG('cc6', 'GBR', str, kwargs)
959| ymax = KWARG('ymax', 0.25, float, kwargs)
960| Log.info('chart-9:: %s' % str(list(kwargs.keys())))
961| Log.info('fpath="%s"' % fpath)
962| Log.info('fname="%s"' % fname)
963| Log.info('cc1="%s"' % cc1)
964| Log.info('cc2="%s"' % cc2)
965| Log.info('cc3="%s"' % cc3)
966| Log.info('cc4="%s"' % cc4)
967| Log.info('cc5="%s"' % cc5)
968| Log.info('cc6="%s"' % cc6)
969| Log.info('ymax=%f' % ymax)
970| Log.info()
971|
972| cols=[3,4,7]
973| ct0 = owid.country('OWID_WRL', cols=cols)
974| ct1 = owid.country(cc1, cols=cols)
975| ct2 = owid.country(cc2, cols=cols)
976| ct3 = owid.country(cc3, cols=cols)
977| ct4 = owid.country(cc4, cols=cols)
978| ct5 = owid.country(cc5, cols=cols)
979| ct6 = owid.country(cc6, cols=cols)
980|
981| xs0 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct0]
982| xs1 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct1]
983| xs2 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct2]
984| xs3 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct3]
985| xs4 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct4]
986| xs5 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct5]
987| xs6 = [datetime.strptime(t[0],'%Y-%m-%d').date() for t in ct6]
988|
989| ys0 = [SafeDivide(Nmbr(t[2]),Nmbr(t[1])) for t in ct0]
990| ys1 = [SafeDivide(Nmbr(t[2]),Nmbr(t[1])) for t in ct1]
991| ys2 = [SafeDivide(Nmbr(t[2]),Nmbr(t[1])) for t in ct2]
992| ys3 = [SafeDivide(Nmbr(t[2]),Nmbr(t[1])) for t in ct3]
993| ys4 = [SafeDivide(Nmbr(t[2]),Nmbr(t[1])) for t in ct4]
994| ys5 = [SafeDivide(Nmbr(t[2]),Nmbr(t[1])) for t in ct5]
995| ys6 = [SafeDivide(Nmbr(t[2]),Nmbr(t[1])) for t in ct6]
996|
997| fig, ax = plt.subplots()
998| fig.suptitle('COVID Chart - Deaths/Cases', fontweight='bold')
999|
1000| ax.set_xlabel('Date', fontweight='bold')
1001| ax.set_xlim(DateFirst, DateLast)
1002| ax.xaxis.set_major_locator(DayLocator(1))
1003| ax.xaxis.set_minor_locator(NullLocator())
1004| ax.xaxis.set_major_formatter(DateFormatter('%b %y'))
1005|
1006| for tick in ax.xaxis.get_major_ticks():
1007| tick.label1.set_rotation('vertical')
1008| tick.label1.set_size(9)
1009|
1010| ax.set_ylabel('Deaths/Cases', fontweight='bold')
1011| ax.set_ylim(0.0, ymax)
1012| ax.yaxis.set_major_locator(MultipleLocator(0.05))
1013| ax.yaxis.set_minor_locator(MultipleLocator(0.01))
1014| ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
1015|
1016| ax.grid(which='major', axis='both', color='#7f7f7f', lw=1.2,ls='-',zorder=1, alpha=1.0)
1017| ax.grid(which='minor', axis='both', color='#bfbfbf', lw=0.6,ls='-',zorder=0, alpha=1.0)
1018| ax.tick_params(which='major', axis='both', direction='out', color='#000000')
1019| ax.tick_params(which='minor', axis='both', direction='out', color='#000000')
1020|
1021| ax.plot(xs0,ys0, label='World', c=Colors[0], lw=3.3, alpha=0.75, zorder=26)
1022| ax.plot(xs1,ys1, label=cc1 , c=Colors[1], lw=3.0, alpha=0.75, zorder=25)
1023| ax.plot(xs2,ys2, label=cc2 , c=Colors[2], lw=2.0, alpha=0.75, zorder=24)
1024| ax.plot(xs3,ys3, label=cc3 , c=Colors[3], lw=2.0, alpha=0.75, zorder=23)
1025| ax.plot(xs4,ys4, label=cc4 , c=Colors[4], lw=2.0, alpha=0.75, zorder=22)
1026| ax.plot(xs5,ys5, label=cc5 , c=Colors[5], lw=2.0, alpha=0.75, zorder=21)
1027| ax.plot(xs6,ys6, label=cc6 , c=Colors[6], lw=2.0, alpha=0.75, zorder=20)
1028| ax.legend(loc='upper right', shadow=True)
1029|
1030| fig.set_figwidth(ChartWidth)
1031| fig.set_figheight(ChartHeight)
1032| fig.subplots_adjust(left=0.05, right=0.99, bottom=0.13, top=0.94)
1033|
1034| fn = path.join(fpath,fname)
1035| fig.savefig(fn)
1036| Log.info('wrote: %s' % fn)
1037| Log.info()
1038| return 'Done'
1039|
1040|
1041| def do_chart_pie (owid, **kwargs):
1042| Log.info('chart-pie:: %s' % str(list(kwargs.keys())))
1043| fpath = kwargs['fpath'] if 'fpath' in kwargs else ImgsPath
1044| fname = kwargs['fname'] if 'fname' in kwargs else 'covid-pie.png'
1045| Log.info('fpath="%s"' % fpath)
1046| Log.info('fname="%s"' % fname)
1047| Log.info()
1048|
1049| n = 15
1050| rcds = [(cc,owid.stats[cc].tdead) for cc in owid.ccs if not cc.startswith('OWID')]
1051| tmax = sum([t[1] for t in rcds])
1052| dat = list(sorted(rcds, key=lambda x:x[1], reverse=True))
1053| dat = dat[0:n]
1054| ns = [t[0].title().upper() for t in dat]
1055| vs = [t[1]/tmax for t in dat]
1056| dtot = sum([t[1] for t in dat])
1057| Log.info('tmax=%s' % tmax)
1058| Log.info('dtot=%s' % dtot)
1059| Log.info('USA-pct=%.2f' % (100.0*(dat[0][1]/tmax)))
1060| ns.append('other\ncountries')
1061| vs.append((tmax-dtot)/tmax)
1062|
1063| fig, ax = plt.subplots()
1064| fig.suptitle('COVID Deaths - Top %d Countries'%n, fontsize=24, fontweight='bold')
1065|
1066| ax.pie(vs, labels=ns, startangle=90, counterclock=False, textprops={'fontsize':18})
1067|
1068|
1069| fig.set_figwidth(8.8)
1070| fig.set_figheight(8.8)
1071| fig.subplots_adjust(left=0.05, right=0.95, bottom=0.00, top=0.98)
1072|
1073| fn = path.join(fpath,fname)
1074| fig.savefig(fn)
1075| Log.info('wrote: %s' % fn)
1076| Log.info()
1077| return 'Done'
1078|
1079|
1080| def do_chart_0 (owid, **kwargs):
1081| Log.info('chart-0:: %s' % str(list(kwargs.keys())))
1082| fpath = kwargs['fpath'] if 'fpath' in kwargs else ImgsPath
1083| fname = kwargs['fname'] if 'fname' in kwargs else 'covid-0.png'
1084| cc1 = kwargs['cc1'] if 'cc1' in kwargs else 'USA'
1085| cc2 = kwargs['cc2'] if 'cc2' in kwargs else 'GBR'
1086| cc3 = kwargs['cc3'] if 'cc3' in kwargs else 'DEU'
1087| cc4 = kwargs['cc4'] if 'cc4' in kwargs else 'ITA'
1088| Log.info('fpath="%s"' % fpath)
1089| Log.info('fname="%s"' % fname)
1090| Log.info('cc1="%s"' % cc1)
1091| Log.info('cc2="%s"' % cc2)
1092| Log.info('cc3="%s"' % cc3)
1093| Log.info('cc4="%s"' % cc4)
1094| Log.info()
1095|
1096| cols = [2,3,4,7,62]
1097| ctry = owid.country(cc1, cols=cols)
1098|
1099| for t in ctry:
1100| Log.info('%s' % str(t))
1101|
1102| Log.info()
1103| return 'Done'
1104|
1105| def do_test (owid, **kwargs):
1106| Log.info('test:: %s' % str(list(kwargs.keys())))
1107| fpath = kwargs['fpath'] if 'fpath' in kwargs else BasePath
1108| fname = kwargs['fname'] if 'fname' in kwargs else 'test.out'
1109| Log.info('fpath="%s"' % fpath)
1110| Log.info('fname="%s"' % fname)
1111| Log.info()
1112| f3 = lambda r: (100.0*(r.tcase / r.tpop)) if 0 < r.tpop else 0.0
1113| f4 = lambda r: (100.0*(r.tdead / r.tpop)) if 0 < r.tpop else 0.0
1114|
1115|
1116|
1117|
1118|
1119|
1120|
1121|
1122|
1123|
1124|
1125|
1126|
1127|
1128|
1129| g = [(cc,int(owid.stats[cc].tpop)) for cc in owid.ccs]
1130| for ix,t in enumerate(sorted(g, key=lambda x:x[1], reverse=True)):
1131| n = owid.cname(t[0])
1132| Log.info(f'{ix+1:3d}: {n} = {t[1]:,}')
1133|
1134| Log.info()
1135| return 'Done'
1136|
1137| def dispatch (cmd, **kwargs):
1138| Log.info('command: %s' % cmd)
1139| for kw in kwargs:
1140| Log.info('argument[%s]: %s' % (kw,kwargs[kw]))
1141| Log.info()
1142|
1143| owid = owid_covid_data()
1144|
1145| if cmd == 'c0': return do_chart_0(owid, **kwargs)
1146| if cmd == 'c1': return do_chart_1(owid, **kwargs)
1147| if cmd == 'c2': return do_chart_2(owid, **kwargs)
1148| if cmd == 'c3': return do_chart_3(owid, **kwargs)
1149| if cmd == 'c4': return do_chart_4(owid, **kwargs)
1150| if cmd == 'c5': return do_chart_5(owid, **kwargs)
1151| if cmd == 'c6': return do_chart_6(owid, **kwargs)
1152| if cmd == 'c7': return do_chart_7(owid, **kwargs)
1153| if cmd == 'c8': return do_chart_8(owid, **kwargs)
1154| if cmd == 'c9': return do_chart_9(owid, **kwargs)
1155| if cmd == 'all':
1156| do_chart_1(owid, **kwargs)
1157| do_chart_2(owid, **kwargs)
1158| do_chart_3(owid, **kwargs)
1159| do_chart_4(owid, **kwargs)
1160| do_chart_5(owid, **kwargs)
1161| do_chart_6(owid, **kwargs)
1162| do_chart_7(owid, **kwargs)
1163| do_chart_8(owid, **kwargs)
1164| do_chart_9(owid, **kwargs)
1165| return 'All Done'
1166| if cmd == 'pie': return do_chart_pie(owid, **kwargs)
1167| if cmd == 'rank': return do_percentage_ranking(owid, **kwargs)
1168| if cmd == 'list': return do_owid_list(owid, **kwargs)
1169| if cmd == 'test': return do_test(owid, **kwargs)
1170| return 'Nothing to do!'
1171|
1172| if __name__ == '__main__':
1173| print('autorun: %s' % argv[0])
1174| Log.start(path.join(BasePath,'owid-covid.log'))
1175| Log.level(debug())
1176| cmd = argv[1] if 1 < len(argv) else CMND
1177| kwargs = ARGS
1178| for a in argv[2:]:
1179| nv = a.split('=', 2)
1180| kwargs[nv[0]] = (nv[1] if 1<len(nv) else '')
1181| Log.debug('%s: "%s"' % (nv[0], kwargs[nv[0]]))
1182| try:
1183| obj = dispatch(cmd, **kwargs)
1184| print()
1185| print(obj)
1186| Log.info()
1187| Log.info(obj)
1188| except:
1189| etype, evalue, tb = exc_info()
1190| ts = extract_tb(tb)
1191| Log.error()
1192| Log.error('%s: %s' % (etype.__name__,str(evalue)))
1193| for t in ts[-5:]:
1194| Log.error('[%d] %s (%s)' % (t[1], t[2], t[0]))
1195| Log.error(' %s' % t[3])
1196| raise
1197| finally:
1198| Log.end()
1199|
1200| '''eof'''
1201|