Interacting with ligo.skymap
If ligo.skymap is installed in your environment and a skymap can be successfully produced, pesummary will store the skymap data in the pesummary metafile when the summarypages executable is run. A fits file is also produced and can be downloaded directly from the webpages. For details about how to install the ligo.skymap package, see the ligo.skymap documentation. Below we will go through how this data can be extracted and a skymap produced. We will also explain how a fits file can be produced and added to an existing pesummary metafile is a skymap is not already present.
A ligo.skymap skymap is produced automatically with the summarypages executable if the ligo.skymap package is installed. The fits file can then be downloaded directly from the webpages, or accessed from the meta file itself. To produce a skymap from the probability array stored in the meta file, we may run,
>>> from pesummary.io import read
>>> f = read('posterior_samples.h5')
>>> label = f.labels[0]
>>> fig = f.skymap[label].plot(contour=[50, 90])
>>> fig.savefig('skymap.png')
If more than one skymap is stored in the meta file, a comparison plot can be produced by running,
>>> from pesummary.io import read
>>> f = read('posterior_samples.h5')
>>> labels = f.labels[:2]
>>> fig = f.skymap.plot(
... colors=["k", "r"], contour=[90], show_probability_map=labels[0],
... labels=labels
... )
>>> fig.savefig('skymap_comparison.png')
If, however, the skymap data is not stored in the meta file, a skymap can be produced by following the instructions below.
Generating a fits file with the ligo.skymap executables
Here we will go through step by step how to generate a skymap with the ligo.skymap package when you have a PESummary metafile. If, you wish to produce the skymap without running the summarypages executable, this can be done by following the instructions below.
If your metafile has a single analysis stored, a skymap can be generated with ease:
$ ligo-skymap-from-samples --enable-multiresolution \
--samples posterior_samples.h5 \
--outdir ./
If your metafile has more than one analysis stored, you will need to select which analysis you wish to use. This may be done with the –path command line executable,
$ ligo-skymap-from-samples --enable-multiresolution \
--samples posterior_samples.h5 \
--outdir ./ \
--path analysis_one/posterior_samples
where –path should be {label}/posterior_samples.
Appending a fits file to an existing metafile
A ligo.skymap fits file may be added to an existing pesummary metafile with the summarymodify executable. An example command line is below which demonstrates how to add the contents of the fits file located at ./skymap.fits to the posterior_samples.h5 file for the analysis with label analysis_one:
$ summarymodify --webdir ./ \
--store_skymap analysis_one:./skymap.fits \
--samples posterior_samples.h5
Generating a skymap
A skymap can be produced easily when the pesummary file has been read in using the pesummary package. For details about how to read in a pesummary file with the pesummary package, see Reading in a pesummary metafile,
1from pesummary.gw.fetch import fetch_open_samples
2import matplotlib.pyplot as plt
3import time
4
5TESTING = True
6
7
8def generate_skymap(samples, **kwargs):
9 """Generate a skymap from a SamplesDict object
10
11 Parameters
12 ----------
13 samples: pesummary.utils.samples_dict.SamplesDict
14 samples you wish to generate a skymap for
15 **kwargs: dict, optional
16 all additional kwargs are passed to the `.plot()` method
17 """
18 return samples.plot(type="skymap", **kwargs)
19
20
21f = fetch_open_samples(
22 "GW190814", catalog="GWTC-2", unpack=True, path="GW190814.h5"
23)
24label = f.labels[0]
25
26# If the pesummary file has the skymap data already
27# stored in the metafile, we may directly use the plot
28# function
29try:
30 skymap = f.skymap[label]
31 fig = skymap.plot()
32 plt.show()
33except (KeyError, AttributeError):
34 print("No skymap data stored in the metafile")
35
36# Otherwise, we may produce a skymap on the fly using
37# the posterior samples already stored. This may take
38# some time
39posterior_samples = f.samples_dict[label]
40if TESTING:
41 import multiprocessing
42
43 p = multiprocessing.Process(target=generate_skymap, args=(posterior_samples,))
44 p.start()
45 # Only let the skymap generation run for 120s
46 time.sleep(120)
47 p.terminate()
48 p.join()
49else:
50 fig = posterior_samples.plot(type="skymap")
51 plt.show()