Exemple #1
0
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
Exemple #2
0
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