From e936d100b8f7cd5b38adc3f7697dd1b26b07257c Mon Sep 17 00:00:00 2001 From: lukas leufen <l.leufen@fz-juelich.de> Date: Thu, 14 Nov 2019 10:22:12 +0100 Subject: [PATCH] implemented time tracking class, /close #22 --- src/helpers.py | 39 +++++++++++++++++++++++++++++ test/test_helpers.py | 59 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/src/helpers.py b/src/helpers.py index 3c422f1b..9c7ab255 100644 --- a/src/helpers.py +++ b/src/helpers.py @@ -9,6 +9,7 @@ import math from typing import Union import numpy as np import os +import time def to_list(arg): @@ -84,3 +85,41 @@ class LearningRateDecay(keras.callbacks.History): self.lr['lr'].append(current_lr) logging.info(f"Set learning rate to {current_lr}") return K.get_value(self.model.optimizer.lr) + + +class TimeTracking(object): + """ + Track time to measure execution time. Time tracking automatically starts on initialisation and ends by calling stop + method. Duration can always be shown by printing the time tracking object or calling get_current_duration. + """ + + def __init__(self, start=True): + self.start = None + self.end = None + if start: + self._start() + + def _start(self): + self.start = time.time() + + def _end(self): + self.end = time.time() + + def _duration(self): + if self.end: + return self.end - self.start + else: + return time.time() - self.start + + def __repr__(self): + return f"{round(self._duration(), 2)}s" + + def run(self): + self._start() + + def stop(self): + self.end = time.time() + return self._duration() + + def duration(self): + return self._duration() diff --git a/test/test_helpers.py b/test/test_helpers.py index ffda6ac4..aa80ec38 100644 --- a/test/test_helpers.py +++ b/test/test_helpers.py @@ -1,5 +1,5 @@ import pytest -from src.helpers import to_list, check_path_and_create, l_p_loss, LearningRateDecay +from src.helpers import * import logging import os import keras @@ -78,3 +78,60 @@ class TestLearningRateDecay: model.compile(optimizer=keras.optimizers.Adam(), loss=l_p_loss(2)) model.fit(np.array([1, 0, 2, 0.5]), np.array([1, 1, 0, 0.5]), epochs=5, callbacks=[lr_decay]) assert lr_decay.lr['lr'] == [0.02, 0.02, 0.02*0.95, 0.02*0.95, 0.02*0.95*0.95] + + +class TestTimeTracking: + + def test_init(self): + t = TimeTracking() + assert t.start is not None + assert t.start < time.time() + assert t.end is None + t2 = TimeTracking(start=False) + assert t2.start is None + + def test__start(self): + t = TimeTracking(start=False) + t._start() + assert t.start < time.time() + + def test__end(self): + t = TimeTracking() + t._end() + assert t.end > t.start + + def test__duration(self): + t = TimeTracking() + d1 = t._duration() + assert d1 > 0 + d2 = t._duration() + assert d2 > d1 + t._end() + d3 = t._duration() + assert d3 > d2 + assert d3 == t._duration() + + def test_repr(self): + t = TimeTracking() + t._end() + duration = t._duration() + assert t.__repr__().rstrip() == f"{round(duration, 2)}s".rstrip() + + def test_run(self): + t = TimeTracking(start=False) + assert t.start is None + t.run() + assert t.start is not None + + def test_stop(self): + t = TimeTracking() + assert t.end is None + duration = t.stop() + assert duration == t._duration() + + def test_duration(self): + t = TimeTracking() + duration = t + assert duration is not None + duration = t.stop() + assert duration == t.duration() -- GitLab