Coverage for pesummary/gw/finish.py: 92.9%

56 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 

3import subprocess 

4import os 

5import time 

6import numpy as np 

7 

8from pesummary.core.cli.parser import convert_dict_to_namespace 

9from pesummary.core.finish import FinishingTouches 

10from pesummary.utils.utils import logger 

11from pesummary.cli.summarymodify import _main, command_line 

12 

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

14 

15 

16class GWFinishingTouches(FinishingTouches): 

17 """Class to handle the finishing touches 

18 

19 Parameters 

20 ---------- 

21 ligo_skymap_PID: dict 

22 dictionary containing the process ID for the ligo.skymap subprocess 

23 for each analysis 

24 """ 

25 def __init__(self, inputs, ligo_skymap_PID=None): 

26 super(GWFinishingTouches, self).__init__(inputs) 

27 self.ligo_skymap_PID = ligo_skymap_PID 

28 self.generate_ligo_skymap_statistics() 

29 

30 def generate_ligo_skymap_statistics(self): 

31 """Extract key statistics from the ligo.skymap fits file 

32 """ 

33 FAILURE = False 

34 if self.ligo_skymap_PID is None: 

35 return 

36 samples_dir = os.path.join(self.inputs.webdir, "samples") 

37 for label in self.inputs.labels: 

38 _path = os.path.join(samples_dir, "{}_skymap.fits".format(label)) 

39 while not os.path.isfile(_path): 

40 try: 

41 output = subprocess.check_output( 

42 ["ps -p {}".format(self.ligo_skymap_PID[label])], 

43 shell=True 

44 ) 

45 cond1 = "summarypages" not in str(output) 

46 cond2 = "defunct" in str(output) 

47 if cond1 or cond2: 

48 if not os.path.isfile(_path): 

49 FAILURE = True 

50 break 

51 except (subprocess.CalledProcessError, KeyError): 

52 FAILURE = True 

53 break 

54 time.sleep(60) 

55 if FAILURE: 

56 continue 

57 ess = subprocess.Popen( 

58 "ligo-skymap-stats {} -p 50 90 -o {}".format( 

59 os.path.join(samples_dir, "{}_skymap.fits".format(label)), 

60 os.path.join( 

61 samples_dir, "{}_skymap_stats.dat".format(label) 

62 ) 

63 ), shell=True 

64 ) 

65 ess.wait() 

66 self.save_skymap_stats_to_metafile( 

67 label, os.path.join(samples_dir, "{}_skymap_stats.dat".format(label)) 

68 ) 

69 self.save_skymap_data_to_metafile( 

70 label, os.path.join(samples_dir, "{}_skymap.fits".format(label)) 

71 ) 

72 

73 def save_skymap_stats_to_metafile(self, label, filename): 

74 """Save the skymap statistics to the PESummary metafile 

75 

76 Parameters 

77 ---------- 

78 label: str 

79 the label of the analysis that the skymap statistics corresponds to 

80 filename: str 

81 name of the file that contains the skymap statistics for label 

82 """ 

83 logger.info("Adding ligo.skymap statistics to the metafile") 

84 try: 

85 skymap_data = np.genfromtxt(filename, names=True, skip_header=True) 

86 except IndexError: 

87 logger.warning( 

88 "Failed to open '{}'. Unable to store skymap statistics in " 

89 "metafile".format(filename) 

90 ) 

91 return 

92 keys = skymap_data.dtype.names 

93 

94 _dict = { 

95 "webdir": self.inputs.webdir, 

96 "samples": [os.path.join(self.inputs.webdir, "samples", "posterior_samples.h5")], 

97 "kwargs": {label: ["{}:{}".format(key, float(skymap_data[key])) for key in keys]}, 

98 "overwrite": True 

99 } 

100 opts = convert_dict_to_namespace(_dict, add_defaults=command_line()) 

101 _main(opts) 

102 

103 def save_skymap_data_to_metafile(self, label, filename): 

104 """Save the skymap data to the PESummary metafile 

105 

106 Parameters 

107 ---------- 

108 label: str 

109 the label of the analysis that the skymap corresponds to 

110 filename: str 

111 name of the fits file that contains the skymap for label 

112 """ 

113 logger.info("Adding ligo.skymap data to the metafile") 

114 

115 _dict = { 

116 "webdir": self.inputs.webdir, 

117 "samples": [os.path.join(self.inputs.webdir, "samples", "posterior_samples.h5")], 

118 "overwrite": True, "store_skymap": {label: filename} 

119 } 

120 opts = convert_dict_to_namespace(_dict, add_defaults=command_line()) 

121 _main(opts)