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# (c) Stefan Countryman, 2019 

2 

3import os 

4from sys import stderr 

5import json 

6import argparse 

7from glob import glob 

8from llama.dev.background.table import __doc__ 

9 

10DEFAULT_OUTFILE = "coinc_significance_outputs.txt" 

11DEFAULT_INPUT = "*/*/*/significance_lvc-i3.json" 

12 

13 

14def log(msg): 

15 stderr.write(msg+'\n') 

16 

17 

18def rowfmts(skymap_info=False): 

19 """Get the header text and row format text. Includes a skymapfilename if 

20 ``skymap_info`` is ``True``.""" 

21 header = ( 

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

23 " {: <7}" + # NUM-NEUTRINOS 

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

25 (" {: <71}" if skymap_info else "") + # SKYMAP-FILENAME 

26 "\n" 

27 ).format("EVENT-ODDS-RATIO", "N", "SOURCE-DIRECTORY", "SKYMAP-FILENAME") 

28 row_fmt = ( 

29 " {odds: <+23.11g}" + 

30 " {num_neutrinos: <7d}" + 

31 " {source_directory: <71}" + 

32 (" {skymap: <71}" if skymap_info else "") + 

33 "\n" 

34 ) 

35 return header, row_fmt 

36 

37 

38def get_parser(): 

39 """Get CLI parser.""" 

40 parser = argparse.ArgumentParser(description=__doc__) 

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

42 help=f"""Name of the output significance table. 

43 DEFAULT: {DEFAULT_OUTFILE}""") 

44 parser.add_argument( "-i", "--input-pattern", default=DEFAULT_INPUT, 

45 help=f"""UNIX-style glob pointing to the significance 

46 output files. DEFAULT: {DEFAULT_INPUT}""") 

47 parser.add_argument("-s", "--skymap-info", action='store_true', help=""" 

48 If specified, get information about the skymap used 

49 from ``skymap_info``. This is supported for old 

50 versions of the analysis runs. If not specified, use 

51 the relative filepath to the significance file (default 

52 for newer runs).""") 

53 return parser 

54 

55 

56def main(): 

57 """Run CLI.""" 

58 parser = get_parser() 

59 args = parser.parse_args() 

60 header, row_fmt = rowfmts(args.skymap_info) 

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

62 out.write(header) 

63 out.write('='*(len(header)-1)+'\n') 

64 for significance_file in glob(args.input_pattern): 

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

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

67 skymap_info = os.path.join(source_directory, "skymap_info.json") 

68 skymap_filename = '' 

69 if args.skymap_info: 

70 try: 

71 with open(skymap_info) as infile: 

72 skymap_filename = json.load(infile)['skymap_filename'] 

73 except FileNotFoundError: 

74 log(f"skymap_info not found, skipping: {skymap_info}") 

75 continue 

76 except JSONDecodeError: 

77 log(f"invalid JSON in {skymap_info}, skipping") 

78 continue 

79 except KeyError: 

80 log(f"no key 'skymap_filename' in {skymap_info}, skipping") 

81 continue 

82 try: 

83 with open(significance_file) as infile: 

84 signif = json.load(infile) 

85 except json.JSONDecodeError: 

86 log(f"Could not decode {significance_file}, skipping") 

87 continue 

88 num = len(signif['inputs']['neutrino_info']) 

89 odds = signif['outputs']['neutrino_gw_combined']['odds_ratio'] 

90 out.write(row_fmt.format(skymap=skymap_filename, 

91 num_neutrinos=num, odds=odds, 

92 source_directory=source_directory)) 

93 

94 

95if __name__ == "__main__": 

96 main()