Loading [MathJax]/extensions/TeX/AMSsymbols.js
LALPulsar 7.1.1.1-8a6b96f
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
sampling_methods.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"""
20Sample the general torque equation using various methods.
21"""
22
23import logging
24
25import numpy as np
26
27from . import basis_functions as bf
28from . import gte_and_other_methods as gom
29from . import errors
30
31
32# Returns sample points where a 'ppint' number of points are evenly spaced between each knot
33def pointsbyknot(ppint):
34 ints = len(bf.knotslist) - 1
35 points = []
36
37 for i in range(ints):
38 ps = bf.p(i)
39 pe = bf.p(i + 1)
40 for j in range(ppint):
41 point = ps + j / ppint * (pe - ps)
42 points.append(point)
43
44 return points
45
46
47# Returns sample points that correspond with values of frequency that are evenly spaced (number of points = ppint *
48# ints)
49def pointsbyfrequency(ppint, f0, ngte, kgte):
50 points = []
51 dur = bf.knotslist[-1] - bf.knotslist[0]
52 ints = len(bf.knotslist) - 1
53
54 freqps = np.linspace(
55 gom.gte(0, f0, ngte, kgte), gom.gte(dur, f0, ngte, kgte), ppint * ints
56 )
57
58 for freq in freqps:
59 points.append(gom.gteinv(freq, f0, ngte, kgte) + bf.knotslist[0])
60
61 if points[-1] > bf.knotslist[-1]:
62 points[-1] = bf.knotslist[-1]
63
64 return points
65
66
67# Returns sample points that are randomly chosen. Number of sample points = ppint * ints
68def pointsrandom(ppint):
69 points = []
70 ints = len(bf.knotslist) - 1
71 dur = bf.knotslist[-1] - bf.knotslist[0]
72
73 for i in range(ppint * ints):
74 point = np.random.uniform(0, dur)
75 points.append(point)
76
77 return points
78
79
80normalpoints = False # Points chosen to be evenly spaced between knots
81freqpoints = True # Points chosen in time to correspond with points evenly spaced in frequency by the GTE (runs into problems for long signal durations)
82
83# Returns an integer value corresponding to the interval that point lies in
84def thisint(point):
85 if point < bf.knotslist[0] or point > bf.knotslist[-1]:
86 logging.debug(
87 "Sample point value not correctly chosen. Point and knot extrema are: "
88 + str([point, bf.knotslist[0], bf.knotslist[-1]])
89 )
91
92 ints = len(bf.knotslist) - 1
93
94 for i in range(ints + 1):
95 ps = bf.p(i)
96
97 if point < ps:
98 return i - 1
99
100 if point == bf.knotslist[-1]:
101 return ints - 1
102
103
104# Returns an integer list with numbers corresponding to which intervals do not contain sample points
105def samplepointcheck(points, checkfirstintervals=True):
106
107 if normalpoints:
108 return []
109 ints = len(bf.knotslist) - 1
110 intscontainingnopoint = [i for i in range(ints)]
111
112 for point in points:
113 j = thisint(point)
114
115 if j in intscontainingnopoint:
116 intscontainingnopoint.remove(j)
117
118 if not checkfirstintervals:
119 if len(intscontainingnopoint) != 0:
120 for j in range(ints - 1):
121 if j in intscontainingnopoint:
122 intscontainingnopoint.remove(j)
123
124 return intscontainingnopoint
125
126
127# Returns our sample points depending upon the boolean values above
128def samplepoints(ppint, f0, ngte, kgte):
129 if normalpoints:
130 points = pointsbyknot(ppint)
131 elif freqpoints:
132 points = pointsbyfrequency(ppint, f0, ngte, kgte)
133 else:
134 points = pointsrandom(ppint)
135
136 sadints = samplepointcheck(points)
137
138 if len(sadints) != 0:
139 logging.error("These intervals contain no sample point: " + str(sadints))
141 return points
142
143
144# As the pointsbyfrequency method but now instead we choose sample points between the times corresponding with the
145# knotnumbers knotnuma and knotnumb. Can be used for calculating the MOLS model of the GTE between two specific times
146# instead of the default times 0 to the signal duration
147def pointsbyfrequencywithinknots(knotnuma, knotnumb, ppint, f0, ngte, kgte):
148 points = []
149
150 ts = bf.knotslist[knotnuma] - bf.knotslist[0]
151 te = bf.knotslist[knotnumb] - bf.knotslist[0]
152
153 ints = knotnumb - knotnuma
154
155 freqps = np.linspace(
156 gom.gte(ts, f0, ngte, kgte), gom.gte(te, f0, ngte, kgte), ppint * ints
157 )
158
159 for freq in freqps:
160 points.append(gom.gteinv(freq, f0, ngte, kgte) + bf.knotslist[0])
161
162 if points[0] < ts + bf.knotslist[0]:
163 points[0] = ts + bf.knotslist[0]
164 if points[-1] > te + bf.knotslist[0]:
165 points[-1] = te + bf.knotslist[0]
166
167 return points
168
169
170# As the the samplepoints method but now instead only generates sample points between the two given knots. As the above
171# method, can be used for calculating the MOLS model between two specific knots instead of the default times 0 to the
172# signal duration.
173def samplepointswithinknots(knotnuma, knotnumb, ppint, f0, ngte, kgte):
174 points = pointsbyfrequencywithinknots(knotnuma, knotnumb, ppint, f0, ngte, kgte)
175
176 sadints = samplepointcheck(points, checkfirstintervals=False)
177
178 if len(sadints) != 0:
179 for thisint in sadints:
180 if knotnuma + 1 <= thisint and thisint < knotnumb:
181 logging.error(
182 "These intervals contain no sample point: " + str(sadints)
183 )
185
186 return points
def samplepointcheck(points, checkfirstintervals=True)
def pointsbyfrequencywithinknots(knotnuma, knotnumb, ppint, f0, ngte, kgte)
def samplepointswithinknots(knotnuma, knotnumb, ppint, f0, ngte, kgte)
def pointsbyfrequency(ppint, f0, ngte, kgte)