Browse Source

Add nilm-decimate-auto script

tags/nilmtools-1.0
Jim Paris 9 years ago
parent
commit
b637f17887
3 changed files with 82 additions and 4 deletions
  1. +1
    -0
      setup.py
  2. +78
    -0
      src/decimate_auto.py
  3. +3
    -4
      src/filter.py

+ 1
- 0
setup.py View File

@@ -72,6 +72,7 @@ setup(name='nilmtools',
entry_points = {
'console_scripts': [
'nilm-decimate = nilmtools.decimate:main',
'nilm-decimate-auto = nilmtools.decimate_auto:main',
'nilm-insert = nilmtools.insert:main',
'nilm-copy = nilmtools.copy_:main',
'nilm-sinefit = nilmtools.sinefit:main',


+ 78
- 0
src/decimate_auto.py View File

@@ -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()

+ 3
- 4
src/filter.py View File

@@ -218,10 +218,9 @@ class Filter(object):
m += " %s = %s\n" % (key, val)
m += "doesn't match desired data:\n"
m += " %s = %s\n" % (key, wanted)
m += "Refusing to change it. You can change the stream's "
m += "metadata manually, or\n"
m += "remove existing data from the stream, to prevent "
m += "this error.\n"
m += "Refusing to change it. To prevent this error, "
m += "change or delete the metadata with nilmtool,\n"
m += "or remove existing data from the stream."
raise Exception(m)
# All good -- write the metadata in case it's not already there
self._client_dest.stream_update_metadata(self.dest.path, data)


Loading…
Cancel
Save