Example #1
0
    def multi_search(self, to_plot=False):
        """Search a lightcurve for a secondary signal."""
        # Start with the corrected lightcurve and its associated period
        # Phase on that period and remove it
        white_out = detrend.pre_whiten(self.time, self.corrected_flux, 
                                       self.corrected_unc, self.corr_prot,
                                       which="phased")
        detrended_flux = self.corrected_flux / white_out[2]

        self.corr_trend = white_out[2]
        self.sec_flux = detrended_flux
        self.sec_unc = self.corrected_unc

        # Run lomb-scargle again and re-measure the period
        fit_out = self._run_fit([self.time, self.sec_flux, self.sec_unc])
        self.sec_prot = fit_out[0]
        self.sec_power = fit_out[1]
        self.sec_periods = fit_out[2]
        self.sec_pgram = fit_out[3]
        self.sec_sigmas = fit_out[5]

        eval_out =  evaluate.test_pgram(self.sec_periods, self.sec_pgram,
                                        self.power_threshold)
        plot_aliases = [None, eval_out[2]]

        white_out2 = detrend.pre_whiten(self.time, self.sec_flux, 
                                        self.sec_unc, self.sec_prot,
                                        which="phased")
        self.sec_trend = white_out2[2]

        # Plot!
        if to_plot:
            # Plot them up
            lcs = [[self.time, self.corrected_flux, self.corrected_unc],
                   [self.time, self.sec_flux, self.sec_unc]]
            pgrams = [[self.corr_periods, self.corr_pgram], 
                      [self.sec_periods, self.sec_pgram]]
            best_periods = [self.corr_prot, self.sec_prot]
            data_labels = ["Corrected", "Fund. Prot="
                           "{0:.2f}d Removed".format(self.corr_prot)]
            sigmas = [self.corr_sigmas, self.sec_sigmas]
            rd_fig, rd_axes = plot.compare_multiple(lcs, pgrams, best_periods, 
                                                    sigmas, 
                                                    aliases=plot_aliases,
                                                    data_labels=data_labels,  
                                                    phase_by=self.sec_prot)

            rd_fig.suptitle(self.name, fontsize="large", y=0.99)

            rd_fig.delaxes(rd_axes[3])

            rd_axes[0].plot(self.time, white_out[2], 'b-', lw=2)

            plt.savefig("{0}plot_outputs/{1}_second_period.png".format(
                        base_path,self.name))
Example #2
0
    def choose_initial(self, to_plot=False):
        """Search raw and detrended LCs for periods, and decide whether there's
        a period there.

        """
        # Run a fit on the raw lc
        r_out = self._run_fit("raw")
        raw_fp, raw_power, raw_prots, raw_pgram, raw_alias, raw_sigma = r_out
        logging.debug("Ran raw fit")

        # Run a fit on the detrended lc
        d_out = self._run_fit("detrended")
        det_fp, det_power, det_prots, det_pgram, det_alias, det_sigma = d_out
        logging.debug("Ran detrended fit")

        # Only consider peaks less than ~half the length of the lightcurve
#        max_peak_loc = 0.75 * (self.time[-1] - self.time[0])
        max_peak_loc = 40
        logging.info("Max Prot = %f", max_peak_loc)

        raw_loc2 = np.argmax(raw_pgram[raw_prots<max_peak_loc])
        raw_power2 = raw_pgram[raw_prots<max_peak_loc][raw_loc2]
        raw_prot2 = raw_prots[raw_prots<max_peak_loc][raw_loc2]
        logging.info("raw %d FP %f Power %f", raw_loc2, raw_prot2, raw_power2)

        det_loc2 = np.argmax(det_pgram[det_prots<max_peak_loc])
        det_power2 = det_pgram[det_prots<max_peak_loc][det_loc2]
        det_prot2 = det_prots[det_prots<max_peak_loc][det_loc2]
        logging.info("det %d FP %f Power %f", det_loc2, det_prot2, det_power2)

        # Compare them
        lc_to_use = self._pick_lc(raw_power2, det_power2)
        if lc_to_use<=1:
            logging.info("Using raw lightcurve")
            self.init_prot , self.init_power = raw_prot2, raw_power2
            self.init_periods_to_test, self.init_pgram = raw_prots, raw_pgram
            self.use_flux = self.flux / self.med
            self.use_unc = self.unc_flux / self.med
            self.init_sigmas = raw_sigma
            self.use = "raw"
            data_labels = ["Raw (Selected)", "Detrended"]
        elif lc_to_use==2:
            logging.info("Using detrended lightcurve")
            self.init_prot , self.init_power = det_prot2, det_power2
            self.init_periods_to_test, self.init_pgram = det_prots, det_pgram
            self.use_flux = self.det_flux 
            self.use_unc = self.unc_flux 
            self.init_sigmas = det_sigma
            self.use = "det"
            data_labels = ["Raw", "Detrended (Selected)"]

        logging.info("Initial Prot %f Power %f", self.init_prot, 
                     self.init_power)
        # get power at harmonics
        self.init_harmonics = self._harmonics(self.init_prot, 
                                              self.init_periods_to_test,
                                              self.init_pgram)

        # Get aliases for selected period
        eval_out = evaluate.test_pgram(self.init_periods_to_test, 
                                       self.init_pgram, self.power_threshold)

        # Get phase-folded, smoothed trend
        white_out2 = detrend.pre_whiten(self.time, self.use_flux, 
                                        self.use_unc, self.init_prot,
                                        which="phased")
        self.init_trend = white_out2[2]

 
        if eval_out[-1]==False:
            logging.warning("Selected lightcurve is not clean")
        else:
            logging.debug("Selected lightcurve is clean")
        plot_aliases = [None, eval_out[2]]

        if to_plot:
            # Plot them up
            lcs = [[self.time, self.flux/self.med, abs(self.unc_flux/self.med)],
                   [self.time, self.det_flux, self.det_unc]]
            pgrams = [[raw_prots, raw_pgram], [det_prots, det_pgram]]
            best_periods = [raw_prot2, det_prot2]
            sigmas = [raw_sigma, det_sigma]
            logging.debug(sigmas)
            rd_fig, rd_axes = plot.compare_multiple(lcs, pgrams, best_periods, 
                                                    sigmas, 
                                                    aliases=plot_aliases,
                                                    data_labels=data_labels,  
                                                    phase_by=self.init_prot)

            rd_fig.suptitle(self.name, fontsize="large", y=0.99)

            rd_fig.delaxes(rd_axes[3])

            plt.savefig("{0}plot_outputs/{1}_raw_detrend.png".format(base_path,
                                                                     self.name))
            plt.close("all")
Example #3
0
def search_and_detrend(time,
                       flux,
                       unc_flux,
                       prot_lims=None,
                       to_plot=False,
                       **detrend_kwargs):
    """Test for a period and then pre-whiten with it.

    Inputs
    ------
    time, flux, unc_flux: array_like

    kind: string, optional
        type of smoothing to use. Defaults to "supersmoother."
        Other types "boxcar", "linear"

    which: string, optional
        whether to smooth the "phased" lightcurve (default) or the "full" 
        lightcurve. 

    phaser: Float, optional (default=None)
        if kind="boxcar", phaser is the Half-width of the smoothing window.
        if kind="supersmoother", phaser is alpha (the "bass enhancement").

    pgram_threshold: float

    prot_lims: list-like, length=2
        minimum and maximum rotation periods to search for lomb-scargle

    num_prot: integer
        How many rotation periods to search

    Outputs
    -------
    fund_period, fund_power, periods_to_test, periodogram 
    white_flux, white_unc, smoothed_flux

    """

    # Search for periodogram
    ls_out = run_ls(time, flux, unc_flux, 0.5, prot_lims=prot_lims)
    fund_period, fund_power, periods_to_test, periodogram = ls_out[:4]

    # Whiten on that period
    white_out = detrend.pre_whiten(time,
                                   flux,
                                   unc_flux,
                                   fund_period,
                                   which="phased",
                                   **detrend_kwargs)

    white_flux, white_unc, smoothed_flux = white_out

    detrended_flux = flux / smoothed_flux
    detrended_unc = unc_flux

    if to_plot == True:
        fig, ax_list = plot.plot_one([time, flux, unc_flux],
                                     [periods_to_test, periodogram],
                                     fund_period,
                                     power_threshold=0,
                                     data_label="Input")
        ax_list[0].plot(time, smoothed_flux, 'b.')

        ax_list[3].plot(time, white_flux, 'r.')
        ax_list[3].set_ylabel("Whitened Flux")
        ax_list[3].set_xlabel("Time (D)")
        plt.tight_layout()

    # Return the detrended flux
    return detrended_flux, detrended_unc, fund_period, fund_power
Example #4
0
def search_and_detrend(time, flux, unc_flux, prot_lims=None,
                       to_plot=False, **detrend_kwargs):
    """Test for a period and then pre-whiten with it.

    Inputs
    ------
    time, flux, unc_flux: array_like

    kind: string, optional
        type of smoothing to use. Defaults to "supersmoother."
        Other types "boxcar", "linear"

    which: string, optional
        whether to smooth the "phased" lightcurve (default) or the "full" 
        lightcurve. 

    phaser: Float, optional (default=None)
        if kind="boxcar", phaser is the Half-width of the smoothing window.
        if kind="supersmoother", phaser is alpha (the "bass enhancement").

    pgram_threshold: float

    prot_lims: list-like, length=2
        minimum and maximum rotation periods to search for lomb-scargle

    num_prot: integer
        How many rotation periods to search

    Outputs
    -------
    fund_period, fund_power, periods_to_test, periodogram 
    white_flux, white_unc, smoothed_flux

    """

    # Search for periodogram
    ls_out = run_ls(time, flux, unc_flux, 0.5,  
                    prot_lims=prot_lims)
    fund_period, fund_power, periods_to_test, periodogram = ls_out[:4]
                                                       
    # Whiten on that period
    white_out = detrend.pre_whiten(time, flux, unc_flux, fund_period,  
                                   which="phased", **detrend_kwargs)

    white_flux, white_unc, smoothed_flux = white_out

    detrended_flux = flux / smoothed_flux
    detrended_unc = unc_flux

    if to_plot==True:
        fig, ax_list = plot.plot_one([time, flux, unc_flux],
                                     [periods_to_test, periodogram],
                                     fund_period,
                                     power_threshold=0, data_label="Input")
        ax_list[0].plot(time, smoothed_flux, 'b.')

        ax_list[3].plot(time, white_flux, 'r.')
        ax_list[3].set_ylabel("Whitened Flux")
        ax_list[3].set_xlabel("Time (D)")
        plt.tight_layout()

    # Return the detrended flux
    return detrended_flux, detrended_unc, fund_period, fund_power