当前位置: 首页>>代码示例>>Python>>正文


Python utils.first函数代码示例

本文整理汇总了Python中utils.first函数的典型用法代码示例。如果您正苦于以下问题:Python first函数的具体用法?Python first怎么用?Python first使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了first函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: fetchSfHistos

def fetchSfHistos(inputSfFiles=[], lepton='', verbose=False):
    from compute_fake_el_scale_factor import histoname_sf_vs_eta
    fileNames = inputSfFiles
    histos = dict()
    print "fetchSfHistos: fileNames ",fileNames
    if not (type(fileNames)==list and
            len(fileNames) in [1, 2]):
        print "fetchSfHistos expects one or two files (hflf+conv), got %s"%str(inputSfFiles)
        print "returning ",histos
        return histos
    if verbose : print "retrieving scale factors from %s"%inputSfFiles
    fname_hflf = first(filter(lambda _ : 'hflf' in _, fileNames))
    fname_conv = first(filter(lambda _ : 'conv' in _, fileNames))
    file_hflf = r.TFile.Open(fname_hflf) if fname_hflf else None
    file_conv = r.TFile.Open(fname_conv) if fname_conv else None
    hname = histoname_sf_vs_eta(lepton)
    histo_hflf = file_hflf.Get(hname) if file_hflf else None
    histo_conv = file_conv.Get(hname) if file_conv else None
    if histo_hflf : histos['hflf'] = composeEtaHistosAs2dPtEta(input1Dhisto=histo_hflf,
                                                               outhistoname=hname+'_hflf')
    if histo_conv : histos['conv'] = composeEtaHistosAs2dPtEta(input1Dhisto=histo_conv,
                                                               outhistoname=hname+'_conv')
    for f in [file_hflf, file_conv] :
        if f : f.Close()
    return histos
开发者ID:gerbaudo,项目名称:DileptonFakeMeasurement,代码行数:25,代码来源:fakeUtils.py

示例2: latest_obs_and_forecast

def latest_obs_and_forecast(site_id):
    result = memcache.get(site_id, "site_latest")
    if result:
        return result

    site = Site.get_by_key_name(site_id)
    if site is None:
        return None

    obs = ObservationTimestep.find_latest_by_site(site, limit=6)
    result = None

    if len(obs) > 0:
        forecasts = ForecastTimestep.find_by_site_closest_by_date(site, first(obs).observation_datetime,
                                                                  limit=50)
        closest_forecast = first(forecasts)
        if closest_forecast:
            matching_obs = first(filter(lambda o: o.observation_datetime == closest_forecast.forecast_datetime, obs))
            matching_forecasts = ifilter(lambda f: f.forecast_datetime == closest_forecast.forecast_datetime, forecasts)
            if matching_obs:
                #finally have both... a single obs report and multiple forecasts

                obs_dict = to_dict_excl_sites(matching_obs)
                obs_dict['best_forecast'] = map(to_dict_excl_sites,  make_five_day_list(matching_forecasts))
                result = {
                    'site': site.to_dict(),
                    'observation': obs_dict
                }
                memcache.set(site_id, result, 60 * 60, namespace='site_latest')

    return result
开发者ID:tempredirect,项目名称:MetOfficeWatch,代码行数:31,代码来源:dataccess.py

示例3: main

def main() :

    parser = optparse.OptionParser()
    parser.add_option("-n", "--histo-name")
    parser.add_option("-o", "--output-dir", default='./')
    parser.add_option("-s", "--suffix")
    (options, args) = parser.parse_args()
    histoName = options.histo_name
    outputDir = options.output_dir
    suffix    = options.suffix
    inputFileNames = args

    print '\n'.join(inputFileNames)
    sampleForFilename = dict((f, guessSampleFromFilename(f, samples)) for f in inputFileNames)
    assert all(s for f,s in sampleForFilename.iteritems())," cannot identify sample for some inputs %s"%pformat(sampleForFilename)
    filenames = collections.defaultdict(list)
    for fn, s in sampleForFilename.iteritems() : filenames[s].append(fn)
    assert all(len(fnames)==1 for s,fnames in filenames.iteritems()),"expected one file per sample, got %s"%pformat(filenames)
    filenames = dict((s, first(fn)) for s, fn in filenames.iteritems())
    files = dict((s, r.TFile.Open(fn)) for s, fn in filenames.iteritems())

    if histoName :
        print "plotting %s"%histoName
        canvasname = histoName+("_%s"%suffix if suffix else '')
        plotHisto(files, histoName, canvasname, outputDir)
    else :
        printHistoNames(first(files))
开发者ID:gerbaudo,项目名称:DileptonFakeMeasurement,代码行数:27,代码来源:plot_histo.py

示例4: weightedAverage

def weightedAverage(histosEff={}, histosWeight={}, histoName='', histoTitle='', verbose=False):
    getBinIndices, getBinContents, getBinning = rootUtils.getBinIndices, rootUtils.getBinContents, rootUtils.getBinning
    assert sorted(histosEff.keys())==sorted(histosWeight.keys()),"effs and weights must have the same keys:\n\teffs %s\n\tweights %s"%(str(histosEff.keys()), str(histosWeight.keys()))
    hout = first(histosEff).Clone(histoName if histoName else 'weighted_average_eff')
    hout.SetTitle(histoTitle)
    hout.Reset()
    def validateWeights(h):
        bins, values = getBinIndices(h), getBinContents(h)
        allValid = all(v>=0.0 and v<=1.0 for v in values)
        if not allValid:
            if verbose :
                print "warning '%s' weights not in [0,1] : [%s]"%(h.GetName(), ', '.join(("%.3f"%v for v in values)))
                print "setting them to 0.0 or 1.0"
            for b, v in zip(bins, values) : h.SetBinContent(b, 0.0 if v<0.0 else 1.0 if v>1.0 else v)
        return allValid
    [validateWeights(hw) for hw in histosWeight.values()]
    bins, binning = getBinIndices(first(histosEff)), getBinning(first(histosEff))
    for h in histosWeight.values()+histosEff.values():
        if getBinning(h)!=binning : print "warning: %s has binning %s, expecting %s"%(h.GetName(), str(getBinning(h)), str(binning))
    groups = sorted(histosEff.keys())
    epsilon = 1.0e-3
    binWeightNormalizations = [sum(histosWeight[g].GetBinContent(b) for g in groups) for b in bins]
    weightsAreNormalized = all(abs(1.0-norm)<epsilon for norm in binWeightNormalizations)
    if not weightsAreNormalized : print "warning, compositions are not normalized : [%s]"%', '.join(("%.3f"%v for v in binWeightNormalizations))
    print '-- ',histoName,'-- '
    for g in groups:
        bws, bcs = getBinContents(histosWeight[g]), getBinContents(histosEff[g])
        print "adding %18s : %s"%(g, ' : '.join("%.4f*%.4f"%(bw, bc) for bw, bc in zip(bws, bcs)))
        histoEff, histoWeight = histosEff[g], histosWeight[g]
        histoEff.Multiply(histoWeight)
        hout.Add(histoEff)
    print "tot weight   : %s"%' '.join(("%.4f"%v for v in (sum(histosWeight[g].GetBinContent(b) for g in groups) for b in bins)))
    print "weighted avg : %s"%' '.join(("%.4f"%v for v in getBinContents(hout)))
    return hout
开发者ID:gerbaudo,项目名称:DileptonFakeMeasurement,代码行数:34,代码来源:build_fake_matrices.py

示例5: plotFractions

def plotFractions(fractDict={}, outplotdir='./', prefix='') :
    """
    input : fractDict[sr][lep_type][sample] = float
    """
    outplotdir = outplotdir if outplotdir.endswith('/') else outplotdir+'/'
    def isInterestingRegion(r) : return any(k in r for k in ['CR8', 'WHSS', 'SSInc', 'SsEwk'])
    regions  = [r for r in selectionRegions() if isInterestingRegion(r)]
    leptypes = sorted(first(fractDict).keys())
    samples  = sorted(first(first(fractDict)).keys())
    ind = np.arange(len(regions))
    width = 0.5
    colors = dict(zip(samples, ['b','g','r','c','m','y']))
    for lt in leptypes :
        fracPerSample = dict((s, np.array([fractDict[r][lt][s] for r in regions])) for s in samples)
        below = np.zeros(len(regions))
        plots = []
        fig, ax = plt.subplots()
        for s, frac in fracPerSample.iteritems() :
            plots.append(plt.bar(ind, frac, width, color=colors[s], bottom=below))
            below = below + frac
        plt.ylabel('fractions')
        plt.title(prefix+' '+lt+' compositions')
        plt.xticks(ind+width/2., regions)
        plt.ylim((0.0, 1.0))
        plt.grid(True)
        plt.yticks(np.arange(0.0, 1.0, 0.2))
        labels = {'heavyflavor' : 'bb/cc', 'diboson' : 'VV', 'ttbar':'tt'}
        labels = [labels[s] if s in labels else s for s in samples]
        leg = plt.legend([p[0] for p in plots], labels, bbox_to_anchor=(1.135, 1.05))
        leg.get_frame().set_alpha(0.5)
        fig.autofmt_xdate(bottom=0.25, rotation=90, ha='center')
        fig.savefig(outplotdir+prefix+'_'+lt+'.png')
开发者ID:JosephineWittkowski,项目名称:SusyTest0,代码行数:32,代码来源:buildWeightedMatrix.py

示例6: plotStackedHistos

def plotStackedHistos(histosFlavorSlice={}, canvasName='', outputDir='./', frameTitle='stack', colors={}) :
    "Plot the input histos used to compute the fractions"
    histos = histosFlavorSlice
    can = r.TCanvas(canvasName, canvasName, 800, 600)
    can.cd()
    stack = r.THStack('stack_'+canvasName, '')
    leg = topRightLegend(can, 0.275, 0.475, shift=-0.025)
    leg.SetBorderSize(0)
    colors = SampleUtils.colors if not colors else colors
    procs = sorted(histos.keys())
    for p in procs:
        h = histos[p]
        h.SetFillColor(colors[p])
        h.SetLineColor(h.GetFillColor())
        h.SetDrawOption('bar')
        stack.Add(h)
    for s in procs[::-1] : leg.AddEntry(histos[s], s, 'F') # stack goes b-t, legend goes t-b
    stack.Draw('hist e')
    leg.Draw('same')
    tex = r.TLatex()
    tex.SetNDC(True)
    tex.SetTextFont(first(histos).GetTitleFont())
    tex.SetTextSize(first(histos).GetTitleSize())
    tex.DrawLatex(0.1, 0.925, frameTitle.split(';')[0])
    can.Update() # force stack to create canMaster
    canMaster = stack.GetHistogram()
    canMaster.SetTitle(frameTitle)
    canMaster.Draw('axis same')
    can._graphical_objects = [stack, canMaster, leg, tex] + [h for h in stack.GetStack()]
    can.Update()
    for ext in ['png','eps'] :
        outFilename = outputDir+'/'+canvasName+'.'+ext
        rmIfExists(outFilename)
        can.SaveAs(outFilename)
开发者ID:gerbaudo,项目名称:DileptonFakeMeasurement,代码行数:34,代码来源:plotParametrizedFractions.py

示例7: buildWeightedHisto

def buildWeightedHisto(histos={}, fractions={}, histoName='', histoTitle='') :
    "was getFinalRate"
    hout = first(histos).Clone(histoName if histoName else 'final_rate') # should pick a better default
    hout.SetTitle(histoTitle)
    hout.Reset()
    flatFraction = type(first(fractions)) is float
    if flatFraction :
        print "averaging flat ",histoName
        print 'keys -> ',histos.keys()
        for b in getBinIndices(hout) :
            tot, err2 = binWeightedSum(histos, fractions, b)
            hout.SetBinContent(b, tot)
            hout.SetBinError(b, sqrt(err2))
    else :
        bH, bF = getBinning(first(histos)), getBinning(first(fractions))
        assert bH == bF,"different binning %s: %s, %s: %s"%(first(histos).GetName(), bH, first(fractions).GetName(), bF)
        weightedHistos = dict((p, h.Clone(h.GetName()+'_weighted_for_'+histoName)) for p,h in histos.iteritems()) # preserve originals
        print "averaging 2d ",histoName
        for b in getBinIndices(hout):
            print "bin %d (w=%.1f):  %.3f = %s"%(b,
                                                 sum(fractions[p].GetBinContent(b) for p in fractions.keys()),
                                                 sum(fractions[p].GetBinContent(b)*weightedHistos[p].GetBinContent(b) for p in fractions.keys()),
                                                 '+'.join("%.2f*%.2f"%(fractions[p].GetBinContent(b), weightedHistos[p].GetBinContent(b))
                                                          for p in fractions.keys()))
        for p,h in weightedHistos.iteritems() :
            h.Multiply(fractions[p])
            hout.Add(h)
    return hout
开发者ID:gerbaudo,项目名称:DileptonFakeMeasurement,代码行数:28,代码来源:buildWeightedMatrix.py

示例8: done

    def done(self, reels, display, line):
        S      = [r.symbol() for r in reels]
        won    = bool(len(set(S)) == 1)
        amount = symbols[first(S)] if won else 0

        if won and display:
            print(winmsg % symbols[first(S)])
        return line, amount
开发者ID:Voder,项目名称:PBE,代码行数:8,代码来源:slotmachine.py

示例9: plotHistos

def plotHistos(bkgHistos, sigHistos, plotdir) :
    llnjs = first      (sigHistos).keys()
    vars  = first(first(sigHistos)).keys()
    for llnj in llnjs :
        for var in vars :
            plotVar(dict((s, bkgHistos[s][llnj][var]) for s in bkgHistos.keys()),
                    dict((s, sigHistos[s][llnj][var]) for s in sigHistos.keys()),
                    llnj+'_'+var, plotdir)
开发者ID:gerbaudo,项目名称:DileptonFakeMeasurement,代码行数:8,代码来源:optimizeSelection.py

示例10: frac2str

def frac2str(frac) :
    flatFraction = type(first(frac)) is float
    return ('\n'.join([''.join("%12s"%s for s in fakeProcesses()),
                       ''.join("%12s"%("%.3f"%frac[s]) for s in fakeProcesses())])
            if flatFraction
            else '\n'.join([''.join("%12s"%s for s in fakeProcesses())]
                           +[''.join("%12s"%("%.3f"%frac[s].GetBinContent(b)) for s in fakeProcesses())
                             for b in getBinIndices(first(frac))]))
开发者ID:gerbaudo,项目名称:DileptonFakeMeasurement,代码行数:8,代码来源:buildWeightedMatrix.py

示例11: plotPerSourceEff

def plotPerSourceEff(histosPerVar={}, outputDir='', lepton='', region='', sample='', verbose=False, zoomIn=True):
    "plot efficiency for each source (and 'anysource') as a function of each var; expect histos[var][source][loose,tight]"
    variables = histosPerVar.keys()
    sources = [s for s in first(histosPerVar).keys() if s!='real'] # only fake eff really need a scale factor
    colors = colorsLineSources
    mkdirIfNeeded(outputDir)
    for var in filter(lambda x : x in ['pt1', 'eta1'], histosPerVar.keys()):
        histosPerSource = dict((s, histosPerVar[var][s]) for s in sources)
        canvasBasename = region+'_efficiency_'+lepton+'_'+var+("_%s"%sample if sample else '')
        missingSources = [s for s, h in histosPerSource.iteritems() if not h['loose'] or not h['tight']]
        if missingSources:
            if verbose : print "skip %s, missing histos for %s"%(var, str(missingSources))
            continue
        anySourceLoose = summedHisto([h['loose'] for h in histosPerSource.values()])
        anySourceTight = summedHisto([h['tight'] for h in histosPerSource.values()])
        anySourceLoose.SetName(histoNamePerSource(var, 'any', 'loose', region))
        anySourceTight.SetName(histoNamePerSource(var, 'any', 'tight', region))
        histosPerSource['any'] = { 'loose' : anySourceLoose, 'tight' : anySourceTight }
        emptyBkg = anySourceLoose.Integral()==0 or anySourceTight.Integral()==0
        if emptyBkg:
            if verbose : print "empty backgrounds, skip %s"%canvasBasename
            continue
        def computeEfficiencies(histosPerSource={}) :
            sources = histosPerSource.keys()
            num = dict((s, histosPerSource[s]['tight']) for s in sources)
            den = dict((s, histosPerSource[s]['loose']) for s in sources)
            eff = dict((s, h.Clone(h.GetName().replace('tight', 'tight_over_loose')))
                       for s, h in num.iteritems())
            [eff[s].Divide(den[s]) for s in sources]
            return eff

        effs = computeEfficiencies(histosPerSource)
        can = r.TCanvas('c_'+canvasBasename, canvasBasename, 800, 600)
        can.cd()
        pm = first(effs) # pad master
        pm.SetStats(False)
        pm.Draw('axis')
        can.Update()
        for s, h in effs.iteritems() :
            h.SetMarkerColor(colors[s] if s in colors else r.kBlack)
            h.SetLineColor(h.GetMarkerColor())
            h.SetLineWidth(2*h.GetLineWidth())
            h.SetMarkerStyle(markersSources[s] if s in markersSources else r.kDot)
            h.Draw('ep same')
            h.SetDirectory(0)
        #pprint.pprint(effs)
        yMin, yMax = getMinMax(effs.values())
        pm.SetMinimum(0.0)
        pm.SetMaximum(0.25 if yMax < 0.5 and zoomIn else 1.1)
        can.Update()
        topRightLabel(can, canvasBasename, xpos=0.125, align=13)
        drawLegendWithDictKeys(can, effs, opt='lp')
        can.RedrawAxis()
        can._histos = effs
        can.Update()
        outFname = os.path.join(outputDir, canvasBasename+'.png')
        utils.rmIfExists(outFname)
        can.SaveAs(outFname)
开发者ID:gerbaudo,项目名称:SusyntHlfv,代码行数:58,代码来源:compute_fake_scale_factor.py

示例12: plotStackedHistos

def plotStackedHistos(histosPerGroup={}, outputDir='', region='', verbose=False):
    groups = histosPerGroup.keys()
    variables = first(histosPerGroup).keys()
    leptonTypes = first(first(histosPerGroup)).keys()
    colors = getGroupColor()
    mkdirIfNeeded(outputDir)
    histosPerName = dict([(region+'_'+var+'_'+lt, # one canvas for each histo, so key with histoname w/out group
                           dict([(g, histosPerGroup[g][var][lt]) for g in groups]))
                          for var in variables for lt in leptonTypes])
    for histoname, histosPerGroup in histosPerName.iteritems():
        missingGroups = [g for g, h in histosPerGroup.iteritems() if not h]
        if missingGroups:
            if verbose : print "skip %s, missing histos for %s"%(histoname, str(missingGroups))
            continue
        bkgHistos = dict([(g, h) for g, h in histosPerGroup.iteritems() if g not in ['data', 'signal']])
        totBkg = summedHisto(bkgHistos.values())
        err_band = None # buildErrBandGraph(totBkg, computeStatErr2(totBkg))
        emptyBkg = totBkg.Integral()==0
        if emptyBkg:
            if verbose : print "empty backgrounds, skip %s"%histoname
            continue
        can = r.TCanvas('c_'+histoname, histoname, 800, 600)
        can.cd()
        pm = totBkg # pad master
        pm.SetStats(False)
        pm.Draw('axis')
        can.Update() # necessary to fool root's dumb object ownership
        stack = r.THStack('stack_'+histoname,'')
        can.Update()
        r.SetOwnership(stack, False)
        for s, h in bkgHistos.iteritems() :
            h.SetFillColor(colors[s] if s in colors else r.kOrange)
            h.SetDrawOption('bar')
            h.SetDirectory(0)
            stack.Add(h)
        stack.Draw('hist same')
        # err_band.Draw('E2 same')
        data = histosPerGroup['data']
        if data and data.GetEntries():
            data.SetMarkerStyle(r.kFullDotLarge)
            data.Draw('p same')
        # yMin, yMax = getMinMax([h for h in [totBkg, data, err_band] if h]) # fixme with err_band
        yMin, yMax = 0.0, data.GetMaximum()
        pm.SetMinimum(0.0)
        pm.SetMaximum(1.1*yMax)
        can.Update()
        topRightLabel(can, histoname, xpos=0.125, align=13)
        # drawLegendWithDictKeys(can, dictSum(bkgHistos, {'stat err':err_band}), opt='f')
        drawLegendWithDictKeys(can, bkgHistos, opt='f')
        can.RedrawAxis()
        can._stack = stack
        can._histos = [h for h in stack.GetHists()]+[data]
        can.Update()
        outFname = os.path.join(outputDir, histoname+'.png')
        utils.rmIfExists(outFname)
        can.SaveAs(outFname)
开发者ID:gerbaudo,项目名称:SusyntHlfv,代码行数:56,代码来源:compute_fake_scale_factor.py

示例13: upstreamCommit

    def upstreamCommit(self):
        """The most recent commit this branch shares with its upstream.

    `git log` and `git reflog` are used to detect rebases on the upstream
    branch, in similar fashion to `git pull`.

    """
        if self.upstream is None:
            return None
        commitHashes = set(c.hash for c in self.allCommits)
        firstUpstreamReference = first(h.hash for h in self.upstream._refLog if h.hash in commitHashes)
        upstreamCommitHashes = set(c.hash for c in self.upstream.allCommits)
        return first(c for c in self.allCommits if c.hash in upstreamCommitHashes or c.hash == firstUpstreamReference)
开发者ID:chrisalice,项目名称:gittools,代码行数:13,代码来源:git.py

示例14: answer

def answer(flow_id, question_id, answer_id):
    flow = graph.flow(flow_id)
    question = graph.question(flow_id, question_id)

    answer = first([answer for answer in question.answers if answer["id"] == answer_id])

    rewards = [rel.end for rel in answer.relationships.outgoing(["Reward"])]

    next_question = first([rel.end for rel in answer.relationships.outgoing(["Next"])])

    return render_template(
        "answer.html", flow=flow, question=question, answer=answer, rewards=rewards, next_question=next_question
    )
开发者ID:rrees,项目名称:flow-web-demo-heroku,代码行数:13,代码来源:app.py

示例15: comp_sum

def comp_sum(supposition_results, ar, rcdl, set_totaller, **kwargs):
    if isinstance(ar, str):
        add_remove_names = ('added', 'removed')
        first_drill_down = first(lambda el: el[0] == ar[0], add_remove_names)
        ar = lambda sr: getattr(sr, first_drill_down)
    if isinstance(rcdl, str):
        sets = ('reachability_set', 'coverage_set', 'dissimilarity_set', 'liability_set')
        set_names = [first(lambda el: el[0] == letter, sets) for letter in rcdl]
        rcdl = lambda profile: [getattr(profile, sn) for sn in set_names]
    
    assert isinstance(supposition_results, SuppositionResults)
    return sum(sum((set_totaller(_set, source, **kwargs) for _set in rcdl(ar(change)))) 
               for (source, change) in supposition_results.iteritems())
开发者ID:charlesmccarthyirl,项目名称:FYP,代码行数:13,代码来源:CompetenceSelectionStrategies.py


注:本文中的utils.first函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。