LALBurst 2.0.7.1-eeff03c
lalburst_cluster.py
Go to the documentation of this file.
1##python
2#
3# Copyright (C) 2006,2012 Kipp Cannon
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by the
7# Free Software Foundation; either version 2 of the License, or (at your
8# option) any later version.
9#
10# This program is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13# Public License for more details.
14#
15# You should have received a copy of the GNU General Public License along
16# with this program; if not, write to the Free Software Foundation, Inc.,
17# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19
20#
21# =============================================================================
22#
23# Preamble
24#
25# =============================================================================
26#
27
28
29from optparse import OptionParser
30
31
32from lal.utils import CacheEntry
33
34
35from igwn_ligolw import utils as ligolw_utils
36from lalburst import git_version
37from lalburst import bucluster
38
39
40__author__ = "Kipp Cannon <kipp.cannon@ligo.org>"
41__version__ = "git id %s" % git_version.id
42__date__ = git_version.date
43
44
45#
46# =============================================================================
47#
48# Command Line
49#
50# =============================================================================
51#
52
53
55 parser = OptionParser(
56 version = "Name: %%prog\n%s" % git_version.verbose_msg,
57 usage = "%prog [options] [file ...]",
58 description = "Run a single-instrument burst clustering algorithm on the sngl_burst events contained in LIGO Light Weight XML files. Files can be listed on the command line and/or in one or more LAL cache files. If no files are named, then input is read from stdin and written to stdout."
59 )
60 parser.add_option("--comment", metavar = "text", help = "Set the comment string to be recorded in the process table (default = None).")
61 parser.add_option("-c", "--cluster-algorithm", metavar = "[excesspower]", help = "Set clustering method (required).")
62 parser.add_option("-i", "--input-cache", metavar = "filename", action = "append", default = [], help = "Process the files listed in this LAL cache.")
63 parser.add_option("-p", "--program", metavar = "name", default = "lalapps_power", help = "Set the name of the program that generated the events as it appears in the process table (default = \"lalapps_power\").")
64 parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
65 options, filenames = parser.parse_args()
66
67 if options.cluster_algorithm is None:
68 raise ValueError("missing required argument --cluster-algorithm")
69 if options.cluster_algorithm not in ("excesspower",):
70 raise ValueError("unrecognized --cluster-algorithm %s" % options.cluster_algorithm)
71
72 for cache in options.input_cache:
73 filenames += [CacheEntry(line).path for line in file(cache)]
74
75 return options, (filenames or [None])
76
77
78#
79# =============================================================================
80#
81# Main
82#
83# =============================================================================
84#
85
86
87options, filenames = parse_command_line()
88
89
90prefunc = {
91 "excesspower": bucluster.ExcessPowerPreFunc
92}[options.cluster_algorithm]
93postfunc = {
94 "excesspower": bucluster.ExcessPowerPostFunc
95}[options.cluster_algorithm]
96testfunc = {
97 "excesspower": bucluster.ExcessPowerTestFunc
98}[options.cluster_algorithm]
99sortkeyfunc = {
100 "excesspower": bucluster.ExcessPowerSortKeyFunc
101}[options.cluster_algorithm]
102bailoutfunc = {
103 "excesspower": bucluster.ExcessPowerBailoutFunc
104}[options.cluster_algorithm]
105clusterfunc = {
106 "excesspower": bucluster.ExcessPowerClusterFunc
107}[options.cluster_algorithm]
108
109
110for filename in filenames:
111 #
112 # Load document
113 #
114
115 xmldoc = ligolw_utils.load_filename(filename, verbose = options.verbose)
116
117 # FIXME: don't do this: fix lalapps_power's output
118 if options.cluster_algorithm in ("excesspower",):
119 bucluster.add_ms_columns(xmldoc)
120
121 #
122 # Add process information
123 #
124
125 process = bucluster.append_process(xmldoc, cluster_algorithm = options.cluster_algorithm, comment = options.comment)
126
127 #
128 # Call clustering library
129 #
130
131 xmldoc, changed = bucluster.bucluster(
132 xmldoc,
133 program = options.program,
134 process = process,
135 prefunc = prefunc,
136 postfunc = postfunc,
137 testfunc = testfunc,
138 clusterfunc = clusterfunc,
139 sortkeyfunc = sortkeyfunc,
140 bailoutfunc = bailoutfunc,
141 verbose = options.verbose
142 )
143
144 #
145 # Finish process information
146 #
147
148 process.set_end_time_now()
149
150 #
151 # Write document
152 #
153
154 if changed:
155 ligolw_utils.write_filename(xmldoc, filename, verbose = options.verbose)
156 xmldoc.unlink()