Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALPulsar 7.1.1.1-5e288d3
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
tbank_estimates.py
Go to the documentation of this file.
1# Copyright (C) 2019--2023 Benjamin Grace
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## \file
18## \ingroup lalpulsar_python_piecewise_model
19"""
20Function to estimate the size of piecewise model template banks.
21"""
22
23import logging
24import time
25
26import numpy as np
27
28import lal
29import lalpulsar as lp
30
31from . import basis_functions as bf
32from . import semicoherent_metric_methods as scmm
33
34
35# Returns tiling statistics from a tiling object
36def tilingstatistics(tiling, dim, iterator=-1):
37
38 if iterator == 0:
39 totalpoints = []
40 minpoints = []
41 maxpoints = []
42 minvals = []
43 maxvals = []
44
45 for i in range(dim):
46 logging.debug("Dim :" + str(i))
47 tilingstats = lp.LatticeTilingStatistics(tiling, i)
48 totalpoints.append(tilingstats.total_points)
49 minpoints.append(tilingstats.min_points)
50 maxpoints.append(tilingstats.max_points)
51 minvals.append(tilingstats.min_value)
52 maxvals.append(tilingstats.max_value)
53
54 logging.info("Total points up to dimension: %s", str(totalpoints))
55 logging.info("Min points in dimension: %s", str(minpoints))
56 logging.info("Max points in dimension: %s", str(maxpoints))
57 logging.info("Min value in dimension: %s", str(minvals))
58 logging.info("Max value in dimension: %s", str(maxvals))
59
60 alltilingstats = [totalpoints, minpoints, maxpoints, minvals, maxvals]
61
62 return alltilingstats
63
64 else:
65 return []
66
67
68# Sets the bounds on the tiling lattice by the parameters given in a TBank object
69def setbounds(tiling, tbank):
70 s = tbank.s
71 fmin = tbank.fmin
72 fmax = tbank.fmax
73 nmin = tbank.nmin
74 nmax = tbank.nmax
75 kmin = tbank.kmin
76 kmax = tbank.kmax
77 dur = tbank.dur
78 mismatch = tbank.maxmismatch
79
80 bbox = tbank.flags_bbox
81 intbox = tbank.flags_intbox
82
83 knots = bf.knotslist
84
85 lp.SetLatticeTilingPiecewiseBounds(
86 tiling, s, fmin, fmax, nmin, nmax, kmin, kmax, knots, bbox, intbox
87 )
88
89
90# Returns number of templates required to cover a parameter space with given knots
92 s,
93 fmin,
94 fmax,
95 nmin,
96 nmax,
97 kmin,
98 kmax,
99 knots,
100 mismatch,
101 printouts=1000000,
102 recdepth=1,
103 prevalidtempsandnmin=[-1, -1],
104 metric=[],
105):
106
107 filename = "BInd_" + str(fmin) + "_" + str(fmax) + ".txt"
108
109 if nmin < nmin:
110 return prevalidtempsandnmin
111
112 finalknot = len(knots)
113
114 # Create LatticeTiling object
115 tiling = lp.CreateLatticeTiling(s * finalknot)
116
117 bf.knotslist = knots
118 logging.info("Knots list: %s", str(bf.knotslist))
119
120 logging.info("Computing metric")
121
122 if metric == []:
123 metric = scmm.metric(s)
124
125 logging.info("Metric calculated")
126
127 stepsizes = [np.sqrt(2 / metric[i][i]) for i in range(s * finalknot)]
128 logging.info("Maximum parameter step sizes: %s", str(stepsizes))
129
130 # Set Bounds
131 lp.SetLatticeTilingPiecewiseBounds(
132 tiling, s, fmin, fmax, nmin, nmax, kmin, kmax, knots
133 )
134
135 # Set metric, mismatch and lattice type
136 lp.SetTilingLatticeAndMetric(tiling, lp.TILING_LATTICE_ANSTAR, metric, mismatch)
137
138 # Create Iterator
139 iterator = lp.CreateLatticeTilingIterator(tiling, s * finalknot)
140 lp.ResetLatticeTilingIterator(iterator)
141
142 start = time.time()
143 tiles = 0
144
145 p = lal.gsl_vector(s * finalknot)
146
147 while lp.NextLatticeTilingPoint(iterator, p) != 0:
148
149 tiles += 1
150 if tiles % printouts == 0:
151 logging.info("Current number of tiles: %s", str(tiles))
152 logging.info("Elapsed time: %s", str(time.time() - start))
153
154 if tiles > 10**8:
155 break
156
157 if lp.NextLatticeTilingPoint(iterator, p) != 0:
158
159 if recdepth >= 12:
160
161 if prevalidtempsandnmin[0] != -1:
162 logging.info(
163 "Final recursive step. Valid range found. Frequency range: %s, braking index range: %s",
164 str([fmin, fmax]),
165 str([nmin, nmax]),
166 )
167
168 f = open(filename, "a")
169 f.write(str([nmin, tiles]) + "\n")
170 f.close()
171
172 return prevalidtempsandnmin
173
174 else:
175 logging.info(
176 "Final recdepth %s reached with no valid range found. Frequency range: %s, braking index range: %s",
177 str(recdepth),
178 str([fmin, fmax]),
179 str([nmin, nmax]),
180 )
181
182 f = open(filename, "a")
183 f.write(str([nmin, tiles]) + "\n")
184 f.close()
185
186 return [-1, 10**8]
187
188 else:
189 logging.info(
190 "Recursive step %s. No valid range found for this nmin. Frequency range: %s, braking index range: %s",
191 str(recdepth),
192 str([fmin, fmax]),
193 str([nmin, nmax]),
194 )
195
196 f = open(filename, "a")
197 f.write(str([nmin, tiles]) + "\n")
198 f.close()
199
200 newnmin = nmin + (nmax - nmin) * 2 ** -(recdepth + 1)
201 return recursivetbanksize(
202 s,
203 fmin,
204 fmax,
205 newnmin,
206 nmax,
207 kmin,
208 kmax,
209 knots,
210 mismatch,
211 printouts=printouts,
212 recdepth=recdepth + 1,
213 metric=metric,
214 )
215
216 else:
217 if recdepth >= 12:
218 logging.info(
219 "Statistics calculated: %s",
220 str(
222 tiling,
223 s * finalknot,
224 iterator=lp.NextLatticeTilingPoint(iterator, p),
225 )
226 ),
227 )
228 logging.info(
229 "Frequency range: %s, Braking index range: %s",
230 str([fmin, fmax]),
231 str([nmin, nmax]),
232 )
233 logging.info("Final tile count: %s", str(tiles))
234 logging.info("Total elapsed time: %s", str(time.time() - start))
235
236 logging.info(
237 "Final recursive step. Valid range found. Frequency range: %s, braking index range: %s",
238 str([fmin, fmax]),
239 str([nmin, nmax]),
240 )
241
242 f = open(filename, "a")
243 f.write(str([nmin, tiles]) + "\n")
244 f.close()
245
246 return [tiles, nmin]
247
248 else:
249 logging.info(
250 "Statistics calculated: %s",
251 str(
253 tiling,
254 s * finalknot,
255 iterator=lp.NextLatticeTilingPoint(iterator, p),
256 )
257 ),
258 )
259 logging.info(
260 "Frequency range: %s, Braking index range: %s",
261 str([fmin, fmax]),
262 str([nmin, nmax]),
263 )
264 logging.info("Final tile count: %s", str(tiles))
265 logging.info("Total elapsed time: %s", str(time.time() - start))
266
267 logging.info(
268 "Recursive step %s. Valid range found for this nmin. Frequency range: %s, braking index range: %s",
269 str(recdepth),
270 str([fmin, fmax]),
271 str([nmin, nmax]),
272 )
273
274 f = open(filename, "a")
275 f.write(str([nmin, tiles]) + "\n")
276 f.close()
277
278 newnmin = nmin - (nmax - nmin) * 2 ** -(recdepth + 1)
279 return recursivetbanksize(
280 s,
281 fmin,
282 fmax,
283 newnmin,
284 nmax,
285 kmin,
286 kmax,
287 knots,
288 mismatch,
289 printouts=printouts,
290 prevalidtempsandnmin=[tiles, nmin],
291 recdepth=recdepth + 1,
292 metric=metric,
293 )
def tilingstatistics(tiling, dim, iterator=-1)
def recursivetbanksize(s, fmin, fmax, nmin, nmax, kmin, kmax, knots, mismatch, printouts=1000000, recdepth=1, prevalidtempsandnmin=[-1, -1], metric=[])