1+ import yaml
2+ import numpy as np
3+ import pylab as plt
4+ import argparse
5+ import os
6+ from pspy import so_dict
7+
8+
9+ parser = argparse .ArgumentParser ()
10+ parser .add_argument ('odir' , help = "Where to save figures" )
11+ parser .add_argument ('--calib-yamls' , nargs = "+" ,
12+ help = 'A list of calib resutls yamls fils to plot' )
13+ parser .add_argument ('--paramfiles' , nargs = "+" ,
14+ help = 'A list of paramfiles to plot calibs (overrides --calib-yamls)' )
15+ parser .add_argument ('--arrays' , nargs = "+" ,
16+ help = 'list of arrays to plot (None = all LAT ISO)' , default = None )
17+ parser .add_argument ('--names' , nargs = "+" , help = 'optionnally give names to these calibs' , default = None )
18+ parser .add_argument ('--colors' , nargs = "+" , help = 'optionnally give colors to these calibs' , default = None )
19+ parser .add_argument ('--test' , help = 'Which test to use' , default = '_all_tubes_Pl143' )
20+ parser .add_argument ('--ylabel' , help = 'Ylabel for the figure' , default = 'Calibration Factor' )
21+ parser .add_argument ('--plot-line' , help = 'axhline for figure, is also the value to compare to get nsigmas' , default = 1 , type = float )
22+ parser .add_argument ('--plot-mean' , help = 'plot mean of all data points (ASSUME UNCORRELATED POINTS)' , default = False )
23+
24+
25+ args = parser .parse_args ()
26+
27+ test_names = args .names or [os .path .basename (os .path .dirname (yaml_fn )) for yaml_fn in args .calib_yamls ]
28+ test_colors = args .colors or [f"C{ c } " for c , yaml_fn in enumerate (args .calib_yamls )]
29+
30+ defaults_calibs = {
31+ "i1_f090" :1 ,
32+ "i1_f150" :1 ,
33+ "i3_f090" :1 ,
34+ "i3_f150" :1 ,
35+ "i4_f090" :1 ,
36+ "i4_f150" :1 ,
37+ "i6_f090" :1 ,
38+ "i6_f150" :1 ,
39+ "c1_f220" :1 ,
40+ "c1_f280" :1 ,
41+ "i5_f220" :1 ,
42+ "i5_f280" :1 ,
43+ }
44+
45+ if args .paramfiles is None :
46+ calib_yamls = args .calib_yamls
47+ defaults = None
48+ else :
49+ defaults = {}
50+ calib_yamls = []
51+ for name , prmfl in zip (args .names , args .paramfiles ):
52+ d = so_dict .so_dict ()
53+ d .read_from_file (prmfl )
54+ calib_yamls .append (f"calib/{ d ['run_name' ]} /calibs_dict{ args .test } .yaml" )
55+
56+ results_dict = {}
57+ errs_dict = {}
58+ for name , yaml_fn in zip (args .names , calib_yamls ):
59+ with open (yaml_fn , 'r' ) as f :
60+ results_dict [name ] = yaml .safe_load (f )
61+ with open (yaml_fn .replace ('_dict' , '_errs_dict' ), 'r' ) as f :
62+ errs_dict [name ] = yaml .safe_load (f )
63+
64+ if args .paramfiles is not None :
65+ for name , prmfl in zip (args .names , args .paramfiles ):
66+ d = so_dict .so_dict ()
67+ d .read_from_file (prmfl )
68+ defaults [name ] = {ar [- 7 :]: d [f"cal_{ ar } " ] for ar in results_dict [name ].keys ()}
69+
70+ arrays_set = args .arrays or ['c1_f220' , 'c1_f280' , 'i1_f090' , 'i1_f150' , 'i3_f090' , 'i3_f150' , 'i4_f090' , 'i4_f150' , 'i5_f220' , 'i5_f280' , 'i6_f090' , 'i6_f150' ]
71+
72+ fig , ax = plt .subplots (figsize = (12 , 6 ))
73+ ax .axhline (args .plot_line , color = 'grey' , ls = '--' , lw = 1 )
74+
75+ x_shift = np .linspace (- .02 * len (test_names ), .02 * len (test_names ), len (test_names ))
76+
77+ for j , (test , results_subdict ) in enumerate (results_dict .items ()):
78+ for i , (name , cal ) in enumerate (results_subdict .items ()):
79+ std = errs_dict [test ][name ]
80+ ax .errorbar (
81+ i + x_shift [j ],
82+ cal ,
83+ std ,
84+ color = test_colors [j ],
85+ marker = "." ,
86+ ls = "" ,
87+ label = test if i == 0 else None ,
88+ markersize = 6.5 ,
89+ # markeredgewidth=2,
90+ fillstyle = 'none' ,
91+ )
92+ if defaults is not None :
93+ ax .plot (
94+ i + x_shift [j ],
95+ defaults [test ][arrays_set [i ]],
96+ color = test_colors [j ],
97+ marker = "_" ,
98+ ls = "" ,
99+ markersize = 6.5 ,
100+ alpha = 1 ,
101+ markeredgewidth = 2 ,
102+ )
103+ if args .plot_mean :
104+ mean_var = 1 / np .sum ([1 / errs_dict [test ][name ]** 2 for name in results_subdict .keys ()])
105+ mean_value = np .sum ([results_subdict [name ] / errs_dict [test ][name ]** 2 for name in results_subdict .keys ()]) * mean_var
106+ ax .errorbar (
107+ i + 1 + x_shift [j ],
108+ mean_value ,
109+ np .sqrt (mean_var ),
110+ color = test_colors [j ],
111+ marker = "." ,
112+ ls = "" ,
113+ markersize = 6.5 ,
114+ # markeredgewidth=2,
115+ fillstyle = 'none' ,
116+ )
117+ print (f"{ test } : { mean_value = :.5f} ±{ np .sqrt (mean_var ):.5f} " )
118+ # ax.set_ylim(.79, 1.02)
119+ ax .legend (fontsize = 15 )
120+
121+ xlabels = [ar [- 7 :] for ar in arrays_set ]
122+ if args .plot_mean :
123+ xlabels += ["Mean" ]
124+ x = np .arange (0 , len (xlabels ))
125+ ax .set_xticks (x , xlabels )
126+ ax .set_ylabel (args .ylabel , fontsize = 18 )
127+ plt .tight_layout ()
128+ plt .savefig (f"{ args .odir } /{ args .ylabel .replace (" " , "_" )} _summary_{ '_' .join (test_names [:len (results_dict .keys ())])} .pdf" , bbox_inches = "tight" )
129+ plt .clf ()
130+ plt .close ()
131+
132+ # Claude wrote that
133+ def print_table (headers , rows , title = None ):
134+ # Column widths: max of header or any cell value
135+ col_widths = [
136+ max (len (str (headers [i ])), max ((len (str (row [i ])) for row in rows ), default = 0 ))
137+ for i in range (len (headers ))
138+ ]
139+
140+ # Box-drawing pieces
141+ top = "┌" + "┬" .join ("─" * (w + 2 ) for w in col_widths ) + "┐"
142+ mid = "├" + "┼" .join ("─" * (w + 2 ) for w in col_widths ) + "┤"
143+ bottom = "└" + "┴" .join ("─" * (w + 2 ) for w in col_widths ) + "┘"
144+
145+ total_width = sum (w + 3 for w in col_widths ) + 1
146+
147+ def row_line (cells , widths ):
148+ return "│" + "│" .join (f" { str (c ):<{w }} " for c , w in zip (cells , widths )) + "│"
149+
150+ # Print
151+ if title :
152+ print ("┌" + "─" * (total_width - 2 ) + "┐" )
153+ print ("│" + title .center (total_width - 2 ) + "│" )
154+
155+ print (top )
156+ print (row_line (headers , col_widths ))
157+ print (mid )
158+ for row in rows :
159+ print (row_line (row , col_widths ))
160+ print (bottom )
161+
162+ for j , (test , results_subdict ) in enumerate (results_dict .items ()):
163+ for i , (name , cal ) in enumerate (results_subdict .items ()):
164+ std = errs_dict [test ][name ]
165+
166+ headers = ["params" ] + [test for test in results_dict .keys ()]
167+ rows = [[name ] + [f"{ results_dict [test ][name ]:.4f} ±{ errs_dict [test ][name ]:.4f} ({ (results_dict [test ][name ] - args .plot_line ) / errs_dict [test ][name ]:.1f} σ)" for test in results_dict .keys ()] for name in results_dict [test ].keys ()]
168+
169+ print_table (headers , rows , title = args .ylabel )
0 commit comments