1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
72 if create_new is None:
73 raise
74 tisitable = lsctables.New(lsctables.TimeSlideTable)
75 xmldoc.childNodes[0].appendChild(tisitable)
76
77 tisitable.sync_next_id()
78
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
86
87
88
89
90
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
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
124 mapping = {}
125
126 while time_slides:
127
128 id1, deltas1 = time_slides.popitem()
129
130
131 ids_to_delete = []
132 for id2, deltas2 in time_slides.items():
133
134
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
143 del progressbar
144 return mapping
145
146
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