chickenhouse/powerlog.py

78 lines
1.9 KiB
Python
Executable File

#!/usr/bin/python
#import matplotlib
#matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import getopt
import sys
def get_time_power(file_name):
offset = 1000000
loop_offset = 0
last_time = 0
time_log = np.array([])
power_log = np.array([])
log = open(file_name, "r")
for line in log:
line = line.strip().split(" ")
time = int(line[0])
if time < last_time:
loop_offset += offset
last_time = time
time_log = np.append(time_log, time + loop_offset)
power_log = np.append(power_log, int(line[2]))
if len(time_log) != len(power_log):
return None
return time_log, power_log
def time_us_to_ms(time_us):
time_ms = np.array([])
for value in time_us:
time_ms = np.append(time_ms, value / 1000)
return time_ms
def get_basename(file_name):
basename = file_name.strip().split(".")
if len(basename) < 1:
return None
return basename[0]
def print_power_diagram(time_ms, power_mw, time_slope, power_slope, basename, image_format="svg"):
plt.clf()
plt.plot(time_ms, power_mw, "r-", time_slope, power_slope, "b-")
plt.show()
return None
def get_slope(x, y, interval):
y_sum = 0
x_start = x[0]
div = 0
x_slope = np.array([])
y_slope = np.array([])
for i in range(0, len(x)):
if x[i] > (x_start + interval):
if div == 0:
div = 1
x_slope = np.append(x_slope, x[i])
y_slope = np.append(y_slope, y_sum / div)
div = 0
y_sum = 0
x_start = x[i]
else:
y_sum += y[i]
div += 1
return x_slope, y_slope
def main(args):
opts, _ = getopt.getopt(args, "f:", ["file="])
for opt, arg in opts:
if opt in ("-f", "--file"):
time_us, power_mw = get_time_power(arg)
time_ms = time_us_to_ms(time_us)
time_slope, power_slope = get_slope(time_ms, power_mw, 1000)
print_power_diagram(time_ms, power_mw, time_slope, power_slope, get_basename(arg))
return None
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))