|
|
@@ -0,0 +1,78 @@ |
|
|
|
#!/usr/bin/python |
|
|
|
|
|
|
|
import nilmtools.filter |
|
|
|
import nilmtools.decimate |
|
|
|
import nilmdb.client |
|
|
|
import argparse |
|
|
|
|
|
|
|
def get_stream_info(client, path): |
|
|
|
"""Return a nilmtools.filter.StreamInfo object about the given |
|
|
|
path, or None if it doesn't exist""" |
|
|
|
streams = client.stream_list(path, extended = True) |
|
|
|
if len(streams) != 1: |
|
|
|
return None |
|
|
|
return nilmtools.filter.StreamInfo(client.geturl(), streams[0]) |
|
|
|
|
|
|
|
def main(argv = None): |
|
|
|
parser = argparse.ArgumentParser( |
|
|
|
formatter_class = argparse.RawDescriptionHelpFormatter, |
|
|
|
version = "1.0", |
|
|
|
description = """\ |
|
|
|
Automatically create multiple decimations from a single source |
|
|
|
stream, continuing until the last decimated level contains fewer |
|
|
|
than 500 points total. |
|
|
|
""") |
|
|
|
parser.add_argument("-u", "--url", action="store", |
|
|
|
default="http://localhost/nilmdb/", |
|
|
|
help="NilmDB server URL (default: %(default)s)") |
|
|
|
parser.add_argument('-f', '--factor', action='store', default=4, type=int, |
|
|
|
help='Decimation factor (default: %(default)s)') |
|
|
|
parser.add_argument("path", action="store", |
|
|
|
help='Path of base stream') |
|
|
|
args = parser.parse_args(argv) |
|
|
|
|
|
|
|
# Pull out info about the base stream |
|
|
|
client = nilmdb.client.Client(args.url) |
|
|
|
|
|
|
|
info = get_stream_info(client, args.path) |
|
|
|
if not info: |
|
|
|
raise Exception("path " + args.path + " not found") |
|
|
|
|
|
|
|
meta = client.stream_get_metadata(args.path) |
|
|
|
if "decimate_source" in meta: |
|
|
|
print "Stream", args.path, "was decimated from", meta["decimate_source"] |
|
|
|
print "You need to pass the base stream instead" |
|
|
|
raise SystemExit(1) |
|
|
|
|
|
|
|
# Figure out the type we should use for decimated streams |
|
|
|
if 'int32' in info.layout_type or 'float64' in info.layout_type: |
|
|
|
decimated_type = 'float64_' + str(info.layout_count * 3) |
|
|
|
else: |
|
|
|
decimated_type = 'float32_' + str(info.layout_count * 3) |
|
|
|
|
|
|
|
# Now do the decimations until we have few enough points |
|
|
|
factor = 1 |
|
|
|
while True: |
|
|
|
print "Level", factor, "decimation has", info.rows, "rows" |
|
|
|
if info.rows <= 500: |
|
|
|
break |
|
|
|
factor *= args.factor |
|
|
|
new_path = "%s~decim-%d" % (args.path, factor) |
|
|
|
|
|
|
|
# Create the stream if needed |
|
|
|
new_info = get_stream_info(client, new_path) |
|
|
|
if not new_info: |
|
|
|
print "Creating stream", new_path |
|
|
|
client.stream_create(new_path, decimated_type) |
|
|
|
|
|
|
|
# Run the decimation as if it were run from the commandline |
|
|
|
nilmtools.decimate.main(["-u", args.url, |
|
|
|
"-f", str(args.factor), |
|
|
|
info.path, |
|
|
|
new_path]) |
|
|
|
|
|
|
|
# Update info using the newly decimated stream |
|
|
|
info = get_stream_info(client, new_path) |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
main() |