From 9a4f85a5271e719ba179781222f106f4ca9e7ada Mon Sep 17 00:00:00 2001 From: xionghuichen Date: Fri, 25 Nov 2022 03:21:19 +0000 Subject: [PATCH 1/6] Add time tracker --- RLA/easy_log/time_used_recorder.py | 71 +++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/RLA/easy_log/time_used_recorder.py b/RLA/easy_log/time_used_recorder.py index 21771da..958cbb7 100644 --- a/RLA/easy_log/time_used_recorder.py +++ b/RLA/easy_log/time_used_recorder.py @@ -3,9 +3,78 @@ from RLA.easy_log import logger import time +class SingleTimeTracker: + def __init__(self,name:str='untitled') -> None: + self.name=name + self.t = 0.0 + self.call_time=0 + self.time_cost = 0.0 -rc_start_time = {} + def __enter__(self): + # trace time + self.call_time+=1 + self.t = time.perf_counter() + + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.time_cost += time.perf_counter() - self.t + + +class TimeTracker: + def __init__(self): + self.t0=time.time()#to calc total time + self.time_dict=dict() + + def add(self,name='untitled'): + """ + :param name: specify the SingleTimeTracker in the time_dict, recommend use + line num in the scripts, like 'xxx.py Line xxx', can be easily got in python scripts using + `os.path.basename(__file__)+' line '+str(sys._getframe().f_lineno)` + """ + if name not in self.time_dict.keys(): + self.time_dict.update({name:SingleTimeTracker(name)}) + return self.time_dict[name] + + def __call__(self, name:str): + return self.time_dict[name] + + def clear(self): + self.time_dict=dict() + def statistic_entry(self,name:str): + """ + calc total calls of the + """ + assert name in self.time_dict.keys() + + t1=time.time() + t_passed=t1-self.t0 + return { + 'total calls/'+name:self.time_dict[name].call_time, + 'total time cost/'+name:self.time_dict[name].time_cost, + 'average time cost/'+name:self.time_dict[name].time_cost/(1e-6+self.time_dict[name].call_time), + 'time cost percentage/'+name:self.time_dict[name].time_cost/(1e-6+t_passed) + } + + def get_info(self): + info={} + for k in self.time_dict.keys(): + for entry_k,entry_v in self.statistic_entry(k).items(): + info[entry_k]=entry_v + return info + + def log(self,exclude_lst=['csv']): + logger.info('---------time dashboard---------') + for k in self.time_dict.keys(): + for entry_k,entry_v in self.statistic_entry(k).items(): + logger.record_tabular('time_used/'+entry_k,entry_v,exclude=exclude_lst) + logger.info(f"[{entry_k}]: {entry_v}") + logger.info('') + logger.info('---------dashboard end---------') + + +rc_start_time = {} def time_record(name): """ From d388b10497d1ee596dc3ed989925deee02e1b713 Mon Sep 17 00:00:00 2001 From: Xiong-Hui Chen Date: Thu, 24 Nov 2022 23:36:19 +0800 Subject: [PATCH 2/6] Update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index d1fb8b1..7b73328 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name='RLA', - version="0.6.0-pre", + version="0.6.0", description=( 'RL assistant' ), From ffd4520c6c1f89094bec5b30bbd53221e815e129 Mon Sep 17 00:00:00 2001 From: Xiong-Hui Chen Date: Fri, 25 Nov 2022 14:14:44 +0800 Subject: [PATCH 3/6] rm dpi (set to default) --- RLA/easy_plot/plot_util.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/RLA/easy_plot/plot_util.py b/RLA/easy_plot/plot_util.py index b82c039..e6a30d5 100644 --- a/RLA/easy_plot/plot_util.py +++ b/RLA/easy_plot/plot_util.py @@ -315,6 +315,7 @@ def plot_results( show_number=True, skip_legend=False, split_by_metrics=False, + base_dpi=90, rescale_idx=None): ''' Plot multiple Results objects @@ -356,6 +357,7 @@ def plot_results( smooth_step: float - when resampling (i.e. when resample > 0 or average_group is True), use this EMA decay parameter (in units of the new grid step). See docstrings for decay_steps in symmetric_ema or one_sided_ema functions. + ''' score_results = {} if vary_len_plot: @@ -401,7 +403,7 @@ def plot_results( # figsize = list(figsize) # figsize[0] += 4 # figsize = tuple(figsize) - f, axarr = plt.subplots(nrows, ncols, sharex=False, squeeze=False, figsize=figsize, dpi=90 * ncols) + f, axarr = plt.subplots(nrows, ncols, sharex=False, squeeze=False, figsize=figsize) groups = [] for results in allresults: groups.extend(group_fn(results)[0]) From 1e1cb8ce3e9028f3fc0678ec88787806829dd637 Mon Sep 17 00:00:00 2001 From: Xiong-Hui Chen Date: Thu, 24 Nov 2022 19:01:51 +0800 Subject: [PATCH 4/6] feat(plot): add multiply-metric plotting mode. add demos of plotting function usages. --- RLA/easy_plot/plot_util.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/RLA/easy_plot/plot_util.py b/RLA/easy_plot/plot_util.py index e6a30d5..4ac899e 100644 --- a/RLA/easy_plot/plot_util.py +++ b/RLA/easy_plot/plot_util.py @@ -315,7 +315,6 @@ def plot_results( show_number=True, skip_legend=False, split_by_metrics=False, - base_dpi=90, rescale_idx=None): ''' Plot multiple Results objects @@ -403,7 +402,7 @@ def plot_results( # figsize = list(figsize) # figsize[0] += 4 # figsize = tuple(figsize) - f, axarr = plt.subplots(nrows, ncols, sharex=False, squeeze=False, figsize=figsize) + f, axarr = plt.subplots(nrows, ncols, sharex=False, squeeze=False, figsize=figsize, dpi=90 * ncols) groups = [] for results in allresults: groups.extend(group_fn(results)[0]) From 33133e860b9dbb05abde4353c2ca110d1626e033 Mon Sep 17 00:00:00 2001 From: Xiong-Hui Chen Date: Fri, 25 Nov 2022 14:14:44 +0800 Subject: [PATCH 5/6] rm dpi (set to default) --- RLA/easy_plot/plot_util.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/RLA/easy_plot/plot_util.py b/RLA/easy_plot/plot_util.py index 4ac899e..e6a30d5 100644 --- a/RLA/easy_plot/plot_util.py +++ b/RLA/easy_plot/plot_util.py @@ -315,6 +315,7 @@ def plot_results( show_number=True, skip_legend=False, split_by_metrics=False, + base_dpi=90, rescale_idx=None): ''' Plot multiple Results objects @@ -402,7 +403,7 @@ def plot_results( # figsize = list(figsize) # figsize[0] += 4 # figsize = tuple(figsize) - f, axarr = plt.subplots(nrows, ncols, sharex=False, squeeze=False, figsize=figsize, dpi=90 * ncols) + f, axarr = plt.subplots(nrows, ncols, sharex=False, squeeze=False, figsize=figsize) groups = [] for results in allresults: groups.extend(group_fn(results)[0]) From 46048a20a007b018653aadccdf8a533e32f333c9 Mon Sep 17 00:00:00 2001 From: Xiong-Hui Chen Date: Fri, 25 Nov 2022 14:19:45 +0800 Subject: [PATCH 6/6] rm useless params --- RLA/easy_plot/plot_util.py | 1 - 1 file changed, 1 deletion(-) diff --git a/RLA/easy_plot/plot_util.py b/RLA/easy_plot/plot_util.py index e6a30d5..fa9176c 100644 --- a/RLA/easy_plot/plot_util.py +++ b/RLA/easy_plot/plot_util.py @@ -315,7 +315,6 @@ def plot_results( show_number=True, skip_legend=False, split_by_metrics=False, - base_dpi=90, rescale_idx=None): ''' Plot multiple Results objects