This commit is contained in:
Ta-Shun Su
2019-08-10 16:43:45 +08:00
parent 5f21e2bae6
commit c0f9a6216c
+102 -20
View File
@@ -44,10 +44,20 @@ class Options:
self.skip = False
self.round = 2
self.overflow_pow: Optional[int] = None
# print options
self.quiet = False
# graph options
self.graph: Optional[str] = None
self.graph_log_scale = False
self.graph_save_path: Optional[str] = None
# fft graph options
self.fft_x_log = False
self.fft_y_norm = False
self.fft_channel = 0
class Result:
def __init__(self, data_file: str, options: Options):
@@ -210,24 +220,25 @@ def print_result(result: Result, options: Options):
for k in sorted(counter.keys()):
print(f % (k, counter[k]))
print(result.data_file)
print('data_count', result.data_count)
print('channel', ' '.join(list(map(str, sorted(result.channel)))))
if not options.quiet:
print(result.data_file)
print('data_count', result.data_count)
print('channel', ' '.join(list(map(str, sorted(result.channel)))))
try:
sample_rate = result.sample_rate
print('sample_rate [1/s]', sample_rate)
print('1/sample_rate [ms]', 1000 / sample_rate)
except RuntimeError:
print('sample_rate', '(E)')
try:
sample_rate = result.sample_rate
print('sample_rate [1/s]', sample_rate)
print('1/sample_rate [ms]', 1000 / sample_rate)
except RuntimeError:
print('sample_rate', '(E)')
print('time_delta')
print_table(result.time_delta_counter, '%10.2f')
print()
print('time_delta')
print_table(result.time_delta_counter, '%10.2f')
print()
print('value_delta')
print_table(result.value_delta_counter)
print()
print('value_delta')
print_table(result.value_delta_counter)
print()
# packet miss rate
@@ -253,6 +264,8 @@ def paint_fft_result(result: Result, options: Options):
time_seq = []
value_seq = []
time_offset = None
total_duration = 0.0
with open(result.data_file) as _file:
for line in _file:
@@ -263,12 +276,25 @@ def paint_fft_result(result: Result, options: Options):
else:
part = line.split(' ', maxsplit=3)
time_seq.append(float(part[0]))
value_seq.append(int(part[2]))
channel = int(part[1])
if channel == options.fft_channel:
time = float(part[0])
if time_offset is None:
time_seq.append(0.0)
time_offset = time
else:
total_duration = time - time_offset
time_seq.append(time - time_offset)
value_seq.append(int(part[2]))
if len(time_seq) == 0:
raise RuntimeError('empty channel data : ' + str(options.fft_channel))
sample_rate = result.sample_rate
time_delta = 1000 / sample_rate
total_duration = result.total_duration
total_time_step = int(total_duration / time_delta)
f = interp1d(time_seq, value_seq, copy=False)
@@ -279,9 +305,14 @@ def paint_fft_result(result: Result, options: Options):
fft_result = fft_result[:len(fft_result) // 2]
fft_x = np.linspace(0, 1000 * total_time_step / (2 * total_duration), len(fft_result))
fft_y = abs(fft_result)
fft_sum = sum(fft_y)
plt.plot(np.log10(fft_x), fft_y / fft_sum)
if options.fft_x_log:
fft_x = np.log10(fft_x)
if options.fft_y_norm:
fft_y /= sum(fft_y)
plt.plot(fft_x, fft_y)
if options.graph_save_path is not None:
plt.savefig(options.graph_save_path, dpi=600)
@@ -331,12 +362,23 @@ def print_help():
print(' --skip : skip if FILE not found')
print(' --round VALUE : floating number round')
print(' --overflow VALUE : overflow number. 2^VALUE')
print()
print('OPTIONS (print):')
print(' -q : do not print table')
print()
print('OPTIONS (graph):')
print(' --graph[=TYPE] : use matplotlib to generate graph. could be:')
print(' ramp : ramp data, calculate value miss')
print(' fft : sin wave data, calculate fft')
print(' --graph-log-scale :')
print(' --graph-save PATH : save graph file path')
print()
print('OPTIONS (graph=fft):')
print(' --fft-channel CH : filter channel')
print(' --fft-log : log x-axis')
print(' --fft-norm : log x-axis normalize')
print(' --fft [opt,...] : above')
print()
print('ARGUMENTS:')
print(' FILE : txt data file')
print()
@@ -388,6 +430,10 @@ def main(argv: List[str]):
o.overflow_pow = int(arg[len('--overflow='):])
i += 1
elif arg == '-q':
o.quiet = True
i += 1
elif arg == '--graph':
o.graph = argv[i + 1]
i += 2
@@ -408,6 +454,42 @@ def main(argv: List[str]):
o.graph = arg[len('--graph-save='):]
i += 1
elif arg == '--fft-channel':
o.fft_channel = int(argv[i + 1])
i += 2
elif arg.startswith('--fft-channel='):
o.fft_channel = int(arg[len('--fft-channel='):])
i += 1
elif arg == '--fft-log':
o.fft_x_log = True
i += 1
elif arg == '--fft-norm':
o.fft_y_norm = True
i += 1
elif arg.startswith('--fft'):
if arg == '--fft':
c = argv[i + 1]
i += 2
elif arg.startswith('--fft='):
c = arg[len('--fft='):]
i += 1
else:
raise ValueError('unknown options : ' + arg)
for a in c.split(','):
if a == 'log':
o.fft_x_log = True
elif a == 'norm':
o.fft_y_norm = True
elif a.startswith('channel='):
o.fft_channel = int(a[len('channel='):])
else:
raise ValueError('unknown options : --fft-' + a)
elif arg.startswith('-'):
raise ValueError('unknown options : ' + arg)