gstlal-inspiral  0.4.2
 All Classes Namespaces Files Functions Variables Pages
simplify_and_cluster.sql
1 -- Copyright (C) 2011--2012,2014 Kipp Cannon, 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 -- coinc_definer clean up
19 --
20 
21 CREATE TEMPORARY TABLE _idmap_ AS
22  SELECT
23  old.coinc_def_id AS old,
24  MIN(new.coinc_def_id) AS new
25  FROM
26  coinc_definer AS old
27  JOIN coinc_definer AS new ON (
28  new.search == old.search
29  AND new.search_coinc_type == old.search_coinc_type
30  )
31  GROUP BY
32  old.coinc_def_id;
33 CREATE INDEX tmpindex ON _idmap_ (old);
34 
35 UPDATE coinc_event SET coinc_def_id = (SELECT new FROM _idmap_ WHERE old == coinc_def_id);
36 DELETE FROM coinc_definer WHERE coinc_def_id IN (SELECT old FROM _idmap_ WHERE old != new);
37 
38 DROP INDEX tmpindex;
39 DROP TABLE _idmap_;
40 
41 --
42 -- segment_definer clean up. NOTE; this assumes no meaningful information
43 -- is stored in the version and comment columns, and will scramble it if
44 -- there is. that is, two segment_definer rows are considered to be
45 -- equivalent even if their version and comment values differ, and the one
46 -- with the higher ID will be discarded.
47 --
48 
49 CREATE TEMPORARY TABLE _idmap_ AS
50  SELECT
51  old.segment_def_id AS old,
52  MIN(new.segment_def_id) AS new
53  FROM
54  segment_definer AS old
55  JOIN segment_definer AS new ON (
56  new.ifos == old.ifos
57  AND new.name == old.name
58  )
59  GROUP BY
60  old.segment_def_id;
61 CREATE INDEX tmpindex ON _idmap_ (old);
62 
63 UPDATE segment_summary SET segment_def_id = (SELECT new FROM _idmap_ WHERE old == segment_def_id);
64 UPDATE segment SET segment_def_id = (SELECT new FROM _idmap_ WHERE old == segment_def_id);
65 DELETE FROM segment_definer WHERE segment_def_id IN (SELECT old FROM _idmap_ WHERE old != new);
66 
67 DROP INDEX tmpindex;
68 DROP TABLE _idmap_;
69 
70 --
71 -- time_slide clean up
72 --
73 
74 CREATE TEMPORARY TABLE _idmap_ AS
75  SELECT
76  time_slide_id AS old,
77  (SELECT group_concat(instrument || "=" || offset) FROM time_slide AS time_slide_a WHERE time_slide_a.time_slide_id == time_slide.time_slide_id ORDER BY instrument) AS repr,
78  NULL AS new
79  FROM
80  time_slide
81  GROUP BY
82  time_slide_id;
83 CREATE INDEX tmpindex ON _idmap_ (repr, old);
84 
85 UPDATE _idmap_ SET new = (SELECT MIN(old) FROM _idmap_ AS a WHERE a.repr == _idmap_.repr);
86 DROP INDEX tmpindex;
87 CREATE INDEX tmpindex ON _idmap_ (old);
88 
89 UPDATE coinc_event SET time_slide_id = (SELECT _idmap_.new FROM _idmap_ WHERE _idmap_.old == time_slide_id);
90 DELETE FROM time_slide WHERE time_slide_id IN (SELECT old FROM _idmap_ WHERE old != new);
91 
92 DROP INDEX tmpindex;
93 DROP TABLE _idmap_;
94 
95 
96 --
97 -- begin clustering
98 --
99 
100 
101 SELECT
102  "Number of coincs before clustering: " || count(*)
103 FROM
104  coinc_event;
105 
106 --
107 -- create a look-up table of info required for clustering
108 --
109 
110 CREATE TEMPORARY TABLE _cluster_info_ AS
111  SELECT
112  coinc_event.coinc_event_id AS coinc_event_id,
113  coinc_event.time_slide_id AS category,
114  (coinc_inspiral.end_time - (SELECT MIN(end_time) FROM coinc_inspiral)) + 1e-9 * coinc_inspiral.end_time_ns AS end_time,
115  coinc_event.likelihood AS ranking_stat
116  FROM
117  coinc_event
118  JOIN coinc_inspiral ON (
119  coinc_inspiral.coinc_event_id == coinc_event.coinc_event_id
120  );
121 CREATE INDEX tmpindex1 ON _cluster_info_ (coinc_event_id);
122 CREATE INDEX tmpindex2 ON _cluster_info_ (category, end_time, ranking_stat);
123 
124 --
125 -- delete coincs that are within 4 s of coincs with higher SNR in the same
126 -- category
127 --
128 
129 DELETE FROM
130  coinc_event
131 WHERE
132  EXISTS (
133  SELECT
134  *
135  FROM
136  _cluster_info_ AS _cluster_info_a_
137  JOIN _cluster_info_ AS _cluster_info_b_ ON (
138  _cluster_info_b_.category == _cluster_info_a_.category
139  AND (_cluster_info_b_.end_time BETWEEN _cluster_info_a_.end_time - 4.0 AND _cluster_info_a_.end_time + 4.0)
140  AND _cluster_info_b_.ranking_stat > _cluster_info_a_.ranking_stat
141  )
142  WHERE
143  _cluster_info_a_.coinc_event_id == coinc_event.coinc_event_id
144  );
145 DROP INDEX tmpindex1;
146 DROP INDEX tmpindex2;
147 DROP TABLE _cluster_info_;
148 
149 SELECT
150  "Number of coincs after clustering: " || count(*)
151 FROM
152  coinc_event;
153 
154 --
155 -- delete unused coinc_inspiral rows
156 --
157 
158 DELETE FROM
159  coinc_inspiral
160 WHERE
161  coinc_event_id NOT IN (
162  SELECT
163  coinc_event_id
164  FROM
165  coinc_event
166  );
167 
168 --
169 -- delete unused coinc_event_map rows
170 --
171 
172 DELETE FROM
173  coinc_event_map
174 WHERE
175  coinc_event_id NOT IN (
176  SELECT
177  coinc_event_id
178  FROM
179  coinc_event
180  );
181 
182 --
183 -- delete unused sngl_inspiral rows
184 --
185 
186 DELETE FROM
187  sngl_inspiral
188 WHERE
189  event_id NOT IN (
190  SELECT
191  event_id
192  FROM
193  coinc_event_map
194  WHERE
195  table_name == 'sngl_inspiral'
196  );
197 
198 --
199 -- shrink the file
200 --
201 
202 VACUUM;