core.py 41.8 KB
Newer Older
Gregory Ashton's avatar
Gregory Ashton committed
1
""" The core tools used in pyfstat """
2
3
from __future__ import division, absolute_import, print_function

Gregory Ashton's avatar
Gregory Ashton committed
4
5
6
7
import os
import logging
import copy

8

9
import glob
Gregory Ashton's avatar
Gregory Ashton committed
10
import numpy as np
11
12
13
14
15
import scipy.special
import scipy.optimize

import lal
import lalpulsar
16
import pyfstat.helper_functions as helper_functions
17
18

# workaround for matplotlib on X-less remote logins
19
if 'DISPLAY' in os.environ:
20
21
    import matplotlib.pyplot as plt
else:
22
23
    logging.info('No $DISPLAY environment variable found, so importing \
                  matplotlib.pyplot with non-interactive "Agg" backend.')
24
25
26
27
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt

Gregory Ashton's avatar
Gregory Ashton committed
28
helper_functions.set_up_matplotlib_defaults()
29
args, tqdm = helper_functions.set_up_command_line_arguments()
Gregory Ashton's avatar
Gregory Ashton committed
30
earth_ephem, sun_ephem = helper_functions.set_up_ephemeris_configuration()
31
detector_colors = {'h1': 'C0', 'l1': 'C1'}
Gregory Ashton's avatar
Gregory Ashton committed
32
33


Gregory Ashton's avatar
Gregory Ashton committed
34
class Bunch(object):
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    """ Turns dictionary into object with attribute-style access

    Parameter
    ---------
    dict
        Input dictionary

    Examples
    --------
    >>> data = Bunch(dict(x=1, y=[1, 2, 3], z=True))
    >>> print(data.x)
    1
    >>> print(data.y)
    [1, 2, 3]
    >>> print(data.z)
    True

    """
Gregory Ashton's avatar
Gregory Ashton committed
53
54
55
56
57
    def __init__(self, dictionary):
        self.__dict__.update(dictionary)


def read_par(filename=None, label=None, outdir=None, suffix='par',
58
59
             return_type='dict', comments=['%', '#'], raise_error=False):
    """ Read in a .par or .loudest file, returns a dict or Bunch of the data
60

Gregory Ashton's avatar
Gregory Ashton committed
61
62
    Parameters
    ----------
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    filename : str
        Filename (path) containing rows of `key=val` data to read in.
    label, outdir, suffix : str, optional
        If filename is None, form the file to read as `outdir/label.suffix`.
    return_type : {'dict', 'bunch'}, optional
        If `dict`, return a dictionary, if 'bunch' return a Bunch
    comments : str or list of strings, optional
        Characters denoting that a row is a comment.
    raise_error : bool, optional
        If True, raise an error for lines which are not comments, but cannot
        be read.

    Notes
    -----
    This can also be used to read in .loudest files, or any file which has
    rows of `key=val` data (in which the val can be understood using eval(val)
Gregory Ashton's avatar
Gregory Ashton committed
79
80
81
82
83

    Returns
    -------
    d: Bunch or dict
        The par values as either a `Bunch` or dict type
84

85
86
87
88
    """
    if filename is None:
        filename = '{}/{}.{}'.format(outdir, label, suffix)
    if os.path.isfile(filename) is False:
89
        raise ValueError("No file {} found".format(filename))
Gregory Ashton's avatar
Gregory Ashton committed
90
91
    d = {}
    with open(filename, 'r') as f:
92
        d = _get_dictionary_from_lines(f, comments, raise_error)
Gregory Ashton's avatar
Gregory Ashton committed
93
94
95
96
97
98
    if return_type in ['bunch', 'Bunch']:
        return Bunch(d)
    elif return_type in ['dict', 'dictionary']:
        return d
    else:
        raise ValueError('return_type {} not understood'.format(return_type))
Gregory Ashton's avatar
Gregory Ashton committed
99
100


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def _get_dictionary_from_lines(lines, comments, raise_error):
    """ Return dictionary of key=val pairs for each line in lines

    Parameters
    ----------
    comments : str or list of strings
        Characters denoting that a row is a comment.
    raise_error : bool
        If True, raise an error for lines which are not comments, but cannot
        be read.

    Returns
    -------
    d: Bunch or dict
        The par values as either a `Bunch` or dict type

    """
118
119
    d = {}
    for line in lines:
120
        if line[0] not in comments and len(line.split('=')) == 2:
121
122
123
124
125
            try:
                key, val = line.rstrip('\n').split('=')
                key = key.strip()
                d[key] = np.float64(eval(val.rstrip('; ')))
            except SyntaxError:
126
127
                if raise_error:
                    raise IOError('Line {} not understood'.format(line))
128
129
130
131
132
                pass
    return d


def predict_fstat(h0, cosi, psi, Alpha, Delta, Freq, sftfilepattern,
Gregory Ashton's avatar
Gregory Ashton committed
133
134
                  minStartTime, maxStartTime, IFO=None, assumeSqrtSX=None,
                  **kwargs):
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
    """ Wrapper to lalapps_PredictFstat

    Parameters
    ----------
    h0, cosi, psi, Alpha, Delta, Freq : float
        Signal properties, see `lalapps_PredictFstat --help` for more info.
    sftfilepattern : str
        Pattern matching the sftfiles to use.
    minStartTime, maxStartTime : int
    IFO : str
        See `lalapps_PredictFstat --help`
    assumeSqrtSX : float or None
        See `lalapps_PredictFstat --help`, if None this option is not used

    Returns
    -------
    twoF_expected, twoF_sigma : float
        The expectation and standard deviation of 2F

    """
155
156
157
158
159
160
161
162
163
164
    cl_pfs = []
    cl_pfs.append("lalapps_PredictFstat")
    cl_pfs.append("--h0={}".format(h0))
    cl_pfs.append("--cosi={}".format(cosi))
    cl_pfs.append("--psi={}".format(psi))
    cl_pfs.append("--Alpha={}".format(Alpha))
    cl_pfs.append("--Delta={}".format(Delta))
    cl_pfs.append("--Freq={}".format(Freq))

    cl_pfs.append("--DataFiles='{}'".format(sftfilepattern))
165
    if assumeSqrtSX:
166
        cl_pfs.append("--assumeSqrtSX={}".format(assumeSqrtSX))
167
    if IFO:
168
        cl_pfs.append("--IFO={}".format(IFO))
169

170
171
172
    cl_pfs.append("--minStartTime={}".format(int(minStartTime)))
    cl_pfs.append("--maxStartTime={}".format(int(maxStartTime)))
    cl_pfs.append("--outputFstat=/tmp/fs")
173

174
175
    cl_pfs = " ".join(cl_pfs)
    helper_functions.run_commandline(cl_pfs)
176
177
178
179
    d = read_par(filename='/tmp/fs')
    return float(d['twoF_expected']), float(d['twoF_sigma'])


Gregory Ashton's avatar
Gregory Ashton committed
180
class BaseSearchClass(object):
181
182
183
184
185
186
187
188
189
    """ The base search class providing parent methods to other searches

    Attributes
    ----------
    earth_ephem_default, sun_ephem_default : str
        Paths to the earth and sun ephemeris files to use if not specified
        elsewhere

    """
Gregory Ashton's avatar
Gregory Ashton committed
190
191
192
193

    earth_ephem_default = earth_ephem
    sun_ephem_default = sun_ephem

194
    def _add_log_file(self):
Gregory Ashton's avatar
Gregory Ashton committed
195
196
197
198
199
200
201
202
203
        """ Log output to a file, requires class to have outdir and label """
        logfilename = '{}/{}.log'.format(self.outdir, self.label)
        fh = logging.FileHandler(logfilename)
        fh.setLevel(logging.INFO)
        fh.setFormatter(logging.Formatter(
            '%(asctime)s %(levelname)-8s: %(message)s',
            datefmt='%y-%m-%d %H:%M'))
        logging.getLogger().addHandler(fh)

204
    def _shift_matrix(self, n, dT):
Gregory Ashton's avatar
Gregory Ashton committed
205
206
207
208
        """ Generate the shift matrix

        Parameters
        ----------
209
        n : int
Gregory Ashton's avatar
Gregory Ashton committed
210
            The dimension of the shift-matrix to generate
211
        dT : float
Gregory Ashton's avatar
Gregory Ashton committed
212
213
214
215
            The time delta of the shift matrix

        Returns
        -------
216
217
        m : ndarray, shape (n,)
            The shift matrix.
Gregory Ashton's avatar
Gregory Ashton committed
218

219
        """
Gregory Ashton's avatar
Gregory Ashton committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
        m = np.zeros((n, n))
        factorial = np.math.factorial
        for i in range(n):
            for j in range(n):
                if i == j:
                    m[i, j] = 1.0
                elif i > j:
                    m[i, j] = 0.0
                else:
                    if i == 0:
                        m[i, j] = 2*np.pi*float(dT)**(j-i) / factorial(j-i)
                    else:
                        m[i, j] = float(dT)**(j-i) / factorial(j-i)
        return m

235
    def _shift_coefficients(self, theta, dT):
Gregory Ashton's avatar
Gregory Ashton committed
236
237
238
239
        """ Shift a set of coefficients by dT

        Parameters
        ----------
240
241
        theta : array-like, shape (n,)
            Vector of the expansion coefficients to transform starting from the
Gregory Ashton's avatar
Gregory Ashton committed
242
            lowest degree e.g [phi, F0, F1,...].
243
244
        dT : float
            Difference between the two reference times as tref_new - tref_old.
Gregory Ashton's avatar
Gregory Ashton committed
245
246
247

        Returns
        -------
248
249
        theta_new : ndarray, shape (n,)
            Vector of the coefficients as evaluated as the new reference time.
Gregory Ashton's avatar
Gregory Ashton committed
250
251
        """
        n = len(theta)
252
        m = self._shift_matrix(n, dT)
Gregory Ashton's avatar
Gregory Ashton committed
253
254
        return np.dot(m, theta)

255
    def _calculate_thetas(self, theta, delta_thetas, tbounds, theta0_idx=0):
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
        """ Calculates the set of thetas given delta_thetas, the jumps

        This is used when generating data containing glitches or timing noise.
        Specifically, the source parameters of the signal are not constant in
        time, but jump by `delta_theta` at `tbounds`.

        Parameters
        ----------
        theta : array_like
            The source parameters of size (n,).
        delta_thetas : array_like
            The jumps in the source parameters of size (m, n) where m is the
            number of jumps.
        tbounds : array_like
            Time boundaries of the jumps of size (m+2,).
        theta0_idx : int
            Index of the segment for which the theta are defined.

        Returns
        -------
        ndarray
            The set of thetas, shape (m+1, n).

        """
Gregory Ashton's avatar
Gregory Ashton committed
280
281
282
        thetas = [theta]
        for i, dt in enumerate(delta_thetas):
            if i < theta0_idx:
283
                pre_theta_at_ith_glitch = self._shift_coefficients(
Gregory Ashton's avatar
Gregory Ashton committed
284
285
                    thetas[0], tbounds[i+1] - self.tref)
                post_theta_at_ith_glitch = pre_theta_at_ith_glitch - dt
286
                thetas.insert(0, self._shift_coefficients(
Gregory Ashton's avatar
Gregory Ashton committed
287
288
289
                    post_theta_at_ith_glitch, self.tref - tbounds[i+1]))

            elif i >= theta0_idx:
290
                pre_theta_at_ith_glitch = self._shift_coefficients(
Gregory Ashton's avatar
Gregory Ashton committed
291
292
                    thetas[i], tbounds[i+1] - self.tref)
                post_theta_at_ith_glitch = pre_theta_at_ith_glitch + dt
293
                thetas.append(self._shift_coefficients(
Gregory Ashton's avatar
Gregory Ashton committed
294
                    post_theta_at_ith_glitch, self.tref - tbounds[i+1]))
295
        self.thetas_at_tref = thetas
Gregory Ashton's avatar
Gregory Ashton committed
296
297
        return thetas

298
    def _get_list_of_matching_sfts(self):
299
        """ Returns a list of sfts matching the attribute sftfilepattern """
300
301
        sftfilepatternlist = np.atleast_1d(self.sftfilepattern.split(';'))
        matches = [glob.glob(p) for p in sftfilepatternlist]
302
        matches = [item for sublist in matches for item in sublist]
303
304
305
306
        if len(matches) > 0:
            return matches
        else:
            raise IOError('No sfts found matching {}'.format(
307
                self.sftfilepattern))
308

Gregory Ashton's avatar
Gregory Ashton committed
309
310
311
312
313
314
315
316

class ComputeFstat(object):
    """ Base class providing interface to `lalpulsar.ComputeFstat` """

    earth_ephem_default = earth_ephem
    sun_ephem_default = sun_ephem

    @helper_functions.initializer
317
    def __init__(self, tref, sftfilepattern=None, minStartTime=None,
Gregory Ashton's avatar
Gregory Ashton committed
318
                 maxStartTime=None, binary=False, transient=True, BSGL=False,
319
                 detectors=None, minCoverFreq=None, maxCoverFreq=None,
320
                 earth_ephem=None, sun_ephem=None, injectSources=None,
321
                 injectSqrtSX=None, assumeSqrtSX=None, SSBprec=None):
Gregory Ashton's avatar
Gregory Ashton committed
322
323
324
        """
        Parameters
        ----------
325
        tref : int
Gregory Ashton's avatar
Gregory Ashton committed
326
            GPS seconds of the reference time.
327
        sftfilepattern : str
328
329
            Pattern to match SFTs using wildcards (*?) and ranges [0-9];
            mutiple patterns can be given separated by colons.
330
        minStartTime, maxStartTime : float GPStime
Gregory Ashton's avatar
Gregory Ashton committed
331
332
            Only use SFTs with timestemps starting from (including, excluding)
            this epoch
333
        binary : bool
Gregory Ashton's avatar
Gregory Ashton committed
334
            If true, search of binary parameters.
335
        transient : bool
Gregory Ashton's avatar
Gregory Ashton committed
336
            If true, allow for the Fstat to be computed over a transient range.
337
        BSGL : bool
Gregory Ashton's avatar
Gregory Ashton committed
338
            If true, compute the BSGL rather than the twoF value.
339
        detectors : str
Gregory Ashton's avatar
Gregory Ashton committed
340
            Two character reference to the data to use, specify None for no
341
            contraint. If multiple-separate by comma.
342
        minCoverFreq, maxCoverFreq : float
Gregory Ashton's avatar
Gregory Ashton committed
343
344
345
            The min and max cover frequency passed to CreateFstatInput, if
            either is None the range of frequencies in the SFT less 1Hz is
            used.
346
        earth_ephem, sun_ephem : str
Gregory Ashton's avatar
Gregory Ashton committed
347
348
            Paths of the two files containing positions of Earth and Sun,
            respectively at evenly spaced times, as passed to CreateFstatInput.
349
350
            If None defaults will be used.
        injectSources : dict or str
351
352
            Either a dictionary of the values to inject, or a string pointing
            to the .cff file to inject
353
        injectSqrtSX :
354
            Not yet implemented
355
        assumeSqrtSX : float
356
357
358
            Don't estimate noise-floors but assume (stationary) per-IFO
            sqrt{SX} (if single value: use for all IFOs). If signal only,
            set sqrtSX=1
359
        SSBprec : int
360
361
            Flag to set the SSB calculation: 0=Newtonian, 1=relativistic,
            2=relativisitic optimised, 3=DMoff, 4=NO_SPIN
Gregory Ashton's avatar
Gregory Ashton committed
362
363
364
365
366
367
368
369
370
371

        """

        if earth_ephem is None:
            self.earth_ephem = self.earth_ephem_default
        if sun_ephem is None:
            self.sun_ephem = self.sun_ephem_default

        self.init_computefstatistic_single_point()

372
373
374
375
376
377
378
379
380
381
382
    def _get_SFTCatalog(self):
        """ Load the SFTCatalog

        If sftfilepattern is specified, load the data. If not, attempt to
        create data on the fly.

        Returns
        -------
        SFTCatalog: lalpulsar.SFTCatalog

        """
Gregory Ashton's avatar
Gregory Ashton committed
383
384
        if hasattr(self, 'SFTCatalog'):
            return
385
        if self.sftfilepattern is None:
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
            for k in ['minStartTime', 'maxStartTime', 'detectors']:
                if getattr(self, k) is None:
                    raise ValueError('You must provide "{}" to injectSources'
                                     .format(k))
            C1 = getattr(self, 'injectSources', None) is None
            C2 = getattr(self, 'injectSqrtSX', None) is None
            if C1 and C2:
                raise ValueError('You must specify either one of injectSources'
                                 ' or injectSqrtSX')
            SFTCatalog = lalpulsar.SFTCatalog()
            Tsft = 1800
            Toverlap = 0
            Tspan = self.maxStartTime - self.minStartTime
            detNames = lal.CreateStringVector(
                *[d for d in self.detectors.split(',')])
            multiTimestamps = lalpulsar.MakeMultiTimestamps(
                self.minStartTime, Tspan, Tsft, Toverlap, detNames.length)
            SFTCatalog = lalpulsar.MultiAddToFakeSFTCatalog(
                SFTCatalog, detNames, multiTimestamps)
            return SFTCatalog

Gregory Ashton's avatar
Gregory Ashton committed
407
408
        logging.info('Initialising SFTCatalog')
        constraints = lalpulsar.SFTConstraints()
409
        if self.detectors:
410
411
412
413
            if ',' in self.detectors:
                logging.info('Using all detector data')
            else:
                constraints.detector = self.detectors
Gregory Ashton's avatar
Gregory Ashton committed
414
415
416
417
418
419
        if self.minStartTime:
            constraints.minStartTime = lal.LIGOTimeGPS(self.minStartTime)
        if self.maxStartTime:
            constraints.maxStartTime = lal.LIGOTimeGPS(self.maxStartTime)

        logging.info('Loading data matching pattern {}'.format(
420
421
                     self.sftfilepattern))
        SFTCatalog = lalpulsar.SFTdataFind(self.sftfilepattern, constraints)
Gregory Ashton's avatar
Gregory Ashton committed
422
423
424
        detector_names = list(set([d.header.name for d in SFTCatalog.data]))
        self.detector_names = detector_names
        SFT_timestamps = [d.header.epoch for d in SFTCatalog.data]
Gregory Ashton's avatar
Gregory Ashton committed
425
        self.SFT_timestamps = [float(s) for s in SFT_timestamps]
426
427
        if len(SFT_timestamps) == 0:
            raise ValueError('Failed to load any data')
Gregory Ashton's avatar
Gregory Ashton committed
428
429
430
431
432
        if args.quite is False and args.no_interactive is False:
            try:
                from bashplotlib.histogram import plot_hist
                print('Data timestamps histogram:')
                plot_hist(SFT_timestamps, height=5, bincount=50)
Gregory Ashton's avatar
Gregory Ashton committed
433
            except ImportError:
Gregory Ashton's avatar
Gregory Ashton committed
434
435
436
437
438
                pass
        if len(detector_names) == 0:
            raise ValueError('No data loaded.')
        logging.info('Loaded {} data files from detectors {}'.format(
            len(SFT_timestamps), detector_names))
439
        cl_tconv1 = 'lalapps_tconvert {}'.format(int(SFT_timestamps[0]))
440
        output = helper_functions.run_commandline(cl_tconv1)
441
442
        tconvert1 = output.rstrip('\n')
        cl_tconv2 = 'lalapps_tconvert {}'.format(int(SFT_timestamps[-1]))
443
        output = helper_functions.run_commandline(cl_tconv2)
444
        tconvert2 = output.rstrip('\n')
Gregory Ashton's avatar
Gregory Ashton committed
445
446
        logging.info('Data spans from {} ({}) to {} ({})'.format(
            int(SFT_timestamps[0]),
447
            tconvert1,
Gregory Ashton's avatar
Gregory Ashton committed
448
            int(SFT_timestamps[-1]),
449
            tconvert2))
450
        return SFTCatalog
Gregory Ashton's avatar
Gregory Ashton committed
451
452
453
454

    def init_computefstatistic_single_point(self):
        """ Initilisation step of run_computefstatistic for a single point """

455
        SFTCatalog = self._get_SFTCatalog()
Gregory Ashton's avatar
Gregory Ashton committed
456
457
458
459
460
461
462
463
464
465
466
467
468

        logging.info('Initialising ephems')
        ephems = lalpulsar.InitBarycenter(self.earth_ephem, self.sun_ephem)

        logging.info('Initialising FstatInput')
        dFreq = 0
        if self.transient:
            self.whatToCompute = lalpulsar.FSTATQ_ATOMS_PER_DET
        else:
            self.whatToCompute = lalpulsar.FSTATQ_2F

        FstatOAs = lalpulsar.FstatOptionalArgs()
        FstatOAs.randSeed = lalpulsar.FstatOptionalArgsDefaults.randSeed
469
470
471
472
473
        if self.SSBprec:
            logging.info('Using SSBprec={}'.format(self.SSBprec))
            FstatOAs.SSBprec = self.SSBprec
        else:
            FstatOAs.SSBprec = lalpulsar.FstatOptionalArgsDefaults.SSBprec
Gregory Ashton's avatar
Gregory Ashton committed
474
475
476
        FstatOAs.Dterms = lalpulsar.FstatOptionalArgsDefaults.Dterms
        FstatOAs.runningMedianWindow = lalpulsar.FstatOptionalArgsDefaults.runningMedianWindow
        FstatOAs.FstatMethod = lalpulsar.FstatOptionalArgsDefaults.FstatMethod
477
478
479
480
481
482
483
484
        if self.assumeSqrtSX is None:
            FstatOAs.assumeSqrtSX = lalpulsar.FstatOptionalArgsDefaults.assumeSqrtSX
        else:
            mnf = lalpulsar.MultiNoiseFloor()
            assumeSqrtSX = np.atleast_1d(self.assumeSqrtSX)
            mnf.sqrtSn[:len(assumeSqrtSX)] = assumeSqrtSX
            mnf.length = len(assumeSqrtSX)
            FstatOAs.assumeSqrtSX = mnf
Gregory Ashton's avatar
Gregory Ashton committed
485
486
487
        FstatOAs.prevInput = lalpulsar.FstatOptionalArgsDefaults.prevInput
        FstatOAs.collectTiming = lalpulsar.FstatOptionalArgsDefaults.collectTiming

Gregory Ashton's avatar
Gregory Ashton committed
488
        if hasattr(self, 'injectSources') and type(self.injectSources) == dict:
Gregory Ashton's avatar
Gregory Ashton committed
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
            logging.info('Injecting source with params: {}'.format(
                self.injectSources))
            PPV = lalpulsar.CreatePulsarParamsVector(1)
            PP = PPV.data[0]
            PP.Amp.h0 = self.injectSources['h0']
            PP.Amp.cosi = self.injectSources['cosi']
            PP.Amp.phi0 = self.injectSources['phi0']
            PP.Amp.psi = self.injectSources['psi']
            PP.Doppler.Alpha = self.injectSources['Alpha']
            PP.Doppler.Delta = self.injectSources['Delta']
            PP.Doppler.fkdot = np.array(self.injectSources['fkdot'])
            PP.Doppler.refTime = self.tref
            if 't0' not in self.injectSources:
                PP.Transient.type = lalpulsar.TRANSIENT_NONE
            FstatOAs.injectSources = PPV
Gregory Ashton's avatar
Gregory Ashton committed
504
        elif hasattr(self, 'injectSources') and type(self.injectSources) == str:
505
506
507
508
            logging.info('Injecting source from param file: {}'.format(
                self.injectSources))
            PPV = lalpulsar.PulsarParamsFromFile(self.injectSources, self.tref)
            FstatOAs.injectSources = PPV
Gregory Ashton's avatar
Gregory Ashton committed
509
510
        else:
            FstatOAs.injectSources = lalpulsar.FstatOptionalArgsDefaults.injectSources
511
512
513
514
        if hasattr(self, 'injectSqrtSX') and self.injectSqrtSX is not None:
            raise ValueError('injectSqrtSX not implemented')
        else:
            FstatOAs.InjectSqrtSX = lalpulsar.FstatOptionalArgsDefaults.injectSqrtSX
Gregory Ashton's avatar
Gregory Ashton committed
515
        if self.minCoverFreq is None or self.maxCoverFreq is None:
516
            fAs = [d.header.f0 for d in SFTCatalog.data]
Gregory Ashton's avatar
Gregory Ashton committed
517
            fBs = [d.header.f0 + (d.numBins-1)*d.header.deltaF
518
                   for d in SFTCatalog.data]
Gregory Ashton's avatar
Gregory Ashton committed
519
520
521
522
523
524
            self.minCoverFreq = np.min(fAs) + 0.5
            self.maxCoverFreq = np.max(fBs) - 0.5
            logging.info('Min/max cover freqs not provided, using '
                         '{} and {}, est. from SFTs'.format(
                             self.minCoverFreq, self.maxCoverFreq))

525
        self.FstatInput = lalpulsar.CreateFstatInput(SFTCatalog,
Gregory Ashton's avatar
Gregory Ashton committed
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
                                                     self.minCoverFreq,
                                                     self.maxCoverFreq,
                                                     dFreq,
                                                     ephems,
                                                     FstatOAs
                                                     )

        logging.info('Initialising PulsarDoplerParams')
        PulsarDopplerParams = lalpulsar.PulsarDopplerParams()
        PulsarDopplerParams.refTime = self.tref
        PulsarDopplerParams.Alpha = 1
        PulsarDopplerParams.Delta = 1
        PulsarDopplerParams.fkdot = np.array([0, 0, 0, 0, 0, 0, 0])
        self.PulsarDopplerParams = PulsarDopplerParams

        logging.info('Initialising FstatResults')
        self.FstatResults = lalpulsar.FstatResults()

        if self.BSGL:
            if len(self.detector_names) < 2:
546
                raise ValueError("Can't use BSGL with single detectors data")
Gregory Ashton's avatar
Gregory Ashton committed
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
            else:
                logging.info('Initialising BSGL')

            # Tuning parameters - to be reviewed
            numDetectors = 2
            if hasattr(self, 'nsegs'):
                p_val_threshold = 1e-6
                Fstar0s = np.linspace(0, 1000, 10000)
                p_vals = scipy.special.gammaincc(2*self.nsegs, Fstar0s)
                Fstar0 = Fstar0s[np.argmin(np.abs(p_vals - p_val_threshold))]
                if Fstar0 == Fstar0s[-1]:
                    raise ValueError('Max Fstar0 exceeded')
            else:
                Fstar0 = 15.
            logging.info('Using Fstar0 of {:1.2f}'.format(Fstar0))
            oLGX = np.zeros(10)
            oLGX[:numDetectors] = 1./numDetectors
            self.BSGLSetup = lalpulsar.CreateBSGLSetup(numDetectors,
                                                       Fstar0,
                                                       oLGX,
                                                       True,
                                                       1)
            self.twoFX = np.zeros(10)
            self.whatToCompute = (self.whatToCompute +
                                  lalpulsar.FSTATQ_2F_PER_DET)

        if self.transient:
            logging.info('Initialising transient parameters')
            self.windowRange = lalpulsar.transientWindowRange_t()
            self.windowRange.type = lalpulsar.TRANSIENT_RECTANGULAR
            self.windowRange.t0Band = 0
            self.windowRange.dt0 = 1
            self.windowRange.tauBand = 0
            self.windowRange.dtau = 1

    def compute_fullycoherent_det_stat_single_point(
            self, F0, F1, F2, Alpha, Delta, asini=None, period=None, ecc=None,
            tp=None, argp=None):
        """ Compute the fully-coherent det. statistic at a single point """

        return self.run_computefstatistic_single_point(
            self.minStartTime, self.maxStartTime, F0, F1, F2, Alpha, Delta,
            asini, period, ecc, tp, argp)

    def run_computefstatistic_single_point(self, tstart, tend, F0, F1,
                                           F2, Alpha, Delta, asini=None,
                                           period=None, ecc=None, tp=None,
                                           argp=None):
        """ Returns twoF or ln(BSGL) fully-coherently at a single point """

        self.PulsarDopplerParams.fkdot = np.array([F0, F1, F2, 0, 0, 0, 0])
        self.PulsarDopplerParams.Alpha = Alpha
        self.PulsarDopplerParams.Delta = Delta
        if self.binary:
            self.PulsarDopplerParams.asini = asini
            self.PulsarDopplerParams.period = period
            self.PulsarDopplerParams.ecc = ecc
            self.PulsarDopplerParams.tp = tp
            self.PulsarDopplerParams.argp = argp

        lalpulsar.ComputeFstat(self.FstatResults,
                               self.FstatInput,
                               self.PulsarDopplerParams,
                               1,
                               self.whatToCompute
                               )

        if self.transient is False:
            if self.BSGL is False:
                return self.FstatResults.twoF[0]

            twoF = np.float(self.FstatResults.twoF[0])
            self.twoFX[0] = self.FstatResults.twoFPerDet(0)
            self.twoFX[1] = self.FstatResults.twoFPerDet(1)
            log10_BSGL = lalpulsar.ComputeBSGL(twoF, self.twoFX,
                                               self.BSGLSetup)
            return log10_BSGL/np.log10(np.exp(1))

        self.windowRange.t0 = int(tstart)  # TYPE UINT4
        self.windowRange.tau = int(tend - tstart)  # TYPE UINT4

        FS = lalpulsar.ComputeTransientFstatMap(
            self.FstatResults.multiFatoms[0], self.windowRange, False)

        if self.BSGL is False:
632
633
634
635
636
            twoF = 2*FS.F_mn.data[0][0]
            if np.isnan(twoF):
                return 0
            else:
                return twoF
Gregory Ashton's avatar
Gregory Ashton committed
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656

        FstatResults_single = copy.copy(self.FstatResults)
        FstatResults_single.lenth = 1
        FstatResults_single.data = self.FstatResults.multiFatoms[0].data[0]
        FS0 = lalpulsar.ComputeTransientFstatMap(
            FstatResults_single.multiFatoms[0], self.windowRange, False)
        FstatResults_single.data = self.FstatResults.multiFatoms[0].data[1]
        FS1 = lalpulsar.ComputeTransientFstatMap(
            FstatResults_single.multiFatoms[0], self.windowRange, False)

        self.twoFX[0] = 2*FS0.F_mn.data[0][0]
        self.twoFX[1] = 2*FS1.F_mn.data[0][0]
        log10_BSGL = lalpulsar.ComputeBSGL(
                2*FS.F_mn.data[0][0], self.twoFX, self.BSGLSetup)

        return log10_BSGL/np.log10(np.exp(1))

    def calculate_twoF_cumulative(self, F0, F1, F2, Alpha, Delta, asini=None,
                                  period=None, ecc=None, tp=None, argp=None,
                                  tstart=None, tend=None, npoints=1000,
657
658
                                  ):
        """ Calculate the cumulative twoF along the obseration span
659
660
661

        Parameters
        ----------
662
663
        F0, F1, F2, Alpha, Delta: float
            Parameters at which to compute the cumulative twoF
664
665
        asini, period, ecc, tp, argp: float, optional
            Binary parameters at which to compute the cumulative 2F
666
667
668
669
670
671
        tstart, tend: int
            GPS times to restrict the range of data used - automatically
            truncated to the span of data available
        npoints: int
            Number of points to compute twoF along the span

672
673
674
        Notes
        -----
        The minimum cumulatibe twoF is hard-coded to be computed over
675
676
677
678
679
680
        the first 6 hours from either the first timestampe in the data (if
        tstart is smaller than it) or tstart.

        """
        SFTminStartTime = self.SFT_timestamps[0]
        SFTmaxStartTime = self.SFT_timestamps[-1]
Gregory Ashton's avatar
Gregory Ashton committed
681
        tstart = np.max([SFTminStartTime, tstart])
682
683
684
        min_tau = np.max([SFTminStartTime - tstart, 0]) + 3600*6
        max_tau = SFTmaxStartTime - tstart
        taus = np.linspace(min_tau, max_tau, npoints)
Gregory Ashton's avatar
Gregory Ashton committed
685
686
687
688
689
690
691
692
693
694
695
696
        twoFs = []
        if self.transient is False:
            self.transient = True
            self.init_computefstatistic_single_point()
        for tau in taus:
            twoFs.append(self.run_computefstatistic_single_point(
                tstart=tstart, tend=tstart+tau, F0=F0, F1=F1, F2=F2,
                Alpha=Alpha, Delta=Delta, asini=asini, period=period, ecc=ecc,
                tp=tp, argp=argp))

        return taus, np.array(twoFs)

697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
    def _calculate_predict_fstat_cumulative(self, N, label=None, outdir=None,
                                            IFO=None, pfs_input=None):
        """ Calculates the predicted 2F and standard deviation cumulatively

        Parameters
        ----------
        N : int
            Number of timesteps to use between minStartTime and maxStartTime.
        label, outdir : str, optional
            The label and directory to read in the .loudest file from
        IFO : str
        pfs_input : dict, optional
            Input kwargs to predict_fstat (alternative to giving label and
            outdir).

        Returns
        -------
        times, pfs, pfs_sigma : ndarray, size (N,)

        """
Gregory Ashton's avatar
Gregory Ashton committed
717
718
719
720
721

        if pfs_input is None:
            if os.path.isfile('{}/{}.loudest'.format(outdir, label)) is False:
                raise ValueError(
                    'Need a loudest file to add the predicted Fstat')
722
            loudest = read_par(label=label, outdir=outdir, suffix='loudest')
Gregory Ashton's avatar
Gregory Ashton committed
723
724
            pfs_input = {key: loudest[key] for key in
                         ['h0', 'cosi', 'psi', 'Alpha', 'Delta', 'Freq']}
725
726
727
        times = np.linspace(self.minStartTime, self.maxStartTime, N+1)[1:]
        times = np.insert(times, 0, self.minStartTime + 86400/2.)
        out = [predict_fstat(minStartTime=self.minStartTime, maxStartTime=t,
728
                             sftfilepattern=self.sftfilepattern, IFO=IFO,
729
730
731
732
                             **pfs_input) for t in times]
        pfs, pfs_sigma = np.array(out).T
        return times, pfs, pfs_sigma

733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
    def plot_twoF_cumulative(self, label, outdir, add_pfs=False, N=15,
                             injectSources=None, ax=None, c='k', savefig=True,
                             title=None, **kwargs):
        """ Plot the twoF value cumulatively

        Parameters
        ----------
        label, outdir : str
        add_pfs : bool
            If true, plot the predicted 2F and standard deviation
        N : int
            Number of points to use
        injectSources : dict
            See `ComputeFstat`
        ax : matplotlib.axes._subplots_AxesSubplot, optional
            Axis to add the plot to.
        c : str
            Colour
        savefig : bool
            If true, save the figure in outdir
        title: str
            Figure title

        Returns
        -------
        tauS, tauF : ndarray shape (N,)
            If savefig, the times and twoF (cumulative) values
        ax : matplotlib.axes._subplots_AxesSubplot, optional
            If savefig is False

        """
Gregory Ashton's avatar
Gregory Ashton committed
764
765
        if ax is None:
            fig, ax = plt.subplots()
Gregory Ashton's avatar
Gregory Ashton committed
766
767
768
769
770
771
772
        if injectSources:
            pfs_input = dict(
                h0=injectSources['h0'], cosi=injectSources['cosi'],
                psi=injectSources['psi'], Alpha=injectSources['Alpha'],
                Delta=injectSources['Delta'], Freq=injectSources['fkdot'][0])
        else:
            pfs_input = None
773
774
775
776

        taus, twoFs = self.calculate_twoF_cumulative(**kwargs)
        ax.plot(taus/86400., twoFs, label='All detectors', color=c)
        if len(self.detector_names) > 1:
777
778
            detector_names = self.detector_names
            detectors = self.detectors
779
780
781
782
            for d in self.detector_names:
                self.detectors = d
                self.init_computefstatistic_single_point()
                taus, twoFs = self.calculate_twoF_cumulative(**kwargs)
783
784
785
786
787
788
                ax.plot(taus/86400., twoFs, label='{}'.format(d),
                        color=detector_colors[d.lower()])
            self.detectors = detectors
            self.detector_names = detector_names

        if add_pfs:
789
790
            times, pfs, pfs_sigma = self._calculate_predict_fstat_cumulative(
                N=N, label=label, outdir=outdir, pfs_input=pfs_input)
791
792
            ax.fill_between(
                (times-self.minStartTime)/86400., pfs-pfs_sigma, pfs+pfs_sigma,
Gregory Ashton's avatar
Gregory Ashton committed
793
                color=c,
794
795
                label=(r'Predicted $\langle 2\mathcal{F} '
                       r'\rangle\pm $ 1-$\sigma$ band'),
796
797
798
                zorder=-10, alpha=0.2)
            if len(self.detector_names) > 1:
                for d in self.detector_names:
799
800
801
802
                    out = self._calculate_predict_fstat_cumulative(
                        N=N, label=label, outdir=outdir, IFO=d.upper(),
                        pfs_input=pfs_input)
                    times, pfs, pfs_sigma = out
803
804
805
806
807
808
809
810
                    ax.fill_between(
                        (times-self.minStartTime)/86400., pfs-pfs_sigma,
                        pfs+pfs_sigma, color=detector_colors[d.lower()],
                        alpha=0.5,
                        label=(
                            'Predicted $2\mathcal{{F}}$ 1-$\sigma$ band ({})'
                            .format(d.upper())),
                        zorder=-10)
811

Gregory Ashton's avatar
Gregory Ashton committed
812
813
814
815
816
817
818
        ax.set_xlabel(r'Days from $t_{{\rm start}}={:.0f}$'.format(
            kwargs['tstart']))
        if self.BSGL:
            ax.set_ylabel(r'$\log_{10}(\mathrm{BSGL})_{\rm cumulative}$')
        else:
            ax.set_ylabel(r'$\widetilde{2\mathcal{F}}_{\rm cumulative}$')
        ax.set_xlim(0, taus[-1]/86400)
Gregory Ashton's avatar
Gregory Ashton committed
819
        ax.legend(frameon=False, loc=2, fontsize=6)
Gregory Ashton's avatar
Gregory Ashton committed
820
821
822
823
824
825
826
827
828
829
830
831
832
833
        if title:
            ax.set_title(title)
        if savefig:
            plt.tight_layout()
            plt.savefig('{}/{}_twoFcumulative.png'.format(outdir, label))
            return taus, twoFs
        else:
            return ax


class SemiCoherentSearch(BaseSearchClass, ComputeFstat):
    """ A semi-coherent search """

    @helper_functions.initializer
834
    def __init__(self, label, outdir, tref, nsegs=None, sftfilepattern=None,
Gregory Ashton's avatar
Gregory Ashton committed
835
836
                 binary=False, BSGL=False, minStartTime=None,
                 maxStartTime=None, minCoverFreq=None, maxCoverFreq=None,
837
                 detectors=None, earth_ephem=None, sun_ephem=None,
838
                 injectSources=None, assumeSqrtSX=None, SSBprec=None):
Gregory Ashton's avatar
Gregory Ashton committed
839
840
841
842
843
844
845
846
847
        """
        Parameters
        ----------
        label, outdir: str
            A label and directory to read/write data from/to.
        tref, minStartTime, maxStartTime: int
            GPS seconds of the reference time, and start and end of the data.
        nsegs: int
            The (fixed) number of segments
848
849
850
        sftfilepattern: str
            Pattern to match SFTs using wildcards (*?) and ranges [0-9];
            mutiple patterns can be given separated by colons.
Gregory Ashton's avatar
Gregory Ashton committed
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871

        For all other parameters, see pyfstat.ComputeFStat.
        """

        self.fs_file_name = "{}/{}_FS.dat".format(self.outdir, self.label)
        if self.earth_ephem is None:
            self.earth_ephem = self.earth_ephem_default
        if self.sun_ephem is None:
            self.sun_ephem = self.sun_ephem_default
        self.transient = True
        self.init_computefstatistic_single_point()
        self.init_semicoherent_parameters()

    def init_semicoherent_parameters(self):
        logging.info(('Initialising semicoherent parameters from {} to {} in'
                      ' {} segments').format(
            self.minStartTime, self.maxStartTime, self.nsegs))
        self.transient = True
        self.whatToCompute = lalpulsar.FSTATQ_2F+lalpulsar.FSTATQ_ATOMS_PER_DET
        self.tboundaries = np.linspace(self.minStartTime, self.maxStartTime,
                                       self.nsegs+1)
872
        self.Tcoh = self.tboundaries[1] - self.tboundaries[0]
Gregory Ashton's avatar
Gregory Ashton committed
873

874
875
876
877
878
879
880
881
882
        if hasattr(self, 'SFT_timestamps'):
            if self.tboundaries[0] < self.SFT_timestamps[0]:
                logging.debug(
                    'Semi-coherent start time {} before first SFT timestamp {}'
                    .format(self.tboundaries[0], self.SFT_timestamps[0]))
            if self.tboundaries[-1] > self.SFT_timestamps[-1]:
                logging.debug(
                    'Semi-coherent end time {} after last SFT timestamp {}'
                    .format(self.tboundaries[-1], self.SFT_timestamps[-1]))
Gregory Ashton's avatar
Gregory Ashton committed
883

884
885
886
887
888
889
    def run_semi_coherent_computefstatistic_single_point(
            self, F0, F1, F2, Alpha, Delta, asini=None,
            period=None, ecc=None, tp=None, argp=None,
            record_segments=False):
        """ Returns twoF or ln(BSGL) semi-coherently at a single point """

Gregory Ashton's avatar
Gregory Ashton committed
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
        self.PulsarDopplerParams.fkdot = np.array([F0, F1, F2, 0, 0, 0, 0])
        self.PulsarDopplerParams.Alpha = Alpha
        self.PulsarDopplerParams.Delta = Delta
        if self.binary:
            self.PulsarDopplerParams.asini = asini
            self.PulsarDopplerParams.period = period
            self.PulsarDopplerParams.ecc = ecc
            self.PulsarDopplerParams.tp = tp
            self.PulsarDopplerParams.argp = argp

        lalpulsar.ComputeFstat(self.FstatResults,
                               self.FstatInput,
                               self.PulsarDopplerParams,
                               1,
                               self.whatToCompute
                               )

907
908
909
910
911
912
913
914
915
        #if self.transient is False:
        #    if self.BSGL is False:
        #        return self.FstatResults.twoF[0]
        #    twoF = np.float(self.FstatResults.twoF[0])
        #    self.twoFX[0] = self.FstatResults.twoFPerDet(0)
        #    self.twoFX[1] = self.FstatResults.twoFPerDet(1)
        #    log10_BSGL = lalpulsar.ComputeBSGL(twoF, self.twoFX,
        #                                       self.BSGLSetup)
        #    return log10_BSGL/np.log10(np.exp(1))
Gregory Ashton's avatar
Gregory Ashton committed
916
917

        detStat = 0
918
919
        if record_segments:
            self.detStat_per_segment = []
Gregory Ashton's avatar
Gregory Ashton committed
920

921
922
923
        self.windowRange.tau = int(self.Tcoh)  # TYPE UINT4
        for tstart in self.tboundaries[:-1]:
            d_detStat = self._get_per_segment_det_stat(tstart)
924
925
926
            detStat += d_detStat
            if record_segments:
                self.detStat_per_segment.append(d_detStat)
Gregory Ashton's avatar
Gregory Ashton committed
927
928
929

        return detStat

930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
    def _get_per_segment_det_stat(self, tstart):
        self.windowRange.t0 = int(tstart)  # TYPE UINT4

        FS = lalpulsar.ComputeTransientFstatMap(
            self.FstatResults.multiFatoms[0], self.windowRange, False)

        if self.BSGL is False:
            d_detStat = 2*FS.F_mn.data[0][0]
        else:
            FstatResults_single = copy.copy(self.FstatResults)
            FstatResults_single.lenth = 1
            FstatResults_single.data = self.FstatResults.multiFatoms[0].data[0]
            FS0 = lalpulsar.ComputeTransientFstatMap(
                FstatResults_single.multiFatoms[0], self.windowRange, False)
            FstatResults_single.data = self.FstatResults.multiFatoms[0].data[1]
            FS1 = lalpulsar.ComputeTransientFstatMap(
                FstatResults_single.multiFatoms[0], self.windowRange, False)

            self.twoFX[0] = 2*FS0.F_mn.data[0][0]
            self.twoFX[1] = 2*FS1.F_mn.data[0][0]
            log10_BSGL = lalpulsar.ComputeBSGL(
                    2*FS.F_mn.data[0][0], self.twoFX, self.BSGLSetup)
            d_detStat = log10_BSGL/np.log10(np.exp(1))
        if np.isnan(d_detStat):
            logging.debug('NaNs in semi-coherent twoF treated as zero')
            d_detStat = 0

        return d_detStat

Gregory Ashton's avatar
Gregory Ashton committed
959
960
961
962
963
964
965
966
967
968
969
970

class SemiCoherentGlitchSearch(BaseSearchClass, ComputeFstat):
    """ A semi-coherent glitch search

    This implements a basic `semi-coherent glitch F-stat in which the data
    is divided into segments either side of the proposed glitches and the
    fully-coherent F-stat in each segment is summed to give the semi-coherent
    F-stat
    """

    @helper_functions.initializer
    def __init__(self, label, outdir, tref, minStartTime, maxStartTime,
971
                 nglitch=0, sftfilepattern=None, theta0_idx=0, BSGL=False,
972
                 minCoverFreq=None, maxCoverFreq=None, assumeSqrtSX=None,
973
                 detectors=None, earth_ephem=None, sun_ephem=None,
Gregory Ashton's avatar
Gregory Ashton committed
974
                 SSBprec=None, injectSources=None):
Gregory Ashton's avatar
Gregory Ashton committed
975
976
977
978
979
980
981
982
983
984
        """
        Parameters
        ----------
        label, outdir: str
            A label and directory to read/write data from/to.
        tref, minStartTime, maxStartTime: int
            GPS seconds of the reference time, and start and end of the data.
        nglitch: int
            The (fixed) number of glitches; this can zero, but occasionally
            this causes issue (in which case just use ComputeFstat).
985
986
987
        sftfilepattern: str
            Pattern to match SFTs using wildcards (*?) and ranges [0-9];
            mutiple patterns can be given separated by colons.
Gregory Ashton's avatar
Gregory Ashton committed
988
989
990
991
992
993
994
995
996
997
998
999
1000
        theta0_idx, int
            Index (zero-based) of which segment the theta refers to - uyseful
            if providing a tight prior on theta to allow the signal to jump
            too theta (and not just from)

        For all other parameters, see pyfstat.ComputeFStat.
        """

        self.fs_file_name = "{}/{}_FS.dat".format(self.outdir, self.label)
        if self.earth_ephem is None:
            self.earth_ephem = self.earth_ephem_default
        if self.sun_ephem is None:
            self.sun_ephem = self.sun_ephem_default