How to use the stingray.Powerspectrum function in stingray

To help you get started, we’ve selected a few stingray examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github StingraySoftware / dave / src / main / python / utils / dave_engine.py View on Github external
if (evt_list.time[evt_list.ncounts - 1] - evt_list.time[0]) >= dt:

                                        lc = evt_list.to_lc(dt)
                                        if lc and np.sqrt(lc.meancounts * lc.meancounts) > 0:

                                            gti = base_gti
                                            if not gti:
                                                gti = lc.gti

                                            if segm_size > lc.tseg:
                                                segm_size = lc.tseg
                                                logging.warn("get_rms_spectrum: range: " + str(energy_low) + " to " + str(energy_high) + ", segmsize bigger than lc.duration, lc.duration applied instead.")

                                            pds = None
                                            if pds_type == 'Sng':
                                                pds = Powerspectrum(lc, norm=norm, gti=gti)
                                            else:
                                                pds = AveragedPowerspectrum(lc=lc, segment_size=segm_size, norm=norm, gti=gti)

                                            if pds:

                                                if df > 0:
                                                    pds = pds.rebin(df=df)

                                                #amp, x0, fwhm, white_noise_offset = ModelHelper.fit_data_with_lorentz_and_const(pds.freq, pds.power)
                                                #logging.info("get_rms_spectrum: amp: " + str(amp) + ", x0: " + str(x0) + ", fwhm: " + str(fwhm) + ", white_noise: " + str(white_noise))

                                                if freq_range[0] < 0:
                                                    freq_low = min(pds.freq)
                                                else:
                                                    freq_low = freq_range[0]
github StingraySoftware / dave / src / main / python / utils / dave_engine.py View on Github external
evt_list = EventList(filtered_event_list[:,0], pi=filtered_event_list[:,1])
                                if evt_list and evt_list.ncounts > 1:

                                    if (evt_list.time[evt_list.ncounts - 1] - evt_list.time[0]) >= dt:

                                        lc = evt_list.to_lc(dt)
                                        if lc and np.sqrt(lc.meancounts * lc.meancounts) > 0:

                                            rms, rms_err = 0, 0

                                            gti = base_gti
                                            if not gti:
                                                gti = lc.gti

                                            pds = Powerspectrum(lc, norm='frac', gti=gti)
                                            if pds:

                                                if df > 0:
                                                    pds = pds.rebin(df=df)

                                                if len(pds.freq):
                                                    if freq_range[0] < 0:
                                                        freq_low = min(pds.freq)
                                                    else:
                                                        freq_low = freq_range[0]

                                                    if freq_min_max[0] >= 0:
                                                        freq_min_max[0] = min([freq_min_max[0], freq_low])
                                                    else:
                                                        freq_min_max[0] = freq_low
github StingraySoftware / dave / src / main / python / utils / dave_engine.py View on Github external
if evt_list and evt_list.ncounts > 1:
        if (evt_list.time[evt_list.ncounts - 1] - evt_list.time[0]) >= dt:

            lc = evt_list.to_lc(dt)
            if lc and np.sqrt(lc.meancounts * lc.meancounts) > 0:

                if not gti:
                    gti = lc.gti

                if segm_size > lc.tseg:
                    segm_size = lc.tseg
                    logging.warn("get_white_noise_offset: segmsize bigger than lc.duration, lc.duration applied instead.")

                pds = None
                if pds_type == 'Sng':
                    pds = Powerspectrum(lc, norm='leahy', gti=gti)
                else:
                    pds = AveragedPowerspectrum(lc=lc, segment_size=segm_size, norm='leahy', gti=gti)

                if pds:

                    if df > 0:
                        pds = pds.rebin(df=df)

                    num_tries = 0
                    while white_noise_offset <= 0.0 and num_tries < 5:
                        amp, x0, fwhm, wno = ModelHelper.fit_data_with_lorentz_and_const(pds.freq, pds.power)
                        white_noise_offset = wno
                        num_tries += 1

    return white_noise_offset
github StingraySoftware / dave / src / main / python / utils / dave_engine.py View on Github external
lc = get_lightcurve_any_dataset(src_destination, bck_destination, gti_destination, filters, dt)
    if not lc:
        logging.warn("Can't create lightcurve or is empty")
        return None, None, None

    # Prepares GTI if passed
    gti = load_gti_from_destination (gti_destination)
    if not gti:
        logging.debug("External GTIs not loaded using defaults")
        gti = lc.gti

    # Creates the power density spectrum
    logging.debug("Create power density spectrum")

    if pds_type == 'Sng':
        pds = Powerspectrum(lc, norm=norm, gti=gti)
    else:
        pds = AveragedPowerspectrum(lc=lc, segment_size=segm_size, norm=norm, gti=gti)

    if pds:
        if df > 0:
            pds = pds.rebin(df=df)
        #pds = rebin_spectrum_if_necessary(pds)
    else:
        logging.warn("Can't create power spectrum")

    return pds, lc, gti
github StingraySoftware / dave / src / main / python / utils / dave_engine.py View on Github external
def get_fit_lomb_scargle_result(src_destination, bck_destination, gti_destination,
                                filters, axis, dt, freq_range, nyquist_factor, ls_norm, samples_per_peak,
                                models, priors=None, sampling_params=None):
    results = []

    try:
        # Calculates the LombScargle values
        frequency, power, lc = get_lomb_scargle(src_destination, bck_destination, gti_destination,
                            filters, axis, dt, freq_range, nyquist_factor, ls_norm, samples_per_peak)
        if not lc:
            return common_error("Can't create lightcurve or is empty")

        pds = Powerspectrum()
        pds.freq = frequency
        pds.power = power

        if pds:
            results = fit_power_density_spectrum(pds, models, priors=priors, sampling_params=sampling_params)
            pds = None  # Dispose memory
        else:
            logging.warn("get_fit_lomb_scargle_result: can't create power spectrum.")

    except:
        logging.error(ExHelper.getException('get_fit_lomb_scargle_result'))
        return common_error(ExHelper.getWarnMsg())

    return results
github StingraySoftware / dave / src / main / python / utils / dave_engine.py View on Github external
N = int(math.ceil(bins_per_segm / 1024) * 1024)  # max([ bins_per_segm, 1024 ])
                #logging.debug('get_bootstrap_results bins_per_segm: ' + str(bins_per_segm))
                #logging.debug('get_bootstrap_results N: ' + str(N))

                models_params = []
                powers = []

                for i in range(n_iter):
                    try:
                        the_simulator = simulator.Simulator(N=N, dt=dt, mean=mean,
                                                             rms=rms, red_noise=red_noise, random_state=seed)

                        sim_lc = the_simulator.simulate(fit_model)

                        if pds_type == 'Sng':
                            sim_pds = Powerspectrum(sim_lc, norm=norm, gti=gti)
                        else:
                            sim_pds = AveragedPowerspectrum(lc=sim_lc, segment_size=segm_size, norm=norm, gti=gti)

                        if sim_pds:

                            if df > 0:
                                pds = pds.rebin(df=df)

                            #sim_pds = rebin_spectrum_if_necessary(sim_pds)

                            parest, res = fit_powerspectrum(sim_pds, fit_model, starting_pars,
                                            max_post=False, priors=None, fitmethod="L-BFGS-B")

                            models_params.append(res.p_opt)
                            powers.append(sim_pds.power)