def fit_multiband_freq(tup): idx, group = tup t, f, e, b = group['mjd'], group['flux'], group['flux_err'], group[ 'passband'] model = LombScargleMultiband(fit_period=True) model.optimizer.period_range = ( 0.1, int((group['mjd'].max() - group['mjd'].min()) / 2)) model.fit(t, f, e, b) return model
def compute_period(obj): """ Computes period required for phase plots for obj. """ model = LombScargleMultiband(fit_period=True) # t_min = max(np.median(np.diff(sorted(obj['mjd']))), 0.1) # t_max = min(10., (obj['mjd'].max() - obj['mjd'].min())/2.) t_min = 0.1 t_max = int((obj['mjd'].max() - obj['mjd'].min()) / 2.0) model.optimizer.set(period_range=(t_min, t_max), first_pass_coverage=5) model.fit(obj['mjd'], obj['flux'], dy=obj['flux_err'], filts=obj['passband']) return model
def plot_example_lightcurve(rrlyrae, lcid): fig = plt.figure(figsize=(10, 4)) gs = plt.GridSpec(2, 2, left=0.07, right=0.95, wspace=0.1, bottom=0.15, top=0.9) ax = [ fig.add_subplot(gs[:, 0]), fig.add_subplot(gs[0, 1]), fig.add_subplot(gs[1, 1]) ] t, y, dy, filts = rrlyrae.get_lightcurve(lcid) # don't plot data with huge errorbars mask = (dy < 1) t, y, dy, filts = t[mask], y[mask], dy[mask], filts[mask] period = rrlyrae.get_metadata(lcid)['P'] phase = (t % period) / period for band in 'ugriz': mask = (filts == band) ax[0].errorbar(phase[mask], y[mask], dy[mask], fmt='.', label=band) ax[0].legend(loc='upper left', ncol=3) ax[0].set(xlabel='phase', ylabel='magnitude', title='Folded Data (P={0:.3f} days)'.format(period)) ylim = ax[0].get_ylim() ax[0].set_ylim(ylim[1], ylim[0] - 0.2 * (ylim[1] - ylim[0])) periods = np.linspace(0.2, 1.0, 4000) models = [ SuperSmoother1Band(band='g'), LombScargleMultiband(Nterms_base=1, Nterms_band=0) ] colors = seaborn.color_palette() for axi, model, color in zip(ax[1:], models, colors): model.fit(t, y, dy, filts) per, scr = plot_filter(periods, model.score(periods)) axi.plot(per, scr, lw=1, color=color) axi.set_ylim(0, 1) ax[1].xaxis.set_major_formatter(plt.NullFormatter()) ax[1].set_title("SuperSmoother on g-band") ax[2].set_title("Shared-phase Multiband") return fig, ax
filts = np.array([f for f in 'ugriz']) # Here's our subset filts = np.take(list('ugriz'), np.arange(Nobs), mode='wrap') mags = mags[np.arange(Nobs) % 5, np.arange(Nobs)] masks = [(filts == band) for band in 'ugriz'] fig, ax = plt.subplots(5, sharex=True, sharey=True) fig.subplots_adjust(left=0.1, right=0.93, hspace=0.1) periods = np.linspace(0.2, 1.4, 1000) combos = [(1, 0), (0, 1), (2, 0), (2, 1), (2, 2)] for axi, (Nbase, Nband) in zip(ax, combos): LS_multi = LombScargleMultiband(Nterms_base=Nbase, Nterms_band=Nband) LS_multi.fit(t, mags, dy, filts) P_multi = LS_multi.periodogram(periods) axi.plot(periods, P_multi, lw=1) text = ('$N_{{base}}={0},\ N_{{band}}={1}\ \ (M^{{eff}}={2})$' ''.format(Nbase, Nband, (2 * max(0, Nbase - Nband) + 5 * (2 * Nband + 1)))) if (Nbase, Nband) == (1, 0): text += ' "shared-phase model"' elif (Nbase, Nband) == (0, 1): text += ' "multi-phase model"' axi.text(0.21, 0.98, text, fontsize=10, ha='left', va='top') axi.yaxis.set_major_locator(plt.MultipleLocator(0.5))
def run_gatspy(self): #this is the general simulation - ellc light curves and gatspy periodograms #for the multiband gatspy fit allObsDates = np.array([]) allAppMagObs = np.array([]) allAppMagObsErr = np.array([]) allObsFilters = np.array([]) minNobs = 1e10 if (self.verbose): print("in run_gatspy") for i, filt in enumerate(self.filters): self.EB.LSS[filt] = -999. if (self.EB.obsDates[filt][0] is not None and min(self.EB.appMagObs[filt]) > 0): #run gatspy for this filter drng = max(self.EB.obsDates[filt]) - min( self.EB.obsDates[filt]) minNobs = min(minNobs, len(self.EB.obsDates[filt])) #print("filter, nobs", filt, len(self.EB.obsDates[filt])) if (self.useFast and len(self.EB.obsDates[filt]) > 50): model = LombScargleFast(fit_period=True, silence_warnings=True, optimizer_kwds={"quiet": True}) else: model = LombScargle(fit_period=True, optimizer_kwds={"quiet": True}) pmin = self.gatspyPeriodMin if (self.EB.period < pmin): pmin = 0.1 * self.EB.period model.optimizer.period_range = (pmin, drng) model.fit(self.EB.obsDates[filt], self.EB.appMagObs[filt], self.EB.appMagObsErr[filt]) self.EB.LSS[filt] = model.best_period self.EB.LSSmodel[filt] = model self.EB.maxDeltaMag = max(self.EB.deltaMag[filt], self.EB.maxDeltaMag) #to use for the multiband fit allObsDates = np.append(allObsDates, self.EB.obsDates[filt]) allAppMagObs = np.append(allAppMagObs, self.EB.appMagObs[filt]) allAppMagObsErr = np.append(allAppMagObsErr, self.EB.appMagObsErr[filt]) allObsFilters = np.append( allObsFilters, np.full(len(self.EB.obsDates[filt]), filt)) if (self.verbose): print('filter = ', filt) print('obsDates = ', self.EB.obsDates[filt][0:10]) print('appMagObs = ', self.EB.appMagObs[filt][0:10]) print('delta_mag = ', self.EB.deltaMag[filt]) print('LSS = ', self.EB.LSS[filt]) if (len(allObsDates) > 0 and self.doLSM): drng = max(allObsDates) - min(allObsDates) if (self.useFast and minNobs > 50): model = LombScargleMultibandFast( fit_period=True, optimizer_kwds={"quiet": True}) else: model = LombScargleMultiband(Nterms_band=self.n_band, Nterms_base=self.n_base, fit_period=True, optimizer_kwds={"quiet": True}) pmin = self.gatspyPeriodMin if (self.EB.period < pmin): pmin = 0.1 * self.EB.period model.optimizer.period_range = (pmin, drng) model.fit(allObsDates, allAppMagObs, allAppMagObsErr, allObsFilters) self.EB.LSM = model.best_period self.EB.LSMmodel = model if (self.verbose): print('LSM =', self.EB.LSM)
if i == 0: ind = np.arange(Nobs) % 5 y = mags[ind, np.arange(Nobs)] f = np.array(filts)[ind] else: arrs = np.broadcast_arrays(t, mags, dy, filts[:, None]) t, y, dy, f = map(np.ravel, arrs) model1 = LombScargle() model1.fit(t, y, dy) yfit = model1.predict(tfit, period=period) plot_data(ax[0, 0], t, y, dy, f) ax[0, 0].plot(tfit / period, yfit, '-', color='gray', lw=4, alpha=0.5) ax[0, 1].plot(periods, model1.score(periods), color='gray') ax[0, 0].set_xlabel('') model2 = LombScargleMultiband(Nterms_base=1, Nterms_band=1) model2.fit(t, y, dy, f) yfits = model2.predict(tfit, filts=filts[:, None], period=period) plot_data(ax[1, 0], t, y, dy, f) for j in range(5): ax[1, 0].plot(tfit / period, yfits[j]) ax[1, 1].plot(periods, model2.score(periods)) fig.savefig('naive_{0}.png'.format(i + 1)) plt.show()
fig.tight_layout(rect=[0, 0, 1, 0.97]) def get_features(self): '''Return all the features for this object''' variables = ['Std', 'Amp', 'MAD', 'Beyond', 'Skew'] feats = [] for i, pb in enumerate(self._passbands): pbname = self._pbnames[pb] feats += [getattr(self, f'{pbname}{x}', np.nan) for x in variables] return feats lc = LightCurve('../input/plasticc-astronomy-starter-kit-media/fake010.csv') lc.plot_multicolor_lc() lc = LightCurve('../input/plasticc-astronomy-starter-kit-media/fake030.csv') lc.plot_multicolor_lc() from gatspy.periodic import LombScargleMultiband model = LombScargleMultiband(fit_period=True) # we'll window the search range by setting minimums and maximums here # but in general, the search range you want to evaluate will depend on the data # and you will not be able to window like this unless you know something about # the class of the object a priori t_min = max(np.median(np.diff(sorted(lc.DFlc['mjd']))), 0.1) t_max = min(10., (lc.DFlc['mjd'].max() - lc.DFlc['mjd'].min())/2.) model.optimizer.set(period_range=(t_min, t_max), first_pass_coverage=5) model.fit(lc.DFlc['mjd'], lc.DFlc['flux'], dy=lc.DFlc['flux_err'], filts=lc.DFlc['passband']) period = model.best_period print(f'{lc.filename} has a period of {period} days') phase = (lc.DFlc['mjd'] /period) % 1 lc.plot_multicolor_lc(phase=phase) header = Table.read('../input/plasticc-astronomy-starter-kit-media/plasticc_training_set_metadata_stub.csv', format='csv')
plot_data(ax) t_all = np.ravel(t * np.ones_like(mags)) mags_all = np.ravel(mags) dy_all = np.ravel(dy * np.ones_like(mags)) basemodel = LombScargle(Nterms=2).fit(t_all, mags_all, dy_all) period = rrlyrae.period tfit = np.linspace(0, period, 1000) base_fit = basemodel.predict(tfit, period=period) ax.plot(tfit / period, base_fit, color='black', lw=5, alpha=0.5) ax.set_title('2-term Base Model') # Plot the band-by-band augmentation multimodel = LombScargleMultiband(Nterms_base=2, Nterms_band=1) t1, y1, dy1, f1 = map(np.ravel, np.broadcast_arrays(t, mags, dy, filts[:, None])) multimodel.fit(t1, y1, dy1, f1) yfits = multimodel.predict(tfit, filts=filts[:, None], period=period) plt.savefig('buildup_2.png') fig, ax = plt.subplots() for i in range(5): ax.plot(tfit / period, yfits[i] - base_fit) ax.plot(tfit / period, 0 * tfit, '--k') ax.set_ylim(1.7, -1.8) ax.set_xlabel('phase')
def ellc_gatspy_sim(j, t_zero, period, a, q, f_c, f_s, ld_1, ld_2, Teff1, Teff2, logg1, logg2, M_H, R_1, R_2, incl, sbratio, shape_1, shape_2, appmag, bnum, n_band, n_base, return_dict, plot_dict, db, dbID_OpSim): #this is the general simulation - ellc light curves and gatspy periodograms #global filters, sigma_sys, totaltime, cadence print("here in ellc_gatspy_sim") totalt = [] totalmag = [] totaldmag = [] totalfilts = [] if (verbose): print(j, 't_zero = ', t_zero) print(j, 'period = ', period) print(j, 'semimajor = ', a) print(j, 'massrat = ', q) print(j, 'f_c = ', f_c) print(j, 'f_s = ', f_s) print(j, 'ld_1 = ', ld_1) print(j, 'ld_2 = ', ld_2) print(j, 'radius_1 = ', R_1) print(j, 'radius_2 = ', R_2) print(j, 'incl = ', incl) print(j, 'sbratio = ', sbratio) print(j, 'Teff1 = ', Teff1) print(j, 'Teff2 = ', Teff2) if (do_plot): # for plotting the periodograms for_plotting = dict() pds = np.linspace(0.2, 2. * period, 10000) for_plotting['periods'] = pds for_plotting['seed'] = bnum #set the random seed #np.random.seed(seed = seed) delta_mag = 0. if (opsim): cursor = getSummaryCursor(db, dbID_OpSim) for i, filt in enumerate(filters): if (opsim): print("I'm running code with OpSim!") time = getexpDate(cursor, dbID_OpSim, filt) nobs = len(time) print("time_OpSim = ", time) else: print("I'm NOT running code with OpSim!") nobs = int(round(totaltime / (cadence * len(filters)))) time = np.sort(totaltime * np.random.random(size=nobs)) #time.sort() #maybe not needed and taking time drng = max(time) - min(time) # if (time_OpSim[0] != None): if (time[0] != None): filtellc = filt if (filt == 'y_'): filtellc = 'z_' #because we don't have limb darkening for y_ ########################## #Can we find limb darkening coefficients for y band?? (Then we wouldn't need this trick) ########################## ldy_filt = ellc.ldy.LimbGravityDarkeningCoeffs(filtellc) a1_1, a2_1, a3_1, a4_1, y = ldy_filt(Teff1, logg1, M_H) a1_2, a2_2, a3_2, a4_2, y = ldy_filt(Teff2, logg2, M_H) ldc_1 = [a1_1, a2_1, a3_1, a4_1] ldc_2 = [a1_2, a2_2, a3_2, a4_2] lc = ellc.lc(time, t_zero=t_zero, period=period, a=a, q=q, f_c=f_c, f_s=f_s, ld_1=ld_1, ldc_1=ldc_1, ld_2=ld_2, ldc_2=ldc_2, radius_1=R_1, radius_2=R_2, incl=incl, sbratio=sbratio, shape_1=shape_1, shape_2=shape_2, grid_1='default', grid_2='default') if (verbose): print(j, 'ldc_1 = ', ldc_1) print(j, 'ldc_2 = ', ldc_2) print(j, 'time = ', time[0:10]) print(j, 'lc = ', lc[0:10]) if (min(lc) >= 0): magn = -2.5 * np.log10(lc) magnitude = appmag + magn #Ivezic 2008, https://arxiv.org/pdf/0805.2366.pdf , Table 2 sigma2_rand = getSig2Rand(filt, magnitude) #random photometric error sigma = ((sigma_sys**2.) + (sigma2_rand))**(1. / 2.) #now add the uncertaintly onto the magnitude #magnitude_obs = magnitude magnitude_obs = [ np.random.normal(loc=x, scale=sig) for (x, sig) in zip(magnitude, sigma) ] delta_mag = max( [delta_mag, abs(min(magnitude_obs) - max(magnitude_obs))]) if (verbose): print(j, 'magn = ', magn[0:10]) print(j, 'magnitude = ', magnitude[0:10]) print(j, 'magnitude_obs = ', magnitude_obs[0:10]) print(j, "sigma2_rand = ", sigma2_rand[0:10]) print(j, "sigma = ", sigma[0:10]) print(j, "delta_mag = ", delta_mag) t = np.array(time) totalt.extend(t) mag = np.array(magnitude_obs) totalmag.extend(mag) dmag = np.array(sigma) totaldmag.extend(dmag) filts = np.array(filt) specfilt = nobs * [filt] totalfilts.extend(specfilt) #model = LombScargle(fit_period = True) model = LombScargleFast(fit_period=True) model.optimizer.period_range = (0.2, drng) model.fit(t, mag, dmag) LSS = model.best_period print('LSS running') return_dict[j] = return_dict[j] + [LSS] print('LSS in return_dict') if (verbose): print(j, "LSS = ", LSS) if (do_plot): if (i == 0): for_plotting['phase_obs'] = dict() for_plotting['mag_obs'] = dict() for_plotting['mag'] = dict() for_plotting['scores'] = dict() for_plotting['LSS'] = dict() phase_obs = np.array([(tt % period) / period for tt in time]) scores = model.score(pds) for_plotting['phase_obs'][filt] = phase_obs for_plotting['mag_obs'][filt] = magnitude_obs for_plotting['mag'][filt] = magnitude for_plotting['scores'][filt] = scores for_plotting['LSS'][filt] = LSS else: return_dict[j] = return_dict[j] + [-999.] if (verbose): print(j, "bad fluxes", filt) #raise Exception('stopping') if (len(totalmag) > 0): t = np.array(totalt) mag = np.array(totalmag) dmag = np.array(totaldmag) filts = np.array(totalfilts) drng = max(t) - min(t) print("drng", drng) model = LombScargleMultiband(Nterms_band=n_band, Nterms_base=n_base, fit_period=True) #model = LombScargleMultibandFast(Nterms=2, fit_period = True) #this is not working in parallel for some reason model.optimizer.period_range = (0.2, drng) model.fit(t, mag, dmag, filts) LSM = model.best_period print('LSM running') return_dict[j] = return_dict[j] + [LSM] #n_totalrun += 1 print('LSM in return_dict') return_dict[j] = return_dict[j] + [delta_mag] else: return_dict[j] = return_dict[j] + [-999.] return_dict[j] = return_dict[j] + [-999.]
def run_ellc_gatspy(self, j): #this is the general simulation - ellc light curves and gatspy periodograms EB = self.return_dict[j] #for the multiband gatspy fit allObsDates = np.array([]) allAppMagObs = np.array([]) allAppMagObsErr = np.array([]) allObsFilters = np.array([]) if (self.verbose): print("in run_ellc_gatspy") for i, filt in enumerate(self.filters): #observe the EB (get dates, create the light curve for this filter) EB.observe(filt) EB.LSS[filt] = -999. if (EB.obsDates[filt][0] != None and min(EB.appMagObs[filt]) > 0): #run gatspy for this filter drng = max(EB.obsDates[filt]) - min(EB.obsDates[filt]) #print("filter, nobs", filt, len(EB.obsDates[filt])) if (self.useFast and len(EB.obsDates[filt]) > 50): model = LombScargleFast(fit_period=True) else: model = LombScargle(fit_period=True) model.optimizer.period_range = (0.2, drng) model.fit(EB.obsDates[filt], EB.appMagObs[filt], EB.appMagObsErr[filt]) EB.LSS[filt] = model.best_period EB.LSSmodel[filt] = model EB.maxDeltaMag = max(EB.deltaMag[filt], EB.maxDeltaMag) #to use for the multiband fit allObsDates = np.append(allObsDates, EB.obsDates[filt]) allAppMagObs = np.append(allAppMagObs, EB.appMagObs[filt]) allAppMagObsErr = np.append(allAppMagObsErr, EB.appMagObsErr[filt]) allObsFilters = np.append( allObsFilters, np.full(len(EB.obsDates[filt]), filt)) if (self.verbose): print(j, 'filter = ', filt) print(j, 'obsDates = ', EB.obsDates[filt][0:10]) print(j, 'appMagObs = ', EB.appMagObs[filt][0:10]) print(j, 'delta_mag = ', EB.deltaMag[filt]) print(j, 'LSS = ', EB.LSS[filt]) if (len(allObsDates) > 0 and self.doLSM): drng = max(allObsDates) - min(allObsDates) if (self.useFast and len(allObsDates) > 50 * len(self.filters)): model = LombScargleMultibandFast(fit_period=True) else: model = LombScargleMultiband(Nterms_band=self.n_band, Nterms_base=self.n_base, fit_period=True) model.optimizer.period_range = (0.2, drng) model.fit(allObsDates, allAppMagObs, allAppMagObsErr, allObsFilters) EB.LSM = model.best_period EB.LSMmodel = model if (self.verbose): print(j, 'LSM =', EB.LSM) #not sure if I need to do this... self.return_dict[j] = EB
min_max_scaler = preprocessing.MinMaxScaler() df['flux1'] = min_max_scaler.fit_transform(df['flux'].values.reshape(-1,1)) df['flux_err1'] = min_max_scaler.fit_transform(df['flux_err'].values.reshape(-1,1)) frequency, power = LombScargle(df['mjd'], df['flux1'], dy=df['flux_err1']).autopower() period = 1/frequency[np.argmax(power)] df['mjd1'] = df['mjd']%period sns.scatterplot(x='mjd1', y='flux1', data=df) for i in range(5): target_obj = target_object_ids.sample().values[0] DFlc = target_df.loc[target_df.object_id==target_obj].copy() DFlc['filts'] = DFlc['passband'].map(pb_mapping) model = LombScargleMultiband(fit_period=True) t_min = max(np.median(np.diff(sorted(DFlc['mjd']))), 0.1) t_max = min(10., (DFlc['mjd'].max() - DFlc['mjd'].min())/2.) model.optimizer.set(period_range=(t_min, t_max), first_pass_coverage=5) model.fit(DFlc['mjd'], DFlc['flux'], dy=DFlc['flux_err'], filts=DFlc['filts']) models.append(model) #calculate lomb error for each object_id for obj_id, DFlc in train.groupby('object_id'): DFlc = DFlc.sort_values('mjd') DFlc['filts'] = DFlc['passband'].map(pb_mapping) y_pred = np.zeros((DFlc.shape[0],)) for mdl in models: y_pred += mdl.predict(DFlc.mjd, filts=DFlc['filts'],