forked from pcubillos/mc3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcplots.py
459 lines (406 loc) · 13.8 KB
/
mcplots.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# ******************************* START LICENSE *****************************
#
# Multi-Core Markov-chain Monte Carlo (MC3), a code to estimate
# model-parameter best-fitting values and Bayesian posterior
# distributions.
#
# This project was completed with the support of the NASA Planetary
# Atmospheres Program, grant NNX12AI69G, held by Principal Investigator
# Joseph Harrington. Principal developers included graduate student
# Patricio E. Cubillos and programmer Madison Stemm. Statistical advice
# came from Thomas J. Loredo and Nate B. Lust.
#
# Copyright (C) 2015 University of Central Florida. All rights reserved.
#
# This is a test version only, and may not be redistributed to any third
# party. Please refer such requests to us. This program is distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.
#
# Our intent is to release this software under an open-source,
# reproducible-research license, once the code is mature and the first
# research paper describing the code has been accepted for publication
# in a peer-reviewed journal. We are committed to development in the
# open, and have posted this code on github.com so that others can test
# it and give us feedback. However, until its first publication and
# first stable release, we do not permit others to redistribute the code
# in either original or modified form, nor to publish work based in
# whole or in part on the output of this code. By downloading, running,
# or modifying this code, you agree to these conditions. We do
# encourage sharing any modifications with us and discussing them
# openly.
#
# We welcome your feedback, but do not guarantee support. Please send
# feedback or inquiries to:
#
# Joseph Harrington <[email protected]>
# Patricio Cubillos <[email protected]>
#
# or alternatively,
#
# Joseph Harrington and Patricio Cubillos
# UCF PSB 441
# 4111 Libra Drive
# Orlando, FL 32816-2385
# USA
#
# Thank you for using MC3!
# ******************************* END LICENSE *******************************
import sys, os
import numpy as np
import matplotlib as mpl
mpl.use("Agg")
import matplotlib.pyplot as plt
sys.path.append(os.path.dirname(os.path.realpath(__file__))+'/cfuncs/lib')
import binarray as ba
def trace(allparams, title=None, parname=None, thinning=1,
fignum=-10, savefile=None, fmt=".", sep=None):
"""
Plot parameter trace MCMC sampling
Parameters:
-----------
allparams: 2D ndarray
An MCMC sampling array with dimension (number of parameters,
sampling length).
title: String
Plot title.
parname: Iterable (strings)
List of label names for parameters. If None use ['P0', 'P1', ...].
thinning: Integer
Thinning factor for plotting (plot every thinning-th value).
fignum: Integer
The figure number.
savefile: Boolean
If not None, name of file to save the plot.
fmt: String
The format string for the line and marker.
sep: Integer
Number of samples per chain. If not None, draw a vertical line
to mark the separation between the chains.
Uncredited Developers:
----------------------
- Kevin Stevenson (UCF)
"""
# Get number of parameters and length of chain:
npars, niter = np.shape(allparams)
fs = 14
# Set default parameter names:
if parname is None:
namelen = int(2+np.log10(np.amax([npars-1,1])))
parname = np.zeros(npars, "|S%d"%namelen)
for i in np.arange(npars):
parname[i] = "P" + str(i).zfill(namelen-1)
# Get location for chains separations:
xmax = len(allparams[0,0::thinning])
if sep is not None:
xsep = np.arange(sep/thinning, xmax, sep/thinning)
# Make the trace plot:
plt.figure(fignum, figsize=(8,8))
plt.clf()
if title is not None:
plt.suptitle(title, size=16)
plt.subplots_adjust(left=0.15, right=0.95, bottom=0.10, top=0.90,
hspace=0.15)
for i in np.arange(npars):
a = plt.subplot(npars, 1, i+1)
plt.plot(allparams[i, 0::thinning], fmt)
yran = a.get_ylim()
if sep is not None:
plt.vlines(xsep, yran[0], yran[1], "0.3")
plt.xlim(0, xmax)
plt.ylim(yran)
plt.ylabel(parname[i], size=fs, multialignment='center')
plt.yticks(size=fs)
if i == npars - 1:
plt.xticks(size=fs)
if thinning > 1:
plt.xlabel('MCMC (thinned) iteration', size=fs)
else:
plt.xlabel('MCMC iteration', size=fs)
else:
plt.xticks(visible=False)
if savefile is not None:
plt.savefig(savefile)
def pairwise(allparams, title=None, parname=None, thinning=1,
fignum=-11, savefile=None, style="hist"):
"""
Plot parameter pairwise posterior distributions
Parameters:
-----------
allparams: 2D ndarray
An MCMC sampling array with dimension (number of parameters,
sampling length).
title: String
Plot title.
parname: Iterable (strings)
List of label names for parameters. If None use ['P0', 'P1', ...].
thinning: Integer
Thinning factor for plotting (plot every thinning-th value).
fignum: Integer
The figure number.
savefile: Boolean
If not None, name of file to save the plot.
style: String
Choose between 'hist' to plot as histogram, or 'points' to plot
the individual points.
Uncredited Developers:
----------------------
- Kevin Stevenson (UCF)
- Ryan Hardy (UCF)
"""
# Get number of parameters and length of chain:
npars, niter = np.shape(allparams)
# Don't plot if there are no pairs:
if npars == 1:
return
# Set default parameter names:
if parname is None:
namelen = int(2+np.log10(np.amax([npars-1,1])))
parname = np.zeros(npars, "|S%d"%namelen)
for i in np.arange(npars):
parname[i] = "P" + str(i).zfill(namelen-1)
fs = 14
# Set palette color:
palette = plt.matplotlib.colors.LinearSegmentedColormap('YlOrRd2',
plt.cm.datad['YlOrRd'], 256)
palette.set_under(alpha=0.0)
palette.set_bad(alpha=0.0)
fig = plt.figure(fignum, figsize=(8,8))
plt.clf()
if title is not None:
plt.suptitle(title, size=16)
h = 1 # Subplot index
plt.subplots_adjust(left=0.15, right=0.95, bottom=0.15, top=0.9,
hspace=0.05, wspace=0.05)
for j in np.arange(1, npars): # Rows
for i in np.arange(npars-1): # Columns
if j > i:
a = plt.subplot(npars-1, npars-1, h)
# Y labels:
if i == 0:
plt.yticks(size=fs)
plt.ylabel(parname[j], size=fs, multialignment='center')
else:
a = plt.yticks(visible=False)
# X labels:
if j == npars-1:
plt.xticks(size=fs, rotation=90)
plt.xlabel(parname[i], size=fs)
else:
a = plt.xticks(visible=False)
# The plot:
if style=="hist":
hist2d, xedges, yedges = np.histogram2d(allparams[i, 0::thinning],
allparams[j, 0::thinning], 20, normed=False)
vmin = 0.0
hist2d[np.where(hist2d == 0)] = np.nan
a = plt.imshow(hist2d.T, extent=(xedges[0], xedges[-1], yedges[0],
yedges[-1]), cmap=palette, vmin=vmin, aspect='auto',
origin='lower', interpolation='bilinear')
elif style=="points":
a = plt.plot(allparams[i], allparams[j], ",")
h += 1
# The colorbar:
if style == "hist":
if npars > 2:
a = plt.subplot(2, 6, 5, frameon=False)
a.yaxis.set_visible(False)
a.xaxis.set_visible(False)
bounds = np.linspace(0, 1.0, 64)
norm = mpl.colors.BoundaryNorm(bounds, palette.N)
ax2 = fig.add_axes([0.85, 0.535, 0.025, 0.36])
cb = mpl.colorbar.ColorbarBase(ax2, cmap=palette, norm=norm,
spacing='proportional', boundaries=bounds, format='%.1f')
cb.set_label("Normalized point density", fontsize=fs)
cb.set_ticks(np.linspace(0, 1, 5))
plt.draw()
# Save file:
if savefile is not None:
plt.savefig(savefile)
def histogram(allparams, title=None, parname=None, thinning=1,
fignum=-12, savefile=None):
"""
Plot parameter marginal posterior distributions
Parameters:
-----------
allparams: 2D ndarray
An MCMC sampling array with dimension (number of parameters,
sampling length).
title: String
Plot title.
parname: Iterable (strings)
List of label names for parameters. If None use ['P0', 'P1', ...].
thinning: Integer
Thinning factor for plotting (plot every thinning-th value).
fignum: Integer
The figure number.
savefile: Boolean
If not None, name of file to save the plot.
Uncredited Developers:
----------------------
- Kevin Stevenson (UCF)
"""
# Get number of parameters and length of chain:
npars, niter = np.shape(allparams)
fs = 14 # Fontsize
# Set default parameter names:
if parname is None:
namelen = int(2+np.log10(np.amax([npars-1,1])))
parname = np.zeros(npars, "|S%d"%namelen)
for i in np.arange(npars):
parname[i] = "P" + str(i).zfill(namelen-1)
# Set number of rows:
if npars < 10:
nrows = (npars - 1)/3 + 1
else:
nrows = (npars - 1)/4 + 1
# Set number of columns:
if npars > 9:
ncolumns = 4
elif npars > 4:
ncolumns = 3
else:
ncolumns = (npars+2)/3 + (npars+2)%3 # (Trust me!)
histheight = np.amin((2 + 2*(nrows), 8))
if nrows == 1:
bottom = 0.25
else:
bottom = 0.15
plt.figure(fignum, figsize=(8, histheight))
plt.clf()
plt.subplots_adjust(left=0.1, right=0.95, bottom=bottom, top=0.9,
hspace=0.4, wspace=0.1)
if title is not None:
a = plt.suptitle(title, size=16)
maxylim = 0 # Max Y limit
for i in np.arange(npars):
ax = plt.subplot(nrows, ncolumns, i+1)
a = plt.xticks(size=fs, rotation=90)
if i%ncolumns == 0:
a = plt.yticks(size=fs)
else:
a = plt.yticks(visible=False)
plt.xlabel(parname[i], size=fs)
a = plt.hist(allparams[i,0::thinning], 20, normed=False)
maxylim = np.amax((maxylim, ax.get_ylim()[1]))
# Set uniform height:
for i in np.arange(npars):
ax = plt.subplot(nrows, ncolumns, i+1)
ax.set_ylim(0, maxylim)
if savefile is not None:
plt.savefig(savefile)
def RMS(binsz, rms, stderr, rmserr, cadence=None, binstep=1,
timepoints=[], ratio=False, fignum=-20,
yran=None, xran=None, savefile=None):
"""
Plot the RMS vs binsize
Parameters:
-----------
binsz: 1D ndarray
Array of bin sizes.
rms: 1D ndarray
RMS of dataset at given binsz.
stderr: 1D ndarray
Gaussian-noise rms Extrapolation
rmserr: 1D ndarray
RMS uncertainty
cadence: Float
Time between datapoints in seconds.
binstep: Integer
Plot every-binstep point.
timepoints: List
Plot a vertical line at each time-points.
ratio: Boolean
If True, plot rms/stderr, else, plot both curves.
fignum: Integer
Figure number
yran: 2-elements tuple
Minimum and Maximum y-axis ranges.
xran: 2-elements tuple
Minimum and Maximum x-axis ranges.
savefile: String
If not None, name of file to save the plot.
"""
if np.size(rms) <= 1:
return
# Set cadence:
if cadence is None:
cadence = 1.0
xlabel = "Bin size"
else:
xlabel = "Bin size (sec)"
# Set plotting limits:
if yran is None:
#yran = np.amin(rms), np.amax(rms)
yran = [np.amin(rms-rmserr), np.amax(rms+rmserr)]
yran[0] = np.amin([yran[0],stderr[-1]])
if ratio:
yran = [0, np.amax(rms/stderr) + 1.0]
if xran is None:
xran = [cadence, np.amax(binsz*cadence)]
fs = 14 # Font size
if ratio:
ylabel = r"$\beta =$ RMS / std. error"
else:
ylabel = "RMS"
plt.figure(fignum, (8,6))
plt.clf()
ax = plt.subplot(111)
if ratio: # Plot the residuals-to-Gaussian RMS ratio:
a = plt.errorbar(binsz[::binstep]*cadence, (rms/stderr)[::binstep],
(rmserr/stderr)[::binstep], fmt='k-', ecolor='0.5',
capsize=0, label="__nolabel__")
a = plt.semilogx(xran, [1,1], "r-", lw=2)
else: # Plot residuals and Gaussian RMS individually:
# Residuals RMS:
a = plt.errorbar(binsz[::binstep]*cadence, rms[::binstep],
rmserr[::binstep], fmt='k-', ecolor='0.5',
capsize=0, label="RMS")
# Gaussian noise projection:
a = plt.loglog(binsz*cadence, stderr, color='red', ls='-',
lw=2, label="Gaussian std.")
a = plt.legend()
for time in timepoints:
a = plt.vlines(time, yran[0], yran[1], 'b', 'dashed', lw=2)
a = plt.yticks(size=fs)
a = plt.xticks(size=fs)
a = plt.ylim(yran)
a = plt.xlim(xran)
a = plt.ylabel(ylabel, fontsize=fs)
a = plt.xlabel(xlabel, fontsize=fs)
if savefile is not None:
plt.savefig(savefile)
def modelfit(data, uncert, indparams, model, nbins=75, title=None,
fignum=-22, savefile=None, fmt="."):
"""
Doc me!
"""
# Bin down array:
binsize = (np.size(data)-1)/nbins + 1
bindata, binuncert, binindp = ba.binarray(data, uncert, indparams, binsize)
binmodel = ba.weightedbin(model, binsize)
fs = 14 # Font-size
p = plt.figure(fignum, figsize=(8,6))
p = plt.clf()
# Residuals:
a = plt.axes([0.15, 0.1, 0.8, 0.2])
p = plt.errorbar(binindp, bindata-binmodel, binuncert, fmt='ko', ms=4)
p = plt.plot([indparams[0], indparams[-1]], [0,0],'k:',lw=1.5)
p = plt.xticks(size=fs)
p = plt.yticks(size=fs)
p = plt.xlabel("x", size=fs)
p = plt.ylabel('Residuals', size=fs)
# Data and Model:
a = plt.axes([0.15, 0.35, 0.8, 0.55])
if title is not None:
p = plt.title(title, size=fs)
p = plt.errorbar(binindp, bindata, binuncert, fmt='ko', ms=4,
label='Binned Data')
p = plt.plot(indparams, model, "b", lw=2, label='Best Fit')
p = plt.setp(a.get_xticklabels(), visible = False)
p = plt.yticks(size=13)
p = plt.ylabel('y', size=fs)
p = plt.legend(loc='best')
if savefile is not None:
p = plt.savefig(savefile)