Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALInference 4.1.9.1-b246709
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
spindisk.py
Go to the documentation of this file.
1# Ben Farr 2016
2
3from __future__ import division
4
5import numpy as np
6import os
7
8__all__=('make_disk_plot',)
9
10def make_disk_plot(post,outpath=None):
11 from matplotlib import pyplot as plt
12 from matplotlib import rc
13 rc('text', usetex=False)
14 rc('font', family='lmodern')
15
16 small, big = 10, 12
17 rc('axes', labelsize=big)
18 rc('font', size=big)
19 rc('legend', fontsize=small)
20 rc('xtick', labelsize=small)
21 rc('ytick', labelsize=small)
22
23
24 try:
25 import corner # noqa: F401
26 except ImportError:
27 print("cannot import corner. Won't plot spin disk")
28 return None
29
30 a1='a1'
31 tilt1='tilt1'
32 a2='a2'
33 tilt2='tilt2'
34
35 names=post.names
36
37 if not set([a1,a2,tilt1,tilt2]).issubset(names):
38 print("Cannot plot spin disk plot. Not all required spin parameters exist in the posterior file. Skipping...\n")
39 return None
40
41 # Spin disk plot
42 fig, axs = plt.subplots(1, 2, sharey=True, figsize=(4, 4))
43
44 Na, Nt = 20, 30
45 xticks = [0., .25, .5, .75, 1.]
46
47 vmin, vmax = 0., 0.
48 # Work out the colour scale
49 for a, tilt in zip([a1, a2], [tilt1, tilt2]):
50 asamps=(post[a].samples).flatten()
51 tsamps=(post[tilt].samples).flatten()
52 try:
53 H, _, _ = np.histogram2d(asamps, np.cos(tsamps), range=[[0, 1], [-1, 1]], bins=(Na, Nt), density=True)
54 except TypeError:
55 # numpy < 1.15 uses normed instead of density
56 H, _, _ = np.histogram2d(asamps, np.cos(tsamps), range=[[0, 1], [-1, 1]], bins=(Na, Nt), normed=True)
57 vmax = H.max() if H.max() > vmax else vmax
58
59 # Make the plots
60 for ax, a, tilt, flip in zip(axs, [a1, a2], [tilt1, tilt2], [True, False]):
61 asamps=(post[a].samples).flatten()
62 tsamps=(post[tilt].samples).flatten()
63 plt.sca(ax)
64 try:
65 H, rs, costs = np.histogram2d(asamps, np.cos(tsamps), range=[[0, 1], [-1, 1]], bins=(Na, Nt), density=True)
66 except TypeError:
67 # numpy < 1.15 uses normed instead of density
68 H, rs, costs = np.histogram2d(asamps, np.cos(tsamps), range=[[0, 1], [-1, 1]], bins=(Na, Nt), normed=True)
69
70 COSTS, RS = np.meshgrid(costs, rs)
71 X = RS * np.sin(np.arccos(COSTS))
72 Y = RS * COSTS
73
74 HS = np.column_stack((X.flatten(), Y.flatten()))
75 XS = np.reshape(HS[:,0], (Na+1,Nt+1))
76 YS = np.reshape(HS[:,1], (Na+1,Nt+1))
77
78 plt.pcolormesh(XS, YS, H, vmin=vmin, vmax=vmax, edgecolor='face', cmap='Greys')
79
80 ax.set_ylim((-1., 1.))
81 ax.set_xlim((0., 1.))
82 if flip:
83 ax.set_xticks(xticks[1:])
84 ax.invert_xaxis()
85 else:
86 ax.set_xticks(xticks)
87 ax.yaxis.tick_right()
88 ax.yaxis.set_label_position("right")
89
90 axs[0].set_xlabel(r'$|\mathbf{S_1} \times \mathbf{\hat{L}}|$')
91 axs[1].set_xlabel(r'$|\mathbf{S_2} \times \mathbf{\hat{L}}|$')
92 axs[0].set_ylabel(r'$\mathbf{S_1}\cdot\mathbf{\hat{L}}$')
93 axs[1].set_ylabel(r'$\mathbf{S_2}\cdot\mathbf{\hat{L}}$')
94
95 fig.subplots_adjust(wspace=0.04)
96 cax = fig.add_axes([0.06, -0.075, 0.9, 0.05])
97
98 cbar = plt.colorbar(orientation='horizontal', cax=cax)
99 cbar.formatter.set_powerlimits((-1, 1))
100 cbar.update_ticks()
101 cbar.set_label('posterior probability')
102 cbar.solids.set_edgecolor("face")
103 plt.savefig(os.path.join(outpath,"comp_spin_pos.png"), bbox_inches='tight')
104 plt.clf()
def make_disk_plot(post, outpath=None)
Definition: spindisk.py:10