You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

71 lines
2.4 KiB

  1. #!/usr/bin/python
  2. import nilmtools.filter
  3. import nilmdb.client
  4. import numpy as np
  5. def main():
  6. f = nilmtools.filter.Filter()
  7. parser = f.setup_parser("Decimate a stream")
  8. group = parser.add_argument_group("Decimate options")
  9. group.add_argument('-f', '--factor', action='store', default=4, type=int,
  10. help='Decimation factor (default: %(default)s)')
  11. # Parse arguments
  12. try:
  13. args = f.parse_args()
  14. except nilmtools.filter.MissingDestination as e:
  15. # If no destination, suggest how to create it by figuring out
  16. # a recommended layout.
  17. print "Source is %s (%s)" % (e.src, e.layout)
  18. print "Destination %s doesn't exist" % (e.dest)
  19. if "decimate_source" in f.client.stream_get_metadata(e.src):
  20. rec = e.layout
  21. elif 'int32' in e.layout_type or 'float64' in e.layout_type:
  22. rec = 'float64_' + str(e.layout_count * 3)
  23. else:
  24. rec = 'float32_' + str(e.layout_count * 3)
  25. print "You could make it with a command like:"
  26. print " nilmtool create", e.dest, rec
  27. raise SystemExit(1)
  28. f.check_dest_metadata({ "decimate_source": args.srcpath,
  29. "decimate_factor": args.factor })
  30. # If source is decimated, we have to decimate a bit differently
  31. if "decimate_source" in f.client.stream_get_metadata(args.srcpath):
  32. f.process(function = decimate_again, rows = args.factor)
  33. else:
  34. f.process(function = decimate_first, rows = args.factor)
  35. def decimate_first(data):
  36. """Decimate original data -- result has 3 times as many columns"""
  37. data = np.array(data)
  38. rows, cols = data.shape
  39. n = cols - 1
  40. out = np.zeros(1 + 3 * n)
  41. out[0] = np.mean(data[:, 0], 0)
  42. out[ 1 : n+1 ] = np.mean(data[:, 1 : n+1], 0)
  43. out[ n+1 : 2*n+1] = np.min( data[:, 1 : n+1], 0)
  44. out[2*n+1 : 3*n+1] = np.max( data[:, 1 : n+1], 0)
  45. return [out]
  46. def decimate_again(data):
  47. """Decimate already-decimated data -- result has the same number
  48. of columns"""
  49. data = np.array(data)
  50. rows, cols = data.shape
  51. n = (cols - 1) // 3
  52. out = np.zeros(1 + 3 * n)
  53. out[0] = np.mean(data[:, 0], 0)
  54. out[ 1 : n+1 ] = np.mean(data[:, 1 : n+1], 0)
  55. out[ n+1 : 2*n+1] = np.min( data[:, n+1 : 2*n+1], 0)
  56. out[2*n+1 : 3*n+1] = np.max( data[:, 2*n+1 : 3*n+1], 0)
  57. return [out]
  58. if __name__ == "__main__":
  59. main()