num_rows = 1000 #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- schema = hep.table.Schema() for i in xrange(0, num_cols): schema.addColumn("COL%d" % i, "float64") table = hep.table.create("stress1.table", schema) table_rows = [] for j in xrange(0, num_rows): row = {} values = [] for i in xrange(0, num_cols): value = random() row["COL%d" % i] = value values.append(value) table.append(row) table_rows.append(values) del row, values, table, schema table = hep.table.open("stress1.table"); compare(len(table.schema), num_cols) compare(len(table), num_rows) for row in table: values = table_rows[row["_index"]] for i in xrange(0, num_cols): compare(row["COL%d" % i], values[i], precision=1e-8)
from hep.test import compare import hep.expr #----------------------------------------------------------------------- # classes #----------------------------------------------------------------------- class TestExpression(hep.expr.Expression): type = int def evaluate(self, symbols): return symbols["x"] * 2 def copy(self, copy_fn=None): return TestExpression() #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- symbols = { "x": 21 } expression = hep.expr.compile(TestExpression()) compare(expression.evaluate(symbols), 42)
from __future__ import division import hep.hist from hep.test import compare #----------------------------------------------------------------------- # tests #----------------------------------------------------------------------- # Create the histogram. axis = hep.hist.UnevenlyBinnedAxis( [0, .1, .2, .3, .4, .45, .5, .55, .6, .8, 1]) histogram = hep.hist.Histogram(axis) for i in range(100): histogram << i / 100 # Test values. compare(histogram.getBinContent("underflow"), 0) compare(histogram.getBinContent(0), 10) compare(histogram.getBinContent(1), 10) compare(histogram.getBinContent(2), 10) compare(histogram.getBinContent(3), 10) compare(histogram.getBinContent(4), 5) compare(histogram.getBinContent(5), 5) compare(histogram.getBinContent(6), 5) compare(histogram.getBinContent(7), 5) compare(histogram.getBinContent(8), 20) compare(histogram.getBinContent(9), 20) compare(histogram.getBinContent("overflow"), 0)
from hep.test import compare from math import sqrt #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- # Create and fill a histogram. histogram = Histogram((10, (0.0, 10.0)), (10, (-1.0, 1.0)), bin_type=float) for i in xrange(0, 100): for j in xrange(-10, 10): histogram << (sqrt(i), 0.1 * j + 0.05) # Check that it's configured correctly. compare(histogram.dimensions, 2) compare(histogram.bin_type, float) x_axis, y_axis = histogram.axes compare(x_axis.number_of_bins, 10) compare(x_axis.range, (0.0, 10.0)) compare(x_axis.type, float) compare(y_axis.number_of_bins, 10) compare(y_axis.range, (-1.0, 1.0)) compare(y_axis.type, float) # Fill it. histogram.accumulate((-10, -10), 42.0) histogram.accumulate((-10, 0.1), 17.5) histogram.accumulate((0.5, 10), -4.0) histogram.accumulate((-10, 10), 3.33)
keys.sort() return keys #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- root_file = hep.root.create("remove2.root") root_file.mkdir("dir1") root_file.mkdir("dir2") root_file.mkdir("dir3") root_file.mkdir("dir3/dir4") del root_file compare(ls("remove2.root", ""), ["dir1", "dir2", "dir3"]) compare(ls("remove2.root", "dir3"), ["dir4"]) del hep.root.open("remove2.root", writable=True)["dir2"] compare(ls("remove2.root", ""), ["dir1", "dir3"]) compare(ls("remove2.root", "dir3"), ["dir4"]) del hep.root.open("remove2.root", writable=True)["dir3/dir4"] compare(ls("remove2.root", ""), ["dir1", "dir3"]) compare(ls("remove2.root", "dir3"), []) del hep.root.open("remove2.root", writable=True)["dir3"] compare(ls("remove2.root", ""), ["dir1"]) #----------------------------------------------------------------------- # PyHEP XFAIL # # OK, so here's the problem: A root.Directory object holds a reference # to the containing root.File object. A directory object also holds
from hep import test from hep.lorentz import lab from math import sqrt x4 = lab.Vector(5.0, 3.0, 2.0, 1.0) t, x, y, z = lab.coordinatesOf(x4) test.compare(t, 5.0) test.compare(x, 3.0) test.compare(y, 2.0) test.compare(z, 1.0) test.compare(x4.norm, sqrt(11.0)) y4 = 2 * lab.Vector(4.0, -2.0, -1.0, 0.0) t, x, y, z = lab.coordinatesOf(x4 + y4) test.compare(t, 13.0) test.compare(x, -1.0) test.compare(y, 0.0) test.compare(z, 1.0) test.compare(x4 ^ y4, 56.0)
import hep.expr from hep.test import compare #----------------------------------------------------------------------- # main script #----------------------------------------------------------------------- i1 = 42 i2 = -4 f1 = 0.5 f2 = 3.0 symbols = { "i1": i1, "i2": i2, "f1": f1, "f2": f2 } tests = [ ("i1", i1), ("(i1)", (i1)), ("(i1, )", (i1, )), ("(i1, i2)", (i1, i2)), ("(i1, i2, f1, f2)", (i1, i2, f1, f2)), ("(i1 + 2, f1 / f2)", (i1 + 2, f1 / f2)), ] for expression, expected in tests: compiled = hep.expr.asExpression( expression, types_from=symbols, compile=True) assert hep.expr.isCompiledExpression(compiled) value = compiled.evaluate(symbols) compare(value, expected) compare(type(value), type(expected))
#----------------------------------------------------------------------- # imports #----------------------------------------------------------------------- from hep.cernlib.minuit import minimize from hep.test import compare #----------------------------------------------------------------------- # tests #----------------------------------------------------------------------- fit_result = minimize("x**2 - x + 1", (("x", 0.0), )) compare(fit_result.values["x"], 0.5, precision=1e-6) fit_result = minimize("hypot(a - 1, b - 2)", (("a", 0.0), ("b", 0.0), )) compare(fit_result.values["a"], 1.0, precision=1e-6) compare(fit_result.values["b"], 2.0, precision=1e-6) fit_result = minimize("x", (("x", 0, 0.1, 3, 4), )) compare(fit_result.values["x"], 3.0, precision=1e-6) fit_result = minimize("-x", (("x", 0, 0.1, 3, 4), )) compare(fit_result.values["x"], 4.0, precision=1e-6)
#----------------------------------------------------------------------- # imports #----------------------------------------------------------------------- from hep import test import hep.expr import hep.table #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- # Open the table. table = hep.table.open("table3.table") selection = hep.expr.parse("index % 4 == 3") for row in table.select(selection): test.compare(row["_index"] % 4, 3)
# Create a new Root file. root_file = hep.fs.getdir("reopen1.root", makedirs=True, writable=True) # Create a tree in it. table = hep.root.createTable("tree", root_file, schema, title="PyHEP test") # Fill 100 random rows into the ntuple. table.append(a=1, b=2) # Release these to close the Root file. del table, root_file # Reopen the Root file. root_file = hep.fs.getdir("reopen1.root", writable=True) # Get the table. table = root_file["tree"] # Fill a row into the ntuple. table.append(a=4, b=5) # Release these to close the Root file. del table, root_file # Reopen the Root file. root_file = hep.fs.getdir("reopen1.root") # Get the table. table = root_file["tree"] # Make sure both rows are there. compare(len(table), 2) compare(table[0]["a"], 1) compare(table[1]["b"], 5) # Release these to close the Root file. del table, root_file
# imports #----------------------------------------------------------------------- import hep.table from hep.test import compare #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- # Create a schema. schema = hep.table.Schema(y="int16") schema.addColumn("x", "float32") schema["y"].description = "'y' must be an integer" # Save it. hep.table.saveSchema(schema, "schema2.xml") del schema # Reload it. schema = hep.table.loadSchema("schema2.xml") # Check it. keys = schema.keys() keys.sort() compare(keys, ["x", "y"]) compare(schema["x"].type, "float32") compare(schema["y"].type, "int16") compare(schema["y"].description, "'y' must be an integer")
from hep.cernlib.minuit import minimize from hep.test import compare from math import hypot # ----------------------------------------------------------------------- # tests # ----------------------------------------------------------------------- def function(x, y, z): return (x - y + 1) ** 2 + (x - z + 2) ** 2 + (y + z) ** 2 fit_result = minimize(function, ("x", "y", "z")) compare(fit_result.values["x"], -3 / 2, precision=1e-6) compare(fit_result.values["y"], -1 / 2, precision=1e-6) compare(fit_result.values["z"], 1 / 2, precision=1e-6) fit_result = minimize(function, (("x", 4, None), "y", "z")) compare(fit_result.values["x"], 4, precision=1e-6) compare(fit_result.values["y"], 4 / 3, precision=1e-6) compare(fit_result.values["z"], 7 / 3, precision=1e-6) fit_result = minimize(function, ("x", ("y", -1, None), "z")) compare(fit_result.values["x"], -5 / 3, precision=1e-6) compare(fit_result.values["y"], -1, precision=1e-6) compare(fit_result.values["z"], 2 / 3, precision=1e-6) fit_result = minimize(function, ("x", ("y", 5, None), ("z", 6, None))) compare(fit_result.values["x"], 4, precision=1e-6)
(int, "track multiplicity"), (float, "missing energy", "GeV"), title="trigger distribution") scatter.accumulate((5, 1.4)) scatter.accumulate((4, 1.1)) scatter << (6, 0.85) scatter << (3, 0.66) # Save and delete the scatter. cPickle.dump(scatter, file("scatter2.pickle", "w")) del scatter # Reload the scatter. scatter = cPickle.load(file("scatter2.pickle")) # Check its contents. compare(scatter.axes[0].type, int) compare(scatter.axes[0].name, "track multiplicity") compare(scatter.axes[1].type, float) compare(scatter.axes[1].name, "missing energy") compare(scatter.axes[1].units, "GeV") compare(len(scatter.points), 4) points = list(scatter.points) points.sort(lambda p0, p1: cmp(p0[0], p1[0]),) compare(tuple(points[0]), (3, 0.66)) compare(tuple(points[1]), (4, 1.10)) compare(tuple(points[2]), (5, 1.40)) compare(tuple(points[3]), (6, 0.85))
from hep import test import hep.lorentz from hep.lorentz import lab from math import sqrt p4 = lab.Momentum(5.0, 3.0, 2.0, 1.0) test.compare(p4.mass, sqrt(11.0)) q4 = lab.Momentum(5.0, -3.0, -2.0, -1.0) test.compare(q4.mass, sqrt(11.0)) test.compare(type(p4 + q4), hep.lorentz.Momentum)
# Create a row-wise ntuple in an HBOOK file. schema = hep.table.Schema() schema.addColumn("index", "int32") schema.addColumn("value32", "float32") schema.addColumn("value64", "float64") hbook_file = create("cwn1.hbook") table = createTable("cwn", hbook_file, schema, column_wise=1) # Fill it with random values, saving these in an array. values = [] for i in xrange(0, 100): value = random() values.append(value) table.append(index=i, value32=value, value64=value) del table, hbook_file # Open the ntuple. table = open("cwn1.hbook")["cwn"] assert_(table.column_wise) compare(len(table), 100) # Compare the values in it to the array. i = 0 for row in table: compare(row["index"], i) compare(row["value32"], values[i], precision=1e-6) compare(row["value64"], values[i], precision=1e-9) i += 1 del row, table
#----------------------------------------------------------------------- # imports #----------------------------------------------------------------------- from hep import test import hep.table #----------------------------------------------------------------------- # classes #----------------------------------------------------------------------- class Row(hep.table.RowObject): computed = property(lambda self: self.twice + self.index) #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- # Open the table, and get the first row. table = hep.table.open("table3.table", row_type=Row) # Check it. for row in table: test.compare(row.computed, 3 * row.index) test.compare(row.computed, 3 * row._index)
row = { "a": i * 52.467, "b": 1.8577928723e-12 * i - (i + 4.3) * 1j, "c": i, "d": 1500000 + 6j * i, "e": i, } table.append(row) # Write and close the table. del schema, row, table # Reopen the table. table = hep.table.open("complex1.table") # Check that the columns were restored correctly. compare(table.schema["a"].type, "float32") compare(table.schema["a"].Python_type, float) compare(table.schema["a"].size, 4) compare(table.schema["b"].type, "complex128") compare(table.schema["b"].Python_type, complex) compare(table.schema["b"].size, 16) compare(table.schema["c"].type, "int16") compare(table.schema["c"].Python_type, int) compare(table.schema["c"].size, 2) compare(table.schema["d"].type, "complex64") compare(table.schema["d"].Python_type, complex) compare(table.schema["d"].size, 8) compare(table.schema["e"].type, "complex128") compare(table.schema["e"].Python_type, complex) compare(table.schema["e"].size, 16)
#----------------------------------------------------------------------- # imports #----------------------------------------------------------------------- import hep.table from hep.test import compare #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- schema = hep.table.Schema() table = hep.table.create("index1.table", schema) for i in range(10): table.append() del table table = hep.table.open("index1.table") index_expr = table.compile("_index") for i in range(9, -1, -1): row = table[i] compare(row["_index"], i) compare(index_expr.evaluate(row), i) for row in table.select("_index % 3 == 1"): compare(row["_index"] % 3, 1)
import hep.table from hep.cernlib.hbook import create, createTable from hep.test import compare #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- # HBOOK requires correct data alignment of columns. If this is not # handled correctly when the schema is used to book the CWN, a schema # such as this one will cause problems, since the float64 columns are # not 64-bit-aligned. schema = hep.table.Schema() schema.addColumn("index1", "int32") schema.addColumn("value1", "float64") schema.addColumn("index2", "int32") schema.addColumn("value2", "float64") schema.addColumn("index3", "int32") schema.addColumn("value3", "float64") schema.addColumn("index4", "int32") schema.addColumn("value4", "float64") schema.addColumn("index5", "int32") schema.addColumn("value5", "float64") schema.addColumn("index6", "int32") schema.addColumn("value6", "float64") hbook_file = create("cwn2.hbook") table = createTable("cwn", hbook_file, schema, column_wise=1) compare(len(table.schema), 12)
from __future__ import division import hep.table from hep.test import compare, assert_ #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- schema = hep.table.Schema() schema.addColumn("x", "float32") schema.addColumn("y", "float32") table = hep.table.create("exception1.table", schema) table.append(x=5, y=2) table.append(x=3, y=4) table.append(x=0, y=4) table.append(x=3, y=0) table.append(x=4, y=3) sum = 0 def callback(value, weight): global sum assert_(weight == 1) sum += value hep.table.project(table, [("x / y", callback)], handle_expr_exceptions=True) compare(sum, 5 / 2 + 3 / 4 + 0 + 4 / 3)
#----------------------------------------------------------------------- # imports #----------------------------------------------------------------------- from hep.hist import Histogram import hep.ext from hep.test import compare #----------------------------------------------------------------------- # tests #----------------------------------------------------------------------- histogram = Histogram((1, (0, 1)), (1, (0.0, 1.0)), bin_type=int) compare(histogram.axes[0].type, int) compare(histogram.axes[1].type, float) compare(histogram.bin_type, int) # Make sure the extension type is used. compare(type(histogram), hep.ext.Histogram2D) histogram = Histogram((1, (0.0, 1.0)), (1, (0, 1)), bin_type=float) compare(histogram.axes[0].type, float) compare(histogram.axes[1].type, int) compare(histogram.bin_type, float) # Make sure the extension type is used. compare(type(histogram), hep.ext.Histogram2D) histogram = Histogram((1, (0, 1)), (1, (0, 1.)), bin_type=float) compare(histogram.axes[0].type, int) compare(histogram.axes[1].type, float) compare(histogram.bin_type, float) # Make sure the extension type is used.
#----------------------------------------------------------------------- # includes #----------------------------------------------------------------------- from hep.hist import Histogram1D from hep.test import compare from math import sqrt #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- histogram = Histogram1D(10, (0.0, 1.0), "time", "sec", bin_type=float, error_model="symmetric") compare(histogram.axes[0], histogram.axis) compare(histogram.axis.type, float) compare(histogram.axis.range, (0.0, 1.0)) compare(histogram.axis.name, "time") compare(histogram.axis.units, "sec") compare(histogram.bin_type, float) compare(histogram.axis.number_of_bins, 10) histogram.accumulate(0.35) histogram << 0.35 histogram << 0.35 histogram.accumulate(0.55, 4.5) histogram.accumulate(0.55, -1) histogram.setBinContent(9, -4.0) histogram.setBinError(9, (1.0, 1.0)) histogram << -0.5 histogram.accumulate(100, 10)
#--------------------------------------------------*- coding: Latin1 -*- # imports #----------------------------------------------------------------------- from hep.num import Statistic from hep.test import compare from math import sqrt #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- s = Statistic(9) compare(float(s), 9) compare(s.uncertainty, 3) s = Statistic(10.5, 2.3) compare(float(s), 10.5) compare(s.uncertainty, 2.3) s = Statistic("10") compare(float(s), 10) compare(s.uncertainty, sqrt(10)) s = Statistic("10", 3) compare(float(s), 10) compare(s.uncertainty, 3) s = Statistic("10 +- 2") compare(float(s), 10) compare(s.uncertainty, 2)
#----------------------------------------------------------------------- # imports #----------------------------------------------------------------------- from hep.cernlib.minuit import maximumLikelihoodFit from hep.test import compare import random #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- result = maximumLikelihoodFit( "gaussian(mu, sigma, x)", (("mu", 0), ("sigma", 10, 0.1, 0, 100)), [ {"x": random.normalvariate(3, 1)} for i in xrange(100) ]) compare(result.minuit_status, 3) compare(result.values["mu"], 3, precision=0.3) compare(result.values["sigma"], 1, precision=0.3)
#----------------------------------------------------------------------- # imports #----------------------------------------------------------------------- from hep.hist import Histogram from hep.test import compare #----------------------------------------------------------------------- # tests #----------------------------------------------------------------------- histogram = Histogram((1, (0.0, 0.5)), (1, (0, 1)), bin_type=int) compare(histogram.getBinContent(("underflow", "underflow")), 0) compare(histogram.getBinContent((0 , "underflow")), 0) compare(histogram.getBinContent(("overflow" , "underflow")), 0) compare(histogram.getBinContent(("underflow", 0 )), 0) compare(histogram.getBinContent((0 , 0 )), 0) compare(histogram.getBinContent(("overflow" , 0 )), 0) compare(histogram.getBinContent(("underflow", "overflow" )), 0) compare(histogram.getBinContent((0 , "overflow" )), 0) compare(histogram.getBinContent(("overflow" , "overflow" )), 0) histogram.accumulate((-1, -1), 4) histogram.accumulate(( 0, -1), 8) histogram.accumulate(( 1, -1), 16) histogram.accumulate((-1, 0), 32) histogram.accumulate(( 0, 0), 64) histogram.accumulate(( 1, 0), 128) histogram.accumulate((-1, 1), 256) histogram.accumulate(( 0, 1), 512)
schema.addColumn("count", "int16", title="number of grapefruits") schema.addColumn("length", "float32", units="meters") schema.name = "sample schema" table = hep.table.create("metadata1.table", schema) table.append(count=10, length=0.65) del table, schema #----------------------------------------------------------------------- table = hep.table.open("metadata1.table") schema = table.schema print schema compare(len(schema), 2) compare(schema["count"].title, "number of grapefruits") compare(schema["length"].units, "meters") compare(schema.name, "sample schema") compare(len(table), 1) del table, schema #----------------------------------------------------------------------- # Overwrite the table with another by the same name but a different # schema, to make sure the old metadata file gets clobbered and doesn't # conflict with the new table. schema = hep.table.Schema() schema.addColumn("x", "int32", title="stuff")
#----------------------------------------------------------------------- # imports #----------------------------------------------------------------------- from hep.hist import Histogram, Histogram1D import hep.ext from hep.test import compare #----------------------------------------------------------------------- # tests #----------------------------------------------------------------------- histogram = Histogram1D(1, (0, 1), bin_type=int) compare(histogram.axis.type, int) compare(histogram.bin_type, int) # Make sure the extension type is used. compare(type(histogram), hep.ext.Histogram1D) histogram = Histogram1D(1, (0.0, 1), bin_type=float) compare(histogram.axis.type, float) compare(histogram.bin_type, float) # Make sure the extension type is used. compare(type(histogram), hep.ext.Histogram1D) histogram = Histogram((1, (0, 1)), bin_type=float) compare(histogram.axis.type, int) compare(histogram.bin_type, float) # Make sure the extension type is used. compare(type(histogram), hep.ext.Histogram1D) histogram = Histogram((1, (0, 1.0)))
from hep.cernlib.minuit import maximumLikelihoodFit from hep.test import compare from math import exp from random import random #----------------------------------------------------------------------- # test #----------------------------------------------------------------------- # The PDF for the test distribution. def pdf(a, b, x, y): return a * b * exp(a * x + b * y) / ((exp(a) - 1) * (exp(b) - 1)) # Use acceptance-rejection to construct a sample from this PDF. samples = [] pdf_max = pdf(3, 1, 1, 1) while len(samples) < 1000: x = random() y = random() if (pdf_max * random() < pdf(3, 1, x, y)): samples.append({"x": x, "y": y}) result = maximumLikelihoodFit(pdf, (("a", 3), ("b", 1)), samples) print result compare(result.minuit_status, 3) compare(result.values["a"], 3, precision=0.5) compare(result.values["b"], 1, precision=0.5)
#----------------------------------------------------------------------- # imports #----------------------------------------------------------------------- import hep.table from hep.test import compare from random import randint #----------------------------------------------------------------------- # tests #----------------------------------------------------------------------- schema = hep.table.Schema() schema.addColumn("x", "int32") schema.addColumn("y", "int32") table = hep.table.create("rowcache1.table", schema) for i in range(100): table.append(x=i, y=i*i-1) del schema, table table = hep.table.open("rowcache1.table") index = 0 for i in range(0, 10000): index = min(max(index + randint(0, 2) - 1, 0), 99) row = table[index] compare(row["x"], index) compare(row["y"], index**2 - 1) del row
# ----------------------------------------------------------------------- # imports # ----------------------------------------------------------------------- import hep.hist from hep.test import compare from math import sqrt # ----------------------------------------------------------------------- # test # ----------------------------------------------------------------------- histogram = hep.hist.Histogram1D(10, (0.0, 1.0), bin_type=int, error_model="poisson") histogram.setBinContent(4, 4) compare(histogram.getBinError(0), (0.0, 1.1478745)) compare(histogram.getBinError(4), (2.2968057, 1.9818581))