Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1#!/usr/bin/env python 

2 

3""" 

4Make a table with the coincident significance values as rows (one NEUTRINO per 

5row). Works on doublet neutrinos too (as long as the directory structure is as 

6expected). Collates output files from distributed ``DigitalOcean`` background 

7runs into a single whitespace-delimited table of results. 

8""" 

9 

10import os 

11import sys 

12import json 

13import argparse 

14from glob import glob 

15 

16DEFAULT_OUTFILE = "coinc_significance_outputs.txt" 

17DEFAULT_IN = "*/*/*/coinc_significance_IceCube-LVC.json" 

18HEADER = ( 

19 " {: <15}" + # RA 

20 " {: <15}" + # DEC 

21 " {: <15}" + # SIGMA 

22 " {: <15}" + # DT 

23 " {: <15}" + # ENERGY 

24 " {: <23}" + # ODDS-RATIO 

25 " {: <23}" + # EVENT-ODDS-RATIO 

26 " {: <23}" + # P_H_C 

27 " {: <23}" + # P_H_S1 

28 " {: <23}" + # P_H_S2 

29 " {: <71}" + # SOURCE-DIRECTORY 

30 " {: <71}" # SKYMAP-FILENAME 

31 "\n" 

32).format("RA", "DEC", "SIGMA", "DT", "ENERGY", "ODDS-RATIO", 

33 "EVENT-ODDS-RATIO", "P_H_C", "P_H_S1", 

34 "P_H_S2", "SOURCE-DIRECTORY", "SKYMAP-FILENAME") 

35ROW_FMT = ( 

36 " {ra: >15.11f}" + 

37 " {dec: >+15.11f}" + 

38 " {sigma: >15.11f}" + 

39 " {dt: >+15.10f}" + 

40 " {energy: >+15.11g}" + 

41 " {odds_ratio: >+23.11g}" + 

42 " {event_odds_ratio: >+23.11g}" + 

43 " {p_h_c: >+23.11g}" + 

44 " {p_h_s1: >+23.11g}" + 

45 " {p_h_s2: >+23.11g}" + 

46 " {source_directory: >71}" + 

47 " {skymap: >71}" 

48 "\n" 

49) 

50 

51 

52def get_parser(): 

53 """Parse command-line arguments.""" 

54 parser = argparse.ArgumentParser(description=__doc__) 

55 parser.add_argument("outfile", default=DEFAULT_OUTFILE, nargs="?", help=""" 

56 Name of the output significance table. DEFAULT: 

57 {}""".format(DEFAULT_OUTFILE)) 

58 parser.add_argument("-i", "--input-pattern", default=DEFAULT_IN, help=""" 

59 UNIX-style glob pointing to the significance output files. DEFAULT: 

60 {}""".format(DEFAULT_IN)) 

61 return parser 

62 

63 

64def main(): 

65 """Run CLI.""" 

66 parser = get_parser() 

67 args = parser.parse_args() 

68 with open(args.outfile, "w") as outfile: 

69 outfile.write(HEADER) 

70 outfile.write('='*(len(HEADER)-1)+'\n') 

71 for significance_file in glob(args.input_pattern): 

72 print("Writing {} to file...".format(significance_file)) 

73 try: 

74 source_directory = os.path.split(significance_file)[0] 

75 skymap_info = os.path.join(source_directory, 

76 "skymap_info.json") 

77 with open(skymap_info) as inf: 

78 skymap_filename = json.load(inf)['skymap_filename'] 

79 with open(significance_file) as inf: 

80 results = json.load(inf)['outputs']['neutrino_gw_triggers'] 

81 event_odds_ratio = sum(n['odds_ratio'] for n in results) 

82 for neutrino in results: 

83 outfile.write(ROW_FMT.format( 

84 skymap=skymap_filename, 

85 event_odds_ratio=event_odds_ratio, 

86 source_directory=source_directory, 

87 **neutrino 

88 )) 

89 except FileNotFoundError: 

90 pass 

91 except json.JSONDecodeError: 

92 sys.stderr.write(f"Could not decode {significance_file}\n") 

93 

94 

95if __name__ == "__main__": 

96 main()