Package glue :: Package ligolw :: Package utils :: Module time_slide
[hide private]
[frames] | no frames]

Source Code for Module glue.ligolw.utils.time_slide

  1  # Copyright (C) 2006--2014  Kipp Cannon 
  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  # 
 21  #                                   Preamble 
 22  # 
 23  # ============================================================================= 
 24  # 
 25   
 26   
 27  from glue import git_version 
 28  from glue.text_progress_bar import ProgressBar 
 29  from .. import lsctables 
 30   
 31   
 32  __author__ = "Kipp Cannon <kipp.cannon@ligo.org>" 
 33  __version__ = "git id %s" % git_version.id 
 34  __date__ = git_version.date 
 35   
 36   
 37  # 
 38  # ============================================================================= 
 39  # 
 40  #                                     I/O 
 41  # 
 42  # ============================================================================= 
 43  # 
 44   
 45   
46 -def get_time_slide_id(xmldoc, time_slide, create_new = None, superset_ok = False, nonunique_ok = False):
47 """ 48 Return the time_slide_id corresponding to the offset vector 49 described by time_slide, a dictionary of instrument/offset pairs. 50 51 Example: 52 53 >>> get_time_slide_id(xmldoc, {"H1": 0, "L1": 0}) 54 'time_slide:time_slide_id:10' 55 56 This function is a wrapper around the .get_time_slide_id() method 57 of the glue.ligolw.lsctables.TimeSlideTable class. See the 58 documentation for that class for the meaning of the create_new, 59 superset_ok and nonunique_ok keyword arguments. 60 61 This function requires the document to contain exactly one 62 time_slide table. If the document does not contain exactly one 63 time_slide table then ValueError is raised, unless the optional 64 create_new argument is not None. In that case a new table is 65 created. This effect of the create_new argument is in addition to 66 the affects described by the TimeSlideTable class. 67 """ 68 try: 69 tisitable = lsctables.TimeSlideTable.get_table(xmldoc) 70 except ValueError: 71 # table not found 72 if create_new is None: 73 raise 74 tisitable = lsctables.New(lsctables.TimeSlideTable) 75 xmldoc.childNodes[0].appendChild(tisitable) 76 # make sure the next_id attribute is correct 77 tisitable.sync_next_id() 78 # get the id 79 return tisitable.get_time_slide_id(time_slide, create_new = create_new, superset_ok = superset_ok, nonunique_ok = nonunique_ok)
80 81 82 # 83 # ============================================================================= 84 # 85 # Time Slide Comparison 86 # 87 # ============================================================================= 88 # 89 90
91 -def time_slides_vacuum(time_slides, verbose = False):
92 """ 93 Given a dictionary mapping time slide IDs to instrument-->offset 94 mappings, for example as returned by the as_dict() method of the 95 TimeSlideTable class in glue.ligolw.lsctables or by the 96 load_time_slides() function in this module, construct and return a 97 mapping indicating time slide equivalences. This can be used to 98 delete redundant time slides from a time slide table, and then also 99 used via the applyKeyMapping() method of glue.ligolw.table.Table 100 instances to update cross references (for example in the 101 coinc_event table). 102 103 Example: 104 105 >>> slides = {"time_slide_id:0": {"H1": 0, "H2": 0}, 106 "time_slide_id:1": {"H1": 10, "H2": 10}, "time_slide_id:2": {"H1": 107 0, "H2": 10}} 108 >>> time_slides_vacuum(slides) 109 {'time_slide_id:1': 'time_slide_id:0'} 110 111 indicating that time_slide_id:1 describes a time slide that is 112 equivalent to time_slide_id:0. The calling code could use this 113 information to delete time_slide_id:1 from the time_slide table, 114 and replace references to that ID in other tables with references 115 to time_slide_id:0. 116 """ 117 # convert offsets to deltas 118 time_slides = dict((time_slide_id, offsetvect.deltas) for time_slide_id, offsetvect in time_slides.items()) 119 if verbose: 120 progressbar = ProgressBar(max = len(time_slides)) 121 else: 122 progressbar = None 123 # old --> new mapping 124 mapping = {} 125 # while there are time slide offset dictionaries remaining 126 while time_slides: 127 # pick an ID/offset dictionary pair at random 128 id1, deltas1 = time_slides.popitem() 129 # for every other ID/offset dictionary pair in the time 130 # slides 131 ids_to_delete = [] 132 for id2, deltas2 in time_slides.items(): 133 # if the relative offset dictionaries are 134 # equivalent record in the old --> new mapping 135 if deltas2 == deltas1: 136 mapping[id2] = id1 137 ids_to_delete.append(id2) 138 for id2 in ids_to_delete: 139 time_slides.pop(id2) 140 if progressbar is not None: 141 progressbar.update(progressbar.max - len(time_slides)) 142 # done 143 del progressbar 144 return mapping
145 146
147 -def time_slide_list_merge(slides1, slides2):
148 """ 149 Merges two lists of offset dictionaries into a single list with 150 no duplicate (equivalent) time slides. 151 """ 152 deltas1 = set(frozenset(offsetvect1.deltas.items()) for offsetvect1 in slides1) 153 return slides1 + [offsetvect2 for offsetvect2 in slides2 if frozenset(offsetvect2.deltas.items()) not in deltas1]
154