Coverage for pesummary/gw/notebook/public.py: 96.5%

57 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-05-02 08:42 +0000

1# Licensed under an MIT style license -- see LICENSE.md 

2 

3from pesummary.io import read 

4from pesummary.core.notebook import ( 

5 NoteBook, imports, pesummary_read, posterior_samples, 

6 samples_dict_plot 

7) 

8from .notebook import psd_plot 

9 

10__author__ = ["Charlie Hoy <charlie.hoy@ligo.org>"] 

11 

12 

13def make_public_notebook( 

14 pesummary_file, publication_title, dcc_link=".", event="", 

15 default_analysis=None, default_parameter="mass_1", 

16 corner_parameters=["mass_1", "mass_2", "luminosity_distance", "iota"], 

17 filename="posterior_samples.ipynb", outdir="./", comparison_analysis=None 

18): 

19 """Make a jupyter notebook showing how to use the PESummary result file 

20 

21 Parameters 

22 ---------- 

23 """ 

24 nb = NoteBook() 

25 f = read(pesummary_file) 

26 if default_analysis is None: 

27 default_analysis = f.labels[0] 

28 elif default_analysis not in f.labels: 

29 raise ValueError( 

30 "The analysis '{}' does not exist in '{}'. The available analyses " 

31 "are {}".format( 

32 default_analysis, pesummary_file, ", ".join(f.labels) 

33 ) 

34 ) 

35 cell = ( 

36 "# Sample release{}\nThis notebook serves as a basic introduction to " 

37 "loading and viewing data\nreleased in associaton with the publication " 

38 "titled {}{}\n\nThe released data file can be read in using the " 

39 "PESummary or h5py libraries. For general instructions on how to " 

40 "manipulate the data file and/or read this data file with h5py, see the " 

41 "[PESummary docs](https://lscsoft.docs.ligo.org/pesummary)".format( 

42 event, publication_title, dcc_link 

43 ) 

44 ) 

45 nb.add_cell(cell, markdown=True) 

46 text, cell = imports( 

47 module_imports=["pesummary", "pesummary.io:read"], 

48 extra_lines=["print(pesummary.__version__)"] 

49 ) 

50 nb.add_cell(text, markdown=True) 

51 nb.add_cell(cell, code=True) 

52 text, cell = pesummary_read( 

53 pesummary_file, read_variable="data", 

54 text=( 

55 "As part of this sample release, we are releasing the posterior " 

56 "samples generated from {} different analyses. The samples for " 

57 "each analysis is stored in the data file. This data file " 

58 "can be read in using the 'pesummary' read function".format( 

59 len(f.labels) 

60 ) 

61 ) 

62 ) 

63 nb.add_cell(text, markdown=True) 

64 nb.add_cell(cell, code=True) 

65 text, cell = posterior_samples( 

66 "data", metafile=True, default_analysis=default_analysis, 

67 print_parameters=True, samples_variable="posterior_samples" 

68 ) 

69 nb.add_cell(text, markdown=True) 

70 nb.add_cell(cell, code=True) 

71 cell = "## {} analysis".format(default_analysis) 

72 nb.add_cell(cell, markdown=True) 

73 text, cell = samples_dict_plot( 

74 "posterior_samples", plot_kwargs={"type": "'hist'", "kde": True}, 

75 plot_args=["'{}'".format(default_parameter)], text=( 

76 "'pesummary' allows for the user to easily make plots. As an " 

77 "example, we show the posterior distribution for '{}' plotted " 

78 "as a KDE.".format(default_parameter) 

79 ), extra_lines=["fig.set_size_inches(12, 8)", "fig.show()"] 

80 ) 

81 nb.add_cell(text, markdown=True) 

82 nb.add_cell(cell, code=True) 

83 

84 samples = f.samples_dict[default_analysis] 

85 spin_params = ["a_1", "a_2", "cos_tilt_1", "cos_tilt_2"] 

86 if all(param in samples.keys() for param in spin_params): 

87 text, cell = samples_dict_plot( 

88 "posterior_samples", plot_kwargs={ 

89 "type": "'spin_disk'", "colorbar": True, "annotate": True, 

90 "show_label": True, "cmap": "'Blues'" 

91 }, extra_lines=["fig.show()"], text=( 

92 "We may also easily generate a spin disk, showing the most " 

93 "probable direction of the spin vectors" 

94 ) 

95 ) 

96 nb.add_cell(text, markdown=True) 

97 nb.add_cell(cell, code=True) 

98 

99 text, cell = samples_dict_plot( 

100 "posterior_samples", plot_kwargs={ 

101 "type": "'corner'", "parameters": corner_parameters 

102 }, text=( 

103 "Corner plots are very useful for spotting degeneracies between " 

104 "parameters. A corner plot can easily be generated using " 

105 "'pesummary'" 

106 ), extra_lines=["fig.show()"] 

107 ) 

108 nb.add_cell(text, markdown=True) 

109 nb.add_cell(cell, code=True) 

110 

111 if len(f.labels) > 1: 

112 cell = "## Comparing multiple analyses" 

113 nb.add_cell(cell, markdown=True) 

114 text, cell = samples_dict_plot( 

115 "samples_dict", plot_args=["'{}'".format(default_parameter)], 

116 plot_kwargs={"type": "'hist'", "kde": True}, text=( 

117 "As the 'pesummary' file is able to store multiple analyses " 

118 "in a single file, we are able to easily generate a comparison " 

119 "plot showing the posterior distribution for '{}' for each " 

120 "analysis".format(default_parameter) 

121 ), extra_lines=["fig.set_size_inches(12, 8)", "fig.show()"] 

122 ) 

123 nb.add_cell(text, markdown=True) 

124 nb.add_cell(cell, code=True) 

125 text, cell = samples_dict_plot( 

126 "samples_dict", plot_args=["'{}'".format(default_parameter)], 

127 plot_kwargs={"type": "'violin'"}, text=( 

128 "A comparison histogram is not the only way to display this " 

129 "data. We may also generate a violin plot showing the " 

130 "posterior distribution for each analysis" 

131 ), extra_lines=["fig.show()"] 

132 ) 

133 nb.add_cell(text, markdown=True) 

134 nb.add_cell(cell, code=True) 

135 text, cell = samples_dict_plot( 

136 "samples_dict", plot_args=["{}".format(corner_parameters[:2])], 

137 plot_kwargs={"type": "'reverse_triangle'", "grid": False}, text=( 

138 "'pesummary' also allows for the user to generate a " 

139 "triangle plot with ease" 

140 ), extra_lines=["fig[0].show()"] 

141 ) 

142 nb.add_cell(text, markdown=True) 

143 nb.add_cell(cell, code=True) 

144 text, cell = samples_dict_plot( 

145 "samples_dict", extra_lines=["fig.show()"], plot_kwargs={ 

146 "type": "'corner'", "parameters": corner_parameters 

147 }, text=( 

148 "It is also useful to see how degeneracies between certain " 

149 "parameters change for different analysis. This can be " 

150 "investigated by generating a comparison corner plot" 

151 ) 

152 ) 

153 nb.add_cell(text, markdown=True) 

154 nb.add_cell(cell, code=True) 

155 cell = "## PSD data" 

156 nb.add_cell(cell, markdown=True) 

157 text, cell = psd_plot( 

158 "data", default_analysis, plot_kwargs={"fmin": 30}, 

159 extra_lines=["fig.show()"], text=( 

160 "The 'pesummary' file also stores the PSD that was used for " 

161 "each analysis. This can be extracted and plotted" 

162 ) 

163 ) 

164 nb.add_cell(text, markdown=True) 

165 nb.add_cell(cell, code=True) 

166 nb.write(filename=filename, outdir=outdir)