Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALPulsar 7.1.1.1-b246709
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
lalpulsar_PiecewiseSearchTemplateBank.py
Go to the documentation of this file.
1##python
2# Copyright (C) 2019--2023 Benjamin Grace
3#
4# This program is free software; you can redistribute it and/or modify it
5# under the terms of the GNU General Public License as published by the
6# Free Software Foundation; either version 2 of the License, or (at your
7# option) any later version.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
12# Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program; if not, write to the Free Software Foundation, Inc.,
16# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18## \file
19## \ingroup lalpulsar_bin_PiecewiseSearch
20"""Search for long-transient gravitational waves using a piecewise frequency model"""
21
22import argparse as ap
23import cProfile
24import io
25import logging
26import pstats
27
28import matplotlib.pyplot as plt
29import numpy as np
30
36
37# Initialise profiler
38pr = cProfile.Profile()
39pr.enable()
40
41parser = ap.ArgumentParser()
42
43parser.add_argument(
44 "--tbankcode",
45 type=str,
46 help="Specifies the tbank object to use for a search",
47 required=True,
48)
49parser.add_argument(
50 "--j", type=int, help="The job array number on OzSTAR", required=True
51)
52parser.add_argument(
53 "--SizeOrTemps",
54 type=str,
55 help="Use a capital S or T to specify whether you want to calculate the size of the template bank, or the templates themselves",
56 required=True,
57)
58
59# Non-parameter space/non-tbank object optional arguments
60parser.add_argument("--tstart", type=int, help="Start time of SFTs", default=900000000)
61parser.add_argument(
62 "--stats",
63 help="Whether to write the statistics of the tiling to a file or not",
64 action="store_true",
65)
66
67# Optional parameter space arguments
68parser.add_argument(
69 "--s", type=int, help="Set the s parameter", nargs="?", const=1, default=-1
70)
71parser.add_argument(
72 "--fmin", type=float, help="Set the fmin parameter", nargs="?", const=1, default=-1
73)
74parser.add_argument(
75 "--fmax", type=float, help="Set the fmax parameter", nargs="?", const=1, default=-1
76)
77parser.add_argument(
78 "--nmin", type=float, help="Set the nmin parameter", nargs="?", const=1, default=-1
79)
80parser.add_argument(
81 "--nmax", type=float, help="Set the nmax parameter", nargs="?", const=1, default=-1
82)
83parser.add_argument(
84 "--kmin", type=float, help="Set the kmin parameter", nargs="?", const=1, default=-1
85)
86parser.add_argument(
87 "--kmax", type=float, help="Set the kmax parameter", nargs="?", const=1, default=-1
88)
89parser.add_argument(
90 "--dur",
91 type=float,
92 help="The maximum duration we want the knot algorithm to calculate up to",
93 nargs="?",
94 const=1,
95 default=-1,
96)
97parser.add_argument(
98 "--knots", type=float, help="Use user defined knots", nargs="+", default=[0]
99)
100parser.add_argument(
101 "--mismatch",
102 type=float,
103 help="Set the mismatch parameter",
104 nargs="?",
105 const=1,
106 default=-1,
107)
108
109args = parser.parse_args()
110
111# Required arguments
112tbankcode = args.tbankcode
113j = args.j
114SizeOrTemps = args.SizeOrTemps
115
116# Optional arguments
117tstart = args.tstart
118stats = args.stats
119
120# Logging
121logging.basicConfig(
122 filename="TBankCommandLineLog_" + str(j) + ".log",
123 level=logging.DEBUG,
124 filemode="w",
125 format="%(asctime)s %(message)s",
126)
127log = logging.getLogger()
128log.setLevel(logging.DEBUG)
129
130# Constructing tbank object and setting parameters
131tbank = cd.TBank()
132
133if tbankcode == "GW170817":
134 tbank.SetDefaultBNSR()
135
136if tbankcode == "1987A":
137 tbank.SetDefault1987A()
138
139if tbankcode == "Small":
140 tbank.SetSmallTestCase()
141
142s = args.s
143fmin = args.fmin
144fmax = args.fmax
145nmin = args.nmin
146nmax = args.nmax
147kmin = args.kmin
148kmax = args.kmax
149dur = args.dur
150knots = args.knots
151mismatch = args.mismatch
152
153# If these parameters are not the set default value, then we make the corresponding change to the tbank object
154if s != -1:
155 tbank.s = s
156if fmin != -1:
157 tbank.fmin = fmin
158if fmax != -1:
159 tbank.fmax = fmax
160if nmin != -1:
161 tbank.nmin = nmin
162if nmax != -1:
163 tbank.nmax = nmax
164if kmin != -1:
165 tbank.kmin = kmin
166if kmax != -1:
167 tbank.kmax = kmax
168if dur != -1:
169 tbank.dur = dur
170if mismatch != -1:
171 tbank.maxmismatch = mismatch
172
173# Checking if we are using user defined knots or if we need to recalculate the knots in case any changed parameters affect knot choice
174if knots != [0]:
175 bf.knotslist = knots
176 tbank.dur = knots[-1] - knots[0]
177else:
178 ek.allidealisedknots(
179 tbank.s, tbank.dur, 40, tbank.fmax, tbank.nmax, tbank.kmin, tbank.maxmismatch
180 )
181
182# Adjusting knot start time
183if bf.knotslist[0] != tstart:
184 for i, knot in enumerate(bf.knotslist):
185 bf.knotslist[i] = knot + tstart
186
187logging.info(tbank.toString())
188
189logging.info("Knots are: " + str(bf.knotslist))
190logging.info("Knots: %s", str(bf.knotslist))
191
192tbank.knots = bf.knotslist
193
194logging.info(bf.knotslist)
195
196if SizeOrTemps == "S":
197 temps = tbe.PWTBankSizeWithObject(tbank, stats=stats)
198 logging.info("Number of temps found: " + str(temps))
199elif SizeOrTemps == "T":
200 temps = tbe.PWTBankWithObject(tbank)
201 logging.info("Number of temps found: " + str(len(temps)))
202
203 midway = int(np.ceil(len(temps) / 2))
204
205 first = temps[0]
206 midway = temps[midway]
207 last = temps[-1]
208
209 logging.info(
210 "First: "
211 + str(first[0 : (2 * tbank.s)])
212 + " ... "
213 + str(first[-(2 * tbank.s + 1) : -1])
214 )
215 logging.info(
216 "Midway: "
217 + str(midway[0 : (2 * tbank.s)])
218 + " ... "
219 + str(midway[-(2 * tbank.s + 1) : -1])
220 )
221 logging.info(
222 "Last: "
223 + str(last[0 : (2 * tbank.s)])
224 + " ... "
225 + str(last[-(2 * tbank.s + 1) : -1])
226 )
227
228 gom.PlotPWModel(first, show=False, label="First", linewidth=4)
229 gom.PlotPWModel(midway, show=False, label="Midway", linewidth=3)
230 gom.PlotPWModel(last, show=False, label="Last", linewidth=2)
231 plt.legend()
232 plt.show()
233
234# Profiling
235pr.disable()
236s = io.StringIO()
237sortby = pstats.SortKey.CUMULATIVE
238ps = pstats.Stats(pr, stream=s).sort_stats("cumtime")
239ps.print_stats()
240
241with open("TBankCommandLineProfile_" + str(j) + ".txt", "w+") as f:
242 f.write(s.getvalue())