def noise_worker(parameters):
    #Name parameters
    t_len = parameters[0]
    f_lower = parameters[1]
    dt = parameters[2]
    num_detectors = parameters[3]
    seed = parameters[4]
    sample_rates = parameters[5]

    DF = 1.0 / t_len
    F_LEN = int(2.0 / (DF * dt))

    #Generate PSD
    psd = aLIGOZeroDetHighPower(length=F_LEN,
                                delta_f=DF,
                                low_freq_cutoff=f_lower)

    T_LEN = int(t_len / dt)

    #Generate noise
    strain_list = [
        noise_from_psd(length=T_LEN, delta_t=dt, psd=psd, seed=seed[i])
        for i in range(num_detectors)
    ]

    #Whiten noise
    strain_list = whiten_data_new(strain_list,
                                  psd=None,
                                  low_freq_cutoff=f_lower)

    #ret = resample_data(strain_list, sample_rates)

    return (resample_data(strain_list, sample_rates))
Exemple #2
0
    def create_data(
        self, detector, starttime, duration, deltat, psd, asd=None, flow=20.0
    ):
        """
        Create a fake time series of data for a given detector based on a given
        power spectral density.

        Parameters
        ----------
        detector: str
            A string giving the detector name.
        starttime: float
            A start time for the simulated data in GPS seconds.
        duration: float
            The duration of the data in seconds.
        deltat: float
            The time step for the time series.
        psd: FrequencySeries, callable, str, float
            A value setting the PSD from which to generate the noise. PyCBC PSD
            functions or FrequencySeries objects can be passed, or strings giving
            files containing PSDs or valid LALSimulation aliases for analytical
            PSDs can be used, or a single float can be used to have a uniform PSD
            as a function of frequency.
        asd: str, float
            If you have a file containing an ASD rather than a PSD, or want to pass
            a single ASD value, use this rather than the psd option.
        flow: float
            The low frequency cut-off of the PSD (Hz). Defaults to 20 Hz.
        """

        self.detector = detector

        # set data information for use when creating PSD
        self.flow = flow
        self.duration = duration
        self.deltat = deltat
        self.nsamples = int(self.duration // self.deltat)

        self.samplerate = 1.0 / self.deltat
        self.nyquist = self.samplerate / 2.0
        self.deltaf = self.samplerate / self.nsamples

        if asd is None:
            self.is_asd_file = False
            self.psd = psd
        else:
            # using file containing ASD rather than PSD
            self.is_asd_file = True
            self.psd = asd

        # create the noise time series
        self.data = noise_from_psd(self.nsamples, self.deltat, self.psd)
        self.data.start_time = starttime
Exemple #3
0
def generate_sample(static_arguments,
                    event_tuple,
                    waveform_params=None):
    """
    Generate a single sample (or example) by taking a piece of LIGO
    background noise (real or synthetic, depending on `event_tuple`),
    optionally injecting a simulated waveform (depending on
    `waveform_params`) and post-processing the result (whitening,
    band-passing).
    
    Args:
        static_arguments (dict): A dictionary containing global
            technical parameters for the sample generation, for example
            the target_sampling_rate of the output.
        event_tuple (tuple): A tuple `(event_time, file_path)`, which
            specifies the GPS time at which to make an injection and
            the path of the HDF file which contains said GPS time.
            If `file_path` is `None`, synthetic noise will be used
            instead and the `event_time` only serves as a seed for
            the corresponding (random) noise generator.
        waveform_params (dict): A dictionary containing the randomly
            sampled parameters that are passed as inputs to the
            waveform model (e.g., the masses, spins, position, ...).

    Returns:
        A tuple `(sample, injection_parameters)`, which contains the
        generated `sample` itself (a dict with keys `{'event_time',
        'h1_strain', 'l1_strain'}`), and the `injection_parameters`,
        which are either `None` (in case no injection was made), or a
        dict containing the `waveform_params` and some additional
        parameters (e.g., single detector SNRs).
    """

    # -------------------------------------------------------------------------
    # Define shortcuts for some elements of self.static_arguments
    # -------------------------------------------------------------------------

    # Read out frequency-related arguments
    original_sampling_rate = static_arguments['original_sampling_rate']
    target_sampling_rate = static_arguments['target_sampling_rate']
    f_lower = static_arguments['f_lower']
    delta_f = static_arguments['delta_f']
    fd_length = static_arguments['fd_length']

    # Get the width of the noise sample that we either select from the raw
    # HDF files, or generate synthetically
    noise_interval_width = static_arguments['noise_interval_width']

    # Get how many seconds before and after the event time to use
    seconds_before_event = static_arguments['seconds_before_event']
    seconds_after_event = static_arguments['seconds_after_event']

    # Get the event time and the dict containing the HDF file path
    event_time, hdf_file_paths = event_tuple

    # -------------------------------------------------------------------------
    # Get the background noise (either from data or synthetically)
    # -------------------------------------------------------------------------

    # If the event_time is None, we generate synthetic noise
    if hdf_file_paths is None:

        # Create an artificial PSD for the noise
        # TODO: Is this the best choice for this task?
        psd = aLIGOZeroDetHighPower(length=fd_length,
                                    delta_f=delta_f,
                                    low_freq_cutoff=f_lower)

        # Actually generate the noise using the PSD and LALSimulation
        noise = dict()
        for i, det in enumerate(('H1', 'L1')):

            # Compute the length of the noise sample in time steps
            noise_length = noise_interval_width * target_sampling_rate

            # Generate the noise for this detector
            noise[det] = noise_from_psd(length=noise_length,
                                        delta_t=(1.0 / target_sampling_rate),
                                        psd=psd,
                                        seed=(2 * event_time + i))

            # Manually fix the noise start time to match the fake event time.
            # However, for some reason, the correct setter method seems broken?
            start_time = event_time - noise_interval_width / 2
            # noinspection PyProtectedMember
            noise[det]._epoch = LIGOTimeGPS(start_time)

    # Otherwise we select the noise from the corresponding HDF file
    else:

        kwargs = dict(hdf_file_paths=hdf_file_paths,
                      gps_time=event_time,
                      interval_width=noise_interval_width,
                      original_sampling_rate=original_sampling_rate,
                      target_sampling_rate=target_sampling_rate,
                      as_pycbc_timeseries=True)
        noise = get_strain_from_hdf_file(**kwargs)

    # -------------------------------------------------------------------------
    # If applicable, make an injection
    # -------------------------------------------------------------------------

    # If no waveform parameters are given, we are not making an injection.
    # In this case, there are no detector signals and no injection
    # parameters, and the strain is simply equal to the noise
    if waveform_params is None:
        detector_signals = None
        output_signals = None
        injection_parameters = None
        output_signals = None
        strain = noise

    # Otherwise, we need to simulate a waveform for the given waveform_params
    # and add it into the noise to create the strain
    else:

        # ---------------------------------------------------------------------
        # Simulate the waveform with the given injection parameters
        # ---------------------------------------------------------------------

        # Actually simulate the waveform with these parameters
        waveform = get_waveform(static_arguments=static_arguments,
                                waveform_params=waveform_params)

        # Get the detector signals by projecting on the antenna patterns
        detector_signals = \
            get_detector_signals(static_arguments=static_arguments,
                                 waveform_params=waveform_params,
                                 event_time=event_time,
                                 waveform=waveform)
        # Store the output_signal
        output_signals = {}
        output_signals = detector_signals.copy()

        # ---------------------------------------------------------------------
        # Add the waveform into the noise as is to calculate the NOMF-SNR
        # ---------------------------------------------------------------------

        # Store the dummy strain, the PSDs and the SNRs for the two detectors
        strain_ = {}
        psds = {}
        snrs = {}

        # Calculate these quantities for both detectors
        for det in ('H1', 'L1'):

            # Add the simulated waveform into the noise to get the dummy strain
            strain_[det] = noise[det].add_into(detector_signals[det])

            # Estimate the Power Spectral Density from the dummy strain, this 1 is the psd segment duration of the strain
            psds[det] = strain_[det].psd(1)
            psds[det] = interpolate(psds[det], delta_f=delta_f)

            # Use the PSD estimate to calculate the optimal matched
            # filtering SNR for this injection and this detector
            snrs[det] = sigma(htilde=detector_signals[det],
                              psd=psds[det],
                              low_frequency_cutoff=f_lower)

        # Calculate the network optimal matched filtering SNR for this
        # injection (which we need for scaling to the chosen injection SNR)
        nomf_snr = np.sqrt(snrs['H1']**2 + snrs['L1']**2)

        # ---------------------------------------------------------------------
        # Add the waveform into the noise with the chosen injection SNR
        # ---------------------------------------------------------------------

        # Compute the rescaling factor
        #injection_snr = waveform_params['injection_snr']
        injection_snr = static_arguments['injection_snr']
        scale_factor = 1.0 * injection_snr / nomf_snr

        strain = {}
        for det in ('H1', 'L1'):

            # Add the simulated waveform into the noise, using a scaling
            # factor to ensure that the resulting NOMF-SNR equals the chosen
            # injection SNR
            strain[det] = noise[det].add_into(scale_factor *
                                              detector_signals[det])
            output_signals[det] = scale_factor * output_signals[det]

        # ---------------------------------------------------------------------
        # Store some information about the injection we just made
        # ---------------------------------------------------------------------

        # Store the information we have computed ourselves
        injection_parameters = {'scale_factor': scale_factor,
                                'h1_snr': snrs['H1'],
                                'l1_snr': snrs['L1']}

        # Also add the waveform parameters we have sampled
        for key, value in waveform_params.iteritems():
            injection_parameters[key] = value

    # -------------------------------------------------------------------------
    # Whiten and bandpass the strain (also for noise-only samples)
    # -------------------------------------------------------------------------

    for det in ('H1', 'L1'):

        # Get the whitening parameters
        segment_duration = static_arguments['whitening_segment_duration']
        max_filter_duration = static_arguments['whitening_max_filter_duration']

        # Whiten the strain (using the built-in whitening of PyCBC)
        # We don't need to remove the corrupted samples here, because we
        # crop the strain later on
        strain[det] = \
            strain[det].whiten(segment_duration=segment_duration,
                               max_filter_duration=max_filter_duration,
                               remove_corrupted=False)
        
        if waveform_params is not None:
            output_signals[det] = \
                signal_whiten(psd = psds[det], 
                              signal = output_signals[det], 
                              segment_duration = segment_duration, 
                              max_filter_duration = max_filter_duration)
    
        # Get the limits for the bandpass
        bandpass_lower = static_arguments['bandpass_lower']
        bandpass_upper = static_arguments['bandpass_upper']

        # Apply a high-pass to remove everything below `bandpass_lower`;
        # If bandpass_lower = 0, do not apply any high-pass filter.
        if bandpass_lower != 0:
            strain[det] = strain[det].highpass_fir(frequency=bandpass_lower,
                                                   remove_corrupted=False,
                                                   order=512)
            if waveform_params is not None:
                output_signals[det] = output_signals[det].highpass_fir(frequency=bandpass_lower,
                                                       remove_corrupted=False,
                                                       order=512)

        # Apply a low-pass filter to remove everything above `bandpass_upper`.
        # If bandpass_upper = sampling rate, do not apply any low-pass filter.
        if bandpass_upper != target_sampling_rate:
            strain[det] = strain[det].lowpass_fir(frequency=bandpass_upper,
                                                  remove_corrupted=False,
                                                  order=512)
            if waveform_params is not None:
                output_signals[det] = output_signals[det].lowpass_fir(frequency=bandpass_upper,
                                                      remove_corrupted=False,
                                                      order=512)

    # -------------------------------------------------------------------------
    # Cut strain (and signal) time series to the pre-specified length
    # -------------------------------------------------------------------------

    for det in ('H1', 'L1'):

        # Define some shortcuts for slicing
        a = event_time - seconds_before_event
        b = event_time + seconds_after_event

        # Cut the strain to the desired length
        strain[det] = strain[det].time_slice(a, b)

        # If we've made an injection, also cut the simulated signal
        if waveform_params is not None:

            # Cut the detector signals to the specified length
            detector_signals[det] = detector_signals[det].time_slice(a, b)
            output_signals[det] = output_signals[det].time_slice(a, b)

            # Also add the detector signals to the injection parameters
            injection_parameters['h1_signal'] = \
                np.array(detector_signals['H1'])
            injection_parameters['l1_signal'] = \
                np.array(detector_signals['L1'])
            
            injection_parameters['h1_output_signal'] = \
                np.array(output_signals['H1'])
            injection_parameters['l1_output_signal'] = \
                np.array(output_signals['L1'])

    # -------------------------------------------------------------------------
    # Collect all available information about this sample and return results
    # -------------------------------------------------------------------------

    # The whitened strain is numerically on the order of O(1), so we can save
    # it as a 32-bit float (unlike the original signal, which is down to
    # O(10^-{30}) and thus requires 64-bit floats).
    sample = {'event_time': event_time,
              'h1_strain': np.array(strain['H1']).astype(np.float32),
              'l1_strain': np.array(strain['L1']).astype(np.float32)}

    return sample, injection_parameters
Exemple #4
0
def Noise(flow,delta_f,delta_t):
	flen = int(2048/delta_f) + 1
	p_s_d = psd.aLIGOZeroDetHighPower(flen, delta_f, flow)
	tlen = int(2048/delta_t)
	return noise.noise_from_psd(tlen, delta_t, p_s_d, seed=127)
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 #6
0
def Noise(flow, delta_f, delta_t, tlen):
    flen = int(1.0 / delta_t / delta_f) / 2 + 1
    p_s_d = psd.aLIGOZeroDetHighPower(flen, delta_f, flow)
    Nt = int(tlen / delta_t)
    return noise.noise_from_psd(Nt, delta_t, p_s_d, seed=127)