def sampleQSO(parser): (options, args) = parser.parse_args() if len(args) == 0: parser.print_help() return if len(args) == 2: othersavefilename = args[1] othersavefile = open(othersavefilename, 'rb') othersamples = pickle.load(othersavefile) othersavefile.close() else: othersamples = {} savefilename = args[0] if os.path.exists(savefilename): savefile = open(savefilename, 'rb') samples = pickle.load(savefile) type = pickle.load(savefile) band = pickle.load(savefile) mean = pickle.load(savefile) savefile.close() else: samples = {} type = options.type mean = options.mean band = options.band if os.path.exists(options.fitsfile): fitsfile = open(options.fitsfile, 'rb') params = pickle.load(fitsfile) fitsfile.close() else: raise IOError( "--fitsfile (or -f) has to be set to the file holding the best-fits" ) if options.star: dir = '../data/star/' elif options.nuvx: dir = '../data/nuvx/' elif options.nuvxall: dir = '../data/nuvx_all/' elif options.uvx: dir = '../data/uvx/' elif options.rrlyrae: dir = '../data/rrlyrae/' else: dir = '../data/s82qsos/' if options.resampled: raise NotImplementedError("resampled not implemented yet") if os.path.exists(options.infile): samplefile = open(options.infile, 'rb') qsos = pickle.load(samplefile) samplefile.close() else: print "'--resampled' is set, but -i filename does not exist ..." print "Returning ..." return None else: qsos = QSOfilenames(dir=dir) #Register time-out handler signal.signal(signal.SIGALRM, handler) savecount = 0 count = len(samples) for qso in qsos: if options.resampled: key = qso[0] else: key = os.path.basename(qso) if samples.has_key(key) or othersamples.has_key(key): continue try: if int(key[5:7]) != options.rah and options.rah != -1: continue except ValueError: if options.rah == -2 or options.rah == -1: pass else: print "Skipping ValueError " + key continue print "Working on " + str(count) + ": " + key if options.resampled: v = qso[1] else: v = VarQso(qso) if v.nepochs(band) < 20: print "This object does not have enough epochs ..." continue #Set best-fit v.LCparams = params[key] v.LC = LCmodel(trainSet=v._build_trainset(band), type=type, mean=mean) v.LCtype = type v.LCmean = mean v.fitband = band #Now sample signal.alarm(options.timeout) try: v.sampleGP(nsamples=options.nsamples, metropolis=options.metropolis, markovpy=options.markovpy, burnin=int(nu.floor(0.2 * options.nsamples))) except Exception, exc: if str(exc) == "Sampling timed out": print exc continue else: raise signal.alarm(0) samples[key] = v.get_sampleGP() if _DEBUG: _print_diagnostics(samples[key]) #print samples[key][options.nsamples] savecount += 1 if savecount == options.saveevery: print "Saving ..." save_pickles(samples, type, band, mean, savefilename) savecount = 0 count += 1
def sampleQSO(parser): (options,args)= parser.parse_args() if len(args) == 0: parser.print_help() return if len(args) == 2: othersavefilename= args[1] othersavefile= open(othersavefilename,'rb') othersamples= pickle.load(othersavefile) othersavefile.close() else: othersamples= {} savefilename= args[0] if os.path.exists(savefilename): savefile= open(savefilename,'rb') samples= pickle.load(savefile) type= pickle.load(savefile) band= pickle.load(savefile) mean= pickle.load(savefile) savefile.close() else: samples= {} type= options.type mean= options.mean band= options.band if os.path.exists(options.fitsfile): fitsfile= open(options.fitsfile,'rb') params= pickle.load(fitsfile) fitsfile.close() else: raise IOError("--fitsfile (or -f) has to be set to the file holding the best-fits") if options.star: dir= '../data/star/' elif options.nuvx: dir= '../data/nuvx/' elif options.nuvxall: dir= '../data/nuvx_all/' elif options.uvx: dir= '../data/uvx/' elif options.rrlyrae: dir= '../data/rrlyrae/' else: dir= '../data/s82qsos/' if options.resampled: raise NotImplementedError("resampled not implemented yet") if os.path.exists(options.infile): samplefile= open(options.infile,'rb') qsos= pickle.load(samplefile) samplefile.close() else: print "'--resampled' is set, but -i filename does not exist ..." print "Returning ..." return None else: qsos= QSOfilenames(dir=dir) #Register time-out handler signal.signal(signal.SIGALRM, handler) savecount= 0 count= len(samples) for qso in qsos: if options.resampled: key= qso[0] else: key= os.path.basename(qso) if samples.has_key(key) or othersamples.has_key(key): continue try: if int(key[5:7]) != options.rah and options.rah != -1: continue except ValueError: if options.rah == -2 or options.rah == -1: pass else: print "Skipping ValueError "+key continue print "Working on "+str(count)+": "+key if options.resampled: v= qso[1] else: v= VarQso(qso) if options.lownepochs: if v.nepochs(band) >= 20: print "This object has too many epochs ..." continue elif v.nepochs(band) < 3: print "This object has < 3 epochs ..." continue elif not options.lownepochs and v.nepochs(band) < 20: print "This object does not have enough epochs ..." continue #Set best-fit v.LCparams= params[key] v.LC= LCmodel(trainSet=v._build_trainset(band),type=type,mean=mean, init_params=params[key]) v.LCtype= type v.LCmean= mean v.fitband= band #Now sample signal.alarm(options.timeout) try: v.sampleGP(nsamples=options.nsamples,metropolis=options.metropolis, markovpy=options.markovpy, burnin=int(nu.floor(0.2*options.nsamples))) except Exception, exc: if str(exc) == "Sampling timed out": print exc continue else: raise signal.alarm(0) samples[key]= v.get_sampleGP() if _DEBUG: _print_diagnostics(samples[key]) #print samples[key][options.nsamples] savecount+= 1 if savecount == options.saveevery: print "Saving ..." save_pickles(samples,type,band,mean,savefilename) savecount= 0 count+= 1
def skewQSO(parser): (options,args)= parser.parse_args() if len(args) == 0: parser.print_help() return savefilename= args[0] if os.path.exists(savefilename): savefile= open(savefilename,'rb') skews= pickle.load(savefile) gaussskews= pickle.load(savefile) fittype= pickle.load(savefile) band= pickle.load(savefile) mean= pickle.load(savefile) taus= pickle.load(savefile) savefile.close() else: skews= {} gaussskews= {} fittype= options.type mean= options.mean band= options.band taus= nu.arange(options.dtau,options.taumax,options.dtau)/365.25 if os.path.exists(options.fitsfile): fitsfile= open(options.fitsfile,'rb') params= pickle.load(fitsfile) fitsfile.close() else: raise IOError("--fitsfile (or -f) has to be set to the file holding the best-fits") if options.star: dir= '../data/star/' elif options.nuvx: dir= '../data/nuvx/' elif options.nuvxall: dir= '../data/nuvx_all/' elif options.uvx: dir= '../data/uvx/' elif options.rrlyrae: dir= '../data/rrlyrae/' else: dir= '../data/s82qsos/' qsos= QSOfilenames(dir=dir) if not options.split is None: splitarr= nu.arange(len(qsos)) / int(nu.ceil(len(qsos)/float(options.split))) splitDict= {} for ii, qso in enumerate(qsos): key= os.path.basename(qso) splitDict[key]= splitarr[ii] print "Running bin %i ..." % (options.rah-1) savecount= 0 count= len(skews) if not options.star and not options.rrlyrae: #Read master file for redshifts dataqsos= open_qsos() qsoDict= {} ii=0 for qso in dataqsos: qsoDict[qso.oname.strip().replace(' ', '')+'.fit']= ii ii+= 1 for qso in qsos: key= os.path.basename(qso) if skews.has_key(key): continue if not options.split is None: if splitDict[key] != (options.rah-1): continue else: try: if int(key[5:7]) != options.rah and options.rah != -1: continue except ValueError: if options.rah == -2 or options.rah == -1: pass else: print "Skipping ValueError "+key continue sys.stdout.write('\r'+_ERASESTR+'\r') sys.stdout.flush() sys.stdout.write('\rWorking on %s: %s\r' % (str(count),key)) sys.stdout.flush() v= VarQso(qso,flux=options.flux) if v.nepochs(band) < 20: #print "This object does not have enough epochs ..." continue #Set best-fit if options.flux: params[key]['logA']+= 2.*nu.log(nu.log(10.)/2.5) if options.mean == 'const': params[key]['m']= -nu.log(10.)/2.5*params[key]['m'] v.LCparams= params[key] v.LC= LCmodel(trainSet=v._build_trainset(band),type=fittype,mean=mean, init_params=params[key]) v.LCtype= fittype v.LCmean= mean v.fitband= band #Now compute skew and Gaussian samples try: thisskew= v.skew(taus,band) except RuntimeError: continue if options.flux: thisskew*= -1. thisgaussskews= nu.zeros((options.nsamples,len(taus))) for ii in range(options.nsamples): #First re-sample if options.star or options.rrlyrae: redshift= 0. else: redshift= dataqsos[qsoDict[key]].z try: o= v.resample(v.mjd[band],band=band,noconstraints=True, wedge=options.wedge, wedgerate=options.wedgerate*365.25/(1.+redshift), wedgetau=(1.+redshift)) #1yr except nu.linalg.linalg.LinAlgError: if params[key]['gamma'] > 1.5 \ or params[key]['logA'] < -10.: continue #re-sampling fails because of bad gamma/logA else: print key, params[key] o.LCparams= v.LCparams o.LC= v.LC o.fitband= v.fitband o.LCtype= v.LCtype o.LCmean= v.LCmean if options.wedge: o.LCparams['gamma']= 1. o.LCparams['logA']= o.LCparams['logA']\ +nu.log(0.025**v.LCparams['gamma']/0.025) o.LCmean= 'zero' #bc we remove the mean when resampling wedge #Set up LC with correct params o.LC= LCmodel(trainSet=o._build_trainset(band), type=o.LCtype,mean=o.LCmean, init_params=o.LCparams) thisgaussskews[ii,:]= o.skew(taus,band) if options.flux: thisgaussskews[ii,:]*= -1. skews[key]= thisskew gaussskews[key]= thisgaussskews savecount+= 1 if savecount == options.saveevery: sys.stdout.write('\r'+_ERASESTR+'\r') sys.stdout.flush() sys.stdout.write('\rSaving ...\r') sys.stdout.flush() save_pickles(savefilename,skews,gaussskews,fittype,band,mean,taus) savecount= 0 count+= 1 sys.stdout.write('\r'+_ERASESTR+'\r') sys.stdout.flush() save_pickles(savefilename,skews,gaussskews,fittype,band,mean,taus) print "All done"
def skew0957(parser): (options,args)= parser.parse_args() if len(args) == 0: parser.print_help() return savefilename= args[0] #Read data if options.vanderriest: vA= VarQso('../data/0957-A.fits',band=options.band) vB= VarQso('../data/0957-B.fits',band=options.band) else: vA= VarQso('../data/L0957-A_%s.fits' % options.band,band=options.band) vB= VarQso('../data/L0957-B_%s.fits' % options.band,band=options.band) if not options.fitsfile is None and os.path.exists(options.fitsfile): fitsfile= open(options.fitsfile,'rb') paramsA= pickle.load(fitsfile) paramsB= pickle.load(fitsfile) paramsAB= pickle.load(fitsfile) fitsfile.close() vA.LCparams= paramsA vB.LCparams= paramsB vA.LCtype='powerlawSF' vB.LCtype='powerlawSF' vA.LCmean= 'const' vB.LCmean= 'const' vA.fitband= options.band vB.fitband= options.band else: #Fit for means print "Fitting SF for both images ..." vA.fit(options.band,mean='const') vB.fit(options.band,mean='const') #Load into single new VarQso newm= list(vA.m[options.band]-vA.LCparams['m']) newerrm= list(vA.err_m[options.band]) newmjd= list(vA.mjd[options.band]) newm.extend(list(vB.m[options.band]+nu.mean(vA.m[options.band]) -nu.mean(vB.m[options.band])-vB.LCparams['m'])) newerrm.extend(list(vB.err_m[options.band])) newmjd.extend(list(vB.mjd[options.band]-417./365.25))#shift lagged B v= VarQso(newmjd,newm,newerrm,band=options.band,medianize=False) if not options.fitsfile is None and os.path.exists(options.fitsfile): v.LCparams= paramsAB v.LCtype='powerlawSF' v.LCmean= 'zero' v.fitband= options.band v.LC= LCmodel(trainSet=v._build_trainset(options.band), type=v.LCtype,mean=v.LCmean, init_params=paramsAB) else: v.fit(options.band) if not options.fitsfile is None and not os.path.exists(options.fitsfile): save_pickles(options.fitsfile,vA.LCparams,vB.LCparams,v.LCparams) taus= nu.arange(1.,201.,1.)/365.25 thisskew= v.skew(taus,options.band,duration=0.7) thisgaussskews= nu.zeros((options.nsamples,len(taus))) print "Calculating simulated skews ..." for ii in range(options.nsamples): #First re-sample redshift= 1.41 o= v.resample(v.mjd[options.band],band=options.band,noconstraints=True, wedge=options.wedge, wedgerate=options.wedgerate*365.25/(1.+redshift), wedgetau=(1.+redshift)) #1yr o.LCparams= v.LCparams o.LC= v.LC o.fitband= v.fitband o.LCtype= v.LCtype o.LCmean= v.LCmean if options.wedge: o.LCparams['gamma']= 1. o.LCparams['logA']= o.LCparams['logA']\ +nu.log(0.05**v.LCparams['gamma']/0.05) o.LCmean= 'zero' #bc we remove the mean when resampling wedge #Set up LC with correct params o.LC= LCmodel(trainSet=o._build_trainset(options.band), type=o.LCtype,mean=o.LCmean, init_params=o.LCparams) thisgaussskews[ii,:]= o.skew(taus,options.band,duration=0.7) skews= {} gaussskews= {} skews['0957']= thisskew gaussskews['0957']= thisgaussskews save_pickles(savefilename,skews,gaussskews, None,options.band,None,taus) return None