From 68fcbd56f66d6b2ec09cdde27e1b933dbf065992 Mon Sep 17 00:00:00 2001 From: Ta-Shun Su Date: Sat, 10 Aug 2019 13:20:39 +0800 Subject: [PATCH] add graph --- python_test_code/uni/briefly_data_analysis.py | 57 ++++++++++++++++++- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/python_test_code/uni/briefly_data_analysis.py b/python_test_code/uni/briefly_data_analysis.py index 34b1cb607..aa9393721 100644 --- a/python_test_code/uni/briefly_data_analysis.py +++ b/python_test_code/uni/briefly_data_analysis.py @@ -36,7 +36,7 @@ method to coped with the corresponding problem: import os.path import sys from collections import Counter -from typing import List, Optional, Dict, Set, Any +from typing import List, Optional, Dict, Set, Any, Union class Options: @@ -44,11 +44,14 @@ class Options: self.skip = False self.round = 2 self.overflow_pow = 10 + self.graph: Union[bool, str] = False + self.graph_log_scale = False class Result: - def __init__(self, options: Options): + def __init__(self, data_file: str, options: Options): # data file information + self.data_file = data_file self.date_time: Optional[str] = None self.device_name: Optional[str] = None self.device_address: Optional[str] = None @@ -108,7 +111,7 @@ def calculate(txt_file: str, options: Options) -> Result: if not os.path.exists(txt_file): raise FileNotFoundError(txt_file) - result = Result(options) + result = Result(txt_file, options) parsing_state = 0 @@ -191,6 +194,7 @@ 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))))) @@ -207,6 +211,36 @@ def print_result(result: Result, options: Options): print('value_delta') print_table(result.value_delta_counter) + print() + + +def paint_result(result: Result, options: Options): + import matplotlib.pyplot as plt + + fig, (at, av) = plt.subplots(2, 1) + + time_delta_data = list(result.time_delta_counter) + expect_time_delta = 1000 / result.sample_rate + time_delta_bins = int((max(time_delta_data) - min(time_delta_data)) / expect_time_delta) + time_delta_weight = [result.time_delta_counter[k] for k in time_delta_data] + at.hist(time_delta_data, + bins=time_delta_bins, + weights=time_delta_weight, + log=options.graph_log_scale, + align='left') + + value_delta_data = list(result.value_delta_counter) + value_delta_bins = max(value_delta_data) - min(value_delta_data) + value_delta_weight = [result.value_delta_counter[k] for k in value_delta_data] + av.hist(value_delta_data, + bins=value_delta_bins, + weights=value_delta_weight, + log=options.graph_log_scale, + align='left') + + fig.tight_layout() + plt.show() + def print_help(): prg = sys.argv[0] @@ -218,6 +252,8 @@ def print_help(): print(' --skip : skip if FILE not found') print(' --round VALUE : floating number round') print(' --overflow VALUE : overflow number. 2^VALUE') + print(' --graph[=FILE] : use matplotlib to generate graph') + print(' --graph-log-scale :') print() print('ARGUMENTS:') print(' FILE : txt data file') @@ -270,6 +306,18 @@ def main(argv: List[str]): o.overflow_pow = int(arg[len('--overflow='):]) i += 1 + elif arg == '--graph': + o.graph = True + i += 1 + + elif arg.startswith('--graph='): + o.graph = arg[len('--graph='):] + i += 1 + + elif arg == '--graph-log-scale': + o.graph_log_scale = True + i += 1 + elif arg.startswith('-'): raise ValueError('unknown options : ' + arg) @@ -286,6 +334,9 @@ def main(argv: List[str]): else: print_result(result, o) + if o.graph: + paint_result(result, o) + if __name__ == '__main__': main(sys.argv[1:])