Exemple #1
0
 def make_strain_from_inj_object(self, inj, delta_t, detector_name,
                                 distance_scale=1, ts=None):
     if ts is None:
         ts = load_timeseries(inj.filename)
         self.set_ref_time(inj, ts)
     # slice and taper
     ts = self.slice_and_taper(inj, ts)
     # shift reference to the detector time
     ts._epoch += inj['{}_gps_time'.format(detector_name).lower()]
     # resample
     ts = resample_to_delta_t(ts, delta_t, method='ldas')
     # apply any phase shift
     try:
         phase_shift = inj[
             '{}_phase_shift'.format(detector_name).lower()]
     except ValueError:
         phase_shift = 0
     if phase_shift:
         fs = ts.to_frequencyseries()
         fs *= np.exp(1j*phase_shift)
         ts = fs.to_timeseries()
     # apply any scaling
     try:
         amp_scale = inj[
             '{}_amp_scale'.format(detector_name).lower()]
     except ValueError:
         amp_scale = 1.
     amp_scale /= distance_scale
     ts *= amp_scale
     return ts
    def _gen_slices(self, index_range):
        X = [
            np.zeros(
                (len(index_range), len(self.ts) * 8, self.final_data_samples))
            for i in range(2)
        ]

        for in_batch, idx in enumerate(index_range):
            for detector in range(len(self.ts)):
                low, up = idx
                #white_full_signal = self.ts[detector][low:up].whiten(4, 4)
                white_full_signal = whiten_data(self.ts[detector][low:up],
                                                psd=self.psd)
                max_idx = len(white_full_signal)
                min_idx = max_idx - int(
                    float(self.num_samples) / float(self.resample_rates[0]) /
                    self.dt)
                for i, sr in enumerate(self.resample_rates):
                    X[0][in_batch][i * len(self.ts) +
                                   detector] = resample_to_delta_t(
                                       white_full_signal[min_idx:max_idx],
                                       1.0 / sr)
                    if not i + 1 == len(self.resample_rates):
                        t_dur = float(self.num_samples) / float(
                            self.resample_rates[i + 1])
                        sample_dur = int(t_dur / self.dt)
                        max_idx = min_idx
                        min_idx -= sample_dur
                #print("{}: in_batch: {}".format(detector, in_batch))

        return ([X[0].transpose(0, 2, 1), X[1].transpose(0, 2, 1)])
Exemple #3
0
def resampleado(strain,hh,grafica,fs,fs_ligo):
    
    template = types.TimeSeries(initial_array=hh, delta_t=1.0/fs , epoch=0)
    
#	Downsampling the data to 4096Hz || 8192Hz || 16KHz

    strain = resample_to_delta_t(strain, 1.0/fs)

   
    if grafica == 1:
    
        pylab.figure('Fig_strain')
        pylab.title('Strain resampled')
        pylab.plot(strain.sample_times, strain)
        pylab.xlabel('Time (s)')
        pylab.grid()
        pylab.show()

        pylab.figure('Fig_template')
        pylab.title('Template resampled')
        pylab.plot(template.sample_times, template)
        pylab.grid()
        pylab.xlabel('Time (s)')
        pylab.show()
   
   

    return template, strain
Exemple #4
0
def get_waveform(approximant, phase_order, amplitude_order, template_params,
                 start_frequency, sample_rate, length, filter_rate):

    if approximant in td_approximants():
        hplus, hcross = get_td_waveform(template_params,
                                        approximant=approximant,
                                        phase_order=phase_order,
                                        delta_t=1.0 / sample_rate,
                                        f_lower=start_frequency,
                                        amplitude_order=amplitude_order)
        hvec = generate_detector_strain(template_params, hplus, hcross)

        if filter_rate != sample_rate:
            delta_t = 1.0 / filter_rate
            hvec = resample_to_delta_t(hvec, delta_t)

    elif approximant in fd_approximants():
        delta_f = filter_rate / length
        if hasattr(template_params, "spin1z") and hasattr(
                template_params, "spin2z"):
            if template_params.spin1z <= -0.9 or template_params.spin1z >= 0.9:
                template_params.spin1z *= 0.99999999
            if template_params.spin2z <= -0.9 or template_params.spin2z >= 0.9:
                template_params.spin2z *= 0.99999999
        if True:
            print("\n spin1z = %f, spin2z = %f, chi = %f" %
                  (template_params.spin1z, template_params.spin2z,
                   lalsimulation.SimIMRPhenomBComputeChi(
                       template_params.mass1 * lal.LAL_MSUN_SI,
                       template_params.mass2 * lal.LAL_MSUN_SI,
                       template_params.spin1z, template_params.spin2z)),
                  file=sys.stderr)
            print("spin1x = %f, spin1y = %f" %
                  (template_params.spin1x, template_params.spin1y),
                  file=sys.stderr)
            print("spin2x = %f, spin2y = %f" %
                  (template_params.spin2x, template_params.spin2y),
                  file=sys.stderr)
            print("m1 = %f, m2 = %f" %
                  (template_params.mass1, template_params.mass2),
                  file=sys.stderr)
        hvec = get_fd_waveform(template_params,
                               approximant=approximant,
                               phase_order=phase_order,
                               delta_f=delta_f,
                               f_lower=start_frequency,
                               amplitude_order=amplitude_order)[0]
        if True:
            print("filter_N = %d in get waveform" % filter_N,
                  "\n type(hvec) in get_waveform:",
                  type(hvec),
                  file=sys.stderr)
            print(hvec, file=sys.stderr)
            print(hvec[0], file=sys.stderr)
            print(hvec[1], file=sys.stderr)
            print(isinstance(hvec, FrequencySeries), file=sys.stderr)
    htilde = make_padded_frequency_series(hvec, filter_N)

    return htilde
Exemple #5
0
    def setUp(self):
        ###### Get data for references analysis of 170817
        m = Merger("GW170817")
        ifos = ['H1', 'V1', 'L1']
        self.psds = {}
        self.data = {}

        for ifo in ifos:
            print("Processing {} data".format(ifo))

            # Download the gravitational wave data for GW170817
            url = "https://dcc.ligo.org/public/0146/P1700349/001/"
            url += "{}-{}1_LOSC_CLN_4_V1-1187007040-2048.gwf"
            fname = download_file(url.format(ifo[0], ifo[0]), cache=True)
            ts = read_frame(fname,
                            "{}:LOSC-STRAIN".format(ifo),
                            start_time=int(m.time - 260),
                            end_time=int(m.time + 40))
            ts = highpass(ts, 15.0)
            ts = resample_to_delta_t(ts, 1.0 / 2048)
            ts = ts.time_slice(m.time - 112, m.time + 16)
            self.data[ifo] = ts.to_frequencyseries()

            psd = interpolate(ts.psd(4), ts.delta_f)
            psd = inverse_spectrum_truncation(psd,
                                              int(4 * psd.sample_rate),
                                              trunc_method='hann',
                                              low_frequency_cutoff=20.0)
            self.psds[ifo] = psd

        self.static = {
            'mass1': 1.3757,
            'mass2': 1.3757,
            'f_lower': 20.0,
            'approximant': "TaylorF2",
            'polarization': 0,
            'ra': 3.44615914,
            'dec': -0.40808407,
            'tc': 1187008882.42840,
        }
        self.variable = (
            'distance',
            'inclination',
        )
        self.flow = {'H1': 25, 'L1': 25, 'V1': 25}
        inclination_prior = SinAngle(inclination=None)
        distance_prior = Uniform(distance=(10, 100))
        tc_prior = Uniform(tc=(m.time - 0.1, m.time + 0.1))
        self.prior = JointDistribution(self.variable, inclination_prior,
                                       distance_prior)

        ###### Expected answers
        # Answer taken from marginalized gaussian model
        self.q1 = {'distance': 42.0, 'inclination': 2.5}
        self.a1 = 541.8235746138382

        # answer taken from brute marginize pol + phase
        self.a2 = 542.581
        self.pol_samples = 200
 def resample_and_whiten(self, data, dt):
     to_resample = data.whiten(4, 4)
     if dt == self.dt:
         return (np.array(to_resample.data[-1 * self.final_data_samples:]))
     else:
         return (np.array(
             resample_to_delta_t(to_resample,
                                 dt).data[-1 * self.final_data_samples:]))
Exemple #7
0
def resampleado(strain, hh, grafica):

    template = types.TimeSeries(initial_array=hh, delta_t=1.0 / 8192, epoch=0)

    #	Downsampling the data to 4096Hz || 8192Hz || 16KHz

    strain = resample_to_delta_t(strain, 1.0 / 8192)

    template = resample_to_delta_t(template, 1.0 / 8192)

    if grafica == 1:

        pylab.figure(figsize=[15, 5])
        pylab.title('strain resampled')
        pylab.plot(strain.sample_times, strain)
        pylab.xlabel('Time (s)')
        pylab.show()
Exemple #8
0
def _lalsim_td_waveform(**p):
    fail_tolerant_waveform_generation
    lal_pars = _check_lal_pars(p)
    #nonGRparams can be straightforwardly added if needed, however they have to
    # be invoked one by one
    try:
        hp1, hc1 = lalsimulation.SimInspiralChooseTDWaveform(
               float(pnutils.solar_mass_to_kg(p['mass1'])),
               float(pnutils.solar_mass_to_kg(p['mass2'])),
               float(p['spin1x']), float(p['spin1y']), float(p['spin1z']),
               float(p['spin2x']), float(p['spin2y']), float(p['spin2z']),
               pnutils.megaparsecs_to_meters(float(p['distance'])),
               float(p['inclination']), float(p['coa_phase']),
               float(p['long_asc_nodes']), float(p['eccentricity']), float(p['mean_per_ano']),
               float(p['delta_t']), float(p['f_lower']), float(p['f_ref']),
               lal_pars,
               _lalsim_enum[p['approximant']])
    except RuntimeError:
        if not fail_tolerant_waveform_generation:
            raise
        # For some cases failure modes can occur. Here we add waveform-specific
        # instructions to try to work with waveforms that are known to fail.
        if 'SEOBNRv3' in p['approximant']:
            # Try doubling the sample time and redoing.
            # Don't want to get stuck in a loop though!
            if 'delta_t_orig' not in p:
                p['delta_t_orig'] = p['delta_t']
            p['delta_t'] = p['delta_t'] / 2.
            if p['delta_t_orig'] / p['delta_t'] > 9:
                raise
            hp, hc = _lalsim_td_waveform(**p)
            p['delta_t'] = p['delta_t_orig']
            hp = resample_to_delta_t(hp, hp.delta_t*2)
            hc = resample_to_delta_t(hc, hc.delta_t*2)
            return hp, hc
        raise

    #lal.DestroyDict(lal_pars)

    hp = TimeSeries(hp1.data.data[:], delta_t=hp1.deltaT, epoch=hp1.epoch)
    hc = TimeSeries(hc1.data.data[:], delta_t=hc1.deltaT, epoch=hc1.epoch)

    return hp, hc
def _lalsim_td_waveform(**p):
    flags = lalsimulation.SimInspiralCreateWaveformFlags()
    lalsimulation.SimInspiralSetSpinOrder(flags, p['spin_order'])
    lalsimulation.SimInspiralSetTidalOrder(flags, p['tidal_order'])

    ###########
    def rulog2(val):
        return 2.0**numpy.ceil(numpy.log2(float(val)))

    f_final = rulog2(seobnrrom_final_frequency(**p))
    f_nyq = 0.5 / p['delta_t']
    if f_final > f_nyq:
        print 'Final frequecny {0} is greater than given nyquist frequecny {1}'.format(
            f_final, f_nyq)
        p['delta_t'] = 0.5 / f_final

    ##########

    if p['numrel_data']:
        lalsimulation.SimInspiralSetNumrelData(flags, str(p['numrel_data']))

    hp1, hc1 = lalsimulation.SimInspiralChooseTDWaveform(
        float(p['coa_phase']), float(p['delta_t']),
        float(pnutils.solar_mass_to_kg(p['mass1'])),
        float(pnutils.solar_mass_to_kg(p['mass2'])), float(p['spin1x']),
        float(p['spin1y']), float(p['spin1z']), float(p['spin2x']),
        float(p['spin2y']), float(p['spin2z']), float(p['f_lower']),
        float(p['f_ref']), pnutils.megaparsecs_to_meters(float(p['distance'])),
        float(p['inclination']), float(p['lambda1']),
        float(p['lambda2']), flags, None, int(p['amplitude_order']),
        int(p['phase_order']), _lalsim_enum[p['approximant']])

    hp = TimeSeries(hp1.data.data[:], delta_t=hp1.deltaT, epoch=hp1.epoch)
    hc = TimeSeries(hc1.data.data[:], delta_t=hc1.deltaT, epoch=hc1.epoch)

    if f_final > f_nyq:
        p['delta_t'] = 0.5 / f_nyq
        print "Need to resample at delta_t {0}".format(p['delta_t'])
        hp = resample_to_delta_t(hp, p['delta_t'])
        hc = resample_to_delta_t(hc, p['delta_t'])

    return hp, hc
Exemple #10
0
def resample(ts):
    time_slices = [
        TimeSeries(ts.data[len(ts) - int(i * ts.sample_rate):],
                   delta_t=ts.delta_t) for i in [1, 2, 4, 8, 16, 32, 64]
    ]
    res_slices = []
    for i, t in enumerate(time_slices):
        res_slices.append(list(resample_to_delta_t(t, 1.0 / 2**(12 - i))))
        #The following line was added to make it suite my current setup
        res_slices.append(list(np.zeros(len(res_slices[-1]))))
    del time_slices
    return (res_slices)
Exemple #11
0
    def setUp(self, *args):
        self.context = _context
        self.scheme = _scheme
        self.tolerance = 1e-6
        xr = numpy.random.uniform(low=-1, high=1.0, size=2**20)
        xi = numpy.random.uniform(low=-1, high=1.0, size=2**20)
        self.x = Array(xr + xi * 1.0j, dtype=complex64)
        self.z = zeros(2**20, dtype=float32)
        for i in range(0, 4):
            trusted_accum(self.z, self.x)

        m = Merger("GW170814")

        ifos = ['H1', 'L1', 'V1']
        data = {}
        psd = {}
        for ifo in ifos:
            # Read in and condition the data and measure PSD
            ts = m.strain(ifo).highpass_fir(15, 512)
            data[ifo] = resample_to_delta_t(ts, 1.0 / 2048).crop(2, 2)
            p = data[ifo].psd(2)
            p = interpolate(p, data[ifo].delta_f)
            p = inverse_spectrum_truncation(p,
                                            int(2 * data[ifo].sample_rate),
                                            low_frequency_cutoff=15.0)
            psd[ifo] = p
        hp, _ = get_fd_waveform(approximant="IMRPhenomD",
                                mass1=31.36,
                                mass2=31.36,
                                f_lower=20.0,
                                delta_f=data[ifo].delta_f)
        hp.resize(len(psd[ifo]))

        # For each ifo use this template to calculate the SNR time series
        snr = {}
        snr_unnorm = {}
        norm = {}
        corr = {}
        for ifo in ifos:
            snr_unnorm[ifo], corr[ifo], norm[ifo] = \
                matched_filter_core(hp, data[ifo], psd=psd[ifo],
                                    low_frequency_cutoff=20)
            snr[ifo] = snr_unnorm[ifo] * norm[ifo]

        self.snr = snr
        self.snr_unnorm = snr_unnorm
        self.norm = norm
        self.corr = corr
        self.hp = hp
        self.data = data
        self.psd = psd
        self.ifos = ifos
def get_nrsub_strain(strain):
    nr = {}
    for ifo in ifos:
        nr[ifo] = load_timeseries('fig1-waveform-%s.txt' % ifo[0]) * 1e-21
        nr[ifo] = resample_to_delta_t(nr[ifo], strain[ifo].delta_t)
        nr[ifo].start_time += event_time
        
    ts2 = {}
    for ifo in ifos:
        ts2[ifo] = strain[ifo].copy()
        spart = ts2[ifo].time_slice(nr[ifo].start_time, nr[ifo].end_time)    
        nr[ifo].start_time = spart.start_time
        spart -= nr[ifo] 
    return ts2
Exemple #13
0
    def setUp(self,*args):
        self.context = _context
        self.scheme = _scheme
        self.tolerance = 1e-6
        xr = numpy.random.uniform(low=-1, high=1.0, size=2**20)
        xi = numpy.random.uniform(low=-1, high=1.0, size=2**20)
        self.x = Array(xr + xi * 1.0j, dtype=complex64)
        self.z = zeros(2**20, dtype=float32)
        for i in range(0, 4):
            trusted_accum(self.z, self.x)

        m = Merger("GW170814")

        ifos = ['H1', 'L1', 'V1']
        data = {}
        psd = {}
        for ifo in ifos:
            # Read in and condition the data and measure PSD
            ts = m.strain(ifo).highpass_fir(15, 512)
            data[ifo] = resample_to_delta_t(ts, 1.0/2048).crop(2, 2)
            p = data[ifo].psd(2)
            p = interpolate(p, data[ifo].delta_f)
            p = inverse_spectrum_truncation(p, 2 * data[ifo].sample_rate,
                                            low_frequency_cutoff=15.0)
            psd[ifo] = p
        hp, _ = get_fd_waveform(approximant="IMRPhenomD",
                                 mass1=31.36, mass2=31.36,
                                 f_lower=20.0, delta_f=data[ifo].delta_f)
        hp.resize(len(psd[ifo]))

        # For each ifo use this template to calculate the SNR time series
        snr = {}
        snr_unnorm = {}
        norm = {}
        corr = {}
        for ifo in ifos:
            snr_unnorm[ifo], corr[ifo], norm[ifo] = \
                matched_filter_core(hp, data[ifo], psd=psd[ifo],
                                    low_frequency_cutoff=20)
            snr[ifo] = snr_unnorm[ifo] * norm[ifo]

        self.snr = snr
        self.snr_unnorm = snr_unnorm
        self.norm = norm
        self.corr = corr
        self.hp = hp
        self.data = data
        self.psd = psd
        self.ifos = ifos
def resample_data(strain_list, sample_rates):
    if not type(strain_list) == list:
        strain_list = [strain_list]

    ret = np.zeros((len(strain_list) * len(sample_rates), 4096))

    for i, dat in enumerate(strain_list):
        for j, sr in enumerate(sample_rates):
            idx = i * len(sample_rates) + j
            ret[idx] = np.array(
                resample_to_delta_t(dat, 1.0 / sr).data[-4096:])

    ret = ret.transpose()

    return (ret)
Exemple #15
0
    def resample(self, delta_t):
        """ Resample this time series to the new delta_t

        Parameters
        -----------
        delta_t: float
            The time step to resample the times series to.

        Returns
        -------
        resampled_ts: pycbc.types.TimeSeries
            The resample timeseries at the new time interval delta_t.
        """
        from pycbc.filter import resample_to_delta_t
        return resample_to_delta_t(self, delta_t)
Exemple #16
0
 def apply(self,
           strain,
           detector_name,
           distance_scale=1,
           injection_sample_rate=None,
           inj_filter_rejector=None):
     if inj_filter_rejector is not None:
         raise NotImplementedError("IncoherentFromFile injections do not "
                                   "support inj_filter_rejector")
     if injection_sample_rate is not None:
         delta_t = 1. / injection_sample_rate
     else:
         delta_t = strain.delta_t
     injections = self.table
     for inj in injections:
         # Check if we should inject or not...
         # loading the time series like this is a bit brute-force, since
         # we really only need to know the delta_t and length of the
         # timeseries if the ref_point is anything but absmax, but that
         # would require adding logic to figure out how to get that metadata
         # based on the filetype and ref_point
         ts = self.loadts(inj)
         # set the ref time
         self.set_ref_time(inj, ts)
         # determine if we inject or not based on the times
         try:
             injtime = inj['{}_gps_time'.format(detector_name).lower()]
         except ValueError:
             injtime = -np.inf
         if np.isnan(injtime):
             # nan means don't inject
             injtime = -np.inf
         start_time = injtime + ts.start_time
         end_time = injtime + ts.end_time
         inject = (start_time < strain.end_time
                   and end_time > strain.start_time)
         if inject:
             ts = self.make_strain_from_inj_object(
                 inj,
                 delta_t,
                 detector_name,
                 distance_scale=distance_scale,
                 ts=ts)
             if ts.delta_t != strain.delta_t:
                 ts = resample_to_delta_t(ts, strain.delta_t, method='ldas')
             strain.inject(ts, copy=False)
Exemple #17
0
    def _gen_slices(self, index_range):
        """Slice, whiten and re-sample the input data.
        
        Arguments
        ---------
        index_range : numpy.array
            Array of shape (num_samples, 2), where each row contains the
            start- and stop-index of the slice.
        
        Returns
        -------
        list of arrays
            A list containing the input for the network. Each array is
            of shape (num_samples, 2048, 2).
        """

        #Number of different sample rates
        num_channels = len(self.resample_rates)

        #Number of detectors
        num_detectors = len(self.ts)

        #Setup return data
        X = [
            np.zeros((len(index_range), num_detectors * num_channels,
                      self.final_data_samples)) for i in range(2)
        ]
        X_ret = [
            np.zeros(
                (num_detectors, self.final_data_samples, len(index_range)))
            for i in range(2 * num_channels)
        ]

        #Slice the time-series
        for in_batch, idx in enumerate(index_range):
            for detector in range(num_detectors):
                low, up = idx
                #Whiten each slice
                white_full_signal = whiten_data(self.ts[detector][low:up],
                                                psd=self.psd)

                #Re-sample each slice
                max_idx = len(white_full_signal)
                min_idx = max_idx - int(
                    float(self.final_data_samples) /
                    float(self.resample_rates[0]) / self.dt)
                for i, sr in enumerate(self.resample_rates):
                    X[0][in_batch][i * num_detectors +
                                   detector] = resample_to_delta_t(
                                       white_full_signal[min_idx:max_idx],
                                       1.0 / sr)
                    if not i + 1 == num_channels:
                        t_dur = float(self.final_data_samples) / float(
                            self.resample_rates[i + 1])
                        sample_dur = int(t_dur / self.dt)
                        max_idx = min_idx
                        min_idx -= sample_dur

        #Change formatting
        for i in range(2):
            X[i] = X[i].transpose(1, 2, 0)

        for i in range(num_channels):
            X_ret[2 * i][0] = X[0][2 * i]
            X_ret[2 * i][1] = X[0][2 * i + 1]
            X_ret[2 * i + 1][0] = X[1][2 * i]
            X_ret[2 * i + 1][1] = X[1][2 * i + 1]

        return ([x.transpose(2, 1, 0) for x in X_ret])
Exemple #18
0
    def apply(self, strain, detector_name, distance_scale=1,
              simulation_ids=None, inj_filter_rejector=None,
              injection_sample_rate=None):
        """Add injection (as seen by a particular detector) to a time series.

        Parameters
        ----------
        strain : TimeSeries
            Time series to inject signals into, of type float32 or float64.
        detector_name : string
            Name of the detector used for projecting injections.
        distance_scale: float, optional
            Factor to scale the distance of an injection with. The default (=1)
            is no scaling.
        simulation_ids: iterable, optional
            If given, only inject signals with the given simulation IDs.
        inj_filter_rejector: InjFilterRejector instance, optional
            Not implemented. If not ``None``, a ``NotImplementedError`` will
            be raised.
        injection_sample_rate: float, optional
            The sample rate to generate the signal before injection

        Returns
        -------
        None

        Raises
        ------
        NotImplementedError
            If an ``inj_filter_rejector`` is provided.
        TypeError
            For invalid types of `strain`.
        """
        if inj_filter_rejector is not None:
            raise NotImplementedError("Ringdown injections do not support "
                                      "inj_filter_rejector")
        if strain.dtype not in (float32, float64):
            raise TypeError("Strain dtype must be float32 or float64, not " \
                    + str(strain.dtype))

        lalstrain = strain.lal()

        # pick lalsimulation injection function
        add_injection = injection_func_map[strain.dtype]

        delta_t = strain.delta_t
        if injection_sample_rate is not None:
            delta_t = 1.0 / injection_sample_rate

        injections = self.table
        if simulation_ids:
            injections = injections[list(simulation_ids)]
        for ii in range(injections.size):
            injection = injections[ii]
            signal = self.make_strain_from_inj_object(
                injection, delta_t, detector_name,
                distance_scale=distance_scale)
            signal = resample_to_delta_t(signal, strain.delta_t, method='ldas')
            signal = signal.astype(strain.dtype)
            signal_lal = signal.lal()
            add_injection(lalstrain, signal_lal, None)

            strain.data[:] = lalstrain.data.data[:]
Exemple #19
0
    def apply(self, strain, detector_name, f_lower=None, distance_scale=1,
              simulation_ids=None,
              inj_filter_rejector=None,
              injection_sample_rate=None,):
        """Add injections (as seen by a particular detector) to a time series.

        Parameters
        ----------
        strain : TimeSeries
            Time series to inject signals into, of type float32 or float64.
        detector_name : string
            Name of the detector used for projecting injections.
        f_lower : {None, float}, optional
            Low-frequency cutoff for injected signals. If None, use value
            provided by each injection.
        distance_scale: {1, float}, optional
            Factor to scale the distance of an injection with. The default is
            no scaling.
        simulation_ids: iterable, optional
            If given, only inject signals with the given simulation IDs.
        inj_filter_rejector: InjFilterRejector instance; optional, default=None
            If given send each injected waveform to the InjFilterRejector
            instance so that it can store a reduced representation of that
            injection if necessary.
        injection_sample_rate: float, optional
            The sample rate to generate the signal before injection

        Returns
        -------
        None

        Raises
        ------
        TypeError
            For invalid types of `strain`.
        """
        if strain.dtype not in (float32, float64):
            raise TypeError("Strain dtype must be float32 or float64, not " \
                    + str(strain.dtype))

        lalstrain = strain.lal()
        earth_travel_time = lal.REARTH_SI / lal.C_SI
        t0 = float(strain.start_time) - earth_travel_time
        t1 = float(strain.end_time) + earth_travel_time

        # pick lalsimulation injection function
        add_injection = injection_func_map[strain.dtype]

        delta_t = strain.delta_t
        if injection_sample_rate is not None:
            delta_t = 1.0 / injection_sample_rate

        injections = self.table
        if simulation_ids:
            injections = injections[list(simulation_ids)]

        injected_ids = []
        for ii, inj in enumerate(injections):
            f_l = inj.f_lower if f_lower is None else f_lower
            # roughly estimate if the injection may overlap with the segment
            # Add 2s to end_time to account for ringdown and light-travel delay
            end_time = inj.tc + 2
            inj_length = tau0_from_mass1_mass2(inj.mass1, inj.mass2, f_l)
            # Start time is taken as twice approx waveform length with a 1s
            # safety buffer
            start_time = inj.tc - 2 * (inj_length + 1)
            if end_time < t0 or start_time > t1:
                continue
            signal = self.make_strain_from_inj_object(inj, delta_t,
                     detector_name, f_lower=f_l,
                     distance_scale=distance_scale)
            signal = resample_to_delta_t(signal, strain.delta_t, method='ldas')
            if float(signal.start_time) > t1:
                continue

            signal = signal.astype(strain.dtype)
            signal_lal = signal.lal()
            add_injection(lalstrain, signal_lal, None)
            injected_ids.append(ii)
            if inj_filter_rejector is not None:
                inj_filter_rejector.generate_short_inj_from_inj(signal, ii)

        strain.data[:] = lalstrain.data.data[:]

        injected = copy.copy(self)
        injected.table = injections[np.array(injected_ids).astype(int)]
        if inj_filter_rejector is not None:
            if hasattr(inj_filter_rejector, 'injected'):
                prev_p = inj_filter_rejector.injection_params
                prev_id = inj_filter_rejector.injection_ids
                injected = np.concatenate([prev_p, injected])
                injected_ids = np.concatenate([prev_id, injected_ids])

            inj_filter_rejector.injection_params = injected
            inj_filter_rejector.injection_ids = injected_ids
        return injected
Exemple #20
0
def detect_loud_glitches(strain, psd_duration=4., psd_stride=2.,
                         psd_avg_method='median', low_freq_cutoff=30.,
                         threshold=50., cluster_window=5., corrupted_time=4.,
                         high_freq_cutoff=None, output_intermediates=False):
    """Automatic identification of loud transients for gating purposes."""

    logging.info('Autogating: tapering strain')
    fade_size = corrupted_time * strain.sample_rate
    w = numpy.arange(fade_size) / float(fade_size)
    strain[0:fade_size] *= Array(w, dtype=strain.dtype)
    strain[(len(strain)-fade_size):] *= Array(w[::-1], dtype=strain.dtype)

    if output_intermediates:
        strain.save_to_wav('strain_conditioned.wav')

    # don't waste time trying to optimize a single FFT
    pycbc.fft.fftw.set_measure_level(0)

    logging.info('Autogating: estimating PSD')
    psd = pycbc.psd.welch(strain, seg_len=int(psd_duration*strain.sample_rate),
                          seg_stride=int(psd_stride*strain.sample_rate),
                          avg_method=psd_avg_method)
    psd = pycbc.psd.interpolate(psd, 1./strain.duration)
    psd = pycbc.psd.inverse_spectrum_truncation(
            psd, int(psd_duration * strain.sample_rate),
            low_frequency_cutoff=low_freq_cutoff,
            trunc_method='hann')

    logging.info('Autogating: time -> frequency')
    strain_tilde = FrequencySeries(numpy.zeros(len(strain) / 2 + 1),
                                   delta_f=psd.delta_f,
                                   dtype=complex_same_precision_as(strain))
    pycbc.fft.fft(strain, strain_tilde)

    logging.info('Autogating: whitening')
    if high_freq_cutoff:
        kmax = int(high_freq_cutoff / strain_tilde.delta_f)
        strain_tilde[kmax:] = 0.
        norm = high_freq_cutoff - low_freq_cutoff
    else:
        norm = strain.sample_rate/2. - low_freq_cutoff
    strain_tilde /= (psd * norm) ** 0.5
    kmin = int(low_freq_cutoff / strain_tilde.delta_f)
    strain_tilde[0:kmin] = 0.

    # FIXME downsample here rather than post-FFT

    logging.info('Autogating: frequency -> time')
    pycbc.fft.ifft(strain_tilde, strain)

    pycbc.fft.fftw.set_measure_level(pycbc.fft.fftw._default_measurelvl)

    if high_freq_cutoff:
        strain = resample_to_delta_t(strain, 0.5 / high_freq_cutoff,
                                     method='ldas')

    logging.info('Autogating: stdev of whitened strain is %.4f', numpy.std(strain))

    if output_intermediates:
        strain.save_to_wav('strain_whitened.wav')

    mag = abs(strain)
    if output_intermediates:
        mag.save('strain_whitened_mag.npy')
    mag = numpy.array(mag, dtype=numpy.float32)

    # remove strain corrupted by filters at the ends
    corrupted_idx = int(corrupted_time * strain.sample_rate)
    mag[0:corrupted_idx] = 0
    mag[-1:-corrupted_idx-1:-1] = 0

    logging.info('Autogating: finding loud peaks')
    indices = numpy.where(mag > threshold)[0]
    cluster_idx = pycbc.events.findchirp_cluster_over_window(
            indices, mag[indices], int(cluster_window*strain.sample_rate))
    times = [idx * strain.delta_t + strain.start_time \
             for idx in indices[cluster_idx]]
    return times
"""Cleaning and applying highpass filter. Downsample to 2048 Hz to make the data analysis more convenient. Power density of the noise is higher than the signal. At higher frequency the amplitude of the noise is lower. And then graph."""

from pycbc.catalog import Merger
from pycbc.filter import resample_to_delta_t, highpass

from pycbc.catalog import Merger
from pycbc.filter import resample_to_delta_t, highpass

merger = Merger("GW170817")
strain, stilde = {}, {} # tuple of directories: strain and stilde=cleaned data
for ifo in ['L1', 'H1']:
    # We'll download the data and select 256 seconds that includes the event time
    ts = merger.strain(ifo)
    # Remove the low frequency content and downsample the data to 2048Hz
    strain[ifo] = resample_to_delta_t(highpass(ts, 15.0), 1.0/2048)
    #remove the begining and ending spikes from the data
    strain[ifo] = strain[ifo].crop(2, 2) # remove 2 first and 2 last seconds
    # Also create a frequency domain version of the data
    stilde[ifo] = strain[ifo].to_frequencyseries()

#print (strain.delta_t)
pylab.plot(strain['L1'].sample_times, strain['L1'])
pylab.xlabel('Time (s)')
pylab.show()

"""***part 2***

Then the power spectral density is plotted.
"""
Exemple #22
0
def detect_loud_glitches(strain, psd_duration=4., psd_stride=2.,
                         psd_avg_method='median', low_freq_cutoff=30.,
                         threshold=50., cluster_window=5., corrupt_time=4.,
                         high_freq_cutoff=None, output_intermediates=False):
    """Automatic identification of loud transients for gating purposes.
    This function first estimates the PSD of the input time series using the
    FindChirp Welch method. Then it whitens the time series using that
    estimate. Finally, it computes the magnitude of the whitened series,
    thresholds it and applies the FindChirp clustering over time to the
    surviving samples.

    Parameters
    ----------
    strain : TimeSeries
        Input strain time series to detect glitches over.

    psd_duration : {float, 4}
        Duration of the segments for PSD estimation in seconds.

    psd_stride : {float, 2}
        Separation between PSD estimation segments in seconds.

    psd_avg_method : {string, 'median'}
        Method for averaging PSD estimation segments.

    low_freq_cutoff : {float, 30}
        Minimum frequency to include in the whitened strain.

    threshold : {float, 50}
        Minimum magnitude of whitened strain for considering a transient to
        be present.

    cluster_window : {float, 5}
        Length of time window to cluster surviving samples over, in seconds.

    corrupt_time : {float, 4}
        Amount of time to be discarded at the beginning and end of the input
        time series.

    high_frequency_cutoff : {float, None}
        Maximum frequency to include in the whitened strain. If given, the
        input series is downsampled accordingly. If omitted, the Nyquist
        frequency is used.

    output_intermediates : {bool, False}
        Save intermediate time series for debugging.

    Returns
    -------
    """

    logging.info('Autogating: tapering strain')
    taper_length = corrupt_time * strain.sample_rate
    w = numpy.arange(taper_length) / float(taper_length)
    strain[0:taper_length] *= pycbc.types.Array(w, dtype=strain.dtype)
    strain[(len(strain)-taper_length):] *= pycbc.types.Array(w[::-1],
                                                             dtype=strain.dtype)

    # don't waste time trying to optimize a single FFT
    pycbc.fft.fftw.set_measure_level(0)

    if high_freq_cutoff:
        logging.info('Autogating: downsampling strain')
        strain = resample_to_delta_t(strain, 0.5 / high_freq_cutoff,
                                     method='ldas')
    if output_intermediates:
        strain.save_to_wav('strain_conditioned.wav')

    corrupt_length = int(corrupt_time * strain.sample_rate)

    # zero-pad strain to a power-of-2 length
    strain_pad_length = next_power_of_2(len(strain))
    pad_start = strain_pad_length/2 - len(strain)/2
    pad_end = pad_start + len(strain)
    strain_pad = pycbc.types.TimeSeries(
            pycbc.types.zeros(strain_pad_length, dtype=strain.dtype),
            delta_t=strain.delta_t, copy=False,
            epoch=strain.start_time-pad_start/strain.sample_rate)
    strain_pad[pad_start:pad_end] = strain[:]

    logging.info('Autogating: estimating PSD')
    psd = pycbc.psd.welch(strain[corrupt_length:(len(strain)-corrupt_length)],
                          seg_len=int(psd_duration * strain.sample_rate),
                          seg_stride=int(psd_stride * strain.sample_rate),
                          avg_method=psd_avg_method,
                          require_exact_data_fit=False)
    psd = pycbc.psd.interpolate(psd, 1./strain_pad.duration)
    psd = pycbc.psd.inverse_spectrum_truncation(
            psd, int(psd_duration * strain.sample_rate),
            low_frequency_cutoff=low_freq_cutoff,
            trunc_method='hann')
    kmin = int(low_freq_cutoff / psd.delta_f)
    psd[0:kmin] = numpy.inf
    if high_freq_cutoff:
        kmax = int(high_freq_cutoff / psd.delta_f)
        psd[kmax:] = numpy.inf

    logging.info('Autogating: time -> frequency')
    strain_tilde = pycbc.types.FrequencySeries(
            pycbc.types.zeros(len(strain_pad) / 2 + 1,
                              dtype=pycbc.types.complex_same_precision_as(strain)),
            delta_f=psd.delta_f, copy=False)
    pycbc.fft.fft(strain_pad, strain_tilde)

    logging.info('Autogating: whitening')
    if high_freq_cutoff:
        norm = high_freq_cutoff - low_freq_cutoff
    else:
        norm = strain.sample_rate/2. - low_freq_cutoff
    strain_tilde *= (psd * norm) ** (-0.5)

    logging.info('Autogating: frequency -> time')
    pycbc.fft.ifft(strain_tilde, strain_pad)

    pycbc.fft.fftw.set_measure_level(pycbc.fft.fftw._default_measurelvl)

    if output_intermediates:
        strain_pad[pad_start:pad_end].save_to_wav('strain_whitened.wav')

    logging.info('Autogating: computing magnitude')
    mag = abs(strain_pad[pad_start:pad_end])
    if output_intermediates:
        mag.save('strain_whitened_mag.npy')

    mag = mag.numpy()

    # remove strain corrupted by filters at the ends
    mag[0:corrupt_length] = 0
    mag[-1:-corrupt_length-1:-1] = 0

    logging.info('Autogating: finding loud peaks')
    indices = numpy.where(mag > threshold)[0]
    cluster_idx = pycbc.events.findchirp_cluster_over_window(
            indices, numpy.array(mag[indices]),
            int(cluster_window*strain.sample_rate))
    times = [idx * strain.delta_t + strain.start_time \
             for idx in indices[cluster_idx]]
    return times
Exemple #23
0
def generate(file_path, duration, seed=0, signal_separation=200,
             signal_separation_interval=20, min_mass=1.2, max_mass=1.6,
             f_lower=20, srate=4096, padding=256, tstart=0):
    """Function that generates test data with injections.
    
    Arguments
    ---------
    file_path : str
        The path at which the data should be stored. 
    duration : int or float
        Duration of the output file in seconds.
    seed : {int, 0}, optional
        A seed to use for generating injection parameters and noise.
    signal_separation : {int or float, 200}, optional
        The average duration between two injections.
    signal_separation_interval : {int or float, 20}, optional
        The duration between two signals will be signal_separation + t,
        where t is drawn uniformly from the interval
        [-signal_separation_interval, signal_separation_interval].
    min_mass : {float, 1.2}, optional
        The minimal mass at which injections will be made (in solar
        masses).
    max_mass : {float, 1.6}, optional
        The maximum mass at which injections will be made (in solar
        masses).
    f_lower : {int or float, 20}, optional
        Noise will be generated down to the specified frequency.
        Below they will be set to zero. (The waveforms are generated
        with a lower frequency cutofff of 25 Hertz)
    srate : {int, 4096}, optional
        The sample rate at which the data is generated.
    padding : {int or float, 256}, optional
        Duration in the beginning and end of the data that does not
        contain any injections.
    tstart : {int or float, 0}, optional
        The inital time of the data.
    """    
    np.random.seed(seed)
    
    size = (duration // signal_separation)
    
    #Generate injection times
    random_time_samples = int(round(float(signal_separation_interval) * float(srate)))
    signal_separation_samples = int(round(float(signal_separation) * float(srate)))
    time_samples = randint(signal_separation_samples - random_time_samples, signal_separation_samples + random_time_samples, size=size)
    time_samples = time_samples.cumsum()
    times = time_samples / float(srate)
    
    times = times[np.where(np.logical_and(times > padding, times < duration - padding))[0]]
    size = len(times)
    
    #Generate parameters
    cphase = uniform(0, np.pi*2.0, size=size)
    
    ra = uniform(0, 2 * np.pi, size=size)
    dec = np.arccos(uniform(-1., 1., size=size)) - np.pi/2
    inc = np.arccos(uniform(-1., 1., size=size))
    pol = uniform(0, 2 * np.pi, size=size)
    dist = power(3, size) * 400

    m1 = uniform(min_mass, max_mass, size=size)
    m2 = uniform(min_mass, max_mass, size=size)
    
    #Save parameters to file.
    stat_file_path, ext = os.path.splitext(file_path)
    stat_file_path = stat_file_path + '_stats' + ext
    with h5py.File(stat_file_path, 'w') as f:
        f['times'] = times
        f['cphase'] = cphase
        f['ra'] = ra
        f['dec'] = dec
        f['inc'] = inc
        f['pol'] = pol
        f['dist'] = dist
        f['mass1'] = m1
        f['mass2'] = m2
        f['seed'] = seed
    
    p = aLIGOZeroDetHighPower(2 * int(duration * srate), 1.0/64, f_lower)
    
    #Generate noise
    data = {}
    for i, ifo in enumerate(['H1', 'L1']):
        data[ifo] = colored_noise(p, int(tstart),
                                    int(tstart + duration), 
                                    seed=seed + i,
                                    low_frequency_cutoff=f_lower)
        data[ifo] = resample_to_delta_t(data[ifo], 1.0/srate)
    
    # make waveforms and add them into the noise
    for i in range(len(times)):
        hp, hc = get_td_waveform(approximant="TaylorF2",
                                mass1=m1[i], 
                                mass2=m2[i],
                                f_lower=25,
                                delta_t=1.0/srate,
                                inclination=inc[i],
                                coa_phase=cphase[i],
                                distance=dist[i])
        hp.start_time += times[i] + int(tstart)
        hc.start_time += times[i] + int(tstart)
        
        for ifo in ['H1', 'L1']:
            ht = Detector(ifo).project_wave(hp, hc, ra[i], dec[i], pol[i])
            time_diff = float(ht.start_time - data[ifo].start_time)
            sample_diff = int(round(time_diff / data[ifo].delta_t))
            ht.prepend_zeros(sample_diff)
            ht.start_time = data[ifo].start_time
            data[ifo] = data[ifo].add_into(ht)
    
    #Save the data
    for ifo in ['H1', 'L1']:
        data[ifo].save(file_path, group='%s' % (ifo))
Exemple #24
0
print "hann props lal", w_lal.sum, w_lal.sumofsquares

# Check that the median bias is the same
print "%s segments" % nsegs
print "BIAS pycbc", pycbc.psd.median_bias(nsegs)
print "BIAS lal", lal.RngMedBias(nsegs)

# Check the psd norm
print "PYCBC psd norm", 2.0 / float(sample_rate) / (w_pycbc.squared_norm().sum()) / pycbc.psd.median_bias(nsegs)

# Same strain preparation for lal and pycbc psd estimation
strain = read_frame("LER2.lcf", "H1:LDAS-STRAIN", start_time=start_time-pad_data, duration=duration+pad_data*2)
strain = highpass(strain, frequency=30)
strain *= pycbc.DYN_RANGE_FAC
strain = TimeSeries(strain, delta_t=strain.delta_t, epoch=strain.start_time, dtype=float32)
strain = resample_to_delta_t(strain, 1.0/sample_rate)
strain = strain[int(pad_data*1.0/strain.delta_t):len(strain)-int(1.0/strain.delta_t*pad_data)]

psd_pycbc = pycbc.psd.welch(strain, seg_len=seg_len, seg_stride=stride)
psd_lal = lal_psd_estimate(seg_len, strain)

print psd_pycbc[-1]
print psd_lal[-1]

pylab.figure(1)
pylab.loglog(psd_pycbc.sample_frequencies.numpy(), psd_pycbc.numpy(), label="PyCBC")
pylab.loglog(psd_lal.sample_frequencies.numpy(), psd_lal.numpy(), label="LAL")
pylab.xlabel("Frequency (Hz)")
pylab.xlim(40, 2000)
pylab.ylabel(" (Strain/rhz * DYN_RANGE_FAC)^2 " ) 
pylab.legend()
Exemple #25
0
def detect_loud_glitches(strain,
                         psd_duration=4.,
                         psd_stride=2.,
                         psd_avg_method='median',
                         low_freq_cutoff=30.,
                         threshold=50.,
                         cluster_window=5.,
                         corrupt_time=4.,
                         high_freq_cutoff=None,
                         output_intermediates=False):
    """Automatic identification of loud transients for gating purposes.
    This function first estimates the PSD of the input time series using the
    FindChirp Welch method. Then it whitens the time series using that
    estimate. Finally, it computes the magnitude of the whitened series,
    thresholds it and applies the FindChirp clustering over time to the
    surviving samples.

    Parameters
    ----------
    strain : TimeSeries
        Input strain time series to detect glitches over.

    psd_duration : {float, 4}
        Duration of the segments for PSD estimation in seconds.

    psd_stride : {float, 2}
        Separation between PSD estimation segments in seconds.

    psd_avg_method : {string, 'median'}
        Method for averaging PSD estimation segments.

    low_freq_cutoff : {float, 30}
        Minimum frequency to include in the whitened strain.

    threshold : {float, 50}
        Minimum magnitude of whitened strain for considering a transient to
        be present.

    cluster_window : {float, 5}
        Length of time window to cluster surviving samples over, in seconds.

    corrupt_time : {float, 4}
        Amount of time to be discarded at the beginning and end of the input
        time series.

    high_frequency_cutoff : {float, None}
        Maximum frequency to include in the whitened strain. If given, the
        input series is downsampled accordingly. If omitted, the Nyquist
        frequency is used.

    output_intermediates : {bool, False}
        Save intermediate time series for debugging.

    Returns
    -------
    """

    logging.info('Autogating: tapering strain')
    taper_length = corrupt_time * strain.sample_rate
    w = numpy.arange(taper_length) / float(taper_length)
    strain[0:taper_length] *= pycbc.types.Array(w, dtype=strain.dtype)
    strain[(len(strain) - taper_length):] *= pycbc.types.Array(
        w[::-1], dtype=strain.dtype)

    # don't waste time trying to optimize a single FFT
    pycbc.fft.fftw.set_measure_level(0)

    if high_freq_cutoff:
        logging.info('Autogating: downsampling strain')
        strain = resample_to_delta_t(strain,
                                     0.5 / high_freq_cutoff,
                                     method='ldas')
    if output_intermediates:
        strain.save_to_wav('strain_conditioned.wav')

    corrupt_length = int(corrupt_time * strain.sample_rate)

    # zero-pad strain to a power-of-2 length
    strain_pad_length = next_power_of_2(len(strain))
    pad_start = strain_pad_length / 2 - len(strain) / 2
    pad_end = pad_start + len(strain)
    strain_pad = pycbc.types.TimeSeries(pycbc.types.zeros(strain_pad_length,
                                                          dtype=strain.dtype),
                                        delta_t=strain.delta_t,
                                        copy=False,
                                        epoch=strain.start_time -
                                        pad_start / strain.sample_rate)
    strain_pad[pad_start:pad_end] = strain[:]

    logging.info('Autogating: estimating PSD')
    psd = pycbc.psd.welch(strain[corrupt_length:(len(strain) -
                                                 corrupt_length)],
                          seg_len=int(psd_duration * strain.sample_rate),
                          seg_stride=int(psd_stride * strain.sample_rate),
                          avg_method=psd_avg_method,
                          require_exact_data_fit=False)
    psd = pycbc.psd.interpolate(psd, 1. / strain_pad.duration)
    psd = pycbc.psd.inverse_spectrum_truncation(
        psd,
        int(psd_duration * strain.sample_rate),
        low_frequency_cutoff=low_freq_cutoff,
        trunc_method='hann')
    kmin = int(low_freq_cutoff / psd.delta_f)
    psd[0:kmin] = numpy.inf
    if high_freq_cutoff:
        kmax = int(high_freq_cutoff / psd.delta_f)
        psd[kmax:] = numpy.inf

    logging.info('Autogating: time -> frequency')
    strain_tilde = pycbc.types.FrequencySeries(pycbc.types.zeros(
        len(strain_pad) / 2 + 1,
        dtype=pycbc.types.complex_same_precision_as(strain)),
                                               delta_f=psd.delta_f,
                                               copy=False)
    pycbc.fft.fft(strain_pad, strain_tilde)

    logging.info('Autogating: whitening')
    if high_freq_cutoff:
        norm = high_freq_cutoff - low_freq_cutoff
    else:
        norm = strain.sample_rate / 2. - low_freq_cutoff
    strain_tilde *= (psd * norm)**(-0.5)

    logging.info('Autogating: frequency -> time')
    pycbc.fft.ifft(strain_tilde, strain_pad)

    pycbc.fft.fftw.set_measure_level(pycbc.fft.fftw._default_measurelvl)

    if output_intermediates:
        strain_pad[pad_start:pad_end].save_to_wav('strain_whitened.wav')

    logging.info('Autogating: computing magnitude')
    mag = abs(strain_pad[pad_start:pad_end])
    if output_intermediates:
        mag.save('strain_whitened_mag.npy')

    mag = mag.numpy()

    # remove strain corrupted by filters at the ends
    mag[0:corrupt_length] = 0
    mag[-1:-corrupt_length - 1:-1] = 0

    logging.info('Autogating: finding loud peaks')
    indices = numpy.where(mag > threshold)[0]
    cluster_idx = pycbc.events.findchirp_cluster_over_window(
        indices, numpy.array(mag[indices]),
        int(cluster_window * strain.sample_rate))
    times = [idx * strain.delta_t + strain.start_time \
             for idx in indices[cluster_idx]]
    return times
Exemple #26
0
pylab.ylim([0, 30])

for filename in infile:

    # Load in data files
    file = cwd + '/' + filename
    hoft = pycbc.types.timeseries.load_timeseries(file)

    # =========================
    # = Precondition the data =
    # =========================

    # Remove low frequency noise
    hoft = highpass(hoft, 15.0)

    hoft = resample_to_delta_t(hoft, 1./2048.)

    # Remove 2 seconds from the start and the end to protect against edge effects
    conditioned = hoft.crop(2, 2)

    # Calculate PSD of data
    psd = conditioned.psd(4)
    psd = interpolate(psd, conditioned.delta_f)
    psd = inverse_spectrum_truncation(psd, 4 * conditioned.sample_rate, low_frequency_cutoff=15)

    psdlist[i] = psd



    # Generate a zeros frequency series the same length as the psd
    filter = FrequencySeries(numpy.zeros(len(psd)), 1, dtype=numpy.complex128)
def worker(kwargs):
    #print("Worker here!")
    #print("Args: {}".format(kwargs))
    full_kwargs = dict(kwargs)
    kwargs = dict(kwargs)

    opt_arg = {}
    opt_keys = [
        'snr', 'gw_prob', 'random_starting_time', 'resample_delta_t', 't_len',
        'resample_t_len', 'time_offset', 'whiten_len', 'whiten_cutoff',
        't_from_right', 'no_gw_snr'
    ]

    for key in opt_keys:
        try:
            opt_arg[key] = kwargs.get(key)
            del kwargs[key]
        except KeyError:
            print("The necessary argument '%s' was not supplied in '%s'" %
                  (key, str(__file__)))

    projection_arg = {}
    projection_arg['end_time'] = 1337 * 137 * 42
    projection_arg['declination'] = 0.0
    projection_arg['right_ascension'] = 0.0
    projection_arg['polarization'] = 0.0
    projection_arg['detectors'] = ['L1', 'H1']

    projection_arg, kwargs = filter_keys(projection_arg, kwargs)

    T_SAMPLES = int(opt_arg['t_len'] / kwargs['delta_t'])
    DELTA_F = 1.0 / opt_arg['t_len']
    F_LEN = int(2.0 / (DELTA_F * kwargs['delta_t']))

    gw_present = bool(random() < opt_arg['gw_prob'])

    psd = generate_psd(**full_kwargs)
    #TODO: Generate the seed for this prior to parallelizing
    noise_list = [
        noise_from_psd(length=T_SAMPLES,
                       delta_t=kwargs['delta_t'],
                       psd=psd,
                       seed=randint(0, 100000))
        for d in projection_arg['detectors']
    ]

    #print("Pre GW generation")

    if gw_present:
        #Generate waveform
        #print("Pre waveform")
        hp, hc = get_td_waveform(**kwargs)

        #Project it onto the considered detectors (This could be handeled using)
        #a list, to make room for more detectors
        #print("Pre projection")
        strain_list = detector_projection(TimeSeries(hp), TimeSeries(hc),
                                          **projection_arg)

        #Enlarge the signals bya adding zeros infront and after. Take care of a
        #random timeshift while still keeping the relative timeshift between
        #detectors
        #TODO: This should also be set outside
        if opt_arg['random_starting_time']:
            t_offset = opt_arg['time_offset']
        else:
            t_offset = 0.0

        #print("Pre embedding in zero")
        set_temp_offset(strain_list, opt_arg['t_len'], t_offset,
                        opt_arg['t_from_right'])

        #Rescale the templates to match wanted SNR
        strain_list = rescale_to_snr(strain_list, opt_arg['snr'], psd,
                                     kwargs['f_lower'])
    else:
        strain_list = [
            TimeSeries(np.zeros(len(n)), n.delta_t) for n in noise_list
        ]
        opt_arg['snr'] = opt_arg['no_gw_snr']

    #print("post generating")
    total_white = []
    matched_snr_sq = []

    #print("Pre loop")
    tmp_white = []
    for i, noise in enumerate(noise_list):
        #print("Loop i: {}".format(i))
        #Add strain to noise
        noise._epoch = strain_list[i]._epoch
        #print("Post epoch, pre adding")
        total = TimeSeries(noise + strain_list[i])

        #print("Post adding, pre whiten")

        #Whiten the total data, downsample and crop the data
        total_white.append(
            total.whiten(opt_arg['whiten_len'],
                         opt_arg['whiten_cutoff'],
                         low_frequency_cutoff=kwargs['f_lower']))
        #print("Post whiten and appending, pre resampling")
        if isinstance(opt_arg['resample_delta_t'], tuple):
            if isinstance(opt_arg['resample_t_len'], tuple) and len(
                    opt_arg['resample_delta_t']) == len(
                        opt_arg['resample_t_len']):
                rdt = opt_arg['resample_delta_t']
                resample_samples = [
                    int(opt_arg['resample_t_len'][idx] / kwargs['delta_t'])
                    for idx in range(len(rdt))
                ]
                for idx, sam in enumerate(resample_samples):
                    #print("Sam: {}".format(sam))
                    #print("Len list: {}".format(len(total_white[i])))
                    #print(i)
                    #print("Resample delta t: {}".format(rdt[idx]))
                    #print("resample_t_len: {}".format(opt_arg['resample_t_len'][idx]))
                    #print("")
                    tmp_white.append(
                        resample_to_delta_t(
                            total_white[i][len(total_white[i]) - sam:],
                            rdt[idx]))
            else:
                raise ValueError(
                    "The options 'resample_delta_t' and 'resample_t_len' have to either both be floats or tuples of floats of the same length."
                )
                #Error or handle
        else:
            total_white[i] = resample_to_delta_t(total_white[i],
                                                 opt_arg['resample_delta_t'])
            #print("Post resampling, pre cropping")
            mid_point = (total_white[i].end_time +
                         total_white[i].start_time) / 2
            total_white[i] = total_white[i].time_slice(
                mid_point - opt_arg['resample_t_len'] / 2,
                mid_point + opt_arg['resample_t_len'] / 2)
        #print("Post cropping, pre matched filtering")
        #print("Strain list: {}\ntotal: {}\nPSD: {}".format(strain_list[i], total, psd))

        #test = matched_filter(strain_list[i], total, psd=psd, low_frequency_cutoff=kwargs['f_lower'])
        #print("Can calc")

        #Calculate matched filter snr
        if gw_present:
            matched_snr_sq.append(
                max(
                    abs(
                        matched_filter(
                            strain_list[i],
                            total,
                            psd=psd,
                            low_frequency_cutoff=kwargs['f_lower'])))**2)
        else:
            #TODO: Implement matched filtering against template bank
            matched_snr_sq.append(opt_arg['no_gw_snr']**2 / len(noise_list))
        #print("Post matched filtering, WTF!")

    if not len(tmp_white) == 0:
        total_white = tmp_white

    del total
    del strain_list

    #Calculate the total SNR of all detectors
    calc_snr = np.sqrt(sum(matched_snr_sq))
    del matched_snr_sq

    #out_wav = []
    #for i, dat in enumerate(total_white):
    #print("Length of total_white[{}]: {}".format(i, len(dat)))
    #for i in range(len(total_white[0])):
    ##print("Length of {}: {}".format(i, len(total_white[i])))
    #tmp = []
    #for j, dat in enumerate(total_white):
    #sys.stdout.write("\ri = {}| j = {}  ".format(i, j))
    #sys.stdout.flush()
    #tmp.append(dat[i])
    #sys.stdout.write('\n')
    #sys.stdout.flush()
    ##print("\n")
    #out_wav.append(tmp)

    out_wav = [[dat[i] for dat in total_white]
               for i in range(len(total_white[0]))]

    #print("Pre return")

    return ((np.array(out_wav), np.array([opt_arg['snr'],
                                          int(gw_present)
                                          ]), np.array(calc_snr),
             np.array(str(kwargs)), np.array(str(opt_arg))))
Exemple #28
0
def from_cli(opt, dyn_range_fac=1, precision='single'):
    """Parses the CLI options related to strain data reading and conditioning.

    Parameters
    ----------
    opt : object
        Result of parsing the CLI with OptionParser, or any object with the
        required attributes  (gps-start-time, gps-end-time, strain-high-pass, 
        pad-data, sample-rate, (frame-cache or frame-files), channel-name, 
        fake-strain, fake-strain-seed, gating_file).

    dyn_range_fac: {float, 1}, optional
        A large constant to reduce the dynamic range of the strain.

    Returns
    -------
    strain : TimeSeries
        The time series containing the conditioned strain data.
    """
    if opt.frame_cache or opt.frame_files:
        if opt.frame_cache:
            frame_source = opt.frame_cache
        if opt.frame_files:
            frame_source = opt.frame_files

        logging.info("Reading Frames")
        strain = read_frame(frame_source, opt.channel_name,
                            start_time=opt.gps_start_time-opt.pad_data,
                            end_time=opt.gps_end_time+opt.pad_data)

        if opt.zpk_z and opt.zpk_p and opt.zpk_k:
            logging.info("Highpass Filtering")
            strain = highpass(strain, frequency=opt.strain_high_pass)

            logging.info("Applying zpk filter")
            z = numpy.array(opt.zpk_z)
            p = numpy.array(opt.zpk_p)
            k = float(opt.zpk_k)
            strain = filter_zpk(strain.astype(numpy.float64), z, p, k)

        if opt.normalize_strain:
            logging.info("Dividing strain by constant")
            l = opt.normalize_strain
            strain = strain / l

        if opt.injection_file:
            logging.info("Applying injections")
            injections = InjectionSet(opt.injection_file)
            injections.apply(strain, opt.channel_name[0:2])

        if opt.sgburst_injection_file:
            logging.info("Applying sine-Gaussian burst injections")
            injections = SGBurstInjectionSet(opt.sgburst_injection_file)
            injections.apply(strain, opt.channel_name[0:2])

        logging.info("Highpass Filtering")
        strain = highpass(strain, frequency=opt.strain_high_pass)

        if precision == 'single':
            logging.info("Converting to float32")
            strain = (strain * dyn_range_fac).astype(float32)

        if opt.gating_file is not None:
            logging.info("Gating glitches")
            gate_params = numpy.loadtxt(opt.gating_file)
            if len(gate_params.shape) == 1:
                gate_params = [gate_params]
            strain = gate_data(
                strain, gate_params,
                data_start_time=(opt.gps_start_time - opt.pad_data))

        logging.info("Resampling data")
        strain = resample_to_delta_t(strain, 1.0/opt.sample_rate, method='ldas')

        logging.info("Highpass Filtering")
        strain = highpass(strain, frequency=opt.strain_high_pass)

        logging.info("Remove Padding")
        start = opt.pad_data*opt.sample_rate
        end = len(strain)-opt.sample_rate*opt.pad_data
        strain = strain[start:end]

    if opt.fake_strain:
        logging.info("Generating Fake Strain")
        duration = opt.gps_end_time - opt.gps_start_time
        tlen = duration * opt.sample_rate
        pdf = 1.0/128
        plen = int(opt.sample_rate / pdf) / 2 + 1

        logging.info("Making PSD for strain")
        strain_psd = psd.from_string(opt.fake_strain, plen,
                                     pdf, opt.low_frequency_cutoff)

        logging.info("Making colored noise")
        strain = pycbc.noise.noise_from_psd(tlen, 1.0/opt.sample_rate,
                                            strain_psd,
                                            seed=opt.fake_strain_seed)
        strain._epoch = lal.LIGOTimeGPS(opt.gps_start_time)

        if opt.injection_file:
            logging.info("Applying injections")
            injections = InjectionSet(opt.injection_file)
            injections.apply(strain, opt.channel_name[0:2])

        if opt.sgburst_injection_file:
            logging.info("Applying sine-Gaussian burst injections")
            injections = SGBurstInjectionSet(opt.sgburst_injection_file)
            injections.apply(strain, opt.channel_name[0:2])

        if precision == 'single':
            logging.info("Converting to float32")
            strain = (dyn_range_fac * strain).astype(float32)

    if opt.injection_file:
        strain.injections = injections

    return strain
Exemple #29
0
def from_cli(opt, dyn_range_fac=1, precision='single'):
    """Parses the CLI options related to strain data reading and conditioning.

    Parameters
    ----------
    opt : object
        Result of parsing the CLI with OptionParser, or any object with the
        required attributes  (gps-start-time, gps-end-time, strain-high-pass, 
        pad-data, sample-rate, (frame-cache or frame-files), channel-name, 
        fake-strain, fake-strain-seed, gating_file).

    dyn_range_fac: {float, 1}, optional
        A large constant to reduce the dynamic range of the strain.

    Returns
    -------
    strain : TimeSeries
        The time series containing the conditioned strain data.
    """

    gating_info = {}

    if opt.frame_cache or opt.frame_files or opt.frame_type:
        if opt.frame_cache:
            frame_source = opt.frame_cache
        if opt.frame_files:
            frame_source = opt.frame_files

        logging.info("Reading Frames")
        
        if opt.frame_type:
            strain = query_and_read_frame(opt.frame_type, opt.channel_name,
                                          start_time=opt.gps_start_time-opt.pad_data,
                                          end_time=opt.gps_end_time+opt.pad_data)
        else:
            strain = read_frame(frame_source, opt.channel_name,
                            start_time=opt.gps_start_time-opt.pad_data,
                            end_time=opt.gps_end_time+opt.pad_data)

        if opt.zpk_z and opt.zpk_p and opt.zpk_k:
            logging.info("Highpass Filtering")
            strain = highpass(strain, frequency=opt.strain_high_pass)

            logging.info("Applying zpk filter")
            z = numpy.array(opt.zpk_z)
            p = numpy.array(opt.zpk_p)
            k = float(opt.zpk_k)
            strain = filter_zpk(strain.astype(numpy.float64), z, p, k)

        if opt.normalize_strain:
            logging.info("Dividing strain by constant")
            l = opt.normalize_strain
            strain = strain / l

        if opt.injection_file:
            logging.info("Applying injections")
            injections = InjectionSet(opt.injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        if opt.sgburst_injection_file:
            logging.info("Applying sine-Gaussian burst injections")
            injections = SGBurstInjectionSet(opt.sgburst_injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        logging.info("Highpass Filtering")
        strain = highpass(strain, frequency=opt.strain_high_pass)

        if precision == 'single':
            logging.info("Converting to float32")
            strain = (strain * dyn_range_fac).astype(pycbc.types.float32)

        if opt.gating_file is not None:
            logging.info("Gating glitches")
            gate_params = numpy.loadtxt(opt.gating_file)
            if len(gate_params.shape) == 1:
                gate_params = [gate_params]
            strain = gate_data(strain, gate_params)
            gating_info['file'] = \
                    [gp for gp in gate_params \
                     if (gp[0] + gp[1] + gp[2] >= strain.start_time) \
                     and (gp[0] - gp[1] - gp[2] <= strain.end_time)]

        if opt.autogating_threshold is not None:
            # the + 0 is for making a copy
            glitch_times = detect_loud_glitches(
                    strain + 0., threshold=opt.autogating_threshold,
                    cluster_window=opt.autogating_cluster,
                    low_freq_cutoff=opt.strain_high_pass,
                    high_freq_cutoff=opt.sample_rate/2,
                    corrupt_time=opt.pad_data+opt.autogating_pad)
            gate_params = [[gt, opt.autogating_width, opt.autogating_taper] \
                           for gt in glitch_times]
            if len(glitch_times) > 0:
                logging.info('Autogating at %s',
                             ', '.join(['%.3f' % gt for gt in glitch_times]))
            strain = gate_data(strain, gate_params)
            gating_info['auto'] = gate_params

        logging.info("Resampling data")
        strain = resample_to_delta_t(strain, 1.0/opt.sample_rate, method='ldas')

        logging.info("Highpass Filtering")
        strain = highpass(strain, frequency=opt.strain_high_pass)

        logging.info("Remove Padding")
        start = opt.pad_data*opt.sample_rate
        end = len(strain)-opt.sample_rate*opt.pad_data
        strain = strain[start:end]

    if opt.fake_strain:
        logging.info("Generating Fake Strain")
        duration = opt.gps_end_time - opt.gps_start_time
        tlen = duration * opt.sample_rate
        pdf = 1.0/128
        plen = int(opt.sample_rate / pdf) / 2 + 1

        logging.info("Making PSD for strain")
        strain_psd = pycbc.psd.from_string(opt.fake_strain, plen, pdf,
                                           opt.low_frequency_cutoff)

        logging.info("Making colored noise")
        strain = pycbc.noise.noise_from_psd(tlen, 1.0/opt.sample_rate,
                                            strain_psd,
                                            seed=opt.fake_strain_seed)
        strain._epoch = lal.LIGOTimeGPS(opt.gps_start_time)

        if opt.injection_file:
            logging.info("Applying injections")
            injections = InjectionSet(opt.injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        if opt.sgburst_injection_file:
            logging.info("Applying sine-Gaussian burst injections")
            injections = SGBurstInjectionSet(opt.sgburst_injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        if precision == 'single':
            logging.info("Converting to float32")
            strain = (dyn_range_fac * strain).astype(pycbc.types.float32)

    if opt.injection_file or opt.sgburst_injection_file:
        strain.injections = injections

    if opt.taper_data:
        logging.info("Tapering data")
        # Use auto-gating stuff for this, a one-sided gate is a taper
        pd_taper_window = opt.taper_data
        gate_params = [(strain.start_time, 0., pd_taper_window)]
        gate_params.append( (strain.end_time, 0.,
                             pd_taper_window) )
        gate_data(strain, gate_params)


    strain.gating_info = gating_info

    return strain
Exemple #30
0
    def mathched_filtering(self,m1,m2,f_highPass = 15,\
                                      fft_crop = 2,\
                                      psd_interval = 4,\
                                      genWave_f_lowerbound = 20,\
                                      snrCrop = 4):
        #done to avoid loading the data every time when used in a loop
        if self.mergerStrain == None:
            #this methode takes in a duration instead of a time interval
            #This automatically pulls strain data centered around the
            #gps time stamp instead of you specifing it yourself.
            self.mergerStrain = self.getMergerStrain()

        merger = Merger(self.event_id)
        '''
        There is an issue for how the strain data is read using this methode
        when being used with the filter.highpass methode

        Need to find a conversion so that a custome time interval can be used
        when selecting a data set
        '''

        #changing from the class wide strain array to a local one at the same
        #time of performing the highpass filtering.
        strain = filter.highpass(self.mergerStrain, f_highPass)
        strain = filter.resample_to_delta_t(strain, 1.0 / 2048)

        #removing discontinuities errors that form at the end due to resampling
        conditioned = strain.crop(fft_crop, fft_crop)
        #crops off the first
        #and last two seconds
        #generating the psd, thats used in the matched filtering methode
        #the psd is used to weight "the frequency components of the
        #potential signal and data by the noise amplitude"
        psd = conditioned.psd(psd_interval)
        #this matches the psd to our conditioned strain data
        psd = pycbc.psd.interpolate(psd, conditioned.delta_f)
        #this generated a 1/psd that is used to further filter the data
        psd = pycbc.psd.inverse_spectrum_truncation(psd,\
                                        psd_interval*conditioned.sample_rate,\
                                        low_frequency_cutoff=f_highPass)

        #Generating matched filtering waveform
        hp, hc = get_td_waveform(approximant="SEOBNRv4_opt",
                                 mass1=m1,
                                 mass2=m2,
                                 delta_t=conditioned.delta_t,
                                 f_lower=genWave_f_lowerbound)

        #Resizing the matched filtering wave form to the size of the our data
        hp.resize(len(conditioned))
        #shifting the moldeled wave form to the aproximant location of the
        #merger event
        template = hp.cyclic_time_shift(hp.start_time)
        #generating the signal to noise ratio data set
        snr = filter.matched_filter(template,
                                    conditioned,
                                    psd=psd,
                                    low_frequency_cutoff=genWave_f_lowerbound)

        #cropping out the problamatic data points. There are discontinuitie
        #errors at the ends of the interval
        snr = snr.crop(snrCrop + psd_interval, snrCrop)
        snrPeakIndex = abs(snr).numpy().argmax()
        snrPeak = abs(snr)[snrPeakIndex]
        snrPeakTime = snr.sample_times[snrPeakIndex]

        # # Shift the template to the peak time
        # dt = snrPeakTime - conditioned.start_time
        # aligned = template.cyclic_time_shift(dt)
        #
        # # scale the template so that it would have SNR 1 in this data
        # aligned /= sigma(aligned, psd=psd, low_frequency_cutoff=20.0)
        #
        # # Scale the template amplitude and phase to the peak value
        # aligned = (aligned.to_frequencyseries() * snrPeak).to_timeseries()
        # aligned.start_time = conditioned.start_time

        # We do it this way so that we can whiten both the template and the data
        white_data = (conditioned.to_frequencyseries() /
                      psd**0.5).to_timeseries()
        white_data = white_data.highpass_fir(f_highPass * 2,
                                             512).lowpass_fir(230, 512)

        # Select the time around the merger
        white_data = white_data.time_slice(merger.time - .1, merger.time + .1)


        outputFormater = namedtuple('signal_to_noise_ratio_data',\
            ['snr','snrPeakIndex','snrPeak','snrPeakTime','white_data'])
        #returning the signal to noise ratio
        return outputFormater(snr, snrPeakIndex, snrPeak, snrPeakTime,
                              white_data)
Exemple #31
0
def calc_filt_psd_variation(strain, segment, short_segment, psd_long_segment,
                            psd_duration, psd_stride, psd_avg_method, low_freq,
                            high_freq):
    """ Calculates time series of PSD variability

    This function first splits the segment up into 512 second chunks. It
    then calculates the PSD over this 512 second. The PSD is used to
    to create a filter that is the composition of three filters:
    1. Bandpass filter between f_low and f_high.
    2. Weighting filter which gives the rough response of a CBC template.
    3. Whitening filter.
    Next it makes the convolution of this filter with the stretch of data.
    This new time series is given to the "mean_square" function, which
    computes the mean square of the timeseries within an 8 seconds window,
    once per second.
    The result, which is the variance of the S/N in that stride for the
    Parseval theorem, is then stored in a timeseries.

    Parameters
    ----------
    strain : TimeSeries
        Input strain time series to estimate PSDs
    segment : {float, 8}
        Duration of the segments for the mean square estimation in seconds.
    short_segment : {float, 0.25}
        Duration of the short segments for the outliers removal.
    psd_long_segment : {float, 512}
        Duration of the long segments for PSD estimation in seconds.
    psd_duration : {float, 8}
        Duration of FFT segments for long term PSD estimation, in seconds.
    psd_stride : {float, 4}
        Separation between FFT segments for long term PSD estimation, in
        seconds.
    psd_avg_method : {string, 'median'}
        Method for averaging PSD estimation segments.
    low_freq : {float, 20}
        Minimum frequency to consider the comparison between PSDs.
    high_freq : {float, 480}
        Maximum frequency to consider the comparison between PSDs.

    Returns
    -------
    psd_var : TimeSeries
        Time series of the variability in the PSD estimation
    """
    # Calculate strain precision
    if strain.precision == 'single':
        fs_dtype = numpy.float32
    elif strain.precision == 'double':
        fs_dtype = numpy.float64

    # Convert start and end times immediately to floats
    start_time = numpy.float(strain.start_time)
    end_time = numpy.float(strain.end_time)

    # Resample the data
    strain = resample_to_delta_t(strain, 1.0 / 2048)
    srate = int(strain.sample_rate)

    # Fix the step for the PSD estimation and the time to remove at the
    # edge of the time series.
    step = 1.0
    strain_crop = 8.0

    # Find the times of the long segments
    times_long = numpy.arange(
        start_time, end_time,
        psd_long_segment - 2 * strain_crop - segment + step)

    # Set up the empty time series for the PSD variation estimate
    ts_duration = end_time - start_time - 2 * strain_crop - segment + 1
    psd_var = TimeSeries(zeros(int(numpy.floor(ts_duration / step))),
                         delta_t=step,
                         copy=False,
                         epoch=start_time + strain_crop + segment)

    # Create a bandpass filter between low_freq and high_freq
    filt = sig.firwin(4 * srate, [low_freq, high_freq],
                      pass_zero=False,
                      window='hann',
                      nyq=srate / 2)
    filt.resize(int(psd_duration * srate))
    # Fourier transform the filter and take the absolute value to get
    # rid of the phase. Save the filter as a frequency series.
    filt = abs(rfft(filt))
    my_filter = FrequencySeries(filt,
                                delta_f=1. / psd_duration,
                                dtype=fs_dtype)

    ind = 0
    for tlong in times_long:
        # Calculate PSD for long segment
        if tlong + psd_long_segment <= float(end_time):
            astrain = strain.time_slice(tlong, tlong + psd_long_segment)
            plong = pycbc.psd.welch(
                astrain,
                seg_len=int(psd_duration * strain.sample_rate),
                seg_stride=int(psd_stride * strain.sample_rate),
                avg_method=psd_avg_method)
        else:
            astrain = strain.time_slice(tlong, end_time)
            plong = pycbc.psd.welch(
                strain.time_slice(end_time - psd_long_segment, end_time),
                seg_len=int(psd_duration * strain.sample_rate),
                seg_stride=int(psd_stride * strain.sample_rate),
                avg_method=psd_avg_method)

        # Make the weighting filter - bandpass, which weight by f^-7/6,
        # and whiten. The normalization is chosen so that the variance
        # will be one if this filter is applied to white noise which
        # already has a variance of one.
        freqs = FrequencySeries(plong.sample_frequencies,
                                delta_f=plong.delta_f,
                                epoch=plong.epoch,
                                dtype=fs_dtype)
        fweight = freqs**(-7. / 6.) * my_filter / numpy.sqrt(plong)
        fweight[0] = 0.
        norm = (sum(abs(fweight)**2) / (len(fweight) - 1.))**-0.5
        fweight = norm * fweight
        fwhiten = numpy.sqrt(2. / srate) / numpy.sqrt(plong)
        fwhiten[0] = 0.
        full_filt = sig.hann(int(psd_duration * srate)) * numpy.roll(
            irfft(fwhiten * fweight),
            int(psd_duration / 2) * srate)
        # Convolve the filter with long segment of data
        wstrain = TimeSeries(sig.fftconvolve(astrain, full_filt, mode='same'),
                             delta_t=strain.delta_t,
                             epoch=astrain.start_time)
        wstrain = wstrain[int(strain_crop * srate):-int(strain_crop * srate)]
        # compute the mean square of the chunk of data
        delta_t = wstrain.end_time.gpsSeconds - wstrain.start_time.gpsSeconds
        variation = mean_square(wstrain, delta_t, short_segment, segment)

        # Store variation value
        for i, val in enumerate(variation):
            psd_var[ind + i] = val

        ind = ind + len(variation)
    return psd_var
Exemple #32
0
# Check the psd norm
print "PYCBC psd norm", 2.0 / float(sample_rate) / (
    w_pycbc.squared_norm().sum()) / pycbc.psd.median_bias(nsegs)

# Same strain preparation for lal and pycbc psd estimation
strain = read_frame("LER2.lcf",
                    "H1:LDAS-STRAIN",
                    start_time=start_time - pad_data,
                    duration=duration + pad_data * 2)
strain = highpass(strain, frequency=30)
strain *= pycbc.DYN_RANGE_FAC
strain = TimeSeries(strain,
                    delta_t=strain.delta_t,
                    epoch=strain.start_time,
                    dtype=float32)
strain = resample_to_delta_t(strain, 1.0 / sample_rate)
strain = strain[int(pad_data * 1.0 / strain.delta_t):len(strain) -
                int(1.0 / strain.delta_t * pad_data)]

psd_pycbc = pycbc.psd.welch(strain, seg_len=seg_len, seg_stride=stride)
psd_lal = lal_psd_estimate(seg_len, strain)

print psd_pycbc[-1]
print psd_lal[-1]

pylab.figure(1)
pylab.loglog(psd_pycbc.sample_frequencies.numpy(),
             psd_pycbc.numpy(),
             label="PyCBC")
pylab.loglog(psd_lal.sample_frequencies.numpy(), psd_lal.numpy(), label="LAL")
pylab.xlabel("Frequency (Hz)")
Exemple #33
0

dt = np.loadtxt('And1815fr1kpc_equ.txt', usecols=(0))

hp = np.loadtxt('And1815fr1kpc_equ.txt', usecols=(1))
hc = np.loadtxt('And1815fr1kpc_equ.txt', usecols=(2))
h = hc + hp

fs = 8192

pylab.figure('Original')
pylab.plot(dt, h)
pylab.grid()
pylab.show()

template = types.TimeSeries(initial_array=h, delta_t=1.0 / 2048, epoch=0)

pylab.figure('Original')
pylab.plot(template.sample_times, template)
pylab.grid()
pylab.show()

template = resample_to_delta_t(template, 1.0 / 1024)

pylab.figure('Original')
pylab.plot(template.sample_times, template)
pylab.grid()
pylab.show()

interpolacion(h, dt, 1, fs)
    def _gen_slices(self, index_range):
        num_channels = 7
        num_detectors = 2
        X = [
            np.zeros((len(index_range), len(self.ts) * num_channels,
                      self.final_data_samples)) for i in range(2)
        ]
        X_ret = [
            np.zeros((len(self.ts), self.final_data_samples, len(index_range)))
            for i in range(num_detectors * num_channels)
        ]

        for in_batch, idx in enumerate(index_range):
            for detector in range(len(self.ts)):
                low, up = idx
                #Using whiten_data works
                white_full_signal = whiten_data_new(self.ts[detector][low:up])
                #white_full_signal = self.ts[detector][low:up].whiten(4, 4)
                max_idx = len(white_full_signal)
                min_idx = max_idx - int(
                    float(self.num_samples) / float(self.resample_rates[0]) /
                    self.dt)
                for i, sr in enumerate(self.resample_rates):
                    X[0][in_batch][i * len(self.ts) +
                                   detector] = resample_to_delta_t(
                                       white_full_signal[min_idx:max_idx],
                                       1.0 / sr)
                    if not i + 1 == len(self.resample_rates):
                        t_dur = float(self.num_samples) / float(
                            self.resample_rates[i + 1])
                        sample_dur = int(t_dur / self.dt)
                        max_idx = min_idx
                        min_idx -= sample_dur
                #print("{}: in_batch: {}".format(detector, in_batch))

        X[0] = X[0].transpose(1, 2, 0)
        X[1] = X[1].transpose(1, 2, 0)

        X_ret[0][0] = X[0][0]
        X_ret[0][1] = X[0][1]
        X_ret[1][0] = X[1][0]
        X_ret[1][1] = X[1][1]
        X_ret[2][0] = X[0][2]
        X_ret[2][1] = X[0][3]
        X_ret[3][0] = X[1][2]
        X_ret[3][1] = X[1][3]
        X_ret[4][0] = X[0][4]
        X_ret[4][1] = X[0][5]
        X_ret[5][0] = X[1][4]
        X_ret[5][1] = X[1][5]
        X_ret[6][0] = X[0][6]
        X_ret[6][1] = X[0][7]
        X_ret[7][0] = X[1][6]
        X_ret[7][1] = X[1][7]
        X_ret[8][0] = X[0][8]
        X_ret[8][1] = X[0][9]
        X_ret[9][0] = X[1][8]
        X_ret[9][1] = X[1][9]
        X_ret[10][0] = X[0][10]
        X_ret[10][1] = X[0][11]
        X_ret[11][0] = X[1][10]
        X_ret[11][1] = X[1][11]
        X_ret[12][0] = X[0][12]
        X_ret[12][1] = X[0][13]
        X_ret[13][0] = X[1][12]
        X_ret[13][1] = X[1][13]

        return ([x.transpose(2, 1, 0) for x in X_ret])
Exemple #35
0
def detect_loud_glitches(strain, psd_duration=4., psd_stride=2.,
                         psd_avg_method='median', low_freq_cutoff=30.,
                         threshold=50., cluster_window=5., corrupted_time=4.,
                         high_freq_cutoff=None, output_intermediates=False):
    """Automatic identification of loud transients for gating purposes."""

    logging.info('Autogating: tapering strain')
    fade_size = corrupted_time * strain.sample_rate
    w = numpy.arange(fade_size) / float(fade_size)
    strain[0:fade_size] *= Array(w, dtype=strain.dtype)
    strain[(len(strain)-fade_size):] *= Array(w[::-1], dtype=strain.dtype)

    if output_intermediates:
        strain.save_to_wav('strain_conditioned.wav')

    # don't waste time trying to optimize a single FFT
    pycbc.fft.fftw.set_measure_level(0)

    logging.info('Autogating: estimating PSD')
    psd = pycbc.psd.welch(strain, seg_len=int(psd_duration*strain.sample_rate),
                          seg_stride=int(psd_stride*strain.sample_rate),
                          avg_method=psd_avg_method)
    psd = pycbc.psd.interpolate(psd, 1./strain.duration)
    psd = pycbc.psd.inverse_spectrum_truncation(
            psd, int(psd_duration * strain.sample_rate),
            low_frequency_cutoff=low_freq_cutoff,
            trunc_method='hann')

    logging.info('Autogating: time -> frequency')
    strain_tilde = FrequencySeries(numpy.zeros(len(strain) / 2 + 1),
                                   delta_f=psd.delta_f,
                                   dtype=complex_same_precision_as(strain))
    pycbc.fft.fft(strain, strain_tilde)

    logging.info('Autogating: whitening')
    if high_freq_cutoff:
        kmax = int(high_freq_cutoff / strain_tilde.delta_f)
        strain_tilde[kmax:] = 0.
        norm = high_freq_cutoff - low_freq_cutoff
    else:
        norm = strain.sample_rate/2. - low_freq_cutoff
    strain_tilde /= (psd * norm) ** 0.5
    kmin = int(low_freq_cutoff / strain_tilde.delta_f)
    strain_tilde[0:kmin] = 0.

    # FIXME downsample here rather than post-FFT

    logging.info('Autogating: frequency -> time')
    pycbc.fft.ifft(strain_tilde, strain)

    pycbc.fft.fftw.set_measure_level(pycbc.fft.fftw._default_measurelvl)

    if high_freq_cutoff:
        strain = resample_to_delta_t(strain, 0.5 / high_freq_cutoff,
                                     method='ldas')

    logging.info('Autogating: stdev of whitened strain is %.4f', numpy.std(strain))

    if output_intermediates:
        strain.save_to_wav('strain_whitened.wav')

    mag = abs(strain)
    if output_intermediates:
        mag.save('strain_whitened_mag.npy')
    mag = numpy.array(mag, dtype=numpy.float32)

    # remove strain corrupted by filters at the ends
    corrupted_idx = int(corrupted_time * strain.sample_rate)
    mag[0:corrupted_idx] = 0
    mag[-1:-corrupted_idx-1:-1] = 0

    logging.info('Autogating: finding loud peaks')
    indices = numpy.where(mag > threshold)[0]
    cluster_idx = pycbc.events.findchirp_cluster_over_window(
            indices, mag[indices], int(cluster_window*strain.sample_rate))
    times = [idx * strain.delta_t + strain.start_time \
             for idx in indices[cluster_idx]]
    return times
Exemple #36
0
def from_cli(opt, dyn_range_fac=1, precision='single'):
    """Parses the CLI options related to strain data reading and conditioning.

    Parameters
    ----------
    opt : object
        Result of parsing the CLI with OptionParser, or any object with the
        required attributes  (gps-start-time, gps-end-time, strain-high-pass, 
        pad-data, sample-rate, (frame-cache or frame-files), channel-name, 
        fake-strain, fake-strain-seed, gating_file).

    dyn_range_fac: {float, 1}, optional
        A large constant to reduce the dynamic range of the strain.

    Returns
    -------
    strain : TimeSeries
        The time series containing the conditioned strain data.
    """

    gating_info = {}

    if opt.frame_cache or opt.frame_files or opt.frame_type:
        if opt.frame_cache:
            frame_source = opt.frame_cache
        if opt.frame_files:
            frame_source = opt.frame_files

        logging.info("Reading Frames")
        
        if opt.frame_type:
            strain = query_and_read_frame(opt.frame_type, opt.channel_name,
                                          start_time=opt.gps_start_time-opt.pad_data,
                                          end_time=opt.gps_end_time+opt.pad_data)
        else:
            strain = read_frame(frame_source, opt.channel_name,
                            start_time=opt.gps_start_time-opt.pad_data,
                            end_time=opt.gps_end_time+opt.pad_data)

        if opt.zpk_z and opt.zpk_p and opt.zpk_k:
            logging.info("Highpass Filtering")
            strain = highpass(strain, frequency=opt.strain_high_pass)

            logging.info("Applying zpk filter")
            z = numpy.array(opt.zpk_z)
            p = numpy.array(opt.zpk_p)
            k = float(opt.zpk_k)
            strain = filter_zpk(strain.astype(numpy.float64), z, p, k)

        if opt.normalize_strain:
            logging.info("Dividing strain by constant")
            l = opt.normalize_strain
            strain = strain / l

        if opt.injection_file:
            logging.info("Applying injections")
            injections = InjectionSet(opt.injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        if opt.sgburst_injection_file:
            logging.info("Applying sine-Gaussian burst injections")
            injections = SGBurstInjectionSet(opt.sgburst_injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        logging.info("Highpass Filtering")
        strain = highpass(strain, frequency=opt.strain_high_pass)

        if precision == 'single':
            logging.info("Converting to float32")
            strain = (strain * dyn_range_fac).astype(float32)

        if opt.gating_file is not None:
            logging.info("Gating glitches")
            gate_params = numpy.loadtxt(opt.gating_file)
            if len(gate_params.shape) == 1:
                gate_params = [gate_params]
            strain = gate_data(strain, gate_params)
            gating_info['file'] = \
                    [gp for gp in gate_params \
                     if (gp[0] + gp[1] + gp[2] >= strain.start_time) \
                     and (gp[0] - gp[1] - gp[2] <= strain.end_time)]

        if opt.autogating_threshold is not None:
            # the + 0 is for making a copy
            glitch_times = detect_loud_glitches(
                    strain + 0., threshold=opt.autogating_threshold,
                    cluster_window=opt.autogating_cluster,
                    low_freq_cutoff=opt.strain_high_pass,
                    high_freq_cutoff=opt.sample_rate/2,
                    corrupted_time=opt.pad_data+opt.autogating_pad)
            gate_params = [[gt, opt.autogating_width, opt.autogating_taper] \
                           for gt in glitch_times]
            if len(glitch_times) > 0:
                logging.info('Autogating at %s',
                             ', '.join(['%.3f' % gt for gt in glitch_times]))
            strain = gate_data(strain, gate_params)
            gating_info['auto'] = gate_params

        logging.info("Resampling data")
        strain = resample_to_delta_t(strain, 1.0/opt.sample_rate, method='ldas')

        logging.info("Highpass Filtering")
        strain = highpass(strain, frequency=opt.strain_high_pass)

        logging.info("Remove Padding")
        start = opt.pad_data*opt.sample_rate
        end = len(strain)-opt.sample_rate*opt.pad_data
        strain = strain[start:end]

    if opt.fake_strain:
        logging.info("Generating Fake Strain")
        duration = opt.gps_end_time - opt.gps_start_time
        tlen = duration * opt.sample_rate
        pdf = 1.0/128
        plen = int(opt.sample_rate / pdf) / 2 + 1

        logging.info("Making PSD for strain")
        strain_psd = pycbc.psd.from_string(opt.fake_strain, plen, pdf,
                                           opt.low_frequency_cutoff)

        logging.info("Making colored noise")
        strain = pycbc.noise.noise_from_psd(tlen, 1.0/opt.sample_rate,
                                            strain_psd,
                                            seed=opt.fake_strain_seed)
        strain._epoch = lal.LIGOTimeGPS(opt.gps_start_time)

        if opt.injection_file:
            logging.info("Applying injections")
            injections = InjectionSet(opt.injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        if opt.sgburst_injection_file:
            logging.info("Applying sine-Gaussian burst injections")
            injections = SGBurstInjectionSet(opt.sgburst_injection_file)
            injections.apply(strain, opt.channel_name[0:2],
                             distance_scale=opt.injection_scale_factor)

        if precision == 'single':
            logging.info("Converting to float32")
            strain = (dyn_range_fac * strain).astype(float32)

    if opt.injection_file or opt.sgburst_injection_file:
        strain.injections = injections

    strain.gating_info = gating_info

    return strain