|
- #!/usr/bin/python
-
- import nilmtools.filter
- import nilmdb.client
- import numpy as np
-
- def main():
- f = nilmtools.filter.Filter()
- parser = f.setup_parser("Decimate a stream")
- group = parser.add_argument_group("Decimate options")
- group.add_argument('-f', '--factor', action='store', default=4, type=int,
- help='Decimation factor (default: %(default)s)')
- # Parse arguments
- try:
- args = f.parse_args()
- except nilmtools.filter.MissingDestination as e:
- # If no destination, suggest how to create it by figuring out
- # a recommended layout.
- print "Source is %s (%s)" % (e.src, e.layout)
- print "Destination %s doesn't exist" % (e.dest)
- if "decimate_source" in f.client.stream_get_metadata(e.src):
- rec = e.layout
- elif 'int32' in e.layout_type or 'float64' in e.layout_type:
- rec = 'float64_' + str(e.layout_count * 3)
- else:
- rec = 'float32_' + str(e.layout_count * 3)
- print "You could make it with a command like:"
- print " nilmtool create", e.dest, rec
- raise SystemExit(1)
-
- f.check_dest_metadata({ "decimate_source": args.srcpath,
- "decimate_factor": args.factor })
-
- # If source is decimated, we have to decimate a bit differently
- if "decimate_source" in f.client.stream_get_metadata(args.srcpath):
- f.process(function = decimate_again, rows = args.factor)
- else:
- f.process(function = decimate_first, rows = args.factor)
-
- def decimate_first(data):
- """Decimate original data -- result has 3 times as many columns"""
- data = np.array(data)
- rows, cols = data.shape
- n = cols - 1
- out = np.zeros(1 + 3 * n)
-
- out[0] = np.mean(data[:, 0], 0)
- out[ 1 : n+1 ] = np.mean(data[:, 1 : n+1], 0)
- out[ n+1 : 2*n+1] = np.min( data[:, 1 : n+1], 0)
- out[2*n+1 : 3*n+1] = np.max( data[:, 1 : n+1], 0)
-
- return [out]
-
- def decimate_again(data):
- """Decimate already-decimated data -- result has the same number
- of columns"""
- data = np.array(data)
- rows, cols = data.shape
- n = (cols - 1) // 3
- out = np.zeros(1 + 3 * n)
-
- out[0] = np.mean(data[:, 0], 0)
- out[ 1 : n+1 ] = np.mean(data[:, 1 : n+1], 0)
- out[ n+1 : 2*n+1] = np.min( data[:, n+1 : 2*n+1], 0)
- out[2*n+1 : 3*n+1] = np.max( data[:, 2*n+1 : 3*n+1], 0)
-
- return [out]
-
- if __name__ == "__main__":
- main()
|