Example #1
0
 def ls_period(self, time, magnitude, error, prange=(0.2, 1.4), **kwargs):
     if len(time) > 50:
         model = LombScargleFast().fit(time, magnitude, error)
         periods, power = model.periodogram_auto(nyquist_factor=100)
         model.optimizer.period_range = prange
     else:
         model = LombScargle().fit(time, magnitude, error)
         periods, power = model.periodogram_auto(nyquist_factor=100)
         model.optimizer.period_range = prange
     return type(model).__name__, model.best_period, periods, power
Example #2
0
 def lombscargle(self):
     print(self.length)
     model = LombScargleFast(silence_warnings=True).fit(self.d, self.m, 0.1)
     model.optimizer.period_range = (0.02, min(10, self.length))
     period = model.best_period
     print "period is ", model.best_period
     sco = model.score(period, )
     print "the score is ", sco
     periods, power = model.periodogram_auto(
         nyquist_factor=2000)  ##Depend the highest fequency of the model.
     #f=open("tet.txt",'wb')
     #f.write('period is:'+str(period))
     #f.close
     x = periods
     y = power
     fig, axes = plt.subplots(1, 2, figsize=(8, 8))
     ax1, ax2 = axes.flatten()
     ax1.plot(x, y, color="red", linewidth=2)
     ax1.set_xlim(0, 10)
     ax1.set_xlabel("period (days)")
     ax1.set_ylabel("Lomb-Scargle Power")
     ax1.legend()
     phase = self.d / (1 * period)
     phase = phase % 1 * 1
     ax2.plot(phase, self.m, 'ro')
     ax2.set_xlabel("phase")
     ax2.set_ylabel("Mag")
     #ax2.set_ylim(0,1)
     ax2.legend()
     # plt.savefig('/home/yf/ptftry/pic/Lomb-Scargle.png')
     plt.show()
     return period
Example #3
0
def ls_period(time, magnitude, error, prange=(0.2, 1.4), **kwargs):
    with ctx.silence_stdout(SILENCE):
        model = LombScargleFast().fit(time, magnitude, error)
        periods, power = model.periodogram_auto(nyquist_factor=100)

        model.optimizer.period_range = prange
        return model.best_period, (periods, power)
Example #4
0
    def psearch(self):
        """
        Convole the two periodograms and find the Max
        """
        model = LombScargleFast().fit(self.t, self.m, self.merr)
        self.periods, self.power = model.periodogram_auto(nyquist_factor=200)
        self.aov, self.fr, _ = pyaov.aovw(self.t,
                                          self.m,
                                          self.merr,
                                          fstop=max(1 / self.periods),
                                          fstep=1 / self.periods[0])
        self.aov = self.aov[1:]
        self.fr = self.fr[1:]
        print len(self.aov), len(self.power)

        self.pgram = self.power * self.aov
        self.pgram_max = max(self.pgram)
        self.fbest = self.fr[np.argmax(self.pgram)]
        self.pbest = 1 / self.fbest

        self.pbest_signif = (self.pgram_max - np.median(self.pgram)) / np.std(
            self.pgram)
        print(
            "best period at {:.3f} days, {:.2f} sigma from the median".format(
                self.pbest, self.pbest_signif))
def get_period(t, mag, dmag, n_periods=1):
    model = LombScargleFast().fit(t, mag, dmag)
    periods, power = model.periodogram_auto(nyquist_factor=200)
    model.optimizer.period_range = (periods.min(),
                                    numpy.min([periods.max(), 2]))
    model.optimizer.quiet = True
    best_period, best_period_scores = model.optimizer.find_best_periods(
        model, n_periods=n_periods, return_scores=True)
    return best_period, best_period_scores
Example #6
0
def normalised_lombscargle(ts, ys, dys, oversampling=5, nyquist_factor=3):
    model = LombScargleFast().fit(ts, ys, dys)

    pers, pows = model.periodogram_auto(oversampling=oversampling,
                                        nyquist_factor=nyquist_factor)
    fs = 1.0 / pers

    T = np.max(ts) - np.min(ts)

    mu = 1 / T * np.trapz(ys, ts)
    s2 = 1 / T * np.trapz(np.square(ys - mu), ts)

    return fs, s2 * pows / np.trapz(pows, fs)
Example #7
0
    def run_periodogram(self, oversampling=5):
        """
        Compute a periodogram using gatspy.LombScargleFast.

        Parameters
        ----------
        oversampling : int, optional
            The oversampling factor for the periodogram.
        """
        model = LombScargleFast().fit(self.l_curve.times, self.l_curve.fluxes,
                                      self.l_curve.flux_errs)

        self.periods, self.powers = model.periodogram_auto(
            oversampling=oversampling)
    def psearch(self):
        """
        Convole the two periodograms and find the Max
        """
        model = LombScargleFast().fit(self.t, self.m, self.merr)
        self.periods, self.power = model.periodogram_auto(nyquist_factor=200)
        self.aov, self.fr, _ = pyaov.aovw(self.t, self.m, self.merr, fstop=max(1/self.periods), fstep=1/self.periods[0])
        self.aov = self.aov[1:]
        self.fr = self.fr[1:]
        print len(self.aov), len(self.power)

        self.pgram = self.power*self.aov
        self.pgram_max = max(self.pgram) 
        self.fbest = self.fr[np.argmax(self.pgram)]
        self.pbest = 1/self.fbest

        self.pbest_signif = (self.pgram_max - np.median(self.pgram))/np.std(self.pgram)
        print("best period at {:.3f} days, {:.2f} sigma from the median".format(self.pbest, self.pbest_signif))
Example #9
0
def periodogram(datax, datay, min_per, max_per, nyquist):
    #finding periodogram
    model = LombScargleFast().fit(datax, datay)
    period, power = model.periodogram_auto(nyquist_factor=nyquist)

    #plotting
    x_label = 'Period'
    y_label = 'Power'
    title = 'Lomb-Scargle Periodogram'

    plt.xlabel(x_label)
    plt.ylabel(y_label)
    plt.semilogx(period, power)

    # set range and find period
    model.optimizer.period_range = (min_per, max_per)
    period = model.best_period
    print("period = {0}".format(period))
    return period
Example #10
0
def l_gatspy():
    from gatspy.periodic import LombScargleFast
    from gatspy.periodic.lomb_scargle_fast import lomb_scargle_fast

    t = [
        2455263.5230960627, 2455263.5481076366, 2455263.5490798587,
        2455263.583848377, 2455263.584832174, 2455263.625989581,
        2455263.6269849516, 2455265.520839118, 2455265.5218113405,
        2455265.5837789327, 2455265.5847627292, 2455265.6254340257,
        2455265.6264293958, 2455266.5401562476, 2455266.541140044,
        2455266.54212384, 2455266.548802081, 2455266.5840335623,
        2455266.585028933, 2455267.524554396, 2455267.525538192,
        2455267.5493344883, 2455267.584103007, 2455267.5850868034,
        2455267.6254340257, 2455267.6264178217, 2455268.517725692,
        2455268.518697914, 2455268.542575229, 2455268.5435474515,
        2455268.5839062477, 2455268.584890044, 2455268.6254455997,
        2455268.6264293958, 2455269.5231192107, 2455269.524103007,
        2455269.542528933, 2455269.5435243035, 2455269.584126155,
        2455269.5850983774, 2455269.6254918957, 2455269.6264756923,
        2455269.751776618, 2455269.7527604145, 2455269.7921701367,
        2455269.793153933, 2455269.834126155, 2455269.8351099514,
        2455269.8758738404, 2455269.8768576365
    ]
    y = [
        13.52, 13.62, 13.67, 13.84, 13.81, 13.96, 13.98, 14.45, 14.43, 14.42,
        14.4, 14.53, 14.5, 14.299999999999999, 14.33, 14.33, 14.36,
        14.389999999999999, 14.36, 13.85, 13.86, 13.93, 14.07, 14.09, 14.2,
        14.229999999999999, 13.37, 13.35, 13.319999999999999, 13.3, 13.54,
        13.55, 13.76, 13.77, 14.42, 14.42, 14.5, 14.51, 14.52, 14.53,
        13.870000000000001, 13.870000000000001, 13.79, 13.77, 13.92, 13.92,
        14.08, 14.11, 14.26, 14.26
    ]

    model = LombScargleFast().fit(t, y)
    periods, power = model.periodogram_auto()
    # periods, power = lomb_scargle_fast(t, y, dy=1, f0=0.03, df=0.03, Nf=375)

    for i in range(0, len(periods)):
        if periods[i] > 0.55 and periods[i] < 0.57:
            print(i, periods[i], power[i])
Example #11
0
def Calc_Period(time, flux, error):

    #    fig, ax = plt.subplots()
    #   ax.errorbar(time, flux, error, fmt='.k', ecolor='gray')
    #   ax.set(xlabel='Time (days)', ylabel='magitude',
    #   title='LINEAR object {0}')
    # ax.invert_yaxis();

    model = LombScargleFast().fit(time, flux, error)
    periods, power = model.periodogram_auto(nyquist_factor=100)

    #fig, ax = plt.subplots()
    #   ax.plot(periods, power)
    #   ax.set(xlim=(2, 400), ylim=(0, 0.8),
    #   xlabel='period (days)',
    #    ylabel='Lomb-Scargle Power');
    #    plt.show()

    # set range and find period
    model.optimizer.period_range = (10, 500)
    period = model.best_period
    # print("period = {0}".format(period))
    #print("error = {0}".format(error))
    return period
Example #12
0
def k2_test(style=None):
    """
    Demonstrate the technique for a K2 EB.

    Parameters
    ----------
    style : str, optional
        The name of a matplotlib style sheet.

    """
    nova = pd.read_csv("villanova-db.csv")

    # Load Aigrain et al. (2015) pipeline light curve.
    hdu = fits.open("hlsp_k2sc_k2_llc_212012387-c05_kepler_v1_lc.fits")
    time = hdu[1].data["time"]
    cadence = hdu[1].data["cadence"]
    # Add back stellar variability correction.
    flux = hdu[1].data["flux"] + hdu[1].data["trend_t"] - \
        np.nanmedian(hdu[1].data["trend_t"])
    flux /= np.nanmedian(flux)

    # Mask out bad fluxes
    finite = np.isfinite(flux)
    time = time[finite]
    flux = flux[finite]
    cadence = cadence[finite]

    # Load parameters from Villanova catalog
    epic = 212012387
    eb = nova[nova["KIC"] == epic]
    p_orb = eb.period.values[0]
    t_0 = eb.bjd0.values[0]
    p_width = eb.pwidth.values[0]
    s_width = eb.swidth.values[0]
    sep = eb.sep.values[0]

    phase = ((time - t_0) % p_orb) / p_orb
    window = 1.0
    mask = ((phase > p_width * window) & (phase < 1 - p_width * window)) & \
        ((phase > sep + s_width * window) | (phase < sep - s_width * window))
    timecut = time[mask]
    fluxcut = flux[mask]
    cadencecut = cadence[mask]

    model = LombScargleFast().fit(time, flux)
    period, power = model.periodogram_auto(oversampling=5)
    model = LombScargleFast().fit(timecut, fluxcut)
    period_c, power_c = model.periodogram_auto(oversampling=5)

    lag, acf = interpacf.interpolated_acf(time,
                                          flux - np.median(flux), cadence)
    lag_c, acf_c = interpacf.interpolated_acf(timecut,
                                              fluxcut - np.median(fluxcut),
                                              cadencecut)

    if style is not None:
        plt.style.use(style)

    plt.rcParams["font.size"] = 22
    fig = plt.figure(figsize=(15, 12))
    ax1 = plt.subplot2grid((2, 2), (0, 0), colspan=2)
    ax2 = plt.subplot2grid((2, 2), (1, 0))
    ax3 = plt.subplot2grid((2, 2), (1, 1))

    # Plot lightcurve
    offset = 2304.
    ax1.plot(time - offset, flux, lw=1, color="k", alpha=0.4)
    ax1.plot(timecut - offset, fluxcut, lw=1, color="k")

    ax1.set_ylim(0.95, 1.01)
    ax1.set_xlabel("BJD - {0:.0f}".format(2454833 + offset))
    ax1.set_ylabel("Normalized Flux")
    ax1.minorticks_on()

    # Plot periodograms
    ax2.plot(period, 2 * power, color="r", lw=1.25, alpha=1.0)
    ax2.plot(period_c, power_c, color="k", lw=1.25)

    ax2.axvline(p_orb, color="k", lw=1.5, linestyle=":")
    ax2.axvline(p_orb / 2., color="k", lw=1.5, linestyle=":")
    ax2.text(p_orb, ax2.get_ylim()[1] + 0.01, "$P_{orb}$", ha="center")
    ax2.text(p_orb / 2., ax2.get_ylim()[1] + 0.01, "$P_{orb}/2$",
             ha="center")

    ax2.set_xlabel("Period (days)")
    ax2.set_ylabel("Normalized Power")
    ax2.minorticks_on()

    ax2.set_xlim(0.01, 10)
    ax2.set_ylim(0, 0.6)

    ax3.plot(lag, acf / acf.max(), color="r", lw=1.5, alpha=1.0)
    ax3.plot(lag_c, acf_c / acf_c.max(), color="k", lw=1.5)

    ax3.axvline(p_orb, color="k", lw=1.5, linestyle=":")
    ax3.axvline(p_orb / 2., color="k", lw=1.5, linestyle=":")
    ax3.text(p_orb, 1.05, "$P_{orb}$", ha="center")
    ax3.text(p_orb / 2., 1.05, "$P_{orb}/2$",
             ha="center")

    ax3.minorticks_on()
    ax3.set_xlim(0.01, 10)
    ax3.set_ylim(-0.55, 1)
    ax3.set_xlabel("Lag (days)")
    ax3.set_ylabel("ACF")

    fig.suptitle("EPIC {0}".format(epic), fontsize=26, y=0.94)
    fig.subplots_adjust(wspace=0.3, hspace=0.35)

    plt.savefig("k2_removal_demo.pdf")
		period = periods[np.argmax(periodogram)]
		extra = ''

	################################################################################
	# gastpy/LombScargleFast
	################################################################################

	elif options.algorithm == 'gatspy-fast':

		from gatspy.periodic import LombScargleFast

		oversampling = 4.0
		hifac = 200
		starttime = datetime.now()
		model = LombScargleFast().fit(t, m, dm)
		periods, periodogram = model.periodogram_auto(oversampling = oversampling, nyquist_factor = hifac)
		endtime = datetime.now()

		# Restrict range
		periods, periodogram = periods[periods <= maximum], periodogram[periods <= maximum]
		period = periods[np.argmax(periodogram)]
		extra = ''

	################################################################################

	################################################################################
	# Conditional entropy (adaptive grid)
	################################################################################

	elif options.algorithm == 'ce-adaptive':
Example #14
0
    def periodogram(self, time, flux, err, p_fold=None, plt_color='k',
                    max_days=100.0, oversampling=5):
        """
        Plot a the light curve, periodogram, and phase-folded light curve.

        The orbital period and possible aliases are indicated in red on the
        periodogram.

        Parameters
        ----------
        time : array_like
            Observation times in days.
        flux : array_like
            Fluxes.
        err : array_like
            Flux errors.
        p_fold : float, optional
            Plot the light curve folded at this period (in days), and indicate
            the period and likely aliases on the periodogram plot.
        plt_color : str, optional
            The line and scatter plot color.
        max_days : float, optional
            The maximum number of days to plot in the light curve and
            periodogram.
        oversampling: int, optional
            The oversampling factor for the periodogram.

        """
        model = LombScargleFast().fit(time, flux, err)
        period, power = model.periodogram_auto(oversampling=oversampling)

        fig1, (ax1, ax2) = plt.subplots(nrows=2, figsize=(7, 14))

        ax1.plot(time, flux, color=plt_color)
        ax1.set_xlim(time.min(), time.min() + max_days)
        ax1.set_xlabel('Time (days)')
        ax1.set_ylabel('Relative Flux')

        ax2.plot(period, power, color=plt_color)
        ax2.set_xlim(0.1, max_days)
        ax2.set_xlabel('Period (days)')
        ax2.set_ylabel('Power')

        # Plot some the most likely aliases of eclipse period.
        factors = [0.5, 1, 2, 3, 4]
        for factor in factors:
            ax2.axvline(self.p_orb / factor, color='r')

        if p_fold is not None:
            for factor in factors:
                ax2.axvline(p_fold / factor, color='b')

        fig1.suptitle('KIC {0:d} --- p_orb = {1:3.5f} days'.
                      format(self.kic, self.p_orb))

        if p_fold is not None:
            fig2, ax3 = plt.subplots()
            phase = (time % p_fold) / p_fold
            ax3.scatter(phase, flux, color=plt_color, s=0.1)
            ax3.set_xlim(0, 1)
            ax3.set_xlabel('Phase')
            ax3.set_ylabel('Relative Flux')

        plt.show()
Example #15
0
def multi_night(sources,
                unique_nights,
                night,
                brightest_mag,
                mags,
                mag_err,
                uniform_ylim=True):
    """
    Plot magnitude vs time data for several sources over several nights
    """
    number_of_nights = len(unique_nights)

    for source in sources:
        f = plt.figure(figsize=(5 * number_of_nights, 5))

        night_means = []
        night_stds = []
        night_bins = []
        source_mags = mags[source.id - 1]
        if uniform_ylim:
            # Use median to handle outliers.
            source_median = np.median(source_mags[np.isfinite(source_mags)])
            # Use median absolute deviation to get measure of scatter.
            # Helps avoid extremely points.
            source_variation = \
                3 * mad_std(source_mags[np.isfinite(source_mags)])

            # Ensure y range will be at least 0.2 magnitudes
            if source_variation < 0.1:
                half_range = 0.1
            else:
                half_range = source_variation

            y_range = (source_median - half_range, source_median + half_range)
        else:
            # Empty if this option wasn't chosen so that automatic limits
            # will be used.
            y_range = []

        last_axis = None
        for i, this_night in enumerate(unique_nights):
            last_axis = plt.subplot(1,
                                    number_of_nights + 1,
                                    i + 1,
                                    sharey=last_axis)
            night_mask = (night == this_night)
            night_mean, night_std = \
                plot_magnitudes(mags=mags[source.id - 1][night_mask],
                                errors=mag_err[source.id - 1][night_mask],
                                times=source.bjd_tdb[night_mask],
                                source=source.id,
                                night=this_night,
                                ref_mag=brightest_mag,
                                y_range=y_range)
            night_means.append(night_mean)
            night_stds.append(night_std)
            night_bins.append(this_night)

        plt.subplot(1, number_of_nights + 1, number_of_nights + 1)

        if uniform_ylim:
            f.subplots_adjust(wspace=0)
            plt.setp([a.get_yticklabels() for a in f.axes[1:]], visible=False)

        # Plot indicators of variation, and information about this source.
        # For simplicity, make the x and y range of this plot be 0 to 1.
        x = np.array([0., 1])
        y = x

        # Add invisible line to make plot.
        plt.plot(x, y, alpha=0, label='source {}'.format(source.id))
        night_means = np.array(night_means)

        # Plot bar proportional to Lomb-Scargle power.
        bad_mags = (np.isnan(mags[source.id - 1])
                    | np.isinf(mags[source.id - 1]))
        bad_errs = (np.isnan(mag_err[source.id - 1])
                    | np.isinf(mag_err[source.id - 1]))
        bads = bad_mags | bad_errs
        good_mags = ~bads
        model = LombScargleFast().fit(source.bjd_tdb[good_mags],
                                      mags[source.id - 1][good_mags],
                                      mag_err[source.id - 1][good_mags])
        periods, power = model.periodogram_auto(nyquist_factor=100,
                                                oversampling=3)
        max_pow = power.max()

        # print(source, max_pow)
        if max_pow > 0.5:
            color = 'green'
        elif max_pow > 0.4:
            color = 'cyan'
        else:
            color = 'gray'

        bar_x = 0.25
        plt.plot([bar_x, bar_x], [0, max_pow],
                 color=color,
                 linewidth=10,
                 label='LS power')

        plt.legend()

        # Add dot for magnitude of star.
        size = 10000. / np.abs(10**((source_median - brightest_mag) / 2.5))
        plt.scatter([0.8], [0.2], c='red', marker='o', s=size)
        plt.ylim(0, 1)
Example #16
0
	def estimate(self, observedLC):
		"""!
		Estimate intrinsicFlux, period, eccentricity, omega, tau, & a2sini 
		"""
		## intrinsicFluxEst
		maxPeriodFactor = 10.0
		model = LombScargleFast().fit(observedLC.t, observedLC.y, observedLC.yerr)
		periods, power = model.periodogram_auto(nyquist_factor = observedLC.numCadences)
		model.optimizer.period_range = (2.0*np.mean(observedLC.t[1:] - observedLC.t[:-1]), maxPeriodFactor*observedLC.T)
		periodEst = model.best_period
		numIntrinsicFlux = 100
		lowestFlux = np.min(observedLC.y[np.where(observedLC.mask == 1.0)])
		highestFlux = np.max(observedLC.y[np.where(observedLC.mask == 1.0)])
		intrinsicFlux = np.linspace(np.min(observedLC.y[np.where(observedLC.mask == 1.0)]), np.max(observedLC.y[np.where(observedLC.mask == 1.0)]), num = numIntrinsicFlux)
		intrinsicFluxList = list()
		totalIntegralList = list()
		for f in xrange(1, numIntrinsicFlux - 1):
			beamedLC = observedLC.copy()
			beamedLC.x = np.require(np.zeros(beamedLC.numCadences), requirements=['F', 'A', 'W', 'O', 'E'])
			for i in xrange(beamedLC.numCadences):
				beamedLC.y[i] = observedLC.y[i]/intrinsicFlux[f]
				beamedLC.yerr[i] = observedLC.yerr[i]/intrinsicFlux[f]
			dopplerLC = beamedLC.copy()
			dopplerLC.x = np.require(np.zeros(dopplerLC.numCadences), requirements=['F', 'A', 'W', 'O', 'E'])
			for i in xrange(observedLC.numCadences):
				dopplerLC.y[i] = math.pow(beamedLC.y[i], 1.0/3.44)
				dopplerLC.yerr[i] = (1.0/3.44)*math.fabs(dopplerLC.y[i]*(beamedLC.yerr[i]/beamedLC.y[i]))
			dzdtLC = dopplerLC.copy()
			dzdtLC.x = np.require(np.zeros(dopplerLC.numCadences), requirements=['F', 'A', 'W', 'O', 'E'])
			for i in xrange(observedLC.numCadences):
				dzdtLC.y[i] = 1.0 - (1.0/dopplerLC.y[i])
				dzdtLC.yerr[i] = math.fabs((-1.0*dopplerLC.yerr[i])/math.pow(dopplerLC.y[i], 2.0))
			foldedLC = dzdtLC.fold(periodEst)
			foldedLC.x = np.require(np.zeros(foldedLC.numCadences), requirements=['F', 'A', 'W', 'O', 'E'])
			integralSpline = UnivariateSpline(foldedLC.t[np.where(foldedLC.mask == 1.0)], foldedLC.y[np.where(foldedLC.mask == 1.0)], 1.0/foldedLC.yerr[np.where(foldedLC.mask == 1.0)], k = 3, s = None, check_finite = True)
			totalIntegral = math.fabs(integralSpline.integral(foldedLC.t[0], foldedLC.t[-1]))
			intrinsicFluxList.append(intrinsicFlux[f])
			totalIntegralList.append(totalIntegral)
		intrinsicFluxEst = intrinsicFluxList[np.where(np.array(totalIntegralList) == np.min(np.array(totalIntegralList)))[0][0]]

		## periodEst
		for i in xrange(beamedLC.numCadences):
			beamedLC.y[i] = observedLC.y[i]/intrinsicFluxEst
			beamedLC.yerr[i] = observedLC.yerr[i]/intrinsicFluxEst
			dopplerLC.y[i] = math.pow(beamedLC.y[i], 1.0/3.44)
			dopplerLC.yerr[i] = (1.0/3.44)*math.fabs(dopplerLC.y[i]*(beamedLC.yerr[i]/beamedLC.y[i]))
			dzdtLC.y[i] = 1.0 - (1.0/dopplerLC.y[i])
			dzdtLC.yerr[i] = math.fabs((-1.0*dopplerLC.yerr[i])/math.pow(dopplerLC.y[i], 2.0))
		model = LombScargleFast().fit(dzdtLC.t, dzdtLC.y, dzdtLC.yerr)
		periods, power = model.periodogram_auto(nyquist_factor = dzdtLC.numCadences)
		model.optimizer.period_range = (2.0*np.mean(dzdtLC.t[1:] - dzdtLC.t[:-1]), maxPeriodFactor*dzdtLC.T)
		periodEst = model.best_period

		## eccentricityEst & omega2Est
		# First find a full period going from rising to falling. 
		risingSpline = UnivariateSpline(dzdtLC.t[np.where(dzdtLC.mask == 1.0)], dzdtLC.y[np.where(dzdtLC.mask == 1.0)], 1.0/dzdtLC.yerr[np.where(dzdtLC.mask == 1.0)], k = 3, s = None, check_finite = True)
		risingSplineRoots = risingSpline.roots()
		firstRoot = risingSplineRoots[0]
		if risingSpline.derivatives(risingSplineRoots[0])[1] > 0.0:
			tRising = risingSplineRoots[0]
		else:
			tRising = risingSplineRoots[1]
		# Now fold the LC starting at tRising and going for a full period.
		foldedLC = dzdtLC.fold(periodEst, tStart = tRising)
		foldedLC.x = np.require(np.zeros(foldedLC.numCadences), requirements=['F', 'A', 'W', 'O', 'E'])
		# Fit the folded LC with a spline to figure out alpha and beta
		fitLC = foldedLC.copy()
		foldedSpline = UnivariateSpline(foldedLC.t[np.where(foldedLC.mask == 1.0)], foldedLC.y[np.where(foldedLC.mask == 1.0)], 1.0/foldedLC.yerr[np.where(foldedLC.mask == 1.0)], k = 3, s = 2*foldedLC.numCadences, check_finite = True)
		for i in xrange(fitLC.numCadences):
			fitLC.x[i] = foldedSpline(fitLC.t[i])
		# Now get the roots and find the falling root
		tZeros = foldedSpline.roots()
		if tZeros.shape[0] == 1: # We have found just tFalling
			tFalling = tZeros[0]
			tRising = fitLC.t[0]
			startIndex = 0
			tFull = fitLC.t[-1]
			stopIndex = fitLC.numCadences
		elif tZeros.shape[0] == 2: # We have found tFalling and one of tRising or tFull
			if foldedSpline.derivatives(tZeros[0])[1] < 0.0:
				tFalling = tZeros[0]
				tFull = tZeros[1]
				stopIndex = np.where(fitLC.t < tFull)[0][-1]
				tRising = fitLC.t[0]
				startIndex = 0
			elif foldedSpline.derivatives(tZeros[0])[1] > 0.0:
				if foldedSpline.derivatives(tZeros[1])[1] < 0.0:
					tRising = tZeros[0]
					startIndex = np.where(fitLC.t > tRising)[0][0]
					tFalling = tZeros[1]
					tFull = fitLC.t[-1]
					stopIndex = fitLC.numCadences
				else:
					raise RuntimeError('Could not determine alpha & omega correctly because the first root is rising but the second root is not falling!')
		elif tZeros.shape[0] == 3:
			tRising = tZeros[0]
			startIndex = np.where(fitLC.t > tRising)[0][0]
			tFalling = tZeros[1]
			tFull = tZeros[2]
			stopIndex = np.where(fitLC.t < tFull)[0][-1]
		else:
			raise RuntimeError('Could not determine alpha & omega correctly because tZeros has %d roots!'%(tZeros.shape[0]))
		# One full period now goes from tRising to periodEst. The maxima occurs between tRising and tFalling while the minima occurs between tFalling and tRising + periodEst  
		# Find the minima and maxima
		alpha = math.fabs(fitLC.x[np.where(np.max(fitLC.x[startIndex:stopIndex]) == fitLC.x)[0][0]])
		beta = math.fabs(fitLC.x[np.where(np.min(fitLC.x[startIndex:stopIndex]) == fitLC.x)[0][0]])
		peakLoc = fitLC.t[np.where(np.max(fitLC.x[startIndex:stopIndex]) == fitLC.x)[0][0]]
		troughLoc = fitLC.t[np.where(np.min(fitLC.x[startIndex:stopIndex]) == fitLC.x)[0][0]]
		KEst = 0.5*(alpha + beta)
		delta2 = (math.fabs(foldedSpline.integral(tRising, peakLoc)) + math.fabs(foldedSpline.integral(troughLoc, tFull)))/2.0
		delta1 = (math.fabs(foldedSpline.integral(peakLoc, tFalling)) + math.fabs(foldedSpline.integral(tFalling, troughLoc)))/2.0
		eCosOmega2 = (alpha - beta)/(alpha + beta)
		eSinOmega2 = ((2.0*math.sqrt(alpha*beta))/(alpha + beta))*((delta2 - delta1)/(delta2 + delta1))
		eccentricityEst = math.sqrt(math.pow(eCosOmega2, 2.0) + math.pow(eSinOmega2, 2.0))
		tanOmega2 = math.fabs(eSinOmega2/eCosOmega2)
		if (eCosOmega2/math.fabs(eCosOmega2) == 1.0) and (eSinOmega2/math.fabs(eSinOmega2) == 1.0):
			omega2Est = math.atan(tanOmega2)*(180.0/math.pi)
		if (eCosOmega2/math.fabs(eCosOmega2) == -1.0) and (eSinOmega2/math.fabs(eSinOmega2) == 1.0):
			omega2Est = 180.0 - math.atan(tanOmega2)*(180.0/math.pi)
		if (eCosOmega2/math.fabs(eCosOmega2) == -1.0) and (eSinOmega2/math.fabs(eSinOmega2) == -1.0):
			omega2Est = 180.0 + math.atan(tanOmega2)*(180.0/math.pi)
		if (eCosOmega2/math.fabs(eCosOmega2) == 1.0) and (eSinOmega2/math.fabs(eSinOmega2) == -1.0):
			omega2Est = 360.0 - math.atan(tanOmega2)*(180.0/math.pi)
		omega1Est = omega2Est - 180.0

		## tauEst
		zDot = KEst*(1.0 + eccentricityEst)*(eCosOmega2/eccentricityEst)
		zDotLC = dzdtLC.copy()
		for i in xrange(zDotLC.numCadences):
			zDotLC.y[i] = zDotLC.y[i] - zDot
		zDotSpline = UnivariateSpline(zDotLC.t[np.where(zDotLC.mask == 1.0)], zDotLC.y[np.where(zDotLC.mask == 1.0)], 1.0/zDotLC.yerr[np.where(zDotLC.mask == 1.0)], k = 3, s = 2*zDotLC.numCadences, check_finite = True)
		for i in xrange(zDotLC.numCadences):
			zDotLC.x[i] = zDotSpline(zDotLC.t[i])
		zDotZeros = zDotSpline.roots()
		zDotFoldedLC = dzdtLC.fold(periodEst)
		zDotFoldedSpline = UnivariateSpline(zDotFoldedLC.t[np.where(zDotFoldedLC.mask == 1.0)], zDotFoldedLC.y[np.where(zDotFoldedLC.mask == 1.0)], 1.0/zDotFoldedLC.yerr[np.where(zDotFoldedLC.mask == 1.0)], k = 3, s = 2*zDotFoldedLC.numCadences, check_finite = True)
		for i in xrange(zDotFoldedLC.numCadences):
			zDotFoldedLC.x[i] = zDotFoldedSpline(zDotFoldedLC.t[i])
		tC = zDotFoldedLC.t[np.where(np.max(zDotFoldedLC.x) == zDotFoldedLC.x)[0][0]]
		nuC = (360.0 - omega2Est)%360.0
		tE = zDotFoldedLC.t[np.where(np.min(zDotFoldedLC.x) == zDotFoldedLC.x)[0][0]]
		nuE = (180.0 - omega2Est)%360.0
		if math.fabs(360.0 - nuC) < math.fabs(360 - nuE):
			tauEst = zDotZeros[np.where(zDotZeros > tC)[0][0]]
		else:
			tauEst = zDotZeros[np.where(zDotZeros > tE)[0][0]]

		## a2sinInclinationEst
		a2sinInclinationEst = ((KEst*periodEst*self.Day*self.c*math.sqrt(1.0 - math.pow(eccentricityEst, 2.0)))/self.twoPi)/self.Parsec

		return intrinsicFluxEst, periodEst, eccentricityEst, omega1Est, tauEst, a2sinInclinationEst
Example #17
0
        period = periods[np.argmax(periodogram)]
        extra = ''

    ################################################################################
    # gastpy/LombScargleFast
    ################################################################################

    elif options.algorithm == 'gatspy-fast':

        from gatspy.periodic import LombScargleFast

        oversampling = 4.0
        hifac = 200
        starttime = datetime.now()
        model = LombScargleFast().fit(t, m, dm)
        periods, periodogram = model.periodogram_auto(
            oversampling=oversampling, nyquist_factor=hifac)
        endtime = datetime.now()

        # Restrict range
        periods, periodogram = periods[periods <= maximum], periodogram[
            periods <= maximum]
        period = periods[np.argmax(periodogram)]
        extra = ''

    ################################################################################

    ################################################################################
    # Conditional entropy (adaptive grid)
    ################################################################################

    elif options.algorithm == 'ce-adaptive':
Example #18
0
    def periodogram(self, time, flux, err, p_fold=None, plt_color='k',
                    max_days=100.0, oversampling=5, plot=False, cut_eclipses=True, best_period = True, period_range = (.05,45)):
        """
        Plot a the light curve, periodogram, and phase-folded light curve.

        The orbital period and possible aliases are indicated in red on the
        periodogram.

        Parameters
        ----------
        time : array_like
            Observation times in days.
        flux : array_like
            Fluxes.
        err : array_like
            Flux errors.
        p_fold : float, optional
            Plot the light curve folded at this period (in days), and indicate
            the period and likely aliases on the periodogram plot.
        plt_color : str, optional
            The line and scatter plot color.
        max_days : float, optional
            The maximum number of days to plot in the light curve and
            periodogram.
        oversampling: int, optional
            The oversampling factor for the periodogram.

        """
        if cut_eclipses:
            time, flux, err = self.curve_cut()
            
        else:
            time=self.time
            flux=self.flux
            error=self.error
            
        model = LombScargleFast().fit(time, flux, err)
        period, power = model.periodogram_auto(oversampling=oversampling)
        
         #new stuff to output the best period
        if best_period:
            model.optimizer.period_range = period_range
            Best_period = model.best_period
            
        if plot:
            fig1, (ax1, ax2) = plt.subplots(nrows=2, figsize=(7, 14))
    
            ax1.plot(time, flux, color=plt_color)
            ax1.set_xlim(time.min(), time.min() + max_days)
            ax1.set_xlabel('Time (days)')
            ax1.set_ylabel('Relative Flux')
    
            ax2.plot(period, power, color=plt_color)
            ax2.set_xlim(0.1, max_days)
            ax2.set_xlabel('Period (days)')
            ax2.set_ylabel('Power')
    
            # Plot some the most likely aliases of eclipse period.
            factors = [0.5, 1, 2, 3, 4]
            for factor in factors:
                ax2.axvline(self.p_orb / factor, color='r')
    
            if p_fold is not None:
                for factor in factors:
                    ax2.axvline(p_fold / factor, color='b')
    
            fig1.suptitle('KIC {0:d} --- p_orb = {1:3.5f} days'.
                          format(self.kic, self.p_orb))
    
            if p_fold is not None:
                fig2, ax3 = plt.subplots()
                phase = (time % p_fold) / p_fold
                ax3.scatter(phase, flux, color=plt_color, s=0.1)
                ax3.set_xlim(0, 1)
                ax3.set_xlabel('Phase')
                ax3.set_ylabel('Relative Flux')
    
            plt.show()
        #I don't know how it would react to returning nonexistent stuff
        if best_period:
            return period, power, Best_period
        else:
            return period, power    
Example #19
0
def find_cycle(feature, strain, mouse=None, bin_width=15,
               methods='LombScargleFast', disturb_t=False, gen_doc=False,
               plot=True, search_range_fit=None, nyquist_factor=3,
               n_cycle=10, search_range_find=(2, 26), sig=np.array([0.05])):
    """
    Use Lomb-Scargel method on different strain and mouse's data to find the
    best possible periods with highest p-values. The function can be used on
    specific strains and specific mouses, as well as just specific strains
    without specifying mouse number. We use the O(NlogN) fast implementation
    of Lomb-Scargle from the gatspy package, and also provide a way to
    visualize the result.

    Note that either plotting or calculating L-S power doesn't use the same
    method in finding best cycle. The former can use user-specified
    search_range, while the latter uses default two grid search_range.

    Parameters
    ----------
    feature: string in {"AS", "F", "M_AS", "M_IS", "W", "Distance"}
        "AS": Active state probalibity
        "F": Food consumed (g)
        "M_AS": Movement outside homebase
        "M_IS": Movement inside homebase
        "W": Water consumed (g)
        "Distance": Distance traveled
    strain: int
        nonnegative integer indicating the strain number
    mouse: int, default is None
        nonnegative integer indicating the mouse number
    bin_width: int, minute unit, default is 15 minutes
        number of minutes, the time interval for data aggregation
    methods: string in {"LombScargleFast", "LombScargle"}
        indicating the method used in determining periods and best cycle.
        If choose 'LombScargle', 'disturb_t' must be True.
    disturb_t: boolean, default is False
        If True, add uniformly distributed noise to the time sequence which
        are used to fit the Lomb Scargle model. This is to avoid the singular
        matrix error that could happen sometimes.
    plot: boolean, default is True
        If True, call the visualization function to plot the Lomb Scargle
        power versus periods plot. First use the data (either strain specific
        or strain-mouse specific) to fit the LS model, then use the
        search_range_fit as time sequence to predict the corresponding LS
        power, at last draw the plot out. There will also be stars and
        horizontal lines indicating the p-value of significance. Three stars
        will be p-value in [0,0.001], two stars will be p-value in
        [0.001,0.01], one star will be p-value in [0.01,0.05]. The horizontal
        line is the LS power that has p-value of 0.05.
    search_range_fit: list, numpy array or numpy arange, hours unit,
        default is None
        list of numbers as the time sequence to predict the corrsponding
        Lomb Scargle power. If plot is 'True', these will be drawn as the
        x-axis. Note that the number of search_range_fit points can not be
        too small, or the prediction smooth line will not be accurate.
        However the plot will always give the right periods and their LS
        power with 1,2 or 3 stars. This could be a sign to check whether
        search_range_fit is not enough to draw the correct plot.
        We recommend the default None, which is easy to use.
    nyquist_factor: int
        If search_range_fit is None, the algorithm will automatically
        choose the periods sequence.
        5 * nyquist_factor * length(time sequence) / 2 gives the number of
        power and periods used to make LS prediction and plot the graph.
    n_cycle: int, default is 10
        numbers of periods to be returned by function, which have the highest
        Lomb Scargle power and p-value.
    search_range_find: list, tuple or numpy array with length of 2, default is
                       (2,26), hours unit
        Range of periods to be searched for best cycle. Note that the minimum
        should be strictly larger than 0 to avoid 1/0 issues.
    sig: list or numpy array, default is [0.05].
        significance level to be used for plot horizontal line.
    gen_doc: boolean, default is False
        If true, return the parameters needed for visualize the LS power versus
        periods

    Returns
    -------
    cycle: numpy array of length 'n_cycle'
         The best periods with highest LS power and p-values.
    cycle_power: numpy array of length 'n_cycle'
         The corrsponding LS power of 'cycle'.
    cycle_pvalue: numpy array of length 'n_cycle'
         The corrsponding p-value of 'cycle'.
    periods: numpy array of the same length with 'power'
        use as time sequence in LS model to make predictions.Only return when
        gen_doc is True.
    power: numpy array of the same length with 'periods'
        the corresponding predicted power of periods. Only return when
        gen_doc is True.
    sig: list, tuple or numpy array, default is [0.05].
        significance level to be used for plot horizontal line.
        Only return when gen_doc is True.
    N: int
        the length of time sequence in the fit model. Only return when
        gen_doc is True.

    Examples
    -------
    >>> a,b,c = find_cycle(feature='F', strain = 0,mouse = 0, plot=False,)
    >>> print(a,b,c)
    >>> [ 23.98055016   4.81080233  12.00693952   6.01216335   8.0356203
         3.4316698    2.56303353   4.9294791   21.37925713   3.5697756 ]
        [ 0.11543449  0.05138839  0.03853218  0.02982237  0.02275952
        0.0147941  0.01151601  0.00998443  0.00845883  0.0082382 ]
        [  0.00000000e+00   3.29976046e-10   5.39367189e-07   8.10528027e-05
          4.71001953e-03   3.70178834e-01   9.52707020e-01   9.99372657e-01
         9.99999981e-01   9.99999998e-01]

    """
    if feature not in ALL_FEATURES:
        raise ValueError(
            'Input value must in {"AS", "F", "M_AS", "M_IS", "W", "Distance"}')
    if methods not in METHOD:
        raise ValueError(
            'Input value must in {"LombScargleFast","LombScargle"}')

    # get data
    if mouse is None:
        data_all = aggregate_data(feature=feature, bin_width=bin_width)
        n_mouse_in_strain = len(
            set(data_all.loc[data_all['strain'] == strain]['mouse']))
        data = [[] for i in range(n_mouse_in_strain)]
        t = [[] for i in range(n_mouse_in_strain)]
        for i in range(n_mouse_in_strain):
            data[i] = data_all.loc[(data_all['strain'] == strain) & (
                data_all['mouse'] == i)][feature]
            t[i] = np.array(np.arange(0, len(data[i]) *
                                      bin_width / 60, bin_width / 60))

        data = [val for sublist in data for val in sublist]
        N = len(data)
        t = [val for sublist in t for val in sublist]
    else:
        if feature == 'Distance':
            data = aggregate_movement(
                strain=strain, mouse=mouse, bin_width=bin_width)
            N = len(data)
            t = np.arange(0, N * bin_width / 60, bin_width / 60)
        else:
            data = aggregate_interval(
                strain=strain, mouse=mouse,
                feature=feature, bin_width=bin_width)
            N = len(data)
            t = np.arange(0, N * bin_width / 60, bin_width / 60)

    y = data

    # fit model
    if disturb_t is True:
        t = t + np.random.uniform(-bin_width / 600, bin_width / 600, N)

    if methods == 'LombScargleFast':
        model = LombScargleFast(fit_period=False).fit(t=t, y=y)
    elif methods == 'LombScargle':
        model = LombScargle(fit_period=False).fit(t=t, y=y)

    # calculate periods' LS power
    if search_range_fit is None:
        periods, power = model.periodogram_auto(nyquist_factor=nyquist_factor)
    else:
        periods = search_range_fit
        power = model.periodogram(periods=search_range_fit)

    # find best cycle
    model.optimizer.period_range = search_range_find
    cycle, cycle_power = model.find_best_periods(
        return_scores=True, n_periods=n_cycle)
    cycle_pvalue = 1 - (1 - np.exp(cycle_power / (-2) * (N - 1))) ** (2 * N)

    # visualization
    if plot is True:
        lombscargle_visualize(periods=periods, power=power, sig=sig, N=N,
                              cycle_power=cycle_power,
                              cycle_pvalue=cycle_pvalue, cycle=cycle)

    if gen_doc is True:
        return periods, power, sig, N, cycle, cycle_power, cycle_pvalue

    return cycle, cycle_power, cycle_pvalue
Example #20
0

args = parser.parse_args()


#print("Input file:")
#print(args.inputfile)

datain=pd.read_csv(args.inputfile,sep=r'\s*',header=0)

from gatspy.periodic import LombScargleFast
dmag=0.000005
nyquist_factor=40

model = LombScargleFast().fit(datain['BJD'], datain['mag'], dmag)
periods, power = model.periodogram_auto(nyquist_factor)

model.optimizer.period_range=(0.2, 10)
period = model.best_period

enablePrint()
print(period)

if args.plot:
    import matplotlib.pyplot as plt
    plt.plot(datain['BJD'],datain['mag'])
    plt.show()

if args.foldedplot:
    import matplotlib.pyplot as plt
    import numpy as np
def multi_night(sources, unique_nights, night,
                brightest_mag, mags, mag_err,
                uniform_ylim=True):
    """
    Plot magnitude vs time data for several sources over several nights
    """
    number_of_nights = len(unique_nights)

    for source in sources:
        f = plt.figure(figsize=(5 * number_of_nights, 5))

        night_means = []
        night_stds = []
        night_bins = []
        source_mags = mags[source.id - 1]
        if uniform_ylim:
            # Use median to handle outliers.
            source_median = np.median(source_mags[np.isfinite(source_mags)])
            # Use median absolute deviation to get measure of scatter.
            # Helps avoid extremely points.
            source_variation = 3 * mad_std(source_mags[np.isfinite(source_mags)])

            # Ensure y range will be at least 0.2 magnitudes
            if source_variation < 0.1:
                half_range = 0.1
            else:
                half_range = source_variation

            y_range = (source_median - half_range, source_median + half_range)
        else:
            # Empty if this option wasn't chosen so that automatic limits will be used.
            y_range = []

        last_axis = None
        for i, this_night in enumerate(unique_nights):
            last_axis = plt.subplot(1, number_of_nights + 1, i + 1,
                                    sharey=last_axis)
            night_mask = (night == this_night)
            night_mean, night_std = plot_magnitudes(mags=mags[source.id - 1][night_mask],
                                                    errors=mag_err[source.id - 1][night_mask],
                                                    times=source.bjd_tdb[night_mask],
                                                    source=source.id, night=this_night,
                                                    ref_mag=brightest_mag,
                                                    y_range=y_range)
            night_means.append(night_mean)
            night_stds.append(night_std)
            night_bins.append(this_night)

        plt.subplot(1, number_of_nights + 1, number_of_nights + 1)

        if uniform_ylim:
            f.subplots_adjust(wspace=0)
            plt.setp([a.get_yticklabels() for a in f.axes[1:]], visible=False)

        # Plot indicators of variation, and information about this source.
        # For simplicity, make the x and y range of this plot be 0 to 1.
        x = np.array([0., 1])
        y = x

        # Add invisible line to make plot.
        plt.plot(x, y, alpha=0, label='source {}'.format(source.id))
        night_means = np.array(night_means)

        # Plot bar proportional to Lomb-Scargle power.
        bad_mags = (np.isnan(mags[source.id - 1]) |
                    np.isinf(mags[source.id - 1]))
        bad_errs = (np.isnan(mag_err[source.id - 1]) |
                    np.isinf(mag_err[source.id - 1]))
        bads = bad_mags | bad_errs
        good_mags = ~bads
        model = LombScargleFast().fit(source.bjd_tdb[good_mags],
                                      mags[source.id - 1][good_mags],
                                      mag_err[source.id - 1][good_mags])
        periods, power = model.periodogram_auto(nyquist_factor=100,
                                                oversampling=3)
        max_pow = power.max()

        # print(source, max_pow)
        if max_pow > 0.5:
            color = 'green'
        elif max_pow > 0.4:
            color = 'cyan'
        else:
            color = 'gray'

        bar_x = 0.25
        plt.plot([bar_x, bar_x], [0, max_pow],
                 color=color, linewidth=10, label='LS power')

        plt.legend()

        # Add dot for magnitude of star.
        size = 10000./np.abs(10**((source_median - brightest_mag)/2.5))
        plt.scatter([0.8], [0.2], c='red', marker='o', s=size)
        plt.ylim(0, 1)
Example #22
0
def find_cycle(feature,
               strain,
               mouse=None,
               bin_width=15,
               methods='LombScargleFast',
               disturb_t=False,
               gen_doc=False,
               plot=True,
               search_range_fit=None,
               nyquist_factor=3,
               n_cycle=10,
               search_range_find=(2, 26),
               sig=np.array([0.05])):
    """
    Use Lomb-Scargel method on different strain and mouse's data to find the
    best possible periods with highest p-values. The function can be used on
    specific strains and specific mouses, as well as just specific strains
    without specifying mouse number. We use the O(NlogN) fast implementation
    of Lomb-Scargle from the gatspy package, and also provide a way to
    visualize the result.

    Note that either plotting or calculating L-S power doesn't use the same
    method in finding best cycle. The former can use user-specified
    search_range, while the latter uses default two grid search_range.

    Parameters
    ----------
    feature: string in {"AS", "F", "M_AS", "M_IS", "W", "Distance"}
        "AS": Active state probalibity
        "F": Food consumed (g)
        "M_AS": Movement outside homebase
        "M_IS": Movement inside homebase
        "W": Water consumed (g)
        "Distance": Distance traveled
    strain: int
        nonnegative integer indicating the strain number
    mouse: int, default is None
        nonnegative integer indicating the mouse number
    bin_width: int, minute unit, default is 15 minutes
        number of minutes, the time interval for data aggregation
    methods: string in {"LombScargleFast", "LombScargle"}
        indicating the method used in determining periods and best cycle.
        If choose 'LombScargle', 'disturb_t' must be True.
    disturb_t: boolean, default is False
        If True, add uniformly distributed noise to the time sequence which
        are used to fit the Lomb Scargle model. This is to avoid the singular
        matrix error that could happen sometimes.
    plot: boolean, default is True
        If True, call the visualization function to plot the Lomb Scargle
        power versus periods plot. First use the data (either strain specific
        or strain-mouse specific) to fit the LS model, then use the
        search_range_fit as time sequence to predict the corresponding LS
        power, at last draw the plot out. There will also be stars and
        horizontal lines indicating the p-value of significance. Three stars
        will be p-value in [0,0.001], two stars will be p-value in
        [0.001,0.01], one star will be p-value in [0.01,0.05]. The horizontal
        line is the LS power that has p-value of 0.05.
    search_range_fit: list, numpy array or numpy arange, hours unit,
        default is None
        list of numbers as the time sequence to predict the corrsponding
        Lomb Scargle power. If plot is 'True', these will be drawn as the
        x-axis. Note that the number of search_range_fit points can not be
        too small, or the prediction smooth line will not be accurate.
        However the plot will always give the right periods and their LS
        power with 1,2 or 3 stars. This could be a sign to check whether
        search_range_fit is not enough to draw the correct plot.
        We recommend the default None, which is easy to use.
    nyquist_factor: int
        If search_range_fit is None, the algorithm will automatically
        choose the periods sequence.
        5 * nyquist_factor * length(time sequence) / 2 gives the number of
        power and periods used to make LS prediction and plot the graph.
    n_cycle: int, default is 10
        numbers of periods to be returned by function, which have the highest
        Lomb Scargle power and p-value.
    search_range_find: list, tuple or numpy array with length of 2, default is
                       (2,26), hours unit
        Range of periods to be searched for best cycle. Note that the minimum
        should be strictly larger than 0 to avoid 1/0 issues.
    sig: list or numpy array, default is [0.05].
        significance level to be used for plot horizontal line.
    gen_doc: boolean, default is False
        If true, return the parameters needed for visualize the LS power versus
        periods

    Returns
    -------
    cycle: numpy array of length 'n_cycle'
         The best periods with highest LS power and p-values.
    cycle_power: numpy array of length 'n_cycle'
         The corrsponding LS power of 'cycle'.
    cycle_pvalue: numpy array of length 'n_cycle'
         The corrsponding p-value of 'cycle'.
    periods: numpy array of the same length with 'power'
        use as time sequence in LS model to make predictions.Only return when
        gen_doc is True.
    power: numpy array of the same length with 'periods'
        the corresponding predicted power of periods. Only return when
        gen_doc is True.
    sig: list, tuple or numpy array, default is [0.05].
        significance level to be used for plot horizontal line.
        Only return when gen_doc is True.
    N: int
        the length of time sequence in the fit model. Only return when
        gen_doc is True.

    Examples
    -------
    >>> a,b,c = find_cycle(feature='F', strain = 0,mouse = 0, plot=False,)
    >>> print(a,b,c)
    >>> [ 23.98055016   4.81080233  12.00693952   6.01216335   8.0356203
         3.4316698    2.56303353   4.9294791   21.37925713   3.5697756 ]
        [ 0.11543449  0.05138839  0.03853218  0.02982237  0.02275952
        0.0147941  0.01151601  0.00998443  0.00845883  0.0082382 ]
        [  0.00000000e+00   3.29976046e-10   5.39367189e-07   8.10528027e-05
          4.71001953e-03   3.70178834e-01   9.52707020e-01   9.99372657e-01
         9.99999981e-01   9.99999998e-01]

    """
    if feature not in ALL_FEATURES:
        raise ValueError(
            'Input value must in {"AS", "F", "M_AS", "M_IS", "W", "Distance"}')
    if methods not in METHOD:
        raise ValueError(
            'Input value must in {"LombScargleFast","LombScargle"}')

    # get data
    if mouse is None:
        data_all = aggregate_data(feature=feature, bin_width=bin_width)
        n_mouse_in_strain = len(
            set(data_all.loc[data_all['strain'] == strain]['mouse']))
        data = [[] for i in range(n_mouse_in_strain)]
        t = [[] for i in range(n_mouse_in_strain)]
        for i in range(n_mouse_in_strain):
            data[i] = data_all.loc[(data_all['strain'] == strain)
                                   & (data_all['mouse'] == i)][feature]
            t[i] = np.array(
                np.arange(0,
                          len(data[i]) * bin_width / 60, bin_width / 60))

        data = [val for sublist in data for val in sublist]
        N = len(data)
        t = [val for sublist in t for val in sublist]
    else:
        if feature == 'Distance':
            data = aggregate_movement(strain=strain,
                                      mouse=mouse,
                                      bin_width=bin_width)
            N = len(data)
            t = np.arange(0, N * bin_width / 60, bin_width / 60)
        else:
            data = aggregate_interval(strain=strain,
                                      mouse=mouse,
                                      feature=feature,
                                      bin_width=bin_width)
            N = len(data)
            t = np.arange(0, N * bin_width / 60, bin_width / 60)

    y = data

    # fit model
    if disturb_t is True:
        t = t + np.random.uniform(-bin_width / 600, bin_width / 600, N)

    if methods == 'LombScargleFast':
        model = LombScargleFast(fit_period=False).fit(t=t, y=y)
    elif methods == 'LombScargle':
        model = LombScargle(fit_period=False).fit(t=t, y=y)

    # calculate periods' LS power
    if search_range_fit is None:
        periods, power = model.periodogram_auto(nyquist_factor=nyquist_factor)
    else:
        periods = search_range_fit
        power = model.periodogram(periods=search_range_fit)

    # find best cycle
    model.optimizer.period_range = search_range_find
    cycle, cycle_power = model.find_best_periods(return_scores=True,
                                                 n_periods=n_cycle)
    cycle_pvalue = 1 - (1 - np.exp(cycle_power / (-2) * (N - 1)))**(2 * N)

    # visualization
    if plot is True:
        lombscargle_visualize(periods=periods,
                              power=power,
                              sig=sig,
                              N=N,
                              cycle_power=cycle_power,
                              cycle_pvalue=cycle_pvalue,
                              cycle=cycle)

    if gen_doc is True:
        return periods, power, sig, N, cycle, cycle_power, cycle_pvalue

    return cycle, cycle_power, cycle_pvalue
Example #23
0
plt.scatter(phases1, fluxes1, s=0.01, color='k')
plt.plot(pf_phases, pf_fluxes, lw=0.5)
plt.xlabel('Phase')
plt.ylabel('Normalized Flux')

plt.figure(2)
plt.step(times1, fluxes1, lw=0.5, label='original')
plt.step(times2, fluxes2 + 1, lw=0.5, label='minus polyfit')
plt.step(times3, fluxes3 + 1, lw=0.5, label='eclipses removed')
plt.xlabel('Time (days)')
plt.ylabel('Normalized Flux')
plt.xlim(times1[0], times1[0] + 10)
plt.legend(loc='lower left')

model1 = LombScargleFast().fit(times1, fluxes1)
periods1, powers1 = model1.periodogram_auto(oversampling=5)

model3 = LombScargleFast().fit(times3, fluxes3)
periods3, powers3 = model3.periodogram_auto(oversampling=5)

plt.figure(3)
plt.step(periods1, powers1, lw=0.5)
plt.step(periods3, powers3, lw=0.5)
plt.xlabel('Period (days)')
plt.ylabel('Normalized power')
plt.xlim(0, 10)

plt.show()

# plt.figure(4)
# lags3, acf3 = interpacf.interpolated_acf(times3, fluxes3)