From: Scott Gasch Date: Thu, 18 Nov 2021 06:14:37 +0000 (-0800) Subject: WaitableState thingy and some stuff in the ML world. X-Git-Url: https://wannabe.guru.org/gitweb/?a=commitdiff_plain;h=6f688ff9bacee93679f6af45a301b4308e19764c;p=python_utils.git WaitableState thingy and some stuff in the ML world. --- diff --git a/ml/model_trainer.py b/ml/model_trainer.py index f9e132e..f61b8e7 100644 --- a/ml/model_trainer.py +++ b/ml/model_trainer.py @@ -356,6 +356,13 @@ class TrainingBlueprint(ABC): import input_utils import string_utils + now: datetime.datetime = datetime_utils.now_pacific() + info = f"""Timestamp: {datetime_utils.datetime_to_string(now)} +Model params: {params} +Training examples: {num_examples} +Training set score: {training_score:.2f}% +Testing set score: {test_score:.2f}%""" + print(f'\n{info}\n') if ( (self.spec.persist_percentage_threshold is not None and test_score > self.spec.persist_percentage_threshold) @@ -376,12 +383,6 @@ class TrainingBlueprint(ABC): print(msg) logger.info(msg) model_info_filename = f"{self.spec.basename}_model_info.txt" - now: datetime.datetime = datetime_utils.now_pacific() - info = f"""Timestamp: {datetime_utils.datetime_to_string(now)} -Model params: {params} -Training examples: {num_examples} -Training set score: {training_score:.2f}% -Testing set score: {test_score:.2f}%""" with open(model_info_filename, "w") as f: f.write(info) msg = f"Wrote {model_info_filename}:" diff --git a/state_tracker.py b/state_tracker.py index 11ce4c3..4f77ff4 100644 --- a/state_tracker.py +++ b/state_tracker.py @@ -4,6 +4,7 @@ from abc import ABC, abstractmethod import datetime import logging import time +import threading from typing import Dict, Optional import pytz @@ -112,7 +113,7 @@ class AutomaticStateTracker(StateTracker): logger.debug('pace_maker noticed event; shutting down') return self.heartbeat() - logger.debug(f'page_maker is sleeping for {self.sleep_delay}s') + logger.debug(f'pace_maker is sleeping for {self.sleep_delay}s') time.sleep(self.sleep_delay) def __init__( @@ -144,3 +145,32 @@ class AutomaticStateTracker(StateTracker): self.should_terminate.set() self.updater_thread.join() logger.debug('Background thread terminated.') + + +class WaitableAutomaticStateTracker(AutomaticStateTracker): + + def __init__( + self, + update_ids_to_update_secs: Dict[str, float], + *, + override_sleep_delay: Optional[float] = None, + ) -> None: + self._something_changed = threading.Event() + super().__init__(update_ids_to_update_secs, + override_sleep_delay=override_sleep_delay) + + def something_changed(self): + self._something_changed.set() + + def did_something_change(self) -> bool: + return self._something_changed.is_set() + + def reset(self): + self._something_changed.clear() + + def wait(self, + *, + timeout=None): + return self._something_changed.wait( + timeout=timeout + )