optimal_setup_functions.py 5.75 KB
Newer Older
Gregory Ashton's avatar
Gregory Ashton committed
1
2
3
4
5
6
7
8
9
10
11
12
"""

Provides functions to aid in calculating the optimal setup based on the metric
volume estimates.

"""

import logging
import numpy as np
import scipy.optimize
import lal
import lalpulsar
13
import helper_functions
Gregory Ashton's avatar
Gregory Ashton committed
14
15
16


def get_optimal_setup(
17
18
        R, Nsegs0, tref, minStartTime, maxStartTime, prior, fiducial_freq,
        detector_names, earth_ephem, sun_ephem):
Gregory Ashton's avatar
Gregory Ashton committed
19
20
21
22
    logging.info('Calculating optimal setup for R={}, Nsegs0={}'.format(
        R, Nsegs0))

    V_0 = get_V_estimate(
23
24
        Nsegs0, tref, minStartTime, maxStartTime, prior, fiducial_freq,
        detector_names, earth_ephem, sun_ephem)
Gregory Ashton's avatar
Gregory Ashton committed
25
26
27
28
29
30
31
32
33
    logging.info('Stage {}, nsegs={}, V={}'.format(0, Nsegs0, V_0))

    nsegs_vals = [Nsegs0]
    V_vals = [V_0]

    i = 0
    nsegs_i = Nsegs0
    while nsegs_i > 1:
        nsegs_i, V_i = get_nsegs_ip1(
34
35
            nsegs_i, R, tref, minStartTime, maxStartTime, prior, fiducial_freq,
            detector_names, earth_ephem, sun_ephem)
Gregory Ashton's avatar
Gregory Ashton committed
36
37
38
39
40
41
42
43
44
45
        nsegs_vals.append(nsegs_i)
        V_vals.append(V_i)
        i += 1
        logging.info(
            'Stage {}, nsegs={}, V={}'.format(i, nsegs_i, V_i))

    return nsegs_vals, V_vals


def get_nsegs_ip1(
46
47
        nsegs_i, R, tref, minStartTime, maxStartTime, prior, fiducial_freq,
        detector_names, earth_ephem, sun_ephem):
Gregory Ashton's avatar
Gregory Ashton committed
48
49
50

    log10R = np.log10(R)
    log10Vi = np.log10(get_V_estimate(
51
52
        nsegs_i, tref, minStartTime, maxStartTime, prior, fiducial_freq,
        detector_names, earth_ephem, sun_ephem))
Gregory Ashton's avatar
Gregory Ashton committed
53
54
55
56
57
58
59
60
61
62

    def f(nsegs_ip1):
        if nsegs_ip1[0] > nsegs_i:
            return 1e6
        if nsegs_ip1[0] < 0:
            return 1e6
        nsegs_ip1 = int(nsegs_ip1[0])
        if nsegs_ip1 == 0:
            nsegs_ip1 = 1
        Vip1 = get_V_estimate(
63
64
            nsegs_ip1, tref, minStartTime, maxStartTime, prior, fiducial_freq,
            detector_names, earth_ephem, sun_ephem)
65
        if Vip1 is None:
Gregory Ashton's avatar
Gregory Ashton committed
66
67
68
            return 1e6
        else:
            log10Vip1 = np.log10(Vip1)
69
            return np.abs(log10Vi + log10R - log10Vip1)
Gregory Ashton's avatar
Gregory Ashton committed
70
71
72
73
74
75
76
    res = scipy.optimize.minimize(f, .5*nsegs_i, method='Powell', tol=0.1,
                                  options={'maxiter': 10})
    nsegs_ip1 = int(res.x)
    if nsegs_ip1 == 0:
        nsegs_ip1 = 1
    if res.success:
        return nsegs_ip1, get_V_estimate(
77
78
            nsegs_ip1, tref, minStartTime, maxStartTime, prior, fiducial_freq,
            detector_names, earth_ephem, sun_ephem)
Gregory Ashton's avatar
Gregory Ashton committed
79
80
81
82
    else:
        raise ValueError('Optimisation unsuccesful')


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def get_parallelepiped(prior):
    keys = ['Alpha', 'Delta', 'F0', 'F1', 'F2']
    spindown_keys = keys[3:]
    sky_keys = keys[:2]
    lims = []
    lims_keys = []
    lims_idxs = []
    for i, key in enumerate(keys):
        if type(prior[key]) == dict:
            if prior[key]['type'] == 'unif':
                lims.append([prior[key]['lower'], prior[key]['upper']])
                lims_keys.append(key)
                lims_idxs.append(i)
            else:
                raise ValueError(
                    "Prior type {} not yet supported".format(
                        prior[key]['type']))
        elif key not in spindown_keys:
            lims.append([prior[key], 0])
    lims = np.array(lims)
    lims_keys = np.array(lims_keys)
    base = lims[:, 0]
    p = [base]
    for i in lims_idxs:
        basex = base.copy()
        basex[i] = lims[i, 1]
        p.append(basex)
    spindowns = np.sum([np.sum(lims_keys == k) for k in spindown_keys])
    sky = any([key in lims_keys for key in sky_keys])
    return np.array(p).T, spindowns , sky


Gregory Ashton's avatar
Gregory Ashton committed
115
def get_V_estimate(
116
117
        nsegs, tref, minStartTime, maxStartTime, prior, fiducial_freq,
        detector_names, earth_ephem, sun_ephem):
118
    """ Returns V estimated from the super-sky metric
Gregory Ashton's avatar
Gregory Ashton committed
119
120
121
122
123
124
125
126
127

    Parameters
    ----------
    nsegs: int
        Number of semi-coherent segments
    tref: int
        Reference time in GPS seconds
    minStartTime, maxStartTime: int
        Minimum and maximum SFT timestamps
128
129
    prior: dict
        The prior dictionary
Gregory Ashton's avatar
Gregory Ashton committed
130
131
132
133
134
135
136
137
    fiducial_freq: float
        Fidicual frequency
    detector_names: array
        Array of detectors to average over
    earth_ephem, sun_ephem: st
        Paths to the ephemeris files

    """
138
139
140
141
142
143
    in_phys, spindowns, sky = get_parallelepiped(prior)
    out_rssky = np.zeros(in_phys.shape)

    in_phys = helper_functions.convert_array_to_gsl_matrix(in_phys)
    out_rssky = helper_functions.convert_array_to_gsl_matrix(out_rssky)

Gregory Ashton's avatar
Gregory Ashton committed
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    tboundaries = np.linspace(minStartTime, maxStartTime, nsegs+1)

    ref_time = lal.LIGOTimeGPS(tref)
    segments = lal.SegListCreate()
    for j in range(len(tboundaries)-1):
        seg = lal.SegCreate(lal.LIGOTimeGPS(tboundaries[j]),
                            lal.LIGOTimeGPS(tboundaries[j+1]),
                            j)
        lal.SegListAppend(segments, seg)
    detNames = lal.CreateStringVector(*detector_names)
    detectors = lalpulsar.MultiLALDetector()
    lalpulsar.ParseMultiLALDetector(detectors, detNames)
    detector_weights = None
    detector_motion = (lalpulsar.DETMOTION_SPIN
                       + lalpulsar.DETMOTION_ORBIT)
    ephemeris = lalpulsar.InitBarycenter(earth_ephem, sun_ephem)
    try:
        SSkyMetric = lalpulsar.ComputeSuperskyMetrics(
            spindowns, ref_time, segments, fiducial_freq, detectors,
            detector_weights, detector_motion, ephemeris)
    except RuntimeError as e:
        logging.debug('Encountered run-time error {}'.format(e))
        return None, None, None

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
    if sky:
        i = 0
    else:
        i = 2

    lalpulsar.ConvertPhysicalToSuperskyPoints(
        out_rssky, in_phys, SSkyMetric.semi_rssky_transf)

    parallelepiped = (out_rssky.data[i:, 1:].T - out_rssky.data[i:, 0]).T

    sqrtdetG = np.sqrt(np.linalg.det(
        SSkyMetric.semi_rssky_metric.data[i:, i:]))

    dV = np.abs(np.linalg.det(parallelepiped))

    V = sqrtdetG * dV

    return V