def channel_performance( vchans, ranges, performances, num_gchs, num_clns, figax=None, cmap=None): if figax == None: figrank = plt.figure(figsize=chan_perf_figsize) axrank = figrank.add_axes(chan_perf_axpos) figeff = plt.figure(figsize=chan_perf_figsize) axeff = figeff.add_axes(chan_perf_axpos) figfap = plt.figure(figsize=chan_perf_figsize) axfap = figfap.add_axes(chan_perf_axpos) else: (figrank, axrank), (figeff, axeff), (figfap, axfap) = figax if cmap==None: cmap = matplotlib.cm.get_cmap() S = int(np.max(ranges)) ind = 0 for ind, vchan in enumerate(vchans): for (s, e), performance, num_gch, num_cln in zip(ranges, performances, num_gchs, num_clns): if performance.has_key(vchan): gch = performance[vchan]['gch'] cln = performance[vchan]['cln'] if num_gch: eff = 1.0*gch/num_gch else: eff = 0 if num_cln: fap = 1.0*cln/num_cln else: fap = 0 if fap > 0: rank = ovl.effbydt_to_rank( eff/fap ) elif eff > 0: rank = 1 else: rank = 0 axrank.fill_between( [s-S, e-S], [ind-0.5]*2, [ind+0.5]*2, color=cmap(rank) ) axeff.fill_between( [s-S, e-S], [ind-0.5]*2, [ind+0.5]*2, color=cmap(eff) ) axfap.fill_between( [s-S, e-S], [ind-0.5]*2, [ind+0.5]*2, color=cmap(fap) ) ticks = range(ind+1) for ax in [axrank, axeff, axfap]: ax.yaxis.set_ticks( ticks ) ax.yaxis.set_ticklabels( [vchan.replace("_","\_") for vchan in vchans] ) ax.set_xlabel('sec relative to %d'%S) ax.set_ylim(ymin=-0.5, ymax=ind+0.5) return (figrank, axrank), (figeff, axeff), (figfap, axfap)
def extract_ovl_vconfigs(rank_frames, channame, traindir, start, end, metric='eff/dt'): """ returns a dictionary mapping active vconfigs to segments does NOT include "none" channel """ vconfigs = [] for rnkfr in rank_frames: trained, calib = idq.extract_timeseries_ranges(rnkfr) classifier = idq.extract_fap_name(rnkfr) vetolist = glob.glob("%s/%d_%d/ovl/ovl/*vetolist.eval" % (traindir, trained[0], trained[1])) if len(vetolist) != 1: raise ValueError( "trouble finding a single vetolist file for : %s" % rnkfr) vetolist = vetolist[0] v = event.loadstringtable(vetolist) rankmap = {0: [(None, None, None, None, 0, 0)]} for line in v: metric_exp = float(line[ovl.vD['metric_exp']]) if metric == 'eff/dt': rnk = ovl.effbydt_to_rank(metric_exp) elif metric == 'vsig': rnk = ovl.vsig_to_rank(metric_exp) elif metric == 'useP': rnk = ovl.useP_to_rank(metric_exp) else: raise ValueError("metric=%s not understood" % metric) if rankmap.has_key(rnk): rankmap[rnk].append( (line[ovl.vD['vchan']], float(line[ovl.vD['vthr']]), float(line[ovl.vD['vwin']]), metric, metric_exp, rnk)) else: rankmap[rnk] = [ (line[ovl.vD['vchan']], float(line[ovl.vD['vthr']]), float(line[ovl.vD['vwin']]), metric, metric_exp, rnk) ] for key, value in rankmap.items(): rankmap[key] = tuple(value) t, ts = idq.combine_gwf([rnkfr], [channame]) t = t[0] truth = (start <= t) * (t <= end) t = t[truth] ts = ts[0][truth] if not len(ts): continue configs = rankmap[ts[0]] segStart = t[0] for T, TS in zip(t, ts): if rankmap[TS] != configs: vconfigs.append((configs, [segStart, T])) segStart = T configs = rankmap[TS] else: pass vconfigs.append((configs, [segStart, T + t[1] - t[0]])) configs = {} for vconfig, seg in vconfigs: if configs.has_key(vconfig): configs[vconfig].append(seg) else: configs[vconfig] = [seg] for key, value in configs.items(): value = event.andsegments([event.fixsegments(value), [[start, end]]]) if event.livetime(value): configs[key] = event.fixsegments(value) else: raise ValueError( "somehow picked up a config with zero livetime...") return vconfigs, configs, { "vchan": 0, "vthr": 1, "vwin": 2, "metric": 3, "metric_exp": 4, "rank": 5 }
def extract_ovl_vconfigs( rank_frames, channame, traindir, start, end, metric='eff/dt' ): """ returns a dictionary mapping active vconfigs to segments does NOT include "none" channel """ vconfigs = [] for rnkfr in rank_frames: trained, calib = idq.extract_timeseries_ranges( rnkfr ) classifier = idq.extract_fap_name( rnkfr ) vetolist = glob.glob( "%s/%d_%d/ovl/ovl/*vetolist.eval"%(traindir, trained[0], trained[1]) ) if len(vetolist) != 1: raise ValueError( "trouble finding a single vetolist file for : %s"%rnkfr ) vetolist=vetolist[0] v = event.loadstringtable( vetolist ) rankmap = { 0:[(None, None, None, None, 0, 0)] } for line in v: metric_exp = float(line[ovl.vD['metric_exp']]) if metric == 'eff/dt': rnk = ovl.effbydt_to_rank( metric_exp ) elif metric == 'vsig': rnk = ovl.vsig_to_rank( metric_exp ) elif metric == 'useP': rnk = ovl.useP_to_rank( metric_exp ) else: raise ValueError("metric=%s not understood"%metric) if rankmap.has_key(rnk): rankmap[rnk].append( (line[ovl.vD['vchan']], float(line[ovl.vD['vthr']]), float(line[ovl.vD['vwin']]), metric, metric_exp, rnk )) else: rankmap[rnk] = [(line[ovl.vD['vchan']], float(line[ovl.vD['vthr']]), float(line[ovl.vD['vwin']]), metric, metric_exp, rnk )] for key, value in rankmap.items(): rankmap[key] = tuple(value) t, ts = idq.combine_gwf( [rnkfr], [channame]) t = t[0] truth = (start <= t)*(t <= end) t = t[truth] ts = ts[0][truth] if not len(ts): continue configs = rankmap[ts[0]] segStart = t[0] for T, TS in zip(t, ts): if rankmap[TS] != configs: vconfigs.append( (configs, [segStart, T] ) ) segStart = T configs = rankmap[TS] else: pass vconfigs.append( (configs, [segStart, T+t[1]-t[0]] ) ) configs = {} for vconfig, seg in vconfigs: if configs.has_key( vconfig ): configs[vconfig].append( seg ) else: configs[vconfig] = [ seg ] for key, value in configs.items(): value = event.andsegments( [event.fixsegments( value ), [[start,end]] ] ) if event.livetime( value ): configs[key] = event.fixsegments( value ) else: raise ValueError("somehow picked up a config with zero livetime...") return vconfigs, configs, {"vchan":0, "vthr":1, "vwin":2, "metric":3, "metric_exp":4, "rank":5}
def chanlist_trending( gps_start, gps_stop, glob_dir, classifier='ovl', figure_name=False, verbose=False, annotated=True, yvalue="rank" ): """ builds a channel performance trending plot using chanlist files """ if yvalue not in ["rank", "eff", "fap"]: raise ValueError, "yvalue=%s not understood"%yvalue ax_bounds = [0.025, 0.075, 0.5, 0.775] ax_cb_bounds = [0.025, 0.875, 0.5, 0.05] # figure out appropriat time variable: dur = gps_stop - gps_start for (tau, tau_name) in [(1., 'seconds'), (60., 'minutes'), (3600., 'hours'), (86400., 'days'), (7 * 86400., 'weeks')]: if dur / tau < 100: break # ## find only the channel lists within this range if verbose: print 'finding %s*channel_summary.txt files within [%f, %f]' \ % (classifier, gps_start, gps_stop) chanlists = [] for _dir in sorted(glob.glob(glob_dir + '/*_*')): try: (_start, _stop) = [int(l) for l in _dir.split('/' )[-1].split('_')] if gps_start <= _start and gps_stop > _start or gps_start \ < _stop and gps_stop >= _stop: chanlists.append(glob.glob(_dir + '/' + classifier + '*_channel_summary.txt')[0]) # expect only one channel_summary.txt per _dir per classifier except: pass ### read in data from chanlists and build dictionary chans = {} for chanlist in chanlists: if verbose: print 'reading data from %s' % chanlist # get time range for this chan list (_start, _dur) = [int(l) for l in chanlist.split('/' )[-1].split('_')[0].split('-')[-2:]] # eg: ovl-1043366400-86400_channel_summary.txt # read in this chanlist c = [[line[0]] + [float(l) for l in line[1:]] for line in [_line.strip().split() for _line in open(chanlist, 'r' ).readlines()[1:]]] for chan_dat in c: channel = chan_dat[chanlistD['channel']] if yvalue == "rank": yValue = ovl.effbydt_to_rank(chan_dat[chanlistD['eff/fap']]) elif yvalue == "eff": yValue = chan_dat[chanlistD['c_eff']] elif yvalue == "fap": yValue = chan_dat[chanlistD['c_fap']] else: raise ValueError, "yvalue=%s not understood"%yvalue if chans.has_key(channel): chans[channel].append([_start, _start + _dur, yValue]) else: chans[channel] = [[_start, _start + _dur, yValue]] # ## build plot if verbose: print 'generating figure' color_map = matplotlib.cm.get_cmap() norm = mpl.colors.Normalize(vmin=0, vmax=1) fig = plt.figure() ax = fig.add_axes(ax_bounds) yticklabels = [] time_min = 0 ind = 0 for (ind, channel) in enumerate([chan for chan in sorted(chans.keys()) if chan != 'none']): # we don't care about the "none" channel yticklabels.append(channel.replace("_","\_")) for (_start, _stop, yValue) in chans[channel]: if _start - gps_stop < time_min: time_min = _start - gps_stop ax.fill_between([(_start - gps_stop) / tau, (_stop - gps_stop) / tau], [ind + 0.49, ind + 0.49], [ind - 0.49, ind - 0.49], facecolor=color_map(yValue), edgecolor='none') if annotated: ax.text(((_start + _stop) / 2. - gps_stop) / tau, ind, '%.2f' % rank, ha='center', va='center') ax.set_xlim(xmin=time_min / tau, xmax=0) ax.set_ylim(ymin=-0.5, ymax=ind + 0.5) ax.set_xlabel('%s relative to gps=%.0f' % (tau_name, gps_stop)) ax.set_yticks(range(ind + 1)) ax.set_yticklabels(yticklabels) ax.yaxis.tick_right() # plt.setp(plt.getp(ax, 'yticklabels'), fontsize=7.5) # plt.setp(plt.getp(ax, 'xticklabels'), fontsize=10) ax_colorbar = fig.add_axes(ax_cb_bounds) cb = mpl.colorbar.ColorbarBase(ax_colorbar, cmap=color_map, norm=norm, orientation='horizontal') if yvalue == "rank": cb.set_label(classifier + ' rank') elif yvalue == "eff": cb.set_label(classifier + ' efficiency') elif yvalue == "fap": cb.set_label(classifier + ' false alarm probability') else: raise ValueError, "yvalue=%s not understood"%yvalue ax_colorbar.xaxis.tick_top() ax_colorbar.xaxis.set_label_position('top') plt.setp(fig, figheight=max(2.54, 1.0 + 0.15 * (ind + 1)), figwidth=10) ax.grid(True) if figure_name: if verbose: print 'saving figure into %s' % figure_name plt.savefig(figure_name) return figure_name else: return fig