def get_noise_array_2D(self, noise, xoffset, zoffset, xsize, zsize, xscale, zscale, noise_scale): """ Create a 2D grid of noise values. """ p = self._p x = arange(xsize) * xscale + xoffset + self._xoffset z = arange(zsize) * zscale + zoffset + self._zoffset x, z = meshgrid(x, z) xi = npfloor(x).astype(int) & 255 zi = npfloor(z).astype(int) & 255 xf = x - npfloor(x) zf = z - npfloor(z) u = fade(xf) v = fade(zf) n00 = gradient2d(p[p[xi] + zi], xf, zf) n01 = gradient2d(p[p[xi] + zi + 1], xf, zf - 1) n11 = gradient2d(p[p[xi + 1] + zi + 1], xf - 1, zf - 1) n10 = gradient2d(p[p[xi + 1] + zi], xf - 1, zf) x1 = lerp(n00, n10, u) x2 = lerp(n01, n11, u) noise += lerp(x1, x2, v) / noise_scale
def _get_phased_quantities(stimes, smags, serrs, period): ''' Given finite and sigma-clipped times, magnitudes, and errors (i.e. the output of lcfit.get_finite_and_sigclipped_data), along with the period at which to phase-fold the data, perform the phase-folding and return: 1) phase: phase-sorted values of phase at each of stimes 2) pmags: phase-sorted magnitudes at each phase 3) perrs: phase-sorted errors 4) ptimes: phase-sorted times 5) mintime: earliest time in stimes. ''' # phase the mag series using the given period and faintest mag time # mintime = stimes[npwhere(smags == npmax(smags))] # phase the mag series using the given period and epoch = min(stimes) mintime = npmin(stimes) # calculate the unsorted phase, then sort it iphase = (stimes - mintime) / period - npfloor((stimes - mintime) / period) phasesortind = npargsort(iphase) # these are the final quantities to use for the Fourier fits phase = iphase[phasesortind] pmags = smags[phasesortind] perrs = serrs[phasesortind] # get the times sorted in phase order (useful to get the fit mag minimum # with respect to phase -- the light curve minimum) ptimes = stimes[phasesortind] return phase, pmags, perrs, ptimes, mintime
def sine_series_sum(fourierparams, times, mags, errs): '''This generates a sinusoidal light curve using a sine series. The series is generated using the coefficients provided in fourierparams. This is a sequence like so: [period, epoch, [ampl_1, ampl_2, ampl_3, ..., ampl_X], [pha_1, pha_2, pha_3, ..., pha_X]] where X is the Fourier order. ''' period, epoch, famps, fphases = fourierparams # figure out the order from the length of the Fourier param list forder = len(famps) # phase the times with this period iphase = (times - epoch) / period iphase = iphase - npfloor(iphase) phasesortind = npargsort(iphase) phase = iphase[phasesortind] ptimes = times[phasesortind] pmags = mags[phasesortind] perrs = errs[phasesortind] # calculate all the individual terms of the series fseries = [ famps[x] * npsin(2.0 * MPI * x * phase + fphases[x]) for x in range(forder) ] # this is the zeroth order coefficient - a constant equal to median mag modelmags = npmedian(mags) # sum the series for fo in fseries: modelmags += fo return modelmags, phase, ptimes, pmags, perrs
def undistortTrajectory(invMap1, invMap2, positions): floorPositions = npfloor(positions) #ceilPositions = npceil(positions) undistortedTrajectory = [[],[]] for i in xrange(len(positions[0])): x,y = None, None if positions[0][i]+1 < invMap1.shape[1] and positions[1][i]+1 < invMap1.shape[0]: floorX = invMap1[floorPositions[1][i], floorPositions[0][i]] floorY = invMap2[floorPositions[1][i], floorPositions[0][i]] ceilX = invMap1[floorPositions[1][i]+1, floorPositions[0][i]+1] ceilY = invMap2[floorPositions[1][i]+1, floorPositions[0][i]+1] #ceilX = invMap1[ceilPositions[1][i], ceilPositions[0][i]] #ceilY = invMap2[ceilPositions[1][i], ceilPositions[0][i]] if floorX >=0 and floorY >=0 and ceilX >=0 and ceilY >=0: x = floorX+(positions[0][i]-floorPositions[0][i])*(ceilX-floorX) y = floorY+(positions[1][i]-floorPositions[1][i])*(ceilY-floorY) undistortedTrajectory[0].append(x) undistortedTrajectory[1].append(y) return undistortedTrajectory
def dworetsky_period_find(time, mag, err, init_p, end_p, f_step, verbose=False): ''' This is the super-slow naive version taken from my thesis work. Uses the string length method in Dworetsky 1983 to calculate the period of a time-series of magnitude measurements and associated magnitude errors. Searches in linear frequency space (which obviously doesn't correspond to a linear period space). PARAMETERS: time: series of times at which mags were measured (usually some form of JD) mag: timeseries of magnitudes (np.array) err: associated errs per magnitude measurement (np.array) init_p, end_p: interval to search for periods between (both ends inclusive) f_step: step in frequency [days^-1] to use RETURNS: tuple of the following form: (periods (np.array), string_lengths (np.array), good_period_mask (boolean array)) ''' mod_mag = (mag - npmin(mag)) / (2.0 * (npmax(mag) - npmin(mag))) - 0.25 fold_time = npmin(time) # fold at the first time element init_f = 1.0 / end_p end_f = 1.0 / init_p n_freqs = npceil((end_f - init_f) / f_step) if verbose: print('searching %s frequencies between %s and %s days^-1...' % (n_freqs, init_f, end_f)) out_periods = npempty(n_freqs, dtype=np.float64) out_strlens = npempty(n_freqs, dtype=np.float64) p_goodflags = npempty(n_freqs, dtype=bool) j_range = len(mag) - 1 for i in range(int(n_freqs)): period = 1.0 / init_f # print('P: %s, f: %s, i: %s, n_freqs: %s, maxf: %s' % # (period, init_f, i, n_freqs, end_f)) phase = (time - fold_time) / period - npfloor( (time - fold_time) / period) phase_sort_ind = npargsort(phase) phase_sorted = phase[phase_sort_ind] mod_mag_sorted = mod_mag[phase_sort_ind] strlen = 0.0 epsilon = 2.0 * npmean(err) delta_l = 0.34 * (epsilon - 0.5 * (epsilon**2)) * (len(time) - npsqrt(10.0 / epsilon)) keep_threshold_1 = 1.6 + 1.2 * delta_l l = 0.212 * len(time) sig_l = len(time) / 37.5 keep_threshold_2 = l + 4.0 * sig_l # now calculate the string length for j in range(j_range): strlen += npsqrt((mod_mag_sorted[j + 1] - mod_mag_sorted[j])**2 + (phase_sorted[j + 1] - phase_sorted[j])**2) strlen += npsqrt((mod_mag_sorted[0] - mod_mag_sorted[-1])**2 + (phase_sorted[0] - phase_sorted[-1] + 1)**2) if ((strlen < keep_threshold_1) or (strlen < keep_threshold_2)): p_goodflags[i] = True out_periods[i] = period out_strlens[i] = strlen init_f += f_step return (out_periods, out_strlens, p_goodflags)
def invgauss_eclipses_func(ebparams, times, mags, errs): '''This returns a double eclipse shaped function. Suitable for first order modeling of eclipsing binaries. ebparams = [period (time), epoch (time), pdepth (mags), pduration (phase), psdepthratio, secondaryphase] period is the period in days epoch is the time of minimum in JD pdepth is the depth of the primary eclipse - for magnitudes -> transitdepth should be < 0 - for fluxes -> transitdepth should be > 0 pduration is the length of the primary eclipse in phase psdepthratio is the ratio in the eclipse depths: depth_secondary/depth_primary. This is generally the same as the ratio of the Teffs of the two stars. secondaryphase is the phase at which the minimum of the secondary eclipse is located. This effectively parameterizes eccentricity. All of these will then have fitted values after the fit is done. ''' (period, epoch, pdepth, pduration, depthratio, secondaryphase) = ebparams # generate the phases iphase = (times - epoch) / period iphase = iphase - npfloor(iphase) phasesortind = npargsort(iphase) phase = iphase[phasesortind] ptimes = times[phasesortind] pmags = mags[phasesortind] perrs = errs[phasesortind] zerolevel = npmedian(pmags) modelmags = npfull_like(phase, zerolevel) primaryecl_amp = -pdepth secondaryecl_amp = -pdepth * depthratio primaryecl_std = pduration / 5.0 # we use 5-sigma as full-width -> duration secondaryecl_std = pduration / 5.0 # secondary eclipse has the same duration halfduration = pduration / 2.0 # phase indices primary_eclipse_ingress = ((phase >= (1.0 - halfduration)) & (phase <= 1.0)) primary_eclipse_egress = ((phase >= 0.0) & (phase <= halfduration)) secondary_eclipse_phase = ((phase >= (secondaryphase - halfduration)) & (phase <= (secondaryphase + halfduration))) # put in the eclipses modelmags[primary_eclipse_ingress] = (zerolevel + _gaussian( phase[primary_eclipse_ingress], primaryecl_amp, 1.0, primaryecl_std)) modelmags[primary_eclipse_egress] = (zerolevel + _gaussian( phase[primary_eclipse_egress], primaryecl_amp, 0.0, primaryecl_std)) modelmags[secondary_eclipse_phase] = ( zerolevel + _gaussian(phase[secondary_eclipse_phase], secondaryecl_amp, secondaryphase, secondaryecl_std)) return modelmags, phase, ptimes, pmags, perrs
def spline_fit_magseries(times, mags, errs, period, knotfraction=0.01, maxknots=30, sigclip=30.0, plotfit=False, ignoreinitfail=False, magsarefluxes=False, verbose=True): '''This fits a univariate cubic spline to the phased light curve. This fit may be better than the Fourier fit for sharply variable objects, like EBs, so can be used to distinguish them from other types of variables. The knot fraction is the number of internal knots to use for the spline. A value of 0.01 (or 1%) of the total number of non-nan observations appears to work quite well, without over-fitting. maxknots controls the maximum number of knots that will be allowed. magsarefluxes is a boolean value for setting the ylabel and ylimits of plots for either magnitudes (False) or flux units (i.e. normalized to 1, in which case magsarefluxes should be set to True). Returns the chisq of the fit, as well as the reduced chisq. FIXME: check this equation below to see if it's right. reduced_chisq = fit_chisq/(len(pmags) - len(knots) - 1) ''' # this is required to fit the spline correctly if errs is None: errs = npfull_like(mags, 0.005) # sigclip the magnitude time series stimes, smags, serrs = sigclip_magseries(times, mags, errs, sigclip=sigclip, magsarefluxes=magsarefluxes) # get rid of zero errs nzind = npnonzero(serrs) stimes, smags, serrs = stimes[nzind], smags[nzind], serrs[nzind] # phase the mag series phase, pmags, perrs, ptimes, mintime = (_get_phased_quantities( stimes, smags, serrs, period)) # now figure out the number of knots up to max knots (=100) nobs = len(phase) nknots = int(npfloor(knotfraction * nobs)) nknots = maxknots if nknots > maxknots else nknots splineknots = nplinspace(phase[0] + 0.01, phase[-1] - 0.01, num=nknots) # generate and fit the spline spl = LSQUnivariateSpline(phase, pmags, t=splineknots, w=1.0 / perrs) # calculate the spline fit to the actual phases, the chisq and red-chisq fitmags = spl(phase) fitchisq = npsum(((fitmags - pmags) * (fitmags - pmags)) / (perrs * perrs)) fitredchisq = fitchisq / (len(pmags) - nknots - 1) if verbose: LOGINFO('spline fit done. nknots = %s, ' 'chisq = %.5f, reduced chisq = %.5f' % (nknots, fitchisq, fitredchisq)) # figure out the time of light curve minimum (i.e. the fit epoch) # this is when the fit mag is maximum (i.e. the faintest) # or if magsarefluxes = True, then this is when fit flux is minimum if not magsarefluxes: fitmagminind = npwhere(fitmags == npmax(fitmags)) else: fitmagminind = npwhere(fitmags == npmin(fitmags)) magseriesepoch = ptimes[fitmagminind] # assemble the returndict returndict = { 'fittype': 'spline', 'fitinfo': { 'nknots': nknots, 'fitmags': fitmags, 'fitepoch': magseriesepoch }, 'fitchisq': fitchisq, 'fitredchisq': fitredchisq, 'fitplotfile': None, 'magseries': { 'times': ptimes, 'phase': phase, 'mags': pmags, 'errs': perrs, 'magsarefluxes': magsarefluxes }, } # make the fit plot if required if plotfit and isinstance(plotfit, str): _make_fit_plot(phase, pmags, perrs, fitmags, period, mintime, magseriesepoch, plotfit, magsarefluxes=magsarefluxes) returndict['fitplotfile'] = plotfit return returndict
def spline_fit_magseries(times, mags, errs, period, knotfraction=0.01, maxknots=30, sigclip=30.0, plotfit=False, ignoreinitfail=False, magsarefluxes=False, verbose=True): '''This fits a univariate cubic spline to the phased light curve. This fit may be better than the Fourier fit for sharply variable objects, like EBs, so can be used to distinguish them from other types of variables. Parameters ---------- times,mags,errs : np.array The input mag/flux time-series to fit a spline to. period : float The period to use for the spline fit. knotfraction : float The knot fraction is the number of internal knots to use for the spline. A value of 0.01 (or 1%) of the total number of non-nan observations appears to work quite well, without over-fitting. maxknots controls the maximum number of knots that will be allowed. maxknots : int The maximum number of knots that will be used even if `knotfraction` gives a value to use larger than `maxknots`. This helps dealing with over-fitting to short time-scale variations. sigclip : float or int or sequence of two floats/ints or None If a single float or int, a symmetric sigma-clip will be performed using the number provided as the sigma-multiplier to cut out from the input time-series. If a list of two ints/floats is provided, the function will perform an 'asymmetric' sigma-clip. The first element in this list is the sigma value to use for fainter flux/mag values; the second element in this list is the sigma value to use for brighter flux/mag values. For example, `sigclip=[10., 3.]`, will sigclip out greater than 10-sigma dimmings and greater than 3-sigma brightenings. Here the meaning of "dimming" and "brightening" is set by *physics* (not the magnitude system), which is why the `magsarefluxes` kwarg must be correctly set. If `sigclip` is None, no sigma-clipping will be performed, and the time-series (with non-finite elems removed) will be passed through to the output. magsarefluxes : bool If True, will treat the input values of `mags` as fluxes for purposes of plotting the fit and sig-clipping. plotfit : str or False If this is a string, this function will make a plot for the fit to the mag/flux time-series and writes the plot to the path specified here. ignoreinitfail : bool If this is True, ignores the initial failure to find a set of optimized Fourier parameters using the global optimization function and proceeds to do a least-squares fit anyway. verbose : bool If True, will indicate progress and warn of any problems. Returns ------- dict This function returns a dict containing the model fit parameters, the minimized chi-sq value and the reduced chi-sq value. The form of this dict is mostly standardized across all functions in this module:: { 'fittype':'spline', 'fitinfo':{ 'nknots': the number of knots used for the fit 'fitmags': the model fit mags, 'fitepoch': the epoch of minimum light for the fit, }, 'fitchisq': the minimized value of the fit's chi-sq, 'fitredchisq':the reduced chi-sq value, 'fitplotfile': the output fit plot if fitplot is not None, 'magseries':{ 'times':input times in phase order of the model, 'phase':the phases of the model mags, 'mags':input mags/fluxes in the phase order of the model, 'errs':errs in the phase order of the model, 'magsarefluxes':input value of magsarefluxes kwarg } } ''' # this is required to fit the spline correctly if errs is None: errs = npfull_like(mags, 0.005) # sigclip the magnitude time series stimes, smags, serrs = sigclip_magseries(times, mags, errs, sigclip=sigclip, magsarefluxes=magsarefluxes) # get rid of zero errs nzind = npnonzero(serrs) stimes, smags, serrs = stimes[nzind], smags[nzind], serrs[nzind] # phase the mag series phase, pmags, perrs, ptimes, mintime = ( get_phased_quantities(stimes, smags, serrs, period) ) # now figure out the number of knots up to max knots (=100) nobs = len(phase) nknots = int(npfloor(knotfraction*nobs)) nknots = maxknots if nknots > maxknots else nknots splineknots = nplinspace(phase[0] + 0.01, phase[-1] - 0.01, num=nknots) # NOTE: newer scipy needs x to be strictly increasing. this means we should # filter out anything that doesn't have np.diff(phase) > 0.0 # FIXME: this needs to be tested phase_diffs_ind = npdiff(phase) > 0.0 incphase_ind = npconcatenate((nparray([True]), phase_diffs_ind)) phase, pmags, perrs = (phase[incphase_ind], pmags[incphase_ind], perrs[incphase_ind]) # generate and fit the spline spl = LSQUnivariateSpline(phase, pmags, t=splineknots, w=1.0/perrs) # calculate the spline fit to the actual phases, the chisq and red-chisq fitmags = spl(phase) fitchisq = npsum( ((fitmags - pmags)*(fitmags - pmags)) / (perrs*perrs) ) fitredchisq = fitchisq/(len(pmags) - nknots - 1) if verbose: LOGINFO( 'spline fit done. nknots = %s, ' 'chisq = %.5f, reduced chisq = %.5f' % (nknots, fitchisq, fitredchisq) ) # figure out the time of light curve minimum (i.e. the fit epoch) # this is when the fit mag is maximum (i.e. the faintest) # or if magsarefluxes = True, then this is when fit flux is minimum if not magsarefluxes: fitmagminind = npwhere(fitmags == npmax(fitmags)) else: fitmagminind = npwhere(fitmags == npmin(fitmags)) if len(fitmagminind[0]) > 1: fitmagminind = (fitmagminind[0][0],) magseriesepoch = ptimes[fitmagminind] # assemble the returndict returndict = { 'fittype':'spline', 'fitinfo':{ 'nknots':nknots, 'fitmags':fitmags, 'fitepoch':magseriesepoch }, 'fitchisq':fitchisq, 'fitredchisq':fitredchisq, 'fitplotfile':None, 'magseries':{ 'times':ptimes, 'phase':phase, 'mags':pmags, 'errs':perrs, 'magsarefluxes':magsarefluxes }, } # make the fit plot if required if plotfit and isinstance(plotfit, str): make_fit_plot(phase, pmags, perrs, fitmags, period, mintime, magseriesepoch, plotfit, magsarefluxes=magsarefluxes) returndict['fitplotfile'] = plotfit return returndict
def floor(x): r"""Round downwards to nearest integer """ return npfloor(x)
def mask_signal(times, mags, errs, signalperiod, signalepoch, magsarefluxes=False, maskphases=[0, 0, 0.5, 1.0], maskphaselength=0.1, plotfit=None, plotfitphasedlconly=True, sigclip=30.0): '''This removes repeating signals in the magnitude time series. Useful for masking transit signals in light curves to search for other variability. ''' stimes, smags, serrs = sigclip_magseries(times, mags, errs, sigclip=sigclip, magsarefluxes=magsarefluxes) # now phase the light curve using the period and epoch provided phases = ((stimes - signalepoch) / signalperiod - npfloor( (stimes - signalepoch) / signalperiod)) # mask the requested phases using the mask length (in phase units) # this gets all the masks into one array masks = nparray([(npabs(phases - x) > maskphaselength) for x in maskphases]) # this flattens the masks to a single array for all combinations masks = npall(masks, axis=0) # apply the mask to the times, mags, and errs mphases = phases[masks] mtimes = stimes[masks] mmags = smags[masks] merrs = serrs[masks] returndict = { 'mphases': mphases, 'mtimes': mtimes, 'mmags': mmags, 'merrs': merrs } # make the fit plot if required if plotfit and isinstance(plotfit, str) or isinstance(plotfit, strio): if plotfitphasedlconly: plt.figure(figsize=(10, 4.8)) else: plt.figure(figsize=(16, 9.6)) if plotfitphasedlconly: # phased series before whitening plt.subplot(121) plt.plot(phases, smags, marker='.', color='k', linestyle='None', markersize=2.0, markeredgewidth=0) if not magsarefluxes: plt.gca().invert_yaxis() plt.ylabel('magnitude') else: plt.ylabel('fluxes') plt.xlabel('phase') plt.title('phased LC before signal masking') # phased series after whitening plt.subplot(122) plt.plot(mphases, mmags, marker='.', color='g', linestyle='None', markersize=2.0, markeredgewidth=0) if not magsarefluxes: plt.gca().invert_yaxis() plt.ylabel('magnitude') else: plt.ylabel('fluxes') plt.xlabel('phase') plt.title('phased LC after signal masking') else: # time series before whitening plt.subplot(221) plt.plot(stimes, smags, marker='.', color='k', linestyle='None', markersize=2.0, markeredgewidth=0) if not magsarefluxes: plt.gca().invert_yaxis() plt.ylabel('magnitude') else: plt.ylabel('fluxes') plt.xlabel('JD') plt.title('LC before signal masking') # time series after whitening plt.subplot(222) plt.plot(mtimes, mmags, marker='.', color='g', linestyle='None', markersize=2.0, markeredgewidth=0) if not magsarefluxes: plt.gca().invert_yaxis() plt.ylabel('magnitude') else: plt.ylabel('fluxes') plt.xlabel('JD') plt.title('LC after signal masking') # phased series before whitening plt.subplot(223) plt.plot(phases, smags, marker='.', color='k', linestyle='None', markersize=2.0, markeredgewidth=0) if not magsarefluxes: plt.gca().invert_yaxis() plt.ylabel('magnitude') else: plt.ylabel('fluxes') plt.xlabel('phase') plt.title('phased LC before signal masking') # phased series after whitening plt.subplot(224) plt.plot(mphases, mmags, marker='.', color='g', linestyle='None', markersize=2.0, markeredgewidth=0) if not magsarefluxes: plt.gca().invert_yaxis() plt.ylabel('magnitude') else: plt.ylabel('fluxes') plt.xlabel('phase') plt.title('phased LC after signal masking') plt.tight_layout() plt.savefig(plotfit, format='png', pad_inches=0.0) plt.close() if isinstance(plotfit, str) or isinstance(plotfit, strio): returndict['fitplotfile'] = plotfit return returndict
def prewhiten_magseries(times, mags, errs, whitenperiod, whitenparams, sigclip=3.0, magsarefluxes=False, plotfit=None, plotfitphasedlconly=True, rescaletomedian=True): '''Removes a periodic sinusoidal signal generated using whitenparams from the input magnitude time series. whitenparams are the Fourier amplitude and phase coefficients: [ampl_1, ampl_2, ampl_3, ..., ampl_X, pha_1, pha_2, pha_3, ..., pha_X] where X is the Fourier order. These are usually the output of a previous Fourier fit to the light curve (from varbase.lcfit.fourier_fit_magseries for example). if rescaletomedian is True, then we add back the constant median term of the magnitudes to the final pre-whitened mag series. ''' stimes, smags, serrs = sigclip_magseries(times, mags, errs, sigclip=sigclip, magsarefluxes=magsarefluxes) median_mag = npmedian(smags) # phase the mag series using the given period and epoch = min(stimes) mintime = npmin(stimes) # calculate the unsorted phase, then sort it iphase = ((stimes - mintime) / whitenperiod - npfloor( (stimes - mintime) / whitenperiod)) phasesortind = npargsort(iphase) # these are the final quantities to use for the Fourier fits phase = iphase[phasesortind] pmags = smags[phasesortind] perrs = serrs[phasesortind] # get the times sorted in phase order (useful to get the fit mag minimum # with respect to phase -- the light curve minimum) ptimes = stimes[phasesortind] # get the Fourier order fourierorder = int(len(whitenparams) / 2) # now subtract the harmonic series from the phased LC # these are still in phase order wmags = pmags - _fourier_func(whitenparams, phase, pmags) # resort everything by time order wtimeorder = npargsort(ptimes) wtimes = ptimes[wtimeorder] wphase = phase[wtimeorder] wmags = wmags[wtimeorder] werrs = perrs[wtimeorder] if rescaletomedian: wmags = wmags + median_mag # prepare the returndict returndict = { 'wtimes': wtimes, # these are in the new time order 'wphase': wphase, 'wmags': wmags, 'werrs': werrs, 'whitenparams': whitenparams, 'whitenperiod': whitenperiod } # make the fit plot if required if plotfit and (isinstance(plotfit, str) or isinstance(plotfit, strio)): if plotfitphasedlconly: plt.figure(figsize=(10, 4.8)) else: plt.figure(figsize=(16, 9.6)) if plotfitphasedlconly: # phased series before whitening plt.subplot(121) plt.plot(phase, pmags, marker='.', color='k', linestyle='None', markersize=2.0, markeredgewidth=0) if not magsarefluxes: plt.gca().invert_yaxis() plt.ylabel('magnitude') else: plt.ylabel('fluxes') plt.xlabel('phase') plt.title('phased LC before pre-whitening') # phased series after whitening plt.subplot(122) plt.plot(wphase, wmags, marker='.', color='g', linestyle='None', markersize=2.0, markeredgewidth=0) if not magsarefluxes: plt.gca().invert_yaxis() plt.ylabel('magnitude') else: plt.ylabel('fluxes') plt.xlabel('phase') plt.title('phased LC after pre-whitening') else: # time series before whitening plt.subplot(221) plt.plot(stimes, smags, marker='.', color='k', linestyle='None', markersize=2.0, markeredgewidth=0) if not magsarefluxes: plt.gca().invert_yaxis() plt.ylabel('magnitude') else: plt.ylabel('fluxes') plt.xlabel('JD') plt.title('LC before pre-whitening') # time series after whitening plt.subplot(222) plt.plot(wtimes, wmags, marker='.', color='g', linestyle='None', markersize=2.0, markeredgewidth=0) if not magsarefluxes: plt.gca().invert_yaxis() plt.ylabel('magnitude') else: plt.ylabel('fluxes') plt.xlabel('JD') plt.title('LC after pre-whitening with period: %.6f' % whitenperiod) # phased series before whitening plt.subplot(223) plt.plot(phase, pmags, marker='.', color='k', linestyle='None', markersize=2.0, markeredgewidth=0) if not magsarefluxes: plt.gca().invert_yaxis() plt.ylabel('magnitude') else: plt.ylabel('fluxes') plt.xlabel('phase') plt.title('phased LC before pre-whitening') # phased series after whitening plt.subplot(224) plt.plot(wphase, wmags, marker='.', color='g', linestyle='None', markersize=2.0, markeredgewidth=0) if not magsarefluxes: plt.gca().invert_yaxis() plt.ylabel('magnitude') else: plt.ylabel('fluxes') plt.xlabel('phase') plt.title('phased LC after pre-whitening') plt.tight_layout() plt.savefig(plotfit, format='png', pad_inches=0.0) plt.close() if isinstance(plotfit, str) or isinstance(plotfit, strio): returndict['fitplotfile'] = plotfit return returndict
def floor(x): r"""Absolute value """ return npfloor(x)
def trapezoid_transit_func(transitparams, times, mags, errs): '''This returns a trapezoid transit-shaped function. Suitable for first order modeling of transit signals. transitparams = [transitperiod (time), transitepoch (time), transitdepth (flux or mags), transitduration (phase), ingressduration (phase)] All of these will then have fitted values after the fit is done. for magnitudes -> transitdepth should be < 0 for fluxes -> transitdepth should be > 0 ''' (transitperiod, transitepoch, transitdepth, transitduration, ingressduration) = transitparams # generate the phases iphase = (times - transitepoch) / transitperiod iphase = iphase - npfloor(iphase) phasesortind = npargsort(iphase) phase = iphase[phasesortind] ptimes = times[phasesortind] pmags = mags[phasesortind] perrs = errs[phasesortind] zerolevel = npmedian(pmags) modelmags = npfull_like(phase, zerolevel) halftransitduration = transitduration / 2.0 bottomlevel = zerolevel - transitdepth slope = transitdepth / ingressduration # the four contact points of the eclipse firstcontact = 1.0 - halftransitduration secondcontact = firstcontact + ingressduration thirdcontact = halftransitduration - ingressduration fourthcontact = halftransitduration ## the phase indices ## # during ingress ingressind = (phase > firstcontact) & (phase < secondcontact) # at transit bottom bottomind = (phase > secondcontact) | (phase < thirdcontact) # during egress egressind = (phase > thirdcontact) & (phase < fourthcontact) # set the mags modelmags[ingressind] = zerolevel - slope * (phase[ingressind] - firstcontact) modelmags[bottomind] = bottomlevel modelmags[egressind] = bottomlevel + slope * (phase[egressind] - thirdcontact) return modelmags, phase, ptimes, pmags, perrs