Exemplo n.º 1
0
def main():
    #Take input image and convert it into 3 parts presenting 3 numbers
    inputurl = "C:\\Users\\Tan\\Desktop\\digital_screen\\DigitsScreen\\empty.png"
    inputnumbers = takeinput(inputurl)

    #Import the source number to compare
    source = "C:\\Users\\Tan\\Desktop\\digital_screen\\source_numbers\\"
    number = insert(source)

    #Compare the processed input with the source numbers
    result = compare(inputnumbers, number)

    #Format the result into xx,x*C
    showresult(result)
Exemplo n.º 2
0
def index():
    """Show portfolio of stocks"""

    # query finance.db sales table for results of purchases for current user grouped by stock symbol (exc. shares that have been sold: having cost > 0)
    # tracker table determines number of shares user currently holds
    session["getStocks"] = db.execute(
        "SELECT sales.user_id, sales.symbol, sales.name, SUM(cost) AS holdings, tracker.shares, ROUND((SUM(cost) / tracker.shares), 2) AS value FROM sales JOIN tracker ON sales.symbol=tracker.symbol WHERE sales.user_id = :user AND tracker.user_id = :user GROUP BY sales.symbol HAVING tracker.shares > 0;",
        user=session["user_id"])

    # get cash balance of current user
    getBalance = db.execute("SELECT cash FROM users WHERE id = ?",
                            session["user_id"])

    # # iterate through rows to determine total value of shares
    # session["overall"] stores total current price of all shares (using lookup())
    session["overall"] = 0
    # session["lossGain"] stores total price of all shares when purchased (using holdings column)
    session["lossGain"] = 0
    for stock in session["getStocks"]:
        session["overall"] += stock["shares"] * (lookup(
            stock["symbol"])["price"])
        session["lossGain"] += stock["holdings"]

    # current cash balance of user
    session["balance"] = getBalance[0]["cash"]
    # current value of shares + cash balance
    session["overall"] += session["balance"]
    # purchased value of shares + cash balance
    session["lossGain"] += session["balance"]
    # check if user has made overall losses or gains and return html element using compare helper
    session["ovrArrow"] = compare(session["overall"], session["lossGain"])

    # push required variables and helpers to index.html
    return render_template('index.html',
                           getStocks=session["getStocks"],
                           balance=session["balance"],
                           overall=session["overall"],
                           ovrArrow=session["ovrArrow"],
                           lookup=lookup,
                           float=float,
                           usd=usd,
                           compare=compare)
Exemplo n.º 3
0
    def check(self):
        # Логинимся и получаем данные
        pik_data = PIKData(self.credentials.login, self.credentials.password)
        changes = []
        init = False
        for step in self.steps:
            try:
                params = step['params']
                label = step['label']
                print("Проверка '{}':".format(label))
                path = os.path.join(folder, params['file'])

                initial_data = flatten(load_data(path), reducer='dot')
                new_data_raw = getattr(pik_data, params['new_data'])
                new_data = flatten(new_data_raw, reducer='dot')

                if not initial_data:
                    init = True
                diffs = compare(initial_data, new_data)
                if diffs:
                    self.log_changes(diffs, initial_data, new_data)
                    print('Обнаружены изменения!')
                    print(diffs)
                    changes.append({'label': label, 'values': diffs})
                    dump_data(new_data_raw, path)
                else:
                    print('    Изменений нет!')
            except Exception as e:
                print('Exception:', str(e))
        if changes and not self.silent:
            if init:
                self.bot.send_init_message(changes)
            else:
                self.bot.send_message(changes)
        if not self.silent:
            self.bot.update_time_message()
Exemplo n.º 4
0
def main():

    ops = options()

    if not ops.plotter:
        fatal("Need --plotter for configuration.")
    plotter = yaml.load(open(ops.plotter))

    trees   = {}
    plots   = {}
    weights = {}
    colors  = {}
    labels  = {}
    stack   = {}
    overlay = {}
    is_data = {}

    # retrieve inputs
    for sample in plotter["samples"]:

        name = sample["name"]

        # input trees
        paths = sample["path"]
        if not glob.glob(paths):
            fatal("Found no files at %s" % (paths))

        trees[name] = ROOT.TChain(plotter["tree"])
        for path in glob.glob(paths):
            trees[name].Add(path)

        # misc
        weights[name] = " * ".join(sample["weights"]) if "weights" in sample else "1"
        colors[name]  = eval(sample["color"]) if sample["color"].startswith("ROOT") else sample["color"] 
        labels[name]  = sample["label"]
        stack[name]   = sample["stack"]
        overlay[name] = sample["overlay"]
        is_data[name] = sample["is_data"]

    # create output file
    output = ROOT.TFile.Open("%s/plots.%s.canv.root" % (plotter["directory"], timestamp), "recreate")

    # make money
    for plot in plotter["plots"]:

        hists = {}
        draw = {}
        if "bins" in plot:
            draw["bins"]      = array.array("d", plot["bins"])
        draw["title"]     = ";%s;%s" % (plot["xtitle"], plot["ytitle"])
        draw["variable"]  = plot["variable"]
        draw["selection"] = " && ".join(plotter["selection"])

        canv = ROOT.TCanvas(plot["name"], plot["name"], 800, 800)
        canv.Draw()
        canv.SetLogy(plot["logY"])

        stacks   = ROOT.THStack(plot["name"]+"stacks",   draw["title"])
        overlays = ROOT.THStack(plot["name"]+"overlays", draw["title"])
        do_stack = False
        do_overlay = False
        
        for sample in trees:

            draw["name"]   = plot["name"]+"__"+sample
            draw["weight"] = weights[sample]
            for option in ["weight"]:
                draw[option] = "(%s)" % (draw[option])


            if "bins" in plot:
                hists[sample] = ROOT.TH1F(draw["name"], draw["title"], len(draw["bins"])-1, draw["bins"])
            else:
                hists[sample] = ROOT.TH1F(draw["name"], draw["title"], plot["n_bins"], plot["bin_low"], plot["bin_high"])
            hists[sample].Sumw2()

            trees[sample].Draw("%(variable)s >> %(name)s" % draw, "(%(selection)s) * %(weight)s" % draw, "goff")
            output.cd()
            hists[sample].Write()
#            print "(%(selection)s) * %(weight)s" % draw
            
            #hists[sample].Scale(1/hists[sample].Integral(0, hists[sample].GetNbinsX()))

            if stack[sample]:
                do_stack = True
                hists[sample].SetFillColor(colors[sample])
                hists[sample].SetLineColor(ROOT.kBlack)
                hists[sample].SetLineWidth(2)
                stacks.Add(copy.copy(hists[sample]), ("ep" if is_data[sample] else "hist"))

            if overlay[sample]:
                do_overlay = True
                hists[sample].SetFillColor(0)
                hists[sample].SetLineColor(colors[sample])
                hists[sample].SetLineWidth(3)
                overlays.Add(copy.copy(hists[sample]), ("ep" if is_data[sample] else "hist"))

        # draw
        maximum = max([stacks.GetMaximum(), overlays.GetMaximum("nostack")])
        maximum = maximum*(100.0 if plot["logY"]     else 2.0)
        maximum = maximum*(1.2   if plotter["ratio"] else 1.0)

        if do_stack:
            stacks.SetMaximum(maximum)
            stacks.Draw()

        if do_overlay and do_stack:
            overlays.SetMaximum(maximum)
            overlays.Draw("nostack,same")
        elif do_overlay:
            overlays.SetMaximum(maximum)
            overlays.Draw("nostack")

        if plotter["data"]:
            pass

        if plotter["ratio"] and stacks.GetStack():

            # numerator definition is a placeholder.
            # only works if overlay[0]=data.
            ratio = helpers.ratio(name   = canv.GetName()+"_ratio",
                                  numer  = overlays.GetHists()[0],   # AHH KILL ME
                                  denom  = stacks.GetStack().Last(),
                                  min    = 0.45,
                                  max    = 1.55,
                                  ytitle = "Data / pred."
                                  )
            share = helpers.same_xaxis(name          = canv.GetName()+"_share",
                                       top_canvas    = canv,
                                       bottom_canvas = ratio,
                                       )
            canv.SetName(canv.GetName()+"_noratio")
            share.SetName(share.GetName().replace("_share", ""))
            canv = share

        elif plotter["ratio"] and not stacks.GetStack():
            warn("Want to make ratio plot but dont have stack. Skipping ratio.")

        # stack legend
        xleg, yleg = 0.6, 0.7
        legend = ROOT.TLegend(xleg, yleg, xleg+0.2, yleg+0.2)

        if do_stack:
            for hist in reversed(stacks.GetHists()):
                legend.AddEntry(hist, labels[hist.GetName().split("__")[1]], "f")

        if do_overlay:
            for hist in reversed(overlays.GetHists()):
                legend.AddEntry(hist, labels[hist.GetName().split("__")[1]], "l")
        
        legend.SetBorderSize(0)
        legend.SetFillColor(0)
        legend.SetMargin(0.3)
        legend.SetTextSize(0.04)
        legend.Draw()

        # watermarks
        xatlas, yatlas = 0.38, 0.87
        atlas = ROOT.TLatex(xatlas,      yatlas, "ATLAS Internal")
        hh4b  = ROOT.TLatex(xatlas, yatlas-0.06, "X #rightarrow HH #rightarrow 4b")
        lumi  = ROOT.TLatex(xatlas, yatlas-0.12, "#sqrt{s} = 13 TeV")
        watermarks = [atlas, hh4b, lumi]

        # KS, chi2
        if stacks.GetStack():
            if plotter.get("ks"):
                kolg, chi2, ndf = helpers.compare(overlays.GetHists()[0],
                                                  stacks.GetStack().Last(),
                                                  ) # AH KILL ME
                yks   = 0.975
                ychi2 = 0.975
                xks   = 0.27
                xchi2 = 0.55
                
                ks = ROOT.TLatex(xks,   yks,   "KS = %5.3f" % (kolg))
                ch = ROOT.TLatex(xchi2, ychi2, "#chi^{2} / ndf = %.1f / %i" % (chi2, ndf))
                watermarks += [ks, ch]

        # draw watermarks
        for wm in watermarks:
            wm.SetTextAlign(22)
            wm.SetTextSize(0.04)
            wm.SetTextFont(42)
            wm.SetNDC()
            wm.Draw()

        canv.SaveAs(plotter["directory"] + "/" + canv.GetName()+".pdf")

        output.Close()
Exemplo n.º 5
0
            'parameters': {
                'CCOOL_COIL': 'DXCOIL',
                'LSOLAR_PANEL': True
            }
        }
        patch_nml = [patch_nml_trial, patch_nml_ref]
        make = [False, False]

    else:
        raise RuntimeError('The correct case was not selected')

    print(
        f'Using configuration: {trial}, {ref}, {case_name}, {build_type}, {patch_nml}, {make}, {allow_failure}'
    )

    compare(trial, ref, case_name, build_type, patch_nml, make, allow_failure)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--build_type',
                        required=True,
                        choices=['Debug', 'Release'],
                        help='CMAKE_BUILD_TYPE')
    parser.add_argument(
        '--case',
        required=True,
        choices=['make_cmake', 'integration', 'psychrometrics', 'minimal_dx'],
        help='The test case to run')
    parser.add_argument('--allow_failure',
                        required=False,
Exemplo n.º 6
0
"""
ENVIRONMENT VARIABLES & GLOBAL OBJECTS
"""
data = None
if not args.filename:
    print('NO FILENAME SPECIFIED')
else:
    with open(args.filename, 'rb') as f:
        data = f.read().decode('utf8', 'ignore') # immediately decode to string

models = {}
mhemingway = Word2Vec.load('models/hemingway.model')
models.update({'hemingway': mhemingway})

"""
ROUTINES
"""
comparisons = {}
for author, model in models.items():
    metric = compare(model, data)
    comparisons.update({author: metric})

maxauthor = None
max = -1
for author, metric in comparisons.items():
    if metric > max:
        maxauthor = author
        max = metric

print("THE APPLICATION PREDICTS THE TEXT WAS WRITTEN BY {} WITH A CONFIDENCE METRIC {}".format(maxauthor, metric))
Exemplo n.º 7
0
# iterate over sentences
for (idx, sentence) in enumerate(nospace_sentences):
	# default max word length as 15 
	mytext = models.NoSpaceText(sentence, 15)

	# set frequency dictionaries
	mytext.freq_dict = freq_dict
	mytext.normFactor = normFactor
	mytext.transition_freq_dict = transition_freq_dict
	mytext.transNormFactor = transNormFactor

	# find segmentation using naive frequencies
	mytext.dpGreedy()
	bestSeg = mytext.getBestSeg()
	if helpers.compare(bestSeg, sentences[idx]):
		tallyNaiveProb += 1

	# find segmentation using transition frequencies
	mytext.dpGreedy(transFreq = True)
	bestSeg = mytext.getBestSeg()
	if helpers.compare(bestSeg, sentences[idx]):
		tallyTransProb += 1

# print results
print "Using Naive Frequencies:"
print float(tallyNaiveProb)/(len(sentences))
print "Using Transition Frequencies:"
print float(tallyTransProb)/(len(sentences))

# print total time
Exemplo n.º 8
0
        legend.SetMargin(0.3)
        legend.SetTextSize(0.04)
        legend.Draw()

        # watermarks
        xatlas, yatlas = 0.38, 0.87
        atlas = ROOT.TLatex(xatlas,      yatlas, "ATLAS Internal")
        hh4b  = ROOT.TLatex(xatlas, yatlas-0.06, "X #rightarrow HH #rightarrow 4b")
        lumi  = ROOT.TLatex(xatlas, yatlas-0.12, "13 TeV")
        watermarks = [atlas, hh4b, lumi]

        # KS, chi2
        if stacks.GetStack():
            if plotter.get("ks"):
                kolg, chi2, ndf = helpers.compare(overlays.GetHists()[0],
                                                  stacks.GetStack().Last(),
                                                  ) # AH KILL ME
                yks   = 0.975
                ychi2 = 0.975
                xks   = 0.27
                xchi2 = 0.55
                
                ks = ROOT.TLatex(xks,   yks,   "KS = %5.3f" % (kolg))
                ch = ROOT.TLatex(xchi2, ychi2, "#chi^{2} / ndf = %.1f / %i" % (chi2, ndf))
                watermarks += [ks, ch]

        # draw watermarks
        for wm in watermarks:
            wm.SetTextAlign(22)
            wm.SetTextSize(0.04)
            wm.SetTextFont(42)
Exemplo n.º 9
0
def main():

    ops = options()

    if not ops.plotter:
        fatal("Need --plotter for configuration.")
    elif ".yml" in ops.plotter:
        print "use yml"
        plotter = yaml.load(open(ops.plotter))
    else:
        plotter = plotConfig.getConfig(ops.plotter)

    if not "directory" in plotter:
        plotter["directory"] = "."
        warn("No directory given. Writing output to cwd.")

    if not "tree" in plotter:
        print "No input Tree, assuming hists"
        files = {}
        folders = {}
        useTree = False
    else:
        useTree = True

    trees = {}
    plots = {}
    weights = {}
    colors = {}
    labels = {}
    stack = {}
    overlay = {}
    is_data = {}
    samples = []
    prename = plotter["prename"]
    inputpath = ""
    if ops.inputdir:
        inputpath = ops.inputdir + "/"
        prename = ops.inputdir + "_" + prename

    # retrieve inputs
    print time.strftime("%Y-%m-%d-%Hh%Mm%Ss"), "Retrieving input files"
    for sample in plotter["samples"]:
        print sample
        name = sample["name"]
        samples.append(name)

        # input files
        paths = sample["path"]
        #set the full path directory here
        paths = "../Output/" + inputpath + paths

        if not glob.glob(paths):
            fatal("Found no files at %s" % (paths))

        if useTree:
            trees[name] = ROOT.TChain(plotter["tree"])
            for path in glob.glob(paths):
                if len(path) > 100:
                    print "adding file:", path[0:50] + "..." + path[len(path) -
                                                                    50:]
                else:
                    print "adding file:", path
                trees[name].Add(path)

        else:
            path = paths
            files[name] = ROOT.TFile(path, "READ")
            folders[name] = sample["folder"]

        # misc
        if useTree:
            weights[name] = " * ".join(
                sample["weights"]) if "weights" in sample else "1"
        else:
            weights[name] = sample["weights"]
        print sample["weights"]
        colors[name] = eval(sample["color"]) if sample["color"].startswith(
            "ROOT") else sample["color"]
        labels[name] = sample["label"]
        stack[name] = sample["stack"]
        overlay[name] = sample["overlay"]
        is_data[name] = sample["is_data"]

    # create output file
    if not os.path.isdir(plotter["directory"]):
        os.makedirs(plotter["directory"])
    output = ROOT.TFile.Open(
        "%s/%splots.root" % (plotter["directory"], prename), "recreate")

    # make money
    for plot in plotter["plots"]:

        hists = {}
        draw = {}
        if "bins" in plot:
            draw["bins"] = array.array("d", [float(x) for x in plot["bins"]])
        draw["title"] = ";%s;%s" % (plot["xtitle"], plot["ytitle"])
        draw["variable"] = plot["variable"]
        if "selection" in plotter:
            draw["selection"] = " && ".join(plotter["selection"])

        canv = ROOT.TCanvas(plot["name"], plot["name"], 800, 800)
        canv.Draw()
        canv.SetLogy(plot["logY"])

        stacks = ROOT.THStack(plot["name"] + "stacks", draw["title"])
        overlays = ROOT.THStack(plot["name"] + "overlays", draw["title"])
        do_stack = False
        do_overlay = False

        for sample in reversed(sorted(samples)):
            draw["name"] = plot["name"] + "__" + sample
            draw["weight"] = weights[sample]
            if useTree:
                for option in ["weight"]:
                    draw[option] = "(%s)" % (draw[option])

            if useTree:
                if "bins" in plot:
                    hists[sample] = ROOT.TH1F(draw["name"], draw["title"],
                                              len(draw["bins"]) - 1,
                                              draw["bins"])
                else:
                    hists[sample] = ROOT.TH1F(draw["name"], draw["title"],
                                              plot["n_bins"], plot["bin_low"],
                                              plot["bin_high"])
                hists[sample].Sumw2()

            else:
                if folders[sample] != "":
                    hists[sample] = files[sample].Get(folders[sample] + "/" +
                                                      draw["variable"])
                else:
                    hists[sample] = files[sample].Get(draw["variable"])

                hists[sample].SetName(draw["name"])
                hists[sample].SetTitle(draw["title"])
                hists[sample].Rebin(plot["rebin"])
                if "bin_high" in plot.keys():
                    bin_low = float(plot["bin_low"])
                    bin_high = float(plot["bin_high"])
                    hists[sample].GetXaxis().SetRangeUser(bin_low, bin_high)

            if is_data[sample]:
                hists[sample].SetMarkerStyle(20)
                hists[sample].SetMarkerSize(1)

            if useTree:
                trees[sample].Draw("%(variable)s >> %(name)s" % draw,
                                   "(%(selection)s) * %(weight)s" % draw,
                                   "goff")
            else:
                hists[sample].Sumw2()
                hists[sample].Scale(float(draw["weight"]))
                hists[sample].Draw()

            # hists[sample].Scale(1/hists[sample].Integral(0, hists[sample].GetNbinsX()))

            if stack[sample]:
                do_stack = True
                hists[sample].SetFillColor(colors[sample])
                hists[sample].SetLineColor(ROOT.kBlack)
                hists[sample].SetLineWidth(2)
                stacks.Add(copy.copy(hists[sample]),
                           ("ep" if is_data[sample] else "hist"))
                if not useTree and "bin_high" in plot.keys(
                ):  #have to set stack xaxis range for zooming because ROOT SUCKS
                    stacks.Draw()
                    stacks.GetXaxis().SetRangeUser(bin_low, bin_high)

            if overlay[sample]:
                do_overlay = True
                hists[sample].SetFillColor(0)
                hists[sample].SetLineColor(colors[sample])
                hists[sample].SetMarkerColor(colors[sample])
                hists[sample].SetLineWidth(3)
                h = copy.copy(hists[sample])
                if plotter["norm"]:
                    print h.GetName()
                    scale = h.Integral(0, h.GetXaxis().GetNbins() + 1)
                    if scale != 0:
                        h.Scale(1.0 / scale)
                overlays.Add(h, ("ep" if is_data[sample] else "hist"))

            print sample
            print "Integral:", hists[sample].Integral(
                0, hists[sample].GetNbinsX() + 1)
            print " Entries:", hists[sample].GetEntries()

        # draw
        maximum = max([stacks.GetMaximum(), overlays.GetMaximum("nostack")])
        maximum = maximum * (100.0 if plot["logY"] else 2.0)
        maximum = maximum * (1.2 if plotter["ratio"] else 1.0)
        minimum = max([stacks.GetMinimum(), overlays.GetMinimum("nostack")])
        minimum = minimum / (2 if plot["logY"] else 1.2)

        if do_stack:
            stacks.SetMaximum(maximum)
            stacks.SetMinimum(minimum)
            stacks.Draw()
            h1stackerror = copy.copy(stacks.GetStack().Last())
            h1stackerror.SetName("stat. error")
            h1stackerror.SetFillColor(ROOT.kGray + 3)
            h1stackerror.SetFillStyle(3005)
            h1stackerror.SetMarkerStyle(0)
            h1stackerror.Draw("SAME,E2")

        if do_overlay and do_stack:
            overlays.SetMaximum(maximum)
            overlays.SetMinimum(minimum)
            overlays.Draw("nostack,same")
        elif do_overlay:
            overlays.SetMaximum(maximum)
            overlays.SetMinimum(minimum)
            overlays.Draw("nostack")

        if plotter["data"]:
            pass

        if plotter["ratio"] and stacks.GetStack():

            # numerator definition is a placeholder.
            # only works if overlay[0]=data.
            if plotter["autoRatio"]:
                top = overlays.GetHists()[0].Clone("hnew")
                bottom = stacks.GetStack().Last().Clone("hnew")
                top.Divide(bottom)
                ratioBins = []
                for bin in range(top.GetSize()):
                    if top.GetBinContent(bin) != 0.0 and top.GetBinContent(
                            bin) * top.GetBinError(bin) < 5.0:
                        ratioBins.append(top.GetBinContent(bin))
                if not ratioBins: ratioBins = [0]

                ratioMin = float(int(min(ratioBins) * 100)) / 100
                ratioMax = float(int(max(ratioBins) * 100)) / 100
                if ratioMax - ratioMin < 0.1:
                    ratioMin = ratioMin - .05
                    ratioMax = ratioMax + 0.5

            else:
                ratioMin = 0
                ratioMax = 2

            ratio = helpers.ratio(
                name=canv.GetName() + "_ratio",
                numer=overlays.GetHists()[0],  # AHH KILL ME
                denom=stacks.GetStack().Last(),
                min=ratioMin,
                max=ratioMax,
                ytitle="Data / pred.")
            share, top, bot = helpers.same_xaxis(
                name=canv.GetName() + "_share",
                top_canvas=canv,
                bottom_canvas=ratio,
            )
            canv.SetName(canv.GetName() + "_noratio")
            share.SetName(share.GetName().replace("_share", ""))
            canv = share

        elif plotter["ratio"] and not stacks.GetStack():
            warn(
                "Want to make ratio plot but dont have stack. Skipping ratio.")

        # stack legend
        xleg, yleg = 0.6, 0.7
        legend = ROOT.TLegend(xleg, yleg, xleg + 0.2, yleg + 0.2)

        if do_stack:
            for hist in reversed(stacks.GetHists()):
                legend.AddEntry(hist, labels[hist.GetName().split("__")[1]],
                                "f")

        if do_overlay:
            for hist in reversed(overlays.GetHists()):
                legend.AddEntry(hist, labels[hist.GetName().split("__")[1]],
                                "l")

        legend.SetBorderSize(0)
        legend.SetFillColor(0)
        legend.SetMargin(0.3)
        legend.SetTextSize(0.04)
        legend.Draw()

        # watermarks
        xatlas, yatlas = 0.38, 0.87
        atlas = ROOT.TLatex(xatlas, yatlas, "ATLAS Internal")
        hh4b = ROOT.TLatex(xatlas, yatlas - 0.06,
                           "X #rightarrow HH #rightarrow 4b")
        lumi = ROOT.TLatex(xatlas, yatlas - 0.12, "13 TeV")
        watermarks = [atlas, hh4b, lumi]

        # KS, chi2
        if stacks.GetStack():
            if plotter.get("ks"):
                kolg, chi2, ndf = helpers.compare(
                    overlays.GetHists()[0],
                    stacks.GetStack().Last(),
                )  # AH KILL ME
                yks = 0.975
                ychi2 = 0.975
                xks = 0.27
                xchi2 = 0.55

                ks = ROOT.TLatex(xks, yks, "KS = %5.3f" % (kolg))
                ch = ROOT.TLatex(xchi2, ychi2,
                                 "#chi^{2} / ndf = %.1f / %i" % (chi2, ndf))
                watermarks += [ks, ch]

        # draw watermarks
        for wm in watermarks:
            wm.SetTextAlign(22)
            wm.SetTextSize(0.04)
            wm.SetTextFont(42)
            wm.SetNDC()
            wm.Draw()

        canv.SaveAs(
            os.path.join(plotter["directory"],
                         prename + canv.GetName() + ".pdf"))

        output.cd()
        canv.Write()

    output.Close()
# -----------------------------------------------------------------------------------------------------------------------------------------------------------

# -------------------------------------------------------------------Get Pearson CC--------------------------------------------------------------------------
correlation, indexes = get_pearson(healthy, cancerous)
# -----------------------------------------------------------------------------------------------------------------------------------------------------------

# -----------------------------------------------------------------Get Max/Min Index-------------------------------------------------------------------------
max_key, max_value, max_index = get_max(correlation, indexes)
# -----------------------------------------------------------------------------------------------------------------------------------------------------------

# -----------------------------------------------------------------Plot Max/Min Gene-------------------------------------------------------------------------
scatter_plot(healthy,
             cancerous,
             max_index[0],
             xlabel='H_Expression_Level',
             ylabel='C_Expression_Level',
             title='Maximum Correlation Gene')
# -----------------------------------------------------------------------------------------------------------------------------------------------------------

# --------------------------------------------------------------Get the p-value as paired--------------------------------------------------------------------
diffrentially_genes_paired = p_values_paired(healthy, cancerous, 0.05)
# -----------------------------------------------------------------------------------------------------------------------------------------------------------

# ------------------------------------------------------------Get the p-value as independant-----------------------------------------------------------------
diffrentially_genes_ind = p_values_ind(healthy, cancerous, 0.05)
# -----------------------------------------------------------------------------------------------------------------------------------------------------------

# -------------------------------------------------------------------Compare the two-------------------------------------------------------------------------
common, paired_only, ind_only = compare(diffrentially_genes_paired,
                                        diffrentially_genes_ind)
# -------------------------------------------------------------------Compare the two-------------------------------------------------------------------------
Exemplo n.º 11
0
def main():

    ops = options()

    if not ops.plotter:
        fatal("Need --plotter for configuration.")
    plotter = yaml.load(open(ops.plotter))

    if not "directory" in plotter:
        plotter["directory"] = "."
        warn("No directory given. Writing output to cwd.")

    trees = {}
    plots = {}
    weights = {}
    colors = {}
    labels = {}
    stack = {}
    overlay = {}
    is_data = {}

    # retrieve inputs
    for sample in plotter["samples"]:

        name = sample["name"]

        # input trees
        paths = sample["path"]
        if not glob.glob(paths):
            fatal("Found no files at %s" % (paths))

        trees[name] = ROOT.TChain(plotter["tree"])
        for path in glob.glob(paths):
            trees[name].Add(path)

        # misc
        weights[name] = " * ".join(
            sample["weights"]) if "weights" in sample else "1"
        colors[name] = eval(sample["color"]) if sample["color"].startswith(
            "ROOT") else sample["color"]
        labels[name] = sample["label"]
        stack[name] = sample["stack"]
        overlay[name] = sample["overlay"]
        is_data[name] = sample["is_data"]

    # create output file
    if not os.path.isdir(plotter["directory"]):
        os.makedirs(plotter["directory"])
    output = ROOT.TFile.Open(
        "%s/plots.%s.canv.root" % (plotter["directory"], timestamp),
        "recreate")

    # make money
    for plot in plotter["plots"]:

        hists = {}
        draw = {}
        if "bins" in plot:
            draw["bins"] = array.array("d", plot["bins"])
        draw["title"] = ";%s;%s" % (plot["xtitle"], plot["ytitle"])
        draw["variable"] = plot["variable"]
        draw["selection"] = " && ".join(plotter["selection"])

        canv = ROOT.TCanvas(plot["name"], plot["name"], 800, 800)
        canv.Draw()
        canv.SetLogy(plot["logY"])

        stacks = ROOT.THStack(plot["name"] + "stacks", draw["title"])
        overlays = ROOT.THStack(plot["name"] + "overlays", draw["title"])
        do_stack = False
        do_overlay = False

        for sample in reversed(sorted(trees.keys())):

            draw["name"] = plot["name"] + "__" + sample
            draw["weight"] = weights[sample]
            for option in ["weight"]:
                draw[option] = "(%s)" % (draw[option])

            if "bins" in plot:
                hists[sample] = ROOT.TH1F(draw["name"], draw["title"],
                                          len(draw["bins"]) - 1, draw["bins"])
            else:
                hists[sample] = ROOT.TH1F(draw["name"], draw["title"],
                                          plot["n_bins"], plot["bin_low"],
                                          plot["bin_high"])
            hists[sample].Sumw2()

            if is_data[sample]:
                hists[sample].SetMarkerStyle(20)
                hists[sample].SetMarkerSize(1)

            trees[sample].Draw("%(variable)s >> %(name)s" % draw,
                               "(%(selection)s) * %(weight)s" % draw, "goff")

            # hists[sample].Scale(1/hists[sample].Integral(0, hists[sample].GetNbinsX()))

            if stack[sample]:
                do_stack = True
                hists[sample].SetFillColor(colors[sample])
                hists[sample].SetLineColor(ROOT.kBlack)
                hists[sample].SetLineWidth(2)
                stacks.Add(copy.copy(hists[sample]),
                           ("ep" if is_data[sample] else "hist"))

            if overlay[sample]:
                do_overlay = True
                hists[sample].SetFillColor(0)
                hists[sample].SetLineColor(colors[sample])
                hists[sample].SetLineWidth(3)
                overlays.Add(copy.copy(hists[sample]),
                             ("ep" if is_data[sample] else "hist"))

            print hists[sample].Integral(0, hists[sample].GetNbinsX() + 1)
            print hists[sample].GetEntries()

        # draw
        maximum = max([stacks.GetMaximum(), overlays.GetMaximum("nostack")])
        maximum = maximum * (100.0 if plot["logY"] else 2.0)
        maximum = maximum * (1.2 if plotter["ratio"] else 1.0)

        if do_stack:
            stacks.SetMaximum(maximum)
            stacks.Draw()
            h1stackerror = copy.copy(stacks.GetStack().Last())
            h1stackerror.SetName("stat. error")
            h1stackerror.SetFillColor(ROOT.kGray + 3)
            h1stackerror.SetFillStyle(3005)
            h1stackerror.SetMarkerStyle(0)
            h1stackerror.Draw("SAME,E2")

        if do_overlay and do_stack:
            overlays.SetMaximum(maximum)
            overlays.Draw("nostack,same")
        elif do_overlay:
            overlays.SetMaximum(maximum)
            overlays.Draw("nostack")

        if plotter["data"]:
            pass

        if plotter["ratio"] and stacks.GetStack():

            # numerator definition is a placeholder.
            # only works if overlay[0]=data.
            ratio = helpers.ratio(
                name=canv.GetName() + "_ratio",
                numer=overlays.GetHists()[0],  # AHH KILL ME
                denom=stacks.GetStack().Last(),
                min=0.45,
                max=2.0,
                ytitle="Data / pred.")
            share = helpers.same_xaxis(
                name=canv.GetName() + "_share",
                top_canvas=canv,
                bottom_canvas=ratio,
            )
            canv.SetName(canv.GetName() + "_noratio")
            share.SetName(share.GetName().replace("_share", ""))
            canv = share

        elif plotter["ratio"] and not stacks.GetStack():
            warn(
                "Want to make ratio plot but dont have stack. Skipping ratio.")

        # stack legend
        xleg, yleg = 0.6, 0.7
        legend = ROOT.TLegend(xleg, yleg, xleg + 0.2, yleg + 0.2)

        if do_stack:
            for hist in reversed(stacks.GetHists()):
                legend.AddEntry(hist, labels[hist.GetName().split("__")[1]],
                                "f")

        if do_overlay:
            for hist in reversed(overlays.GetHists()):
                legend.AddEntry(hist, labels[hist.GetName().split("__")[1]],
                                "l")

        legend.SetBorderSize(0)
        legend.SetFillColor(0)
        legend.SetMargin(0.3)
        legend.SetTextSize(0.04)
        legend.Draw()

        # watermarks
        xatlas, yatlas = 0.38, 0.87
        atlas = ROOT.TLatex(xatlas, yatlas, "ATLAS Internal")
        hh4b = ROOT.TLatex(xatlas, yatlas - 0.06,
                           "X #rightarrow HH #rightarrow 4b")
        lumi = ROOT.TLatex(xatlas, yatlas - 0.12, "13 TeV, 78.7 pb^{-1}")
        watermarks = [atlas, hh4b, lumi]

        # KS, chi2
        if stacks.GetStack():
            if plotter.get("ks"):
                kolg, chi2, ndf = helpers.compare(
                    overlays.GetHists()[0],
                    stacks.GetStack().Last(),
                )  # AH KILL ME
                yks = 0.975
                ychi2 = 0.975
                xks = 0.27
                xchi2 = 0.55

                ks = ROOT.TLatex(xks, yks, "KS = %5.3f" % (kolg))
                ch = ROOT.TLatex(xchi2, ychi2,
                                 "#chi^{2} / ndf = %.1f / %i" % (chi2, ndf))
                watermarks += [ks, ch]

        # draw watermarks
        for wm in watermarks:
            wm.SetTextAlign(22)
            wm.SetTextSize(0.04)
            wm.SetTextFont(42)
            wm.SetNDC()
            wm.Draw()

        canv.SaveAs(os.path.join(plotter["directory"],
                                 canv.GetName() + ".pdf"))

        output.cd()
        canv.Write()

    output.Close()