1
2
3 """
4 Module with specialized plotting functions for the Melding results
5 """
6
7 __author__="fccoelho"
8 __date__ ="$06/01/2010 11:24:20$"
9 __docformat__ = "restructuredtext en"
10
11 from itertools import cycle
12 from scipy import stats
13 import glob
14 from numpy import *
15 import matplotlib.pyplot as P
16 from scipy.stats import gaussian_kde
17
18
20 c = cycle(['b','g','r','c','m','y','k'])
21 if not names:
22 names = series.dtype.names
23
24 for i,n in enumerate(names):
25
26
27 co = c.next()
28 P.plot(tim,[stats.scoreatpercentile(t,5) for t in series[n].T],co+'-.')
29 P.plot(tim,[stats.scoreatpercentile(t,95) for t in series[n].T],co+'-.')
30 P.plot(tim,[stats.scoreatpercentile(t,50) for t in series[n].T],co+'-',lw=2,label=n)
31
32 if n in y:
33 P.plot(tim,y[n],'*',label=n+' obs.')
34 P.savefig(title+'.png')
35
36 -def plot_pred(tim,series,y, fig,names=[],title='series'):
37 c = cycle(['b','g','r','c','m','y','k'])
38 if not names:
39 names = series.dtype.names
40 l = len(names)
41 ax = fig.add_subplot(111)
42 for i,n in enumerate(names):
43 co = c.next()
44 for b in [2.5,25,50,75,97.5]:
45 if b == 50:
46 st = 'k-'
47 else:
48 st = '%s:'%co
49 try:
50 lower = [stats.scoreatpercentile(t,0+b) for t in series[n].T]
51 upper =[stats.scoreatpercentile(t,100-b) for t in series[n].T]
52 ax.plot(tim,lower,st,tim,upper,st,label=n)
53 ax.fill_between(tim,lower,upper,facecolor=co,alpha=0.12)
54 except:
55 pass
56 if n in y:
57
58 try:
59 P.plot(tim,y[n],'^',label=n+' obs.')
60 except:
61 pass
62 P.savefig(title+'.png')
63
65 """
66 Predicted total new cases in a week vs oserved.
67 """
68 fig =P.figure()
69 P.title('Total new cases per week: predicted vs observed')
70 if not names:
71 names = series.dtype.names
72 ax = fig.add_subplot(111)
73
74 for n in names:
75 ax.boxplot([sum(s[n],axis=1) for s in series] ,positions = range(weeks),notch=1,vert=1)
76 ax.plot(range(weeks),[mean(sum(s[n],axis=1)) for s in series],'^', label="Mean pred. %s"%n)
77 ax.plot(range(weeks-1),[sum(obs[n][(w+1)*ws:(w+1)*ws+ws]) for w in range(weeks-1)],'r-o', label="obs. Prev")
78 P.xlabel('weeks')
79 ax.legend(loc=0)
80
81 -def plot_series2(tim,obs,series,names=[],tit='Simulated vs Observed series',ws=7,lag=False):
82 ser2={}
83 for n in series[0].dtype.names:
84 ser2[n] = concatenate([s[n] for s in series],axis=1)
85
86 fig =P.figure()
87 P.title(tit)
88 if not names:
89 names = ser2.keys()
90 ax = fig.add_subplot(111)
91 c = cycle(['b','g','r','c','m','y','k'])
92 print len(tim), len(obs['I']),len(median(ser2['I'],axis=0))
93 for n in names:
94 co = c.next()
95 ax.plot(tim,obs[n],'%s+'%co, label="Observed %s"%n)
96 ax.plot(array(tim)+ws*int(lag),median(ser2[n],axis=0),'k-')
97 lower = [stats.scoreatpercentile(t,2) for t in ser2[n].T]
98 upper =[stats.scoreatpercentile(t,98) for t in ser2[n].T]
99 ax.fill_between(array(tim)+ws*int(lag),lower,upper,facecolor=co,alpha=0.6)
100 P.xlabel('days')
101 ax.legend()
102
104 P.figure()
105 P.title('Parameters temporal variation')
106 for i,n in enumerate(ptlist[0].dtype.names):
107 P.subplot(3,1,i+1)
108 P.boxplot([s[n] for s in ptlist],notch=1,positions=tim,vert=1)
109
110 P.ylabel(n)
111 P.xlabel('Weeks')
112
114 fig = P.figure()
115
116 for i,n in enumerate(ptlist[0].dtype.names):
117 ax = fig.add_subplot(3,1,i+1)
118 violin_plot(ax,[s[n] for s in ptlist],tim,bp=True)
119 P.ylabel(n)
120 P.xlabel('Weeks')
121
123 '''
124 Create violin plots on an axis
125 '''
126 dist = max(positions)-min(positions)
127 w = min(0.15*max(dist,1.0),0.5)
128 for d,p in zip(data,positions):
129 k = gaussian_kde(d)
130 m = k.dataset.min()
131 M = k.dataset.max()
132 x = arange(m,M,(M-m)/100.)
133 v = k.evaluate(x)
134 v = v/v.max()*w
135 ax.fill_betweenx(x,p,v+p,facecolor='y',alpha=0.3)
136 ax.fill_betweenx(x,p,-v+p,facecolor='y',alpha=0.3)
137 if bp:
138 ax.boxplot(data,notch=1,positions=positions,vert=1)
139
140 if __name__ == "__main__":
141 print "Hello World";
142