-
Notifications
You must be signed in to change notification settings - Fork 2
/
flowOutput.py
202 lines (161 loc) · 6.28 KB
/
flowOutput.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from numpy import *
import scipy.stats as st
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
matplotlib.rc('xtick', labelsize=4)
matplotlib.rc('ytick', labelsize=4)
from scipy.stats import norm
class output_handler:
def __init__(self):
pass
def plot_sim_res_1D(self, file, results, ntimes):
pp = PdfPages(file)
nBins = 50
for jt in range(ntimes):
plt.subplot(4,5,jt+1)
plt.hist( results[0,:,jt,0], nBins )
plt.xlim(0,1500)
# plt.xscale('log')
pp.savefig()
plt.close()
pp.close()
def plot_data_dict_1D(self, results_path, file_n, data, timepoints):
print 'plotting now...'
pp = PdfPages(results_path+'/'+file_n)
#nBins = 50
cc = 0
xmin, xmax = -1, 4
x_grid = linspace(xmin, xmax, 1000)
for tp in timepoints:
dat = log10(1+data[tp][:,0])
dat[isneginf(dat)] = 0
#print dat
kde = st.gaussian_kde(dat, bw_method=0.2)
pdf = kde.evaluate(x_grid)
ax = plt.subplot(4, 5, cc + 1)
ax.plot(x_grid, pdf, color='blue', alpha=0.5, lw=3)
ax.set_xlim([-1, 7])
#plt.hist( data[tp], nBins )
#plt.xscale('log')
cc += 1
pp.savefig()
plt.close()
pp.close()
def plot_data_dict_2D(self, results_path, file_n, data, timepoints):
pp = PdfPages(results_path+'/'+file_n)
cc = 0
xmin, xmax = -1, 4
for tp in timepoints:
#print 'tp', tp
xmin, xmax = 0, 3
ymin, ymax = 0, 3
ax = plt.subplot(4,5,cc+1)
if 0:
xx, yy = mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = vstack([xx.ravel(), yy.ravel()])
values = vstack([ log10(data[tp][:,0]), log10(data[tp][:,1]) ])
values[isneginf(values)] = 0
kernel = st.gaussian_kde(values)
f = reshape(kernel(positions).T, xx.shape)
ax.contourf(xx, yy, f, cmap='Blues')
if 1:
ax.scatter(log10(1+data[tp][:,0]), log10(1+data[tp][:,1]), s=0.1)
ax.set_xlim([xmin,xmax])
ax.set_ylim([ymin,ymax])
cc += 1
pp.savefig()
plt.close()
pp.close()
def plot_data_comb_2D(self, results_path, file_n, data, fit, timepoints):
pp = PdfPages(results_path+'/'+file_n)
cc = 0
for tp in timepoints:
xmin, xmax = -3, 3
ymin, ymax = -3, 3
xx, yy = mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions = vstack([xx.ravel(), yy.ravel()])
values = vstack([ log10(1+data[tp][:, 0]), log10(1+data[tp][:, 1])])
kernel = st.gaussian_kde(values)
f = reshape(kernel(positions).T, xx.shape)
xxf, yyf = mgrid[xmin:xmax:100j, ymin:ymax:100j]
positions_f = vstack([xxf.ravel(), yyf.ravel()])
values_f = vstack([log10(1+fit[tp][:, 0]), log10(1+fit[tp][:, 1])])
kernel_f = st.gaussian_kde(values_f)
ff = reshape(kernel_f(positions_f).T, xxf.shape)
ax = plt.subplot(4, 5, cc+1)
ax.contourf(xx, yy, f, cmap='Blues')
ax.contourf(xxf, yyf, ff, cmap='Reds')
ax.set_xlim([-1, 3])
ax.set_ylim([-1, 3])
cc += 1
pp.savefig()
plt.close()
pp.close()
def make_qq_plots(self, results_path, file_n, data, sims, timepoints, ind=0):
pp = PdfPages(results_path+'/'+file_n)
cc = 0
for tp in timepoints:
plt.subplot(4, 5, cc+1)
y = sorted(data[tp][:, ind])
x = sorted(sims[tp][:, ind])
plt.plot( x, y, ".")
ax = plt.gca()
xlim=ax.get_xlim()
ylim=ax.get_ylim()
lims = [ min([xlim,ylim]), max([xlim,ylim]) ]
plt.plot(lims, lims, 'r-', linewidth=3)
cc += 1
pp.savefig()
plt.close()
pp.close()
def make_comp_plot_1D(self, results_path, file_n, data, sims, timepoints, ind=0):
pp = PdfPages(results_path+'/'+file_n)
cc = 0
xmin, xmax = -1, 7
x_grid = linspace(xmin, xmax, 1000)
def kernel_est(d, ind, x_grid):
dl = log10(1+d[:, ind])
#dl[isneginf(dl)] = 0
dl = dl[isfinite(dl)]
kde = st.gaussian_kde(dl, bw_method=0.2)
pdf = kde.evaluate(x_grid)
return pdf
for tp in timepoints:
pdf_data = kernel_est(data[tp], ind, x_grid)
pdf_sim = kernel_est(sims[tp], ind, x_grid)
ax = plt.subplot(4, 5, cc + 1)
ax.plot(x_grid, pdf_data, color='blue', alpha=0.5, lw=3)
ax.plot(x_grid, pdf_sim, color='red', alpha=0.5, lw=3)
cc += 1
pp.savefig()
plt.close()
pp.close()
def make_post_hists(self, results_path, file_n, post, npar):
pp = PdfPages(results_path+'/'+file_n)
#npar = len(pars)
for i in range(npar):
plt.subplot(1, npar, i)
plt.hist(post[:, i-1], normed=1)
pp.savefig()
plt.close()
pp.close()
def write_post_params_to_file(self, results_path, file_n, post, pars):
savetxt(results_path+'/'+file_n, post, delimiter=' ')
def write_post_weight_to_file(self, results_path, file_n, wgts, pars):
savetxt(results_path+'/'+file_n, wgts, delimiter=' ')
def write_post_data_to_file(results_path, file_n, data, timepoints):
values = []
for tp in timepoints:
#print data[tp]
for sp in range(len(data[tp])):
#print "\t", data[tp][sp]
values.append([tp, data[tp][sp][0][0], data[tp][sp][0][1]])
savetxt(results_path+'/'+file_n, asarray(values), delimiter=' ')
#def write_post_data_to_file(self, results_path, file_n, data, timepoints):
# values = []
# for tp in timepoints:
# for sp in range(len(data[tp][:, 0])):
# values.append([tp, data[tp][sp, 0], data[tp][sp, 1]])
# savetxt(results_path+'/'+file_n, asarray(values), delimiter=' ')