def wavelet(Y, dt, param, dj, s0, j1, mother): """Computes the wavelet continuous transform of the vector Y, by definition: W(a,b) = sum(f(t)*psi[a,b](t) dt) a dilate/contract psi[a,b](t) = 1/sqrt(a) psi(t-b/a) b displace Only Morlet wavelet (k0=6) is used The wavelet basis is normalized to have total energy = 1 at all scales _____________________________________________________________________ Input: Y - time series dt - sampling rate mother - the mother wavelet function param - the mother wavelet parameter Output: ondaleta - wavelet bases at scale 10 dt wave - wavelet transform of Y period - the vector of "Fourier"periods ( in time units) that correspond to the scales scale - the vector of scale indices, given by S0*2(j*DJ), j =0 ...J1 coi - cone of influence Call function: ondaleta, wave, period, scale, coi = wavelet(Y,dt,mother,param) _____________________________________________________________________ """ n1 = len(Y) # time series length #s0 = 2 * dt # smallest scale of the wavelet # dj = 0.25 # spacing between discrete scales # J1 = int(np.floor((np.log10(n1*dt/s0))/np.log10(2)/dj)) J1 = int(np.floor(np.log2(n1 * dt / s0) / dj)) # J1+1 total os scales # print 'Nr of Scales:', J1 # J1= 60 # pad if necessary x = detrend_mean(Y) # extract the mean of time series pad = 1 if (pad == 1): base2 = nextpow2(n1) # call det nextpow2 n = base2 """CAUTION""" # construct wavenumber array used in transform # simetric eqn 5 k = np.arange(n / 2) import math k_pos, k_neg = [], [] for i in range(0, n / 2 + 1): k_pos.append(i * ((2 * math.pi) / (n * dt))) # frequencies as in eqn5 k_neg = k_pos[::-1] # inversion vector k_neg = [e * (-1) for e in k_neg] # negative part # delete the first value of k_neg = last value of k_pos k_neg = k_neg[1:-1] k = np.concatenate((k_pos, k_neg), axis=1) # vector of symmetric # compute fft of the padded time series f = np.fft.fft(x, n) scale = [] for i in range(J1 + 1): scale.append(s0 * pow(2, (i) * dj)) period = scale # print period wave = np.zeros((J1 + 1, n)) # define wavelet array wave = wave + 1j * wave # make it complex # loop through scales and compute transform for a1 in range(J1 + 1): daughter, fourier_factor, coi, dofmin = wave_bases( mother, k, scale[a1], param) # call wave_bases wave[a1, :] = np.fft.ifft(f * daughter) # wavelet transform if a1 == 11: ondaleta = daughter # ondaleta = daughter period = np.array(period) period = period[:] * fourier_factor # cone-of-influence, differ for uneven len of timeseries: if (((n1) / 2.0).is_integer()) is True: # create mirrored array) mat = np.concatenate((range(1, n1 / 2), range(1, n1 / 2)[::-1]), axis=1) # insert zero at the begining of the array mat = np.insert(mat, 0, 0) mat = np.append(mat, 0) # insert zero at the end of the array elif (((n1) / 2.0).is_integer()) is False: # create mirrored array mat = np.concatenate((range(1, n1 / 2 + 1), range(1, n1 / 2)[::-1]), axis=1) # insert zero at the begining of the array mat = np.insert(mat, 0, 0) mat = np.append(mat, 0) # insert zero at the end of the array coi = [coi * dt * m for m in mat] # create coi matrix # problem with first and last entry in coi added next to lines because # log2 of zero is not defined and cannot be plottet later: coi[0] = 0.1 # coi[0] is normally 0 coi[len(coi) - 1] = 0.1 # coi[last entry] is normally 0 too wave = wave[:, 0:n1] return ondaleta, wave, period, scale, coi, f
def fftransform(self, fancy=True, cutoff_freq=8.83): """fftransform(KeplerData kd) Perform the FFT of the data stored in self and return the same KeplerData object with the transformed data. """ #fancy = False print "________________________________________" if fancy: print " performing fancy FFT " else: print " performing FFT " if not self.is_fft: self.flux_data = copy.deepcopy(self.data) self.cadence_time = copy.deepcopy(self.time_data) dataT = self.data.T newdata = [] for j in range(len(dataT)): pdc_data = dataT[j] # print "pdc_data: ", pdc_data time = self.time_data # Ensure that no NaNs exist for i in range(len(pdc_data)): if pylab.isnan(pdc_data[i]): pdc_data[i] = pdc_data[i - 1] # Ensure that time starts from zero # Time is now in days time = time - time[0] pdc_data = pylab.detrend_mean(pdc_data) pdc_data = pdc_data / np.std(pdc_data) # Do FFT n = len(pdc_data) if fancy: fft_pdc = abs(10 * np.fft.fft(pdc_data, 10 * n)) # More data points else: fft_pdc = abs(np.fft.fft(pdc_data, n)) # Normal FFT n = len(fft_pdc) timediffs = [self.cadence_time[i + 1] - self.cadence_time[i] for i in \ range(len(self.cadence_time) - 1)] spacing = np.mean( np.array(timediffs)[np.where(~np.isnan(timediffs))]) freq = np.fft.fftfreq(n, spacing) half_n = np.floor(n / 2.0) fft_pdc_half = (2.0 / n) * fft_pdc[:half_n] freq_half = freq[:half_n] # Low pass filter # Use frequency of 8.83: exoplanet with highest frequency is 2.207 / day # Capture two harmonics if fancy: wherefq = np.where(freq_half > cutoff_freq) cutoff = wherefq[0][0] freq_half = freq_half[:cutoff] fft_pdc_half = fft_pdc_half[:cutoff] if newdata == []: newdata = np.zeros((dataT.shape[0], len(fft_pdc_half))) newdata[j] = fft_pdc_half # print "newdata[%d]: " % j, newdata[j] # print "newdata[0]: ", newdata[0] # print "j: ", j # Time.sleep(5) self.time_data = freq_half self.xvals = freq_half self.data = newdata.T self.is_fft = True
def read_kepler_dir(cls, kd, filepath, extension): """read_kepler_dir(filepath, extension) Read in all files with given extension in the folder given by filepath and save to a pickle. The pickle is called filepath/kepler.extension.pkl """ import pyfits # GET ALL FILES WITH GIVEN EXTENSION IN FILEPATH files = glob.glob(str(filepath) + "*" + str(extension)) print "found %d files with extension %s in %s:" % (len(files), extension, filepath) assert len(files) > 0 numfiles = len(files) seen = 0 percent = 0.0 printed = [False for foo in range(1000)] cadence = [] flux = [] data = [] labels = [] for filename in files: hdulist = pyfits.open(filename) d = np.array([(t,cad,s,pdc,q) for \ (t,corr,cad,s,s_err,bg,bg_err,pdc,pdc_err, q,psf1,psf1_err,psf2,psf2_err, mom1,mom1_err,mom2,mom2_err,pc1,pc2) in hdulist[1].data]) # print "Read in %s" % filename use = ((d[:, 2] > 0).nonzero())[0] #data = d[use,2] # ignore all nans time_data = d[:, 0] cadence_data = d[:, 1] flux_data = d[:, 2] pdc_data = d[:, 3] keplerid = int(hdulist[0].header['KEPLERID']) obsmode = hdulist[0].header['OBSMODE'].split()[0] # Replace nan with predecessor (fill) # Need to do this to use "detrend" below # Temporary and breaks value of "missingmethod" in demud for i in range(len(flux_data)): if pylab.isnan(flux_data[i]): flux_data[i] = flux_data[i - 1] for i in range(len(pdc_data)): if pylab.isnan(pdc_data[i]): pdc_data[i] = pdc_data[i - 1] # Make sure all sources have same cadence (they should) if cadence == []: cadence = cadence_data assert (cadence == cadence_data).all() # Subtract mean from data pdc_data = pylab.detrend_mean(pdc_data) flux_data = pylab.detrend_mean(flux_data) flux += [[float(x) for x in pdc_data]] labels.append(str(keplerid) + '-' + obsmode) seen += 1 # output read-in progress if percent < 100: if (round((seen / float(numfiles)) * 100, 1) >= percent) and (printed[int(percent * 10)] == False): print "...%3.1f%%..." % percent printed[int(percent * 10)] == True percent = round(((seen / float(numfiles)) * 100), 1) + 0.1 print "...100%..." data = np.array(flux).T # Output the pickle outf = open(kd.archive, 'w') pickle.dump((data, time_data, cadence, labels), outf) outf.close() print "Wrote pickle to " + kd.archive
def read_kepler_dir(cls, kd, filepath, extension): """read_kepler_dir(filepath, extension) Read in all files with given extension in the folder given by filepath and save to a pickle. The pickle is called filepath/kepler.extension.pkl """ import pyfits # GET ALL FILES WITH GIVEN EXTENSION IN FILEPATH files = glob.glob(str(filepath) + "*" + str(extension)) print "found %d files with extension %s in %s:" % (len(files), extension, filepath) assert len(files) > 0 numfiles = len(files) seen = 0 percent = 0.0 printed = [False for foo in range(1000)] cadence = [] flux = [] data = [] labels = [] for filename in files: hdulist = pyfits.open(filename) d = np.array([(t,cad,s,pdc,q) for \ (t,corr,cad,s,s_err,bg,bg_err,pdc,pdc_err, q,psf1,psf1_err,psf2,psf2_err, mom1,mom1_err,mom2,mom2_err,pc1,pc2) in hdulist[1].data]) # print "Read in %s" % filename use = ((d[:,2] > 0).nonzero())[0] #data = d[use,2] # ignore all nans time_data = d[:,0] cadence_data = d[:,1] flux_data = d[:,2] pdc_data = d[:,3] keplerid = int(hdulist[0].header['KEPLERID']) obsmode = hdulist[0].header['OBSMODE'].split()[0] # Replace nan with predecessor (fill) # Need to do this to use "detrend" below # Temporary and breaks value of "missingmethod" in demud for i in range(len(flux_data)): if pylab.isnan(flux_data[i]): flux_data[i] = flux_data[i-1] for i in range(len(pdc_data)): if pylab.isnan(pdc_data[i]): pdc_data[i] = pdc_data[i-1] # Make sure all sources have same cadence (they should) if cadence == []: cadence = cadence_data assert (cadence == cadence_data).all() # Subtract mean from data pdc_data = pylab.detrend_mean(pdc_data) flux_data = pylab.detrend_mean(flux_data) flux += [[float(x) for x in pdc_data]] labels.append(str(keplerid) + '-' + obsmode) seen += 1 # output read-in progress if percent < 100: if (round((seen / float(numfiles)) * 100, 1) >= percent) and (printed[int(percent * 10)] == False): print "...%3.1f%%..." % percent printed[int(percent * 10)] == True percent = round(((seen / float(numfiles)) * 100), 1) + 0.1 print "...100%..." data = np.array(flux).T # Output the pickle outf = open(kd.archive, 'w') pickle.dump((data, time_data, cadence, labels), outf) outf.close() print "Wrote pickle to " + kd.archive
def fftransform(self, fancy=True, cutoff_freq=8.83): """fftransform(KeplerData kd) Perform the FFT of the data stored in self and return the same KeplerData object with the transformed data. """ #fancy = False print "________________________________________" if fancy: print " performing fancy FFT " else: print " performing FFT " if not self.is_fft: self.flux_data = copy.deepcopy(self.data) self.cadence_time = copy.deepcopy(self.time_data) dataT = self.data.T newdata = [] for j in range(len(dataT)): pdc_data = dataT[j] # print "pdc_data: ", pdc_data time = self.time_data # Ensure that no NaNs exist for i in range(len(pdc_data)): if pylab.isnan(pdc_data[i]): pdc_data[i] = pdc_data[i-1] # Ensure that time starts from zero # Time is now in days time = time - time[0] pdc_data = pylab.detrend_mean(pdc_data) pdc_data = pdc_data / np.std(pdc_data) # Do FFT n = len(pdc_data) if fancy: fft_pdc = abs(10 * np.fft.fft(pdc_data, 10 * n)) # More data points else: fft_pdc = abs(np.fft.fft(pdc_data, n)) # Normal FFT n = len(fft_pdc) timediffs = [self.cadence_time[i + 1] - self.cadence_time[i] for i in \ range(len(self.cadence_time) - 1)] spacing = np.mean(np.array(timediffs)[np.where(~np.isnan(timediffs))]) freq = np.fft.fftfreq(n, spacing) half_n = np.floor(n / 2.0) fft_pdc_half = (2.0 / n) * fft_pdc[:half_n] freq_half = freq[:half_n] # Low pass filter # Use frequency of 8.83: exoplanet with highest frequency is 2.207 / day # Capture two harmonics if fancy: wherefq = np.where(freq_half > cutoff_freq) cutoff = wherefq[0][0] freq_half = freq_half[:cutoff] fft_pdc_half = fft_pdc_half[:cutoff] if newdata == []: newdata = np.zeros((dataT.shape[0], len(fft_pdc_half))) newdata[j] = fft_pdc_half # print "newdata[%d]: " % j, newdata[j] # print "newdata[0]: ", newdata[0] # print "j: ", j # Time.sleep(5) self.time_data = freq_half self.xvals = freq_half self.data = newdata.T self.is_fft = True
def wavelet(Y, dt, param, dj, s0, j1, mother): """Computes the wavelet continuous transform of the vector Y, by definition: W(a,b) = sum(f(t)*psi[a,b](t) dt) a dilate/contract psi[a,b](t) = 1/sqrt(a) psi(t-b/a) b displace Only Morlet wavelet (k0=6) is used The wavelet basis is normalized to have total energy = 1 at all scales _____________________________________________________________________ Input: Y - time series dt - sampling rate mother - the mother wavelet function param - the mother wavelet parameter Output: ondaleta - wavelet bases at scale 10 dt wave - wavelet transform of Y period - the vector of "Fourier"periods ( in time units) that correspond to the scales scale - the vector of scale indices, given by S0*2(j*DJ), j =0 ...J1 coi - cone of influence Call function: ondaleta, wave, period, scale, coi = wavelet(Y,dt,mother,param) _____________________________________________________________________ """ n1 = len(Y) # time series length #s0 = 2 * dt # smallest scale of the wavelet # dj = 0.25 # spacing between discrete scales # J1 = int(np.floor((np.log10(n1*dt/s0))/np.log10(2)/dj)) J1 = int(np.floor(np.log2(n1 * dt / s0) / dj)) # J1+1 total os scales # print 'Nr of Scales:', J1 # J1= 60 # pad if necessary x = detrend_mean(Y) # extract the mean of time series pad = 1 if (pad == 1): base2 = nextpow2(n1) # call det nextpow2 n = base2 print ("n") """CAUTION""" # construct wavenumber array used in transform # simetric eqn 5 #k = np.arange(n / 2) import math k_pos, k_neg = [], [] for i in range(0, int(n / 2)): k_pos.append(i * ((2 * math.pi) / (n * dt))) # frequencies as in eqn5 k_neg = k_pos[::-1] # inversion vector k_neg = [e * (-1) for e in k_neg] # negative part # delete the first value of k_neg = last value of k_pos #k_neg = k_neg[1:-1] k = np.concatenate((k_pos, k_neg), axis=0) # vector of symmetric # compute fft of the padded time series f = np.fft.fft(x, n) scale = [] for i in range(J1 + 1): scale.append(s0 * pow(2, (i) * dj)) period = scale # print period wave = np.zeros((J1 + 1, n)) # define wavelet array wave = wave + 1j * wave # make it complex # loop through scales and compute transform for a1 in range(J1 + 1): daughter, fourier_factor, coi, dofmin = wave_bases( mother, k, scale[a1], param) # call wave_bases wave[a1, :] = np.fft.ifft(f * daughter) # wavelet transform if a1 == 11: ondaleta = daughter # ondaleta = daughter period = np.array(period) period = period[:] * fourier_factor # cone-of-influence, differ for uneven len of timeseries: if (((n1) / 2.0).is_integer()) is True: # create mirrored array) mat = np.concatenate( (arange(1,int( n1 / 2)), arange(1,int( n1 / 2))[::-1]), axis=0) # insert zero at the begining of the array mat = np.insert(mat, 0, 0) mat = np.append(mat, 0) # insert zero at the end of the array elif (((n1) / 2.0).is_integer()) is False: # create mirrored array mat = np.concatenate( (arange(1,int( n1 / 2) + 1), arange(1, int(n1 / 2))[::-1]), axis=0) # insert zero at the begining of the array mat = np.insert(mat, 0, 0) mat = np.append(mat, 0) # insert zero at the end of the array coi = [coi * dt * m for m in mat] # create coi matrix # problem with first and last entry in coi added next to lines because # log2 of zero is not defined and cannot be plottet later: coi[0] = 0.1 # coi[0] is normally 0 coi[len(coi)-1] = 0.1 # coi[last entry] is normally 0 too wave = wave[:, 0:n1] return ondaleta, wave, period, scale, coi, f
def wavelet(Y, dt, mother, param, dj): #,pad,s0,J1,mother,param): import warnings warnings.filterwarnings('ignore') """Computes the wavelet continuous transform of the vector Y, by definition: W(a,b) = sum(f(t)*psi[a,b](t) dt) a dilate/contract psi[a,b](t) = 1/sqrt(a) psi(t-b/a) b displace Only Morlet wavelet (k0=6) is used The wavelet basis is normalized to have total energy = 1 at all scales _____________________________________________________________________ Input: Y - time series dt - sampling rate mother - the mother wavelet function param - the mother wavelet parameter dj - spacing between scales Output: ondaleta - wavelet bases at scale 10 dt wave - wavelet transform of Y period - the vector of "Fourier"periods ( in time units) that correspond to the scales scale - the vector of scale indices, given by S0*2(j*DJ), j =0 ...J1 coi - cone of influence Call function: ondaleta, wave, period, scale, coi = wavelet(Y,dt,mother,param) _____________________________________________________________________ """ # if(param == -1): param == -1 # if ~exist(c,var) || isempty(c): c = 10 """CAUTION : default values""" n1 = len(Y) # time series length s0 = 2 * dt # smallest scale of the wavelet #dj = 0.25 # spacing between discrete scales ## 0.25 --> now taken as input arg J1 = int(np.floor( (np.log10(n1 * dt / s0)) / np.log10(2) / dj)) # J1+1 total os scales #mother = 'Morlet' # pad if necessary x = detrend_mean(Y) #extract the mean of time series pad = 1 if (pad == 1): base2 = nextpow2(n1) #call det nextpow2 n = base2 """CAUTION""" # construct wavenumber array used in transform # simetric eqn 5 k = np.arange(n / 2) import math k_pos, k_neg = [], [] for i in range(0, n / 2 + 1): k_pos.append(i * ((2 * math.pi) / (n * dt))) # frequencies as in eqn5 k_neg = k_pos[::-1] # inversion vector k_neg = [e * (-1) for e in k_neg] # negative part k_neg = k_neg[ 1:-1] # delete the first value of k_neg = last value of k_pos k = np.concatenate((k_pos, k_neg), axis=0) # vector of symmetric # compute fft of the padded time series f = np.fft.fft(x, n) scale = [] for i in range(J1 + 1): scale.append(s0 * pow(2, (i) * dj)) period = scale wave = np.zeros((J1 + 1, n)) #define wavelet array wave = wave + 1j * wave # make it complex #loop through scales and compute transform for a1 in range(J1 + 1): daughter, fourier_factor, coi, dofmin = wave_bases( mother, k, scale[a1], param) #call wave_bases #zf = np.zeros(len(f)) #zf[len(f)/2:len(f)*3/2] = daughter #wavelet in the middle of the zero vector wave[a1, :] = np.fft.ifft(f * daughter) # wavelet transform if a1 == 11: ondaleta = daughter period = np.array(period) period = period[:] * fourier_factor #cone-of-influence mat = np.concatenate((range(1, n1 / 2), range(1, n1 / 2)[::-1]), axis=0) # create mirrored array mat = np.insert(mat, 0, 0) # insert zero at the begining of the array mat = np.append(mat, 0) # insert zero at the end of the array coi = [coi * dt * m for m in mat] # create coi matrix wave = wave[:, 0:n1] return ondaleta, wave, period, scale, coi, f
def wavelet(Y,dt,mother,param):#,pad,dj,s0,J1,mother,param): """Computes the wavelet continuous transform of the vector Y, by definition: W(a,b) = sum(f(t)*psi[a,b](t) dt) a dilate/contract psi[a,b](t) = 1/sqrt(a) psi(t-b/a) b displace Only Morlet wavelet (k0=6) is used The wavelet basis is normalized to have total energy = 1 at all scales _____________________________________________________________________ Input: Y - time series dt - sampling rate mother - the mother wavelet function param - the mother wavelet parameter Output: ondaleta - wavelet bases at scale 10 dt wave - wavelet transform of Y period - the vector of "Fourier"periods ( in time units) that correspond to the scales scale - the vector of scale indices, given by S0*2(j*DJ), j =0 ...J1 coi - cone of influence Call function: ondaleta, wave, period, scale, coi = wavelet(Y,dt,mother,param) _____________________________________________________________________ """ # if(param == -1): param == -1 # if ~exist(c,var) || isempty(c): c = 10 """CAUTION : default values""" n1 = len(Y) # time series length s0 = 2*dt # smallest scale of the wavelet dj = 0.25 # spacing between discrete scales J1= int(np.floor((np.log10(n1*dt/s0))/np.log10(2)/dj)) # J1+1 total os scales #mother = 'Morlet' # pad if necessary x = detrend_mean(Y) #extract the mean of time series pad = 1 if (pad ==1) : base2 = nextpow2(n1) #call det nextpow2 n = base2 """CAUTION""" # construct wavenumber array used in transform # simetric eqn 5 k = np.arange(n/2) import math k_pos,k_neg=[],[] for i in range(0,n/2+1): k_pos.append(i*((2*math.pi)/(n*dt))) # frequencies as in eqn5 k_neg = k_pos[::-1] # inversion vector k_neg = [e * (-1) for e in k_neg] # negative part k_neg = k_neg[1:-1] # delete the first value of k_neg = last value of k_pos k = np.concatenate((k_pos,k_neg), axis =1) # vector of symmetric # compute fft of the padded time series f = np.fft.fft(x,n) scale=[] for i in range(J1+1): scale.append(s0*pow(2,(i)*dj)) period = scale wave = np.zeros((J1+1,n)) #define wavelet array wave = wave+1j * wave # make it complex #loop through scales and compute transform for a1 in range(J1+1): daughter,fourier_factor,coi,dofmin = wave_bases(mother,k,scale[a1],param) #call wave_bases #zf = np.zeros(len(f)) #zf[len(f)/2:len(f)*3/2] = daughter #wavelet in the middle of the zero vector wave[a1,:] = np.fft.ifft(f*daughter) # wavelet transform if a1==11 : ondaleta =daughter period = np.array(period) period = period[:]*fourier_factor #cone-of-influence mat = np.concatenate((range(1,n1/2),range(1,n1/2)[::-1]), axis=1) # create mirrored array mat = np.insert(mat,0,0) # insert zero at the begining of the array mat = np.append(mat,0) # insert zero at the end of the array coi = [coi*dt*m for m in mat] # create coi matrix wave = wave[:,0:n1] return ondaleta, wave, period, scale, coi, f