Ejemplo n.º 1
0
def textFileToTable(input_file_name, table_file_name):
    """Convert a text file containing tabular values to a table.

    'input_file_name' -- The file name of a text file containing a table
    of floating-point values.  The first line is assumed to contain the
    column names.  All additional lines are assumed to contain values
    for each column.

    'table_file_name' -- The file name of the table to create."""

    lines = iter(file(input_file_name))

    # Read the first line in the file, and split it into column names.
    heading_row = lines.next()
    column_names = heading_row.split()

    # Construct the schema from these column names.
    schema = hep.table.Schema()
    for column_name in column_names:
        schema.addColumn(column_name, "float32")
    # Create the table.
    table = hep.table.create(table_file_name, schema)

    # Scan over remaining lines in the file.
    for line in lines:
        # Split the line into values and convert them to numbers.
        values = map(float, line.split())
        # Make sure the line contains the right number of values.
        if len(values) != len(column_names):
            raise RuntimeError, "format error"
        # Construct a dictionary mapping column names to values.
        row = dict(zip(column_names, values))
        # Add the row to the table.
        table.append(row)

    del row, table
Ejemplo n.º 2
0
#-----------------------------------------------------------------------
# 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

Ejemplo n.º 3
0
schema.addColumn("b", "complex128")
schema.addColumn("c", "int16")
schema.addColumn("d", "complex64")
schema.addColumn("e", "complex128")
table = hep.table.create("complex1.table", schema)

# Fill in some rows.
for i in xrange(0, 1000):
    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)
Ejemplo n.º 4
0
#-----------------------------------------------------------------------

# 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
Ejemplo n.º 5
0
#-----------------------------------------------------------------------

import hep.table
from   hep.test import compare

#-----------------------------------------------------------------------
# test
#-----------------------------------------------------------------------

schema = hep.table.Schema()
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)
Ejemplo n.º 6
0
#-----------------------------------------------------------------------

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)
Ejemplo n.º 7
0
#-----------------------------------------------------------------------
# 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)
Ejemplo n.º 8
0
import hep.root
import hep.hist
import hep.table 
from   random import random

# Construct a schema with three columns.
schema = hep.table.Schema(a="float32", b="float32", c="float32")

# Create a new Root file.
root_file = hep.fs.getdir("test.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.
for i in xrange(0, 100):
    table.append(a=random(), b=random(), c=random())
# Release these to close the Root file.
del table, root_file

# Reopen the Root file.
root_file = hep.fs.getdir("test.root", writable=True)
# Get the table.
table = root_file["tree"]
# Project a histogram of the sum of the three values in each row.
histogram = hep.hist.Histogram1D(30, (0.0, 3.0), expression="a + b + c")
hep.hist.project(table.rows, (histogram, ))
# Write the histogram to the Root file.
root_file["histogram"] = histogram

Ejemplo n.º 9
0
#-----------------------------------------------------------------------
# imports
#-----------------------------------------------------------------------

from   hep.cernlib.minuit import maximumLikelihoodFit
import hep.table
from   hep.test import compare
import random

#-----------------------------------------------------------------------
# test
#-----------------------------------------------------------------------

schema = hep.table.Schema()
schema.addColumn("x", "float32")
table = hep.table.create("minuit-umlfit2.table", schema)
for i in xrange(100):
    table.append(x=random.normalvariate(3, 1))

result = maximumLikelihoodFit(
    "gaussian(mu, sigma, x)", (("mu", 0), ("sigma", 10, 0.1, 0, 1000)),
    table)
print result

compare(result.minuit_status, 3)
compare(result.values["mu"], 3, precision=0.3)
compare(result.values["sigma"], 1, precision=0.3)
Ejemplo n.º 10
0
if "reopen1.root" in cwd:
    del cwd["reopen1.root"]

#-----------------------------------------------------------------------
# test
#-----------------------------------------------------------------------

# Construct a schema with three columns.
schema = hep.table.Schema(a="float32", b="float32")

# 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.
Ejemplo n.º 11
0
if "replace1.root" in cwd:
    del cwd["replace1.root"]

#-----------------------------------------------------------------------
# test
#-----------------------------------------------------------------------

# Construct a schema with three columns.
schema = hep.table.Schema(a="float32", b="float32", c="float32")

# Create a new Root file.
root_file = hep.fs.getdir("replace1.root", makedirs=True, writable=True)
# Create a tree in it.
table = hep.root.createTable("tree", root_file, schema, title="PyHEP test")
# Fill a row into the ntuple.
table.append(a=1, b=2, c=3)
table.append(a=4, b=5, c=6)
# Release these to close the Root file.
del table, root_file

# Recreate the Root file.
root_file = hep.fs.getdir("replace1.root", makedirs=True, writable=True)
# Get the table.
table = root_file["tree"]
# Fill a row into the ntuple.
table.append(a=7, b=8, c=9)
# Release these to close the Root file.
del table, root_file

# Reopen the Root file.
root_file = hep.fs.getdir("replace1.root")
Ejemplo n.º 12
0
# -----------------------------------------------------------------------
# imports
# -----------------------------------------------------------------------

import hep.table
from hep.test import compare

# -----------------------------------------------------------------------
# test
# -----------------------------------------------------------------------

schema = hep.table.Schema()
schema.addColumn("x", "int16")
schema.addColumn("y", "int16")
table = hep.table.create("object1.table", schema)
table.append(x=10, y=20)
del table

table = hep.table.open("object1.table")
table.row_type = hep.table.RowObject
row = table[0]
compare(row._table, table)
del table
compare(row.x, 10)
compare(row.y, 20)
compare(row._index, 0)

row_dict = row.__dict__
compare(row_dict["x"], 10)
compare(row_dict["y"], 20)
compare(row_dict["_index"], 0)
Ejemplo n.º 13
0
#-----------------------------------------------------------------------
# test
#-----------------------------------------------------------------------

# Create a new table.
schema = hep.table.Schema()
schema.addColumn("c", "complex128")
table = hep.table.create("complex1.table", schema)

# Fill in some rows.
values1 = []
values2 = []
for i in xrange(0, 1000):
    value = random() + random() * 1j
    table.append(c=value)
    if abs(value) > 1:
        values1.append(value)
    if value.real > 0.5 and value.imag < -0.5:
        values2.append(value)

# Write and close the table.
del schema, table

# Reopen the table.
table = hep.table.open("complex1.table")

# Check rows.
for row, value in zip(table.select("abs(c) > 1"), values1):
    compare(row["c"], value)
for row, value in zip(table.select("c.real > 0.5 and c.imag < -0.5"), values2):
Ejemplo n.º 14
0
    hists.append(hist)

schema = hep.table.Schema()
schema.addColumn("index", "int32")
schema.addColumn("value", "float32")

tables = {}
for i in xrange(0, num_tables):
    subdir = subdirs[randint(0, len(subdirs) - 1)]
    name = "TABLE%d" % i
    table = hep.root.createTable(name, file[subdir], schema)
    values = []
    for j in xrange(0, num_rows):
        value = random()
        values.append(value)
        table.append(index=j, value=value)
    tables[file.join(subdir, name)] = values

schema = hep.table.Schema()
for i in xrange(0, 50):
    schema.addColumn("COL%d" % i, "float64")
path = "BIGTABLE";
table = hep.root.createTable(path, file, schema)
table_rows = []
for j in xrange(0, num_rows):
    values = []
    row = {}
    for i in xrange(0, 50):
        value = random()
        row["COL%d" % i] = value
        values.append(value)