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

Source Code for Module pylal.farutils

  1  # Copyright (C) 2010 Chad Hanna 
  2  # 
  3  # This program is free software; you can redistribute it and/or modify it 
  4  # under the terms of the GNU General Public License as published by the 
  5  # Free Software Foundation; either version 2 of the License, or (at your 
  6  # option) any later version. 
  7  # 
  8  # This program is distributed in the hope that it will be useful, but 
  9  # WITHOUT ANY WARRANTY; without even the implied warranty of 
 10  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 11  # Public License for more details. 
 12  # 
 13  # You should have received a copy of the GNU General Public License along 
 14  # with this program; if not, write to the Free Software Foundation, Inc., 
 15  # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
 16   
 17  # 
 18  # ============================================================================= 
 19  # 
 20  #                                   Preamble 
 21  # 
 22  # ============================================================================= 
 23  # 
 24   
 25  import itertools 
 26  from glue import segments 
 27  from glue.ligolw import lsctables 
 28  from glue.ligolw import dbtables 
 29  from glue.ligolw.utils import search_summary as ligolw_search_summary 
 30  from glue.ligolw.utils import segments as ligolw_segments 
 31  from pylal import db_thinca_rings 
 32   
 33  # get choices from a set (useful for on/off ifos) 
34 -def detector_combos( instruments ):
35 out = [] 36 instruments = tuple(instruments) 37 for i in range(1, len(instruments)): 38 X = list(itertools.combinations(instruments, i)) 39 Y = list(itertools.combinations(instruments, len(instruments) - i)) 40 Y.reverse() 41 out.extend(zip(X,Y)) #out.extend(zip(X, Y)) 42 instruments = list(instruments) 43 instruments.sort() 44 instruments = tuple(instruments) 45 out.append((instruments, ())) 46 return out
47
48 -def background_livetime_nonring_by_slide(connection, seglists, veto_segments=None, coinc_segments=None, verbose = False):
49 # get the segment lists and live time 50 # FIXME veto segments not handled yet 51 seglists = seglists.copy() 52 if veto_segments is not None: 53 seglists -= veto_segments 54 55 background_time_slides = dict((time_slide_id, offsetvector) for time_slide_id, offsetvector in dbtables.TimeSlideTable(connection = connection).as_dict() if any(offsetvector.values())) 56 instruments = frozenset(seglists.keys()) 57 background_livetime = {} 58 for on_inst, off_inst in detector_combos(list(instruments)): 59 on_inst = frozenset(on_inst) 60 off_inst = frozenset(off_inst) 61 key = on_inst 62 old_offsets = seglists.offsets.copy() 63 background_livetime.setdefault(key, {}) 64 for id, time_slide in background_time_slides.items(): 65 seglists.offsets.update(time_slide) 66 segs=seglists.intersection(list(on_inst))-seglists.union(list(off_inst)) 67 if coinc_segments is not None: 68 segs &= coinc_segments 69 tskey = frozenset(time_slide.items()) 70 background_livetime[key].setdefault(tskey,0) 71 background_livetime[key][tskey] += float(abs(segs)) 72 seglists.offsets.update(old_offsets) 73 return background_livetime
74
75 -def background_livetime_ring_by_slide(connection, live_time_program, seglists, veto_segments, verbose = False):
76 background_livetime = {} 77 instruments = frozenset(seglists.keys()) 78 offset_vectors = db_thinca_rings.get_background_offset_vectors(connection) 79 # first work out time slide live time 80 for on_instruments, livetimes in db_thinca_rings.get_thinca_livetimes(db_thinca_rings.get_thinca_rings_by_available_instruments(connection, program_name = live_time_program), veto_segments, offset_vectors, verbose = verbose).items(): 81 on_instruments = frozenset(on_instruments)#lsctables.ifos_from_instrument_set(on_instruments) 82 for offset, lt in zip(offset_vectors,livetimes): 83 background_livetime.setdefault(on_instruments,{}) 84 key = frozenset(offset.items()) 85 background_livetime[on_instruments].setdefault(key, 0) 86 background_livetime[on_instruments][key] += lt 87 88 return background_livetime
89
90 -def add_background_livetime(connection, live_time_program, seglists, veto_segments, coinc_segments=None, verbose=False):
91 if live_time_program == "thinca": lt = background_livetime_ring_by_slide(connection, live_time_program, seglists, veto_segments, verbose) 92 if live_time_program == "gstlal_inspiral": lt = background_livetime_nonring_by_slide(connection, seglists, veto_segments, coinc_segments, verbose) 93 if live_time_program == "lalapps_ring": lt = background_livetime_nonring_by_slide(connection, seglists, veto_segments, coinc_segments, verbose) 94 out = {} 95 for k, v in lt.items(): 96 out.setdefault(k,0) 97 out[k] += sum(v.values()) 98 return out
99
100 -def playground_nonplayground_livetime(seglists, playground_segs=None, verbose=False):
101 playground_livetime = {} 102 nonplayground_livetime = {} 103 instruments = frozenset(seglists.keys()) 104 for on_inst, off_inst in detector_combos(list(instruments)): 105 on_inst = frozenset(on_inst) 106 off_inst = frozenset(off_inst) 107 key = lsctables.ifos_from_instrument_set(on_inst) 108 selected_segs = seglists.intersection(list(on_inst))-seglists.union(list(off_inst)) 109 if playground_segs: 110 playground_livetime[on_inst] = float(abs(selected_segs & playground_segs)) 111 nonplayground_livetime[on_inst] = float(abs(selected_segs - playground_segs)) 112 else: 113 playground_livetime[on_inst] = 0 114 nonplayground_livetime[on_inst] = float(abs(selected_segs)) 115 116 return playground_livetime, nonplayground_livetime
117
118 -def get_veto_segments(connection, program_name, xmldoc=None, veto_segments_name=None):
119 veto_segments = segments.segmentlistdict() 120 #FIXME only handles thinca case 121 if not veto_segments_name: return veto_segments 122 if program_name == "thinca": veto_segments = db_thinca_rings.get_veto_segments(connection, veto_segments_name) 123 if program_name == "rinca": veto_segments = ligolw_segments.segmenttable_get_by_name(xmldoc, veto_segments_name).coalesce() 124 return veto_segments
125 126
127 -def get_segments(connection, xmldoc, program_name):
128 seglists = segments.segmentlistdict() 129 if program_name == "thinca": 130 seglists = db_thinca_rings.get_thinca_zero_lag_segments(connection, program_name) 131 if program_name == "gstlal_inspiral" or program_name == "lalapps_ring": 132 seglists = ligolw_search_summary.segmentlistdict_fromsearchsummary(xmldoc, program_name).coalesce() 133 return seglists
134
135 -def get_background_livetime_by_slide(connection, program_name, seglists, veto_segments=None, verbose = False):
136 137 if program_name == "thinca": 138 return background_livetime_ring_by_slide(connection, program_name, seglists, veto_segments, verbose) 139 140 if program_name == "gstlal_inspiral": 141 return background_livetime_nonring_by_slide(connection, seglists, veto_segments, verbose)
142
143 -def get_livetime_by_offset(background_by_slide, zero_live_time=None, det='L1'):
144 lt = {} 145 if zero_live_time: 146 for inst in zero_live_time.keys(): 147 lt.setdefault(inst,{}) 148 lt[inst][0] = zero_live_time[inst] 149 150 for inst, slide in background_by_slide.items(): 151 lt.setdefault(inst,{}) 152 for offsets, value in slide.items(): 153 for ifo, offset in offsets: 154 if ifo == det: 155 lt[inst][offset] = value 156 return lt
157