Compare commits
4 Commits
nilmtools-
...
nilmtools-
Author | SHA1 | Date | |
---|---|---|---|
9c5f07106d | |||
62e11a11c0 | |||
2bdcee2c36 | |||
6dce8c5296 |
4
Makefile
4
Makefile
@@ -11,10 +11,14 @@ endif
|
|||||||
test: test_trainola
|
test: test_trainola
|
||||||
|
|
||||||
test_trainola:
|
test_trainola:
|
||||||
|
-nilmtool -u http://bucket/nilmdb remove -s min -e max \
|
||||||
|
/sharon/prep-a-matches
|
||||||
|
nilmtools/trainola.py "$$(cat extras/trainola-test-param-2.js)"
|
||||||
-nilmtool -u http://bucket/nilmdb remove -s min -e max \
|
-nilmtool -u http://bucket/nilmdb remove -s min -e max \
|
||||||
/sharon/prep-a-matches
|
/sharon/prep-a-matches
|
||||||
nilmtools/trainola.py "$$(cat extras/trainola-test-param.js)"
|
nilmtools/trainola.py "$$(cat extras/trainola-test-param.js)"
|
||||||
|
|
||||||
|
|
||||||
test_cleanup:
|
test_cleanup:
|
||||||
nilmtools/cleanup.py -e extras/cleanup.cfg
|
nilmtools/cleanup.py -e extras/cleanup.cfg
|
||||||
nilmtools/cleanup.py extras/cleanup.cfg
|
nilmtools/cleanup.py extras/cleanup.cfg
|
||||||
|
@@ -5,7 +5,7 @@ by Jim Paris <jim@jtan.com>
|
|||||||
Prerequisites:
|
Prerequisites:
|
||||||
|
|
||||||
# Runtime and build environments
|
# Runtime and build environments
|
||||||
sudo apt-get install python2.7 python2.7-dev python-setuptools python-pip
|
sudo apt-get install python2.7 python2.7-dev python-setuptools
|
||||||
sudo apt-get install python-numpy python-scipy
|
sudo apt-get install python-numpy python-scipy
|
||||||
|
|
||||||
nilmdb (1.8.1+)
|
nilmdb (1.8.1+)
|
||||||
|
29
extras/trainola-test-param-2.js
Normal file
29
extras/trainola-test-param-2.js
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
{ "columns" : [ { "index" : 0, "name" : "P1" },
|
||||||
|
{ "index" : 1, "name" : "Q1" },
|
||||||
|
{ "index" : 2, "name" : "P3" } ],
|
||||||
|
"stream" : "/sharon/prep-a",
|
||||||
|
"url" : "http://bucket.mit.edu/nilmdb",
|
||||||
|
"dest_stream" : "/sharon/prep-a-matches",
|
||||||
|
"start" : 1365153062643133.5,
|
||||||
|
"end" : 1365168814443575.5,
|
||||||
|
"exemplars" : [ { "columns" : [ { "index" : 0,
|
||||||
|
"name" : "P1"
|
||||||
|
} ],
|
||||||
|
"dest_column" : 0,
|
||||||
|
"end" : 1365073657682000,
|
||||||
|
"name" : "Turn ON",
|
||||||
|
"start" : 1365073654321000,
|
||||||
|
"stream" : "/sharon/prep-a",
|
||||||
|
"url" : "http://bucket.mit.edu/nilmdb"
|
||||||
|
},
|
||||||
|
{ "columns" : [ { "index" : 2, "name" : "P3" },
|
||||||
|
{ "index" : 0, "name" : "P1" } ],
|
||||||
|
"dest_column" : 1,
|
||||||
|
"end" : 1365176528818000,
|
||||||
|
"name" : "Type 2 turn ON",
|
||||||
|
"start" : 1365176520030000,
|
||||||
|
"stream" : "/sharon/prep-a",
|
||||||
|
"url" : "http://bucket.mit.edu/nilmdb"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
@@ -6,6 +6,7 @@ import nilmtools.filter
|
|||||||
from nilmdb.utils.time import (timestamp_to_human,
|
from nilmdb.utils.time import (timestamp_to_human,
|
||||||
timestamp_to_seconds,
|
timestamp_to_seconds,
|
||||||
seconds_to_timestamp)
|
seconds_to_timestamp)
|
||||||
|
from nilmdb.utils import datetime_tz
|
||||||
from nilmdb.utils.interval import Interval
|
from nilmdb.utils.interval import Interval
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@@ -15,6 +16,7 @@ from numpy.core.umath_tests import inner1d
|
|||||||
import nilmrun
|
import nilmrun
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
import functools
|
import functools
|
||||||
import collections
|
import collections
|
||||||
|
|
||||||
@@ -26,12 +28,12 @@ def build_column_mapping(colinfo, streaminfo):
|
|||||||
pull out a dictionary mapping for the column names/numbers."""
|
pull out a dictionary mapping for the column names/numbers."""
|
||||||
columns = OrderedDict()
|
columns = OrderedDict()
|
||||||
for c in colinfo:
|
for c in colinfo:
|
||||||
if (c['name'] in columns.keys() or
|
col_num = c['index'] + 1 # skip timestamp
|
||||||
c['index'] in columns.values()):
|
if (c['name'] in columns.keys() or col_num in columns.values()):
|
||||||
raise DataError("duplicated columns")
|
raise DataError("duplicated columns")
|
||||||
if (c['index'] < 0 or c['index'] >= streaminfo.layout_count):
|
if (c['index'] < 0 or c['index'] >= streaminfo.layout_count):
|
||||||
raise DataError("bad column number")
|
raise DataError("bad column number")
|
||||||
columns[c['name']] = c['index']
|
columns[c['name']] = col_num
|
||||||
if not len(columns):
|
if not len(columns):
|
||||||
raise DataError("no columns")
|
raise DataError("no columns")
|
||||||
return columns
|
return columns
|
||||||
@@ -52,6 +54,9 @@ class Exemplar(object):
|
|||||||
# Get stream info
|
# Get stream info
|
||||||
self.client = nilmdb.client.numpyclient.NumpyClient(self.url)
|
self.client = nilmdb.client.numpyclient.NumpyClient(self.url)
|
||||||
self.info = nilmtools.filter.get_stream_info(self.client, self.stream)
|
self.info = nilmtools.filter.get_stream_info(self.client, self.stream)
|
||||||
|
if not self.info:
|
||||||
|
raise DataError(sprintf("exemplar stream '%s' does not exist " +
|
||||||
|
"on server '%s'", self.stream, self.url))
|
||||||
|
|
||||||
# Build up name => index mapping for the columns
|
# Build up name => index mapping for the columns
|
||||||
self.columns = build_column_mapping(exinfo['columns'], self.info)
|
self.columns = build_column_mapping(exinfo['columns'], self.info)
|
||||||
@@ -74,10 +79,17 @@ class Exemplar(object):
|
|||||||
maxrows = self.count)
|
maxrows = self.count)
|
||||||
self.data = list(datagen)[0]
|
self.data = list(datagen)[0]
|
||||||
|
|
||||||
# Discard timestamp
|
# Extract just the columns that were specified in self.columns,
|
||||||
self.data = self.data[:,1:]
|
# skipping the timestamp.
|
||||||
|
extract_columns = [ value for (key, value) in self.columns.items() ]
|
||||||
|
self.data = self.data[:,extract_columns]
|
||||||
|
|
||||||
# Subtract the mean from each column
|
# Fix the column indices in e.columns, since we removed/reordered
|
||||||
|
# columns in self.data
|
||||||
|
for n, k in enumerate(self.columns):
|
||||||
|
self.columns[k] = n
|
||||||
|
|
||||||
|
# Subtract the means from each column
|
||||||
self.data = self.data - self.data.mean(axis=0)
|
self.data = self.data - self.data.mean(axis=0)
|
||||||
|
|
||||||
# Get scale factors for each column by computing dot product
|
# Get scale factors for each column by computing dot product
|
||||||
@@ -117,6 +129,10 @@ def peak_detect(data, delta):
|
|||||||
lookformax = True
|
lookformax = True
|
||||||
return (mins, maxs)
|
return (mins, maxs)
|
||||||
|
|
||||||
|
def timestamp_to_short_human(timestamp):
|
||||||
|
dt = datetime_tz.datetime_tz.fromtimestamp(timestamp_to_seconds(timestamp))
|
||||||
|
return dt.strftime("%H:%M:%S")
|
||||||
|
|
||||||
def trainola_matcher(data, interval, args, insert_func, final_chunk):
|
def trainola_matcher(data, interval, args, insert_func, final_chunk):
|
||||||
"""Perform cross-correlation match"""
|
"""Perform cross-correlation match"""
|
||||||
( src_columns, dest_count, exemplars ) = args
|
( src_columns, dest_count, exemplars ) = args
|
||||||
@@ -138,7 +154,7 @@ def trainola_matcher(data, interval, args, insert_func, final_chunk):
|
|||||||
|
|
||||||
# Compute cross-correlation for each column
|
# Compute cross-correlation for each column
|
||||||
for col_name in e.columns:
|
for col_name in e.columns:
|
||||||
a = data[:, src_columns[col_name] + 1]
|
a = data[:, src_columns[col_name]]
|
||||||
b = e.data[:, e.columns[col_name]]
|
b = e.data[:, e.columns[col_name]]
|
||||||
corr = scipy.signal.fftconvolve(a, np.flipud(b), 'valid')[0:valid]
|
corr = scipy.signal.fftconvolve(a, np.flipud(b), 'valid')[0:valid]
|
||||||
|
|
||||||
@@ -183,7 +199,10 @@ def trainola_matcher(data, interval, args, insert_func, final_chunk):
|
|||||||
insert_func(out)
|
insert_func(out)
|
||||||
|
|
||||||
# Return how many rows we processed
|
# Return how many rows we processed
|
||||||
return max(valid, 0)
|
valid = max(valid, 0)
|
||||||
|
printf(" [%s] matched %d exemplars in %d rows\n",
|
||||||
|
timestamp_to_short_human(data[0][0]), np.sum(out[:,1:]), valid)
|
||||||
|
return valid
|
||||||
|
|
||||||
def trainola(conf):
|
def trainola(conf):
|
||||||
print "Trainola", nilmtools.__version__
|
print "Trainola", nilmtools.__version__
|
||||||
@@ -247,14 +266,20 @@ def trainola(conf):
|
|||||||
src.path, layout = src.layout, maxrows = rows)
|
src.path, layout = src.layout, maxrows = rows)
|
||||||
inserter = functools.partial(dest_client.stream_insert_numpy_context,
|
inserter = functools.partial(dest_client.stream_insert_numpy_context,
|
||||||
dest.path)
|
dest.path)
|
||||||
|
start = time.time()
|
||||||
|
processed_time = 0
|
||||||
|
printf("Processing intervals:\n")
|
||||||
for interval in intervals:
|
for interval in intervals:
|
||||||
printf("Processing interval:\n")
|
|
||||||
printf("%s\n", interval.human_string())
|
printf("%s\n", interval.human_string())
|
||||||
nilmtools.filter.process_numpy_interval(
|
nilmtools.filter.process_numpy_interval(
|
||||||
interval, extractor, inserter, rows * 3,
|
interval, extractor, inserter, rows * 3,
|
||||||
trainola_matcher, (src_columns, dest.layout_count, exemplars))
|
trainola_matcher, (src_columns, dest.layout_count, exemplars))
|
||||||
|
processed_time += (timestamp_to_seconds(interval.end) -
|
||||||
|
timestamp_to_seconds(interval.start))
|
||||||
|
elapsed = max(time.time() - start, 1e-3)
|
||||||
|
|
||||||
return "done"
|
printf("Done. Processed %.2f seconds per second.\n",
|
||||||
|
processed_time / elapsed)
|
||||||
|
|
||||||
def main(argv = None):
|
def main(argv = None):
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
|
Reference in New Issue
Block a user