commit dfaeddefa240831cd7e24b44a6738a7bb454ca75 Author: Jim Paris Date: Mon Jan 23 00:40:48 2012 +0000 Mics git-svn-id: https://bucket.mit.edu/svn/nilm/nilmdb-new@10326 ddd99763-3ecb-0310-9145-efcb8ce7c51f diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..17a1ed8 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +all: + nosetests + +clean: + find . -name '*pyc' | xargs rm \ No newline at end of file diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..8cb7243 --- /dev/null +++ b/README.txt @@ -0,0 +1,4 @@ +To install, + + python setup.py install + diff --git a/nilmdb/__init__.py b/nilmdb/__init__.py new file mode 100644 index 0000000..4a701d3 --- /dev/null +++ b/nilmdb/__init__.py @@ -0,0 +1,3 @@ +# empty +from nilmdb import NilmDB +from layout import * diff --git a/nilmdb/layout.py b/nilmdb/layout.py new file mode 100644 index 0000000..a6eab87 --- /dev/null +++ b/nilmdb/layout.py @@ -0,0 +1,34 @@ +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(), + } + +# 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(), + } + +# 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(), + }) + diff --git a/nilmdb/nilmdb.py b/nilmdb/nilmdb.py new file mode 100644 index 0000000..0723fc6 --- /dev/null +++ b/nilmdb/nilmdb.py @@ -0,0 +1,33 @@ +"""NilmDB + +Object that represents a NILM database file""" + +import tables +import time + +class NilmDB(object): + def __init__(self, filename): + # Open or create the file + self.h5file = tables.openFile(filename, "a", "NILM Database") + + def close(self): + self.h5file.close() + + def stream_list(self): + """Return list of paths to all Tables in the database""" + iterator = self.h5file.walkNodes('/', 'Table') + 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) + try: + self.h5file.getNode(group) + except tables.NoSuchNodeError as e: + self.h5file.createGroup("/", group.lstrip('/')) + + self.h5file.createTable(group, table, cls) +# group = h5file.createGroup("/", "newton", "Newton school") +# table = h5file.createTable(group, "prep", PrepData, "Prep Data") diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..abfe423 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,9 @@ +[nosetests] +nocapture=1 +with-coverage=1 +cover-inclusive=1 +cover-package=nilmdb +cover-erase=1 +stop=1 +verbosity=2 + diff --git a/tests/test.db b/tests/test.db new file mode 100644 index 0000000..1373149 Binary files /dev/null and b/tests/test.db differ diff --git a/tests/test_nilmdb.py b/tests/test_nilmdb.py new file mode 100644 index 0000000..ee36402 --- /dev/null +++ b/tests/test_nilmdb.py @@ -0,0 +1,45 @@ +import nilmdb +from nilmdb import NilmDB + +from nose.tools import assert_raises +import itertools +import os +import sys +import atexit + +testdb = "tests/test.db" + +#@atexit.register +#def cleanup(): +# os.unlink(testdb) + +def test_NilmDB(): + try: + os.unlink(testdb) + except: + pass + + with assert_raises(IOError): + nilmdb.NilmDB("/nonexistant-db/foo") + + db = nilmdb.NilmDB(testdb) + db.close() + db = nilmdb.NilmDB(testdb) + +def test_stream(): + db = nilmdb.NilmDB(testdb) + assert(db.stream_list() == []) + + db.stream_create("/newton/prep", nilmdb.layout.PrepData) + db.stream_create("/newton/raw", nilmdb.layout.RawData) + db.stream_create("/newton/rawnotch", nilmdb.layout.RawNotchedData) + + # Verify we got 3 streams + assert(db.stream_list() == [ "/newton/prep", + "/newton/raw", + "/newton/rawnotch" ]) + + # Verify that columns were made right + assert(len(db.h5file.getNode("/newton/prep").cols) == 9) + assert(len(db.h5file.getNode("/newton/raw").cols) == 7) + assert(len(db.h5file.getNode("/newton/rawnotch").cols) == 10)