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.

decimate.py 2.9 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/python
  2. import nilmtools.filter
  3. import nilmdb.client
  4. import numpy as np
  5. import operator
  6. def main():
  7. f = nilmtools.filter.Filter()
  8. parser = f.setup_parser("Decimate a stream")
  9. group = parser.add_argument_group("Decimate options")
  10. group.add_argument('-f', '--factor', action='store', default=4, type=int,
  11. help='Decimation factor (default: %(default)s)')
  12. # Parse arguments
  13. try:
  14. args = f.parse_args()
  15. except nilmtools.filter.MissingDestination as e:
  16. # If no destination, suggest how to create it by figuring out
  17. # a recommended layout.
  18. src = e.src
  19. dest = e.dest
  20. print "Source is %s (%s)" % (src.path, src.layout)
  21. print "Destination %s doesn't exist" % (dest.path)
  22. if "decimate_source" in f.client.stream_get_metadata(src.path):
  23. rec = src.layout
  24. elif 'int32' in src.layout_type or 'float64' in src.layout_type:
  25. rec = 'float64_' + str(src.layout_count * 3)
  26. else:
  27. rec = 'float32_' + str(src.layout_count * 3)
  28. print "You could make it with a command like:"
  29. print " nilmtool create", dest.path, rec
  30. raise SystemExit(1)
  31. if not (args.factor >= 2):
  32. raise Exception("factor needs to be 2 or more")
  33. f.check_dest_metadata({ "decimate_source": args.srcpath,
  34. "decimate_factor": args.factor })
  35. # If source is decimated, we have to decimate a bit differently
  36. if "decimate_source" in f.client.stream_get_metadata(args.srcpath):
  37. n = f.src.layout_count // 3
  38. f.process_python(function = decimate_again, rows = args.factor,
  39. args = (n,))
  40. else:
  41. n = f.src.layout_count
  42. f.process_python(function = decimate_first, rows = args.factor,
  43. args = (n,))
  44. def decimate_first(data, n):
  45. """Decimate original data -- result has 3 times as many columns"""
  46. # For this simple calculation, converting to a Numpy array
  47. # and doing the math is slower than just doing it directly.
  48. rows = iter(data)
  49. r_sum = r_min = r_max = rows.next()
  50. for row in rows:
  51. r_sum = map(operator.add, r_sum, row)
  52. r_min = map(min, r_min, row)
  53. r_max = map(max, r_max, row)
  54. r_mean = [ x / len(data) for x in r_sum ]
  55. return [ [ r_mean[0] ] + r_mean[1:] + r_min[1:] + r_max[1:] ]
  56. def decimate_again(data, n):
  57. """Decimate already-decimated data -- result has the same number
  58. of columns"""
  59. rows = iter(data)
  60. r = rows.next()
  61. r_sum = r[0:(n+1)]
  62. r_min = r[(n+1):(2*n+1)]
  63. r_max = r[(2*n+1):(3*n+1)]
  64. for r in rows:
  65. r_sum = map(operator.add, r_sum, r[0:(n+1)])
  66. r_min = map(min, r_min, r[(n+1):(2*n+1)])
  67. r_max = map(max, r_max, r[(2*n+1):(3*n+1)])
  68. r_mean = [ x / len(data) for x in r_sum ]
  69. return [ r_mean + r_min + r_max ]
  70. if __name__ == "__main__":
  71. main()