Browse Source

indexing

git-svn-id: https://bucket.mit.edu/svn/nilm/nilmdb-new@10330 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/bxinterval-last
Jim Paris 12 years ago
parent
commit
6c9cf198d7
3 changed files with 45 additions and 27 deletions
  1. +19
    -19
      nilmdb/layout.py
  2. +20
    -8
      nilmdb/nilmdb.py
  3. +6
    -0
      tests/test_nilmdb.py

+ 19
- 19
nilmdb/layout.py View File

@@ -2,33 +2,33 @@ import tables

# Table description for typical prep output
PrepData = {
'timestamp': tables.Int64Col(),
'p1': tables.Float32Col(),
'q1': tables.Float32Col(),
'p3': tables.Float32Col(),
'q3': tables.Float32Col(),
'p5': tables.Float32Col(),
'q5': tables.Float32Col(),
'p7': tables.Float32Col(),
'q7': tables.Float32Col(),
'timestamp': tables.Int64Col(pos=1),
'p1': tables.Float32Col(pos=2),
'q1': tables.Float32Col(pos=3),
'p3': tables.Float32Col(pos=4),
'q3': tables.Float32Col(pos=5),
'p5': tables.Float32Col(pos=6),
'q5': tables.Float32Col(pos=7),
'p7': tables.Float32Col(pos=8),
'q7': tables.Float32Col(pos=9),
}

# Table description for raw data
RawData = {
'timestamp': tables.Int64Col(),
'va': tables.Int16Col(),
'vb': tables.Int16Col(),
'vc': tables.Int16Col(),
'ia': tables.Int16Col(),
'ib': tables.Int16Col(),
'ic': tables.Int16Col(),
'timestamp': tables.Int64Col(pos=1),
'va': tables.UInt16Col(pos=2),
'vb': tables.UInt16Col(pos=3),
'vc': tables.UInt16Col(pos=4),
'ia': tables.UInt16Col(pos=5),
'ib': tables.UInt16Col(pos=6),
'ic': tables.UInt16Col(pos=7),
}

# Table description for raw data plus 60 Hz notched current
RawNotchedData = dict(RawData)
RawNotchedData.update({
'notch_ia': tables.Int16Col(),
'notch_ib': tables.Int16Col(),
'notch_ic': tables.Int16Col(),
'notch_ia': tables.UInt16Col(pos=8),
'notch_ib': tables.UInt16Col(pos=9),
'notch_ic': tables.UInt16Col(pos=10),
})


+ 20
- 8
nilmdb/nilmdb.py View File

@@ -19,11 +19,13 @@ class NilmDB(object):
paths = [ x._v_pathname for x in iterator ]
return sorted(paths)

def stream_create(self, path, cls):
"""Create a table at the given path, with the contents matching the
given class description (e.g. nilmdb.PrepData)."""

[ group, table ] = path.rsplit("/", 1)
def stream_create(self, path, cls, index = None):
"""Create a table at the given path, with the contents
matching the given class description (e.g. nilmdb.PrepData).
Columns listed in 'index' are marked as indices. If index =
None, the 'timestamp' column is indexed if it exists. Pass
an empty list to prevent indexing"""
[ group, node ] = path.rsplit("/", 1)
if group == '':
raise ValueError("Invalid path")

@@ -36,6 +38,16 @@ class NilmDB(object):
self.h5file.createGroup(parent, child)
except tables.NodeError:
pass
self.h5file.createTable(group, table, cls)

table = self.h5file.createTable(group, node, cls)

# Create indices
try:
if index is None and "timestamp" in table.colnames:
index = [ "timestamp" ]
for ind in index:
table.cols._f_col(ind).createIndex()
except KeyError, e:
# Remove this table if we got an error
self.h5file.removeNode(group, node)
raise e

+ 6
- 0
tests/test_nilmdb.py View File

@@ -30,8 +30,12 @@ def test_stream():
db = nilmdb.NilmDB(testdb)
assert(db.stream_list() == [])

# Bad path
with assert_raises(ValueError):
db.stream_create("/foo", nilmdb.layout.PrepData)
# Bad index columns
with assert_raises(KeyError):
db.stream_create("/newton/prep", nilmdb.layout.PrepData, ["nonexistant"])
db.stream_create("/newton/prep", nilmdb.layout.PrepData)
db.stream_create("/newton/raw", nilmdb.layout.RawData)
db.stream_create("/newton/zzz/rawnotch", nilmdb.layout.RawNotchedData)
@@ -45,3 +49,5 @@ def test_stream():
assert(len(db.h5file.getNode("/newton/prep").cols) == 9)
assert(len(db.h5file.getNode("/newton/raw").cols) == 7)
assert(len(db.h5file.getNode("/newton/zzz/rawnotch").cols) == 10)
assert(db.h5file.getNode("/newton/prep").colindexed["timestamp"])
assert(not db.h5file.getNode("/newton/prep").colindexed["p1"])

Loading…
Cancel
Save