def psd(self,xstr,nfft=256,noverlap=0,coarsable=False): """Return the PSD(power spectral density) of the time-series Positional parameter: xstr -- string naming the observable, either 'A', 'E', or 'T' Keyword parameters: nfft -- length of the fast fourier transform (fft) (default 256) noverlap -- number of data points overlapping between segments (default 0) coarsable -- return the PSD as a Coarsable object (default False) OUTPUT: f -- frequencies P -- PSD Note: The PSD is calculated using pl.psd() with an additional factor of dt in the normalisation. This gives the correct dimension. """ x = getattr(self,xstr) fs = 1. / x.Cadence1 P , f = pl.psd( x.data , nfft , fs , noverlap=noverlap , scale_by_freq=False ) P = P / fs if coarsable: scale = {'Offset1' : f[0] , 'Cadence1': f[1] - f[0] } f = AS.Coarsable( f , **scale ) P = AS.Coarsable( P , **scale ) return f , P
def G1Michelson_to_G1Sagnac( FT ) : L = 16.6782 f = FT.s1.Offset1 + FT.s1.Cadence1 * np.arange( FT.s1.data.shape[0] ) fdict = { 'Offset1' : FT.s1.Offset1 , 'Cadence1' : FT.s1.Cadence1 } Xdata , Ydata , Zdata = FT.s1.data , FT.s2.data , FT.s3.data e1 = np.exp( - 1j * 2 * np.pi * L * f ) e2 = np.exp( - 1j * 4 * np.pi * L * f ) e3 = np.exp( - 1j * 6 * np.pi * L * f ) A = 1 - e1 + e2 B = 1 - e1 - e2 + e3 alphadata = ( A*Xdata + e1 * ( Ydata + Zdata ) ) / B betadata = ( A*Ydata + e1 * ( Zdata + Xdata ) ) / B gammadata = ( A*Zdata + e1 * ( Xdata + Ydata ) ) / B alpha = AS.Coarsable( alphadata , **fdict ) beta = AS.Coarsable( betadata , **fdict ) gamma = AS.Coarsable( gammadata , **fdict ) SagnacFT = AS.ShortTermFT( alpha , beta , gamma ) return SagnacFT
def csd(self,xstr,ystr,nfft=256,noverlap=0,coarsable=False): """Return CSD(cross spectral density) of the time-series. Positional parameters: xstr -- string naming one observable, either 'A', 'E' or 'T' ystr -- string naming one observable, either 'A', 'E' or 'T' Keyword parameters: nfft -- length of fast fourier transform(fft) (default 256) noverlap -- number of data points overlapping between segments (default 0) coarsable -- return the CSD as a Coarsable object (default False) OUTPUT: f -- frequencies P -- CSD Note: The CPSD is calculated using pl.csd() with an additional factor of dt in the normalisation. If xstr='A' and ystr='E', then the CSD between A and E is returned. """ x = getattr(self,xstr) y = getattr(self,ystr) fs = 1. / x.Cadence1 P , f = pl.csd( x.data , y.data , nfft , fs , noverlap=noverlap ) P = P / fs if coarsable: scale = {'Offset1' : f[0] , 'Cadence1': f[1] - f[0] } f = AS.Coarsable( f , **scale ) P = AS.Coarsable( P , **scale ) return f , P
def FracPhase_to_FracFreq( FT ) : f = FT.s1.Offset1 + FT.s1.Cadence1 * np.arange( FT.s1.data.shape[0] ) fdict = { 'Offset1' : FT.s1.Offset1 , 'Cadence1' : FT.s1.Cadence1 } phase1data , phase2data , phase3data = FT.s1.data , FT.s2.data , FT.s3.data a = 1 / ( 1j * 2 * np.pi * f ) freq1data = phase1data * a freq2data = phase2data * a freq3data = phase3data * a freq1 = AS.Coarsable( freq1data , **fdict ) freq2 = AS.Coarsable( freq2data , **fdict ) freq3 = AS.Coarsable( freq3data , **fdict ) freqFT = AS.ShortTermFT( freq1 , freq2 , freq3 ) return freqFT
def G1Sagnac_to_GmSagnac( FT ) : L = 16.6782 f = FT.s1.Offset1 + FT.s1.Cadence1 * np.arange( FT.s1.data.shape[0] ) fdict = { 'Offset1' : FT.s1.Offset1 , 'Cadence1' : FT.s1.Cadence1 } G1adata , G1bdata , G1cdata = FT.s1.data , FT.s2.data , FT.s3.data e3 = np.exp( - 1j * 6 * np.pi * L * f ) Gmadata = ( e3 - 1 ) * G1adata Gmbdata = ( e3 - 1 ) * G1bdata Gmcdata = ( e3 - 1 ) * G1cdata Gma = AS.Coarsable( Gmadata , **fdict ) Gmb = AS.Coarsable( Gmbdata , **fdict ) Gmc = AS.Coarsable( Gmcdata , **fdict ) GmSagnac = AS.ShortTermFT( Gma , Gmb , Gmc ) return GmSagnac
def GpretdiMichelson_to_G1Michelson( FT ) : L = 16.6782 f = FT.s1.Offset1 + FT.s1.Cadence1 * np.arange( FT.s1.data.shape[0] ) fdict = { 'Offset1' : FT.s1.Offset1 , 'Cadence1' : FT.s1.Cadence1 } Xdata , Ydata , Zdata = FT.s1.data , FT.s2.data , FT.s3.data e2 = np.exp( - 1j * 4 * np.pi * L * f ) G1Xdata = ( e2 - 1 ) * Xdata G1Ydata = ( e2 - 1 ) * Ydata G1Zdata = ( e2 - 1 ) * Zdata G1X = AS.Coarsable( G1Xdata , **fdict ) G1Y = AS.Coarsable( G1Ydata , **fdict ) G1Z = AS.Coarsable( G1Zdata , **fdict ) G1Michelson = AS.ShortTermFT( G1X , G1Y , G1Z ) return G1Michelson
ts = AS.TimeSeries( tsdict ) if options.scale_ts : ts.scale_by( options.scale_ts ) t , n1 , n2 , n3 = ts.t.data , ts.A.data , ts.E.data , ts.T.data stime = ts.t.Cadence1 ; inittime = ts.t.Offset1 ; N = t.shape[0] nfft = int( options.segduration / stime ) ; noverlap = nfft / 2 ; fs = 1. / stime P11data , fdata = mymlab.psd( n1 , nfft , fs , noverlap = noverlap ) P22data , fdata = mymlab.psd( n2 , nfft , fs , noverlap = noverlap ) P33data , fdata = mymlab.psd( n3 , nfft , fs , noverlap = noverlap ) f0 = fdata[0] ; df = fdata[1] - fdata[0] ; fscale = { 'Offset1':f0 , 'Cadence1':df } f = AS.Coarsable( fdata , **fscale ) P11 = AS.Coarsable( P11data , **fscale ) P22 = AS.Coarsable( P22data , **fscale ) P33 = AS.Coarsable( P33data , **fscale ) psddict = { 'f': f , 'AA': P11 , 'EE': P22 , 'TT': P33 } file = open( psdpath , 'wb' ) cpkl.dump( psddict , file , -1 ) file.close() print "done"
parser = optparse.OptionParser( 'Usage: ./x_simulate_noise.py SETUP.pkl' ) ( options , args ) = parser.parse_args() if len( args ) < 1 : parser.error( 'You must specify a SETUP.pkl containing input parameters!' ) file = open( args[0] , 'rb' ) ; setup = cpkl.load( file ) ; file.close() tditype , tdigen , whichtdis = setup['tditype'] , setup['tdigen'] , setup['whichtdis'] stime , duration , inittime , seed = setup['stime'] , setup['duration'] , setup['inittime'] , setup['seed'] tspath = setup['tspath'] tsdir = '/'.join( re.split( '/' , tspath )[:-1] ) + '/' print tsdir if not tspath in glob.glob( tspath ) : t , n = mlisar.get_tdiNoise_freq_domain_CovarMatrix( tditype , tdigen , whichtdis , stime , duration , inittime , seed ) tscale = { 'Cadence1':stime , 'Offset1':inittime } tsdict = {} tsdict['1'] , tsdict['2'] , tsdict['3'] = AS.Coarsable( n[0] , **tscale ) , AS.Coarsable( n[1] , **tscale ) , AS.Coarsable( n[2] , **tscale ) tsdict['t'] = AS.Coarsable( t , **tscale ) if tsdir not in glob.glob( tsdir ) : os.system( 'mkdir %s' % tsdir ) print 'saving time-series to disk' file = open( tspath , 'wb' ) cpkl.dump( tsdict , file , -1 ) ; file.close() else : print '%s already saved to disk. Nothing to do.' % tspath
[file.close() for file in files] s1datas = [ float(options.mergs[k][0]) * tsdicts[k]['1'].data for k in range(Nmerg) ] s2datas = [ float(options.mergs[k][0]) * tsdicts[k]['2'].data for k in range(Nmerg) ] s3datas = [ float(options.mergs[k][0]) * tsdicts[k]['3'].data for k in range(Nmerg) ] s1data = np.sum(np.array(s1datas), 0) s2data = np.sum(np.array(s2datas), 0) s3data = np.sum(np.array(s3datas), 0) t = tsdicts[0]['t'] tscale = {'Offset1': t.Offset1, 'Cadence1': t.Cadence1} s1 = AS.Coarsable(s1data, **tscale) s2 = AS.Coarsable(s2data, **tscale) s3 = AS.Coarsable(s3data, **tscale) tsdict = {'t': t, '1': s1, '2': s2, '3': s3} tsdir = os.path.dirname(tspath) if tsdir not in glob.glob(tsdir): os.system('mkdir -p %s' % tsdir) file = open(tspath, 'wb') cpkl.dump(tsdict, file, -1) file.close()
stime = ts.t.Cadence1 inittime = ts.t.Offset1 N = t.shape[0] nfft = int(options.segduration / stime) noverlap = nfft / 2 fs = 1. / stime P12data, fdata = mymlab.csd(n1, n2, nfft, fs, noverlap=noverlap) P13data, fdata = mymlab.csd(n1, n3, nfft, fs, noverlap=noverlap) P23data, fdata = mymlab.csd(n2, n3, nfft, fs, noverlap=noverlap) f0 = fdata[0] df = fdata[1] - fdata[0] fscale = { 'Offset1': f0, 'Cadence1': df } f = AS.Coarsable(fdata, **fscale) P12 = AS.Coarsable(P12data, **fscale) P13 = AS.Coarsable(P13data, **fscale) P23 = AS.Coarsable(P23data, **fscale) csddict = {'f': f, 'AE': P12, 'AT': P13, 'ET': P23} file = open(csdpath, 'wb') cpkl.dump(csddict, file, -1) file.close() print "done"
y2r = n2r + hr """ Straight cross-correlation """ Ometer = np.mean(y1 * y2) Ometer_ana, var_Ometer_ana = Omega, (var_n1 + Omega) * ( var_n2 + Omega) / N + Omega**2 / N """ Cross-correlation with filtering """ Y1 = stime * np.fft.fft(y1) Y2 = stime * np.fft.fft(y2) df_c12 = 1. / (N * stime) if N % 2 == 0: c12 = 2 * np.conj(Y1[:N / 2 + 1]) * Y2[:N / 2 + 1] / (N * stime) f_c12 = df_c12 * np.arange(N / 2 + 1) else: c12 = 2 * np.conj(Y1[:(N + 1) / 2]) * Y2[:(N + 1) / 2] / (N * stime) f_c12 = df_c12 * np.arange((N + 1) / 2) Qc12 = AS.Coarsable(c12, Offset1=f_c12[0], Cadence1=df_c12) Qf_c12 = AS.Coarsable(f_c12, Offset1=f_c12[0], Cadence1=df_c12) p11l, f_p11l = pl.psd(y1l, nfft, fs, noverlap=noverlap) p11r, f_p11r = pl.psd(y1r, nfft, fs, noverlap=noverlap) p11 = (p11l + p11r) / 2 f_p11 = np.copy(f_p11l) df_p11 = f_p11[1] - f_p11[0] Qp11 = AS.Coarsable(p11, Offset1=f_p11[0], Cadence1=df_p11) Qf_p11 = AS.Coarsable(f_p11, Offset1=f_p11[0], Cadence1=df_p11) p22l, f_p22l = pl.psd(y2l, nfft, fs, noverlap=noverlap) p22r, f_p22r = pl.psd(y2r, nfft, fs, noverlap=noverlap) p22 = (p22l + p22r) / 2 f_p22 = np.copy(f_p22l) df_p22 = f_p22[1] - f_p22[0] Qp22 = AS.Coarsable(p22, Offset1=f_p22[0], Cadence1=df_p22)
"You must specify CSDDIR! Type './x_simulate_expected_signal_CSD.py -h' for help " ) else: csddir = args[0] if options.whichtdi == 'ordinary': IJs = ['12', '13', '23'] elif options.whichtdi == 'optimal': IJs = ['AE', 'AT', 'ET'] if csddir not in glob.glob(csddir): os.system('mkdir %s' % csddir) fdata = options.f0 + options.df * np.arange(options.Nf) fscale = {'Offset1': options.f0, 'Cadence1': options.df} f = AS.Coarsable(fdata, **fscale) print "Calculating expected cross NSD..." PIJdatas = [ mlisar.get_tdiNSD(options.tditype, options.tdigen, IJ[0], IJ[1], fdata) for IJ in IJs ] PIJs = [AS.Coarsable(PIJdata, **fscale) for PIJdata in PIJdatas] P12, P13, P23 = PIJs print 'done' psddict = {'f': f, 'AE': P12, 'AT': P13, 'ET': P23} file = open(csddir + 'd001.pkl', 'wb') cpkl.dump(psddict, file, -1) file.close()
usage = """ $prog [ GPATH,... ] NETWORKGPATH\n GPATH --- path to file of Gs to be summed(can have more than one) NETWORKGPATH --- path to file in which to save network G """ parser = optparse.OptionParser( usage = usage ) ( options , args ) = parser.parse_args() if len( args ) < 2 : parser.error( 'You must specify at least an GPATH and a NETWORKPATH! ( See help: x_network_G.py -h )' ) else : Gpaths = args[ : -1 ] ; netGpath = args[ -1 ] for i , Gpath in enumerate( Gpaths ) : file = open( Gpath , 'rb' ) ; Gdict = cpkl.load( file ) ; file.close() if i == 0 : netGdata = np.copy( Gdict['G'].data ) lmax = Gdict['ntrunc'] else : netGdata += np.copy( Gdict['G'].data ) netG = AS.Coarsable( netGdata ) Gdict = {} ; Gdict['G'] = netG ; Gdict['ntrunc'] = lmax netGdir = os.path.dirname( netGpath ) if netGdir not in glob.glob( netGdir ) : os.system( 'mkdir -p %s' % netGdir ) file = open( netGpath , 'wb') ; cpkl.dump( Gdict , file , -1 ) ; file.close()
N = ts.t.data.shape[0] T = ts.t.Cadence1 * N df = ggg.fcoarse.Cadence1 if options.window == 'None': window = np.ones(N) elif options.window == 'hanning': window = np.hanning(N) norm = (np.sum(window**2) / N)**2 / (np.sum(window**4) / N) * T * df print 'norm', norm firstavailable = False dGdata = norm * GG.data G += dGdata "~~Write G_day to disk for each day here" dG = AS.Coarsable(dGdata) dGdict = { 'G': dG, 'ntrunc': options.lmax } if Gdir + '/../GG' not in glob.glob(Gdir + '/..//GG'): os.system('mkdir -p %s' % (Gdir + '/../GG')) file = open(Gdir + '/../GG/GG_d%03d.pkl' % day, 'wb') cpkl.dump(dGdict, file, -1) file.close() "~~" G = AS.Coarsable(G) Gdict = {'G': G, 'ntrunc': options.lmax} if Gdir not in glob.glob(Gdir): os.system('mkdir -p %s' % Gdir)
for v in range(Nvar): print 'v = ', v if s == 1: print 'tails[v]', tails[v] else: print 'tails[v][:3]', tails[v][:3] ts, tail = mufls.window_and_join(tsl[v], tsr[v], tails[v]) TSs[v] += list(ts) tails[v] = np.copy(tail) print 'tails[v][:3]', tails[v][:3] s += 2 TSs = np.array(TSs)[:, :N] tscale = {'Cadence1': options.stime, 'Offset1': t0} tsdict = { 't': AS.Coarsable(t, **tscale), '1': AS.Coarsable(TSs[0], **tscale), '2': AS.Coarsable(TSs[1], **tscale), '3': AS.Coarsable(TSs[2], **tscale) } if tsdir == '': pass elif tsdir not in glob.glob(tsdir): os.system('mkdir -p %s' % tsdir) print 'saving time-series to disk...' file = open(tsdir + '/d%03d.pkl' % day, 'wb') cpkl.dump(tsdict, file, -1) file.close()
( options , args ) = parser.parse_args() if len( args ) < 1 : parser.error( "You must specify a TSPATH! Type './x_simulate_defined_stationary_noise.py' " ) else : tspath = args[ 0 ] ; tsdir = os.path.dirname( tspath ) stime , duration , inittime , N_previous_draws = float(options.stime) , float(options.duration) , float(options.inittime) , int( options.N_previous_draws ) N = np.round( duration / stime ) if N % 2 == 0 : Nf = int( N/2 - 1 ) ; parityN = 'Even' else : Nf = int( ( N-1 ) / 2 ) ; parityN = 'Odd' df = 1 / (N*stime) f = df * np.arange( 1 , Nf + 1 ) comatrix = get_comatrix( f ) t , n = mufls.get_noise_freq_domain_CovarMatrix( comatrix , df , inittime , parityN , options.seed , N_previous_draws ) tscale = { 'Cadence1':stime , 'Offset1':inittime } tsdict = { 't':AS.Coarsable( t , **tscale ) , '1':AS.Coarsable( n[0] , **tscale ) , '2':AS.Coarsable( n[1] , **tscale ) , '3':AS.Coarsable( n[2] , **tscale ) } if tsdir not in glob.glob( tsdir ) : os.system( 'mkdir %s' % tsdir ) print 'saving time-series to disk...' file = open( tspath , 'wb' ) ; cpkl.dump( tsdict , file , -1 ) ; file.close()
PAAdatas = [ float(options.mergs[k][0]) * psddicts[k]['AA'].data for k in range(Nmerg) ] PEEdatas = [ float(options.mergs[k][0]) * psddicts[k]['EE'].data for k in range(Nmerg) ] PTTdatas = [ float(options.mergs[k][0]) * psddicts[k]['TT'].data for k in range(Nmerg) ] PAAdata = np.sum(np.array(PAAdatas), 0) PEEdata = np.sum(np.array(PEEdatas), 0) PTTdata = np.sum(np.array(PTTdatas), 0) f = psddicts[0]['f'] fscale = {'Offset1': f.Offset1, 'Cadence1': f.Cadence1} PAA = AS.Coarsable(PAAdata, **fscale) PEE = AS.Coarsable(PEEdata, **fscale) PTT = AS.Coarsable(PTTdata, **fscale) psddict = {'f': f, 'AA': PAA, 'EE': PEE, 'TT': PTT} psddict['f'], psddict['AA'], psddict['EE'], psddict['TT'] = f, PAA, PEE, PTT psddir = os.path.dirname(psdpath) if psddir not in glob.glob(psddir): os.system('mkdir -p %s' % psddir) file = open(psdpath, 'wb') cpkl.dump(psddict, file, -1) file.close()
N = ts.t.data.shape[0] T = ts.t.Cadence1 * N fcoarse = AS.coarsefrequency(orf.f, psddict['f'], csddict['f']) df = fcoarse.Cadence1 if options.window == 'None': window = np.ones(N) elif options.window == 'hanning': window = np.hanning(N) norm = (np.sum(window**2) / N)**2 / (np.sum(window**4) / N) * T * df firstavailable = False SSdata *= norm Sdata += SSdata "~~Write S_day to disk for each day here" SS = AS.Coarsable(SSdata) SSdict = { 'S': SS, 'ntrunc': options.lmax } if Sdir + '/../SS' not in glob.glob(Sdir + '/../SS'): os.system('mkdir -p %s' % (Sdir + '/../SS')) file = open(Sdir + '/../SS/SS_d%03d.pkl' % day, 'wb') cpkl.dump(SSdict, file, -1) file.close() "~~" S = AS.Coarsable(Sdata) Sdict = {'S': S, 'ntrunc': options.lmax} if Sdir not in glob.glob(Sdir):
PAEdatas = [ float(options.mergs[k][0]) * csddicts[k]['AE'].data for k in range(Nmerg) ] PATdatas = [ float(options.mergs[k][0]) * csddicts[k]['AT'].data for k in range(Nmerg) ] PETdatas = [ float(options.mergs[k][0]) * csddicts[k]['ET'].data for k in range(Nmerg) ] PAEdata = np.sum(np.array(PAEdatas), 0) PATdata = np.sum(np.array(PATdatas), 0) PETdata = np.sum(np.array(PETdatas), 0) f = csddicts[0]['f'] fscale = {'Offset1': f.Offset1, 'Cadence1': f.Cadence1} PAE = AS.Coarsable(PAEdata, **fscale) PAT = AS.Coarsable(PATdata, **fscale) PET = AS.Coarsable(PETdata, **fscale) csddict = {'f': f, 'AE': PAE, 'AT': PAT, 'ET': PET} csddict['f'], csddict['AE'], csddict['AT'], csddict['ET'] = f, PAE, PAT, PET csddir = os.path.dirname(csdpath) if csddir not in glob.glob(csddir): os.system('mkdir -p %s' % csddir) file = open(csdpath, 'wb') cpkl.dump(csddict, file, -1) file.close()
elif options.whichtdi == 'optimal': IIs = ['AE', 'AT', 'ET'] if csddir not in glob.glob(csddir): os.system('mkdir %s' % csddir) for day in options.days: print "~~~~~~~ Day %d ~~~~~~~" % day orfids = [(options.tditype, options.tdigen, II[0], options.tditype, options.tdigen, II[1], options.f0, options.df, options.Nf, day) for II in IIs] orfpaths = [( orfdir + 'tdiI_%s_%s_%s_tdiJ_%s_%s_%s_lmax_20_f0_%f_df_%f_Nf_%d/data_nlon_120_nlat_61/orf_d%03d.pkl' % orfid) for orfid in orfids] orfs = [AS.OrfMultipleMoments(orfpath) for orfpath in orfpaths] PIJs = [AS.Convolve(orf, skymap, options.GWslope) for orf in orfs] P12, P13, P23 = PIJs fdata = P12.Offset1 + P12.Cadence1 * np.arange(P12.data.shape[0]) f = AS.Coarsable(fdata, Offset1=P12.Offset1, Cadence1=P12.Cadence1) csddict = {'f': f, 'AE': P12, 'AT': P13, 'ET': P23} file = open(csddir + 'd%03d.pkl' % day, 'wb') cpkl.dump(csddict, file, -1) file.close()
rems = [joinrem[1] for joinrem in joinrems] rem_dict = dict(zip(slist, rems)) leftover = AS.sTimeSeries(**rem_dict) oldandnews = zip([Ajoins, Ejoins, Tjoins], joins) [oldandnew[0].append(oldandnew[1].data) for oldandnew in oldandnews] tsdatas = [ np.concatenate(tuple(Ijoins)) for Ijoins in [Ajoins, Ejoins, Tjoins] ] numberofsamplesinaday = int(duration / Cadence) onedaytsdatas = [tsdata[:numberofsamplesinaday] for tsdata in tsdatas] tscoarsables = [ AS.Coarsable(onedaytsdata, Offset1=Offset, Cadence1=Cadence) for onedaytsdata in onedaytsdatas ] tsdict = dict(zip(['1', '2', '3'], tscoarsables)) tsdict['t'] = AS.Coarsable(Offset + Cadence * np.arange(numberofsamplesinaday), Offset1=Offset, Cadence1=Cadence) if tsdir not in glob.glob(tsdir): os.system('mkdir -p %s' % tsdir) file = open(tsdir + '/d%03d.pkl' % day, 'wb') cpkl.dump(tsdict, file, -1) file.close()