Package pylal :: Module mvsc_htmlwriter
[hide private]
[frames] | no frames]

Source Code for Module pylal.mvsc_htmlwriter

  1  #!/usr/bin/python 
  2  """Assists pylal_mvsc_player in writing html files.""" 
  3   
  4  __author__ = 'Tristan Miller <tmiller@caltech.edu>' 
  5  __prog__ = 'mvsc_htmlwriter' 
  6   
  7  ############################################################################## 
  8  # import modules 
  9  import pylab,os,re 
 10   
 11  ############################################################################## 
 12   
13 -class mvsc_html:
14 """Contains all necessary data to be put in html file, including links to 15 plots and thumbnails, and info. 16 17 The most essential function is write_html, which writes an html file from 18 a template, inserting the info and images in the right places.""" 19
20 - def __init__(self,opts,fname,templatepath):
21 self.opts = opts 22 self.filename = fname 23 self.htmlpath = opts.output_path + '/' 24 self.templatepath = templatepath 25 26 #I would make datapath and patpath relative paths rather than 27 #absolute paths, but I don't know how. 28 self.datapath = os.path.abspath(opts.data_path)+'/' 29 self.patpath = os.path.abspath(opts.pat_path)+'/' 30 31 self.letters = None 32 self.figs = {} 33 self.gpstime = None 34 self.top_events = None 35 self.op_point = None 36 self.trigger_info = None 37 self.treesplits = None 38 self.cols = None 39 self.zeronum = None 40 self.efficiency = None 41 self.comments = ''
42 43 #functions to set parameters 44
45 - def add_figure(self,type,fig=None,dpi=None,dpi_thumb=50):
46 """Adds figure of given type to be displayed in html file. 47 48 Figure types: 'FOM','ROC','Frac_vs_SNR','MVSC_vs_effSNR','missed_inj' 49 and possibly more to be added""" 50 51 if fig is None: 52 fig = pylab.gcf() 53 if dpi is None: 54 dpi = pylab.rcParams["savefig.dpi"] 55 56 #save picture into file 57 figpath = self.htmlpath+'Images/' 58 figname = self.filename+'_'+type+'.png' 59 figname_th = self.filename+'_'+type+'_th.png' 60 fig.savefig(figpath+figname,dpi=dpi) 61 fig.savefig(figpath+figname_th, dpi=dpi_thumb) 62 63 self.figs[type] = [figname,figname_th]
64
65 - def set_gpstime(self,gpstime):
66 self.gpstime = gpstime
67
68 - def set_top_events(self,events,cols):
69 self.top_events = events 70 self.cols = cols
71
72 - def set_op_point(self,afar,mvsc_cutoff,effsnr_cutoff):
73 self.op_point = [afar,mvsc_cutoff,effsnr_cutoff]
74
75 - def set_trigger_info(self,ts_tr,inj_tr,ts_va,inj_va):
76 self.trigger_info = [ts_tr,inj_tr,ts_va,inj_va]
77
78 - def set_treesplits(self,treesplits):
79 self.treesplits = treesplits
80
81 - def set_data_sets(self,patletters):
82 self.letters = patletters
83
84 - def set_zeronum(self,zeronum):
85 self.zeronum = zeronum
86
87 - def set_efficiency(self,lower,upper):
88 self.efficiency = [lower,upper]
89
90 - def set_comments(self,comments):
91 self.comments = comments
92 93 ############################################################################## 94
95 - def write_file(self):
96 """Writes an html file putting all information in a template.""" 97 98 #open template 99 tempfile = open(self.templatepath) 100 template = tempfile.read() 101 tempfile.close() 102 103 #Looks for the following pattern in the template: 104 p = re.compile(r'\[InsertCode:\s+(\S+)\s+AltText:\s+([^\]]*)\]') 105 106 #Replaces pattern with appropriate figure or info 107 match = p.search(template) 108 while match: 109 #First, try to find matching InsertCode 110 #If the necessary information exists, insert it 111 #If not, insert the AltText 112 113 # insert figures 114 if match.group(1)[:4] == 'fig_': 115 if self.figs.has_key(match.group(1)[4:]): 116 figpaths = self.figs[match.group(1)[4:]] 117 htmlcode = '<a href="Images/' + figpaths[0] + \ 118 '"><img src="Images/' + figpaths[1] + '"></a>' 119 else: 120 htmlcode = match.group(2) 121 122 # insert gps time 123 elif match.group(1) == 'gpstime': 124 if self.gpstime: 125 htmlcode = self.gpstime 126 else: 127 htmlcode = match.group(2) 128 129 # insert options 130 elif match.group(1) == 'opts': 131 try: 132 if self.opts.balance_data: 133 bstr = ' -b' 134 else: 135 bstr = '' 136 137 if self.opts.seed: 138 rstr = ' -R ' + self.opts.seed 139 else: 140 rstr = '' 141 142 astr = ' -a ' 143 for i in range(2): 144 astr += self.letters[i] 145 146 if self.opts.open_box: 147 zstr = ' --open-box' 148 elif self.opts.zero_lag: 149 zstr = ' --zero-lag' 150 elif self.opts.hardware: 151 zstr = ' --hardware' 152 else: 153 zstr = '' 154 155 if self.opts.s: 156 sstr = ' -s ' + self.opts.s 157 else: 158 sstr = '' 159 160 htmlcode = '-n '+self.opts.n+' -l '+self.opts.l+' -c '+ \ 161 self.opts.c+' -g '+self.opts.g + sstr + bstr+ \ 162 rstr + astr + zstr 163 except: 164 htmlcode = match.group(2) 165 166 # insert file links 167 elif match.group(1) == 'files': 168 try: 169 training_path = self.patpath+ \ 170 self.opts.run_name+\ 171 '/'+self.opts.stations+ 'set'+ self.letters[0] + \ 172 'Known.pat' 173 validation_path = self.patpath+ \ 174 self.opts.run_name+\ 175 '/'+self.opts.stations+ 'set'+ self.letters[1] + \ 176 'Known.pat' 177 testing_path = self.patpath+ \ 178 self.opts.run_name+\ 179 '/'+self.opts.stations+ 'sets'+ self.letters[2] + \ 180 '.pat' 181 182 tree_path = self.datapath+self.filename 183 test_path = tree_path + '_test.dat' 184 info_path = tree_path + '_info' 185 tree_path += '.spr' 186 187 htmlcode = '<a href="'+training_path+'">Training Set</a>'+\ 188 ', <a href="'+validation_path+'">Validation Set</a>'+\ 189 ', <a href="'+testing_path+'">Testing Set</a><br>'+\ 190 '<a href="'+info_path+'">SprBaggerDecisionTreeApp '+\ 191 'printout</a>, <a href="'+tree_path+ \ 192 '">Generated trees</a>, <a href="'+test_path+ \ 193 '">Test results</a>' 194 195 if self.opts.open_box | self.opts.zero_lag: 196 zeropath = self.patpath+ \ 197 self.opts.run_name+\ 198 '/'+self.opts.stations+ 'setZeroLag_' 199 200 zerotest = self.datapath+self.filename+'_' 201 202 if self.opts.open_box: 203 zeropath += 'fulldata.pat' 204 zerotest += 'fulldata.dat' 205 zstr = 'full data' 206 else: 207 zeropath += 'playground.pat' 208 zerotest += 'playground.dat' 209 zstr = 'playground' 210 211 htmlcode += '<br><a href="'+zeropath+ \ 212 '">Zero Lag Set ('+zstr+')</a>' 213 htmlcode += ', <a href="'+zerotest+ \ 214 '">Zero Lag test results</a>' 215 216 except: 217 htmlcode = match.group(2) 218 219 # insert trigger info 220 elif match.group(1) == 'trigger_info': 221 if self.trigger_info: 222 htmlcode = 'Number of timeslides in training set: ' + \ 223 str(self.trigger_info[0]) + \ 224 '<br>Number of injections in training set: ' + \ 225 str(self.trigger_info[1]) + \ 226 '<br>Number of timeslides in validation set: ' + \ 227 str(self.trigger_info[2]) + \ 228 '<br>Number of injections in validation set: ' + \ 229 str(self.trigger_info[3]) 230 231 if self.zeronum: 232 htmlcode += '<br>Number of zero lag triggers: ' + \ 233 str(self.zeronum) 234 else: 235 htmlcode = match.group(2) 236 237 # insert operating point info 238 elif match.group(1) == 'op_point': 239 if self.op_point: 240 htmlcode = 'False alarm rate per trigger: ' + \ 241 str(self.op_point[0]) + '<br>MVSC cutoff value: ' + \ 242 str(self.op_point[1]) + \ 243 '<br>Combined Effective SNR squared cutoff value: ' + \ 244 str(self.op_point[2]) 245 246 if self.efficiency: 247 htmlcode += '<br>Resulting efficiency: ' + \ 248 str(self.efficiency[0]) + ' < p < ' + \ 249 str(self.efficiency[1]) + ' (at 68% CL)' 250 else: 251 htmlcode = match.group(2) 252 253 # insert tree splits info 254 elif match.group(1) == 'treesplits': 255 if self.treesplits: 256 htmlcode = '' 257 for i in range(len(self.treesplits)): 258 htmlcode += '<tr>' 259 for j in range(3): 260 htmlcode += '<td>'+str(self.treesplits[i][j])+\ 261 '</td>' 262 htmlcode += '</tr>' 263 264 else: 265 htmlcode = match.group(2) 266 267 # insert list of top events 268 elif match.group(1) == 'top_events': 269 if self.top_events: 270 htmlcode = '' 271 for i in range(len(self.top_events)): 272 htmlcode += '<tr><td>'+str(i+1)+'</td>' 273 274 colinfo = ['get_end()','snr','chisq','eff_distance'] 275 for j in range(len(colinfo)): 276 htmlcode += '<td>' 277 htmlcode += str(self.top_events[i][ \ 278 self.cols['t1'+colinfo[j]]]) 279 280 k = 2 281 while self.cols.has_key('t'+str(k)+colinfo[j]): 282 htmlcode += '<br>' 283 htmlcode += str(self.top_events[i][ \ 284 self.cols['t'+str(k)+colinfo[j]]]) 285 k += 1 286 287 htmlcode += '</td>' 288 289 htmlcode += '<td>'+\ 290 str(self.top_events[i][self.cols['t1t2mchirp']]) +\ 291 '</td><td>' + \ 292 str(self.top_events[i][self.cols['FAN']]) + \ 293 '</td><td>' + \ 294 str(self.top_events[i][self.cols['Bagger']]) + \ 295 '</td></tr>' 296 else: 297 htmlcode = match.group(2) 298 elif match.group(1) == 'filename': 299 try: 300 htmlcode = 'Html file for ' + self.filename 301 except: 302 htmlcode = match.group(2) 303 elif match.group(1) == 'comments': 304 if self.comments: 305 htmlcode = 'Comments: ' + self.comments 306 else: 307 htmlcode = match.group(2) 308 else: 309 htmlcode = 'Error! "'+match.group(1)+ \ 310 '" is an invalid InsertCode!' 311 312 template = template[:match.start()]+htmlcode+template[match.end():] 313 314 m = p.findall(template) 315 match = p.search(template) 316 317 #Writes the html file 318 fname = self.htmlpath + self.filename + '.html' 319 os.system('touch ' + fname) 320 321 f = open(fname,'w') 322 f.write(template) 323 f.close()
324