From 9997aca958821ab83c281d99793973fbb71cd2db Mon Sep 17 00:00:00 2001 From: Milo Date: Sat, 6 Apr 2019 17:40:51 +0200 Subject: [PATCH 1/4] added weights for milo strats --- server/activate.sh | 1 - server/nightr/strategies/miloStrats.py | 7 ++++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/server/activate.sh b/server/activate.sh index cda886f..81ffbe1 100755 --- a/server/activate.sh +++ b/server/activate.sh @@ -12,7 +12,6 @@ source venv/bin/activate echo Installing required Python packages pip install -Ur requirements.txt - function run() { python -m nightr } diff --git a/server/nightr/strategies/miloStrats.py b/server/nightr/strategies/miloStrats.py index be105e5..3b08314 100644 --- a/server/nightr/strategies/miloStrats.py +++ b/server/nightr/strategies/miloStrats.py @@ -14,8 +14,9 @@ def camImgStrat(context : Context) -> Prediction: """ img = cv2.imread(str(Path(__file__).parent.joinpath("night.jpg")), 0) average = img.mean(axis=0).mean(axis=0) - print(average) p = Prediction() + p.weight = 0.7 + if average < 100: p.probability = 1.0 p.reasons.append('Image was dark') @@ -33,6 +34,7 @@ def australiaStrat(context : Context) -> Prediction: t = datetime.now().astimezone(australia) hour = t.hour p = Prediction() + if hour > 22 or hour < 6: p.probability = 0.0 p.reasons.append('It\'s night-time in Australia') @@ -55,9 +57,8 @@ def tv2newsStrat(context : Context) -> Prediction: avg_delta += d avg_timestamp = avg_delta // len(delta_times) // 60 p = Prediction() - print('average time between articles on tv2:', avg_timestamp, 'minutes') + p.weight = 0.7 p.probability = 1.0 if avg_timestamp > 50 else 0.0 p.reasons.append('There were ' + ('few' if avg_timestamp > 50 else 'many') + ' recent articles on TV2 News') - print(p.reasons[0]) return p From d51a81de49a557861d19713d130e56a969aac271 Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Sat, 6 Apr 2019 17:45:03 +0200 Subject: [PATCH 2/4] Logging on server. --- server/nightr/app.py | 8 ++++---- server/nightr/strategies/iss.py | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/server/nightr/app.py b/server/nightr/app.py index cf488fe..63f9720 100644 --- a/server/nightr/app.py +++ b/server/nightr/app.py @@ -1,18 +1,17 @@ import inspect -import logging import statistics from dataclasses import asdict from datetime import timedelta from typing import List import requests_cache -from flask import Flask, jsonify +from flask import Flask, jsonify, logging from .strategies import miloStrats, iss, cars_in_traffic, tide_strat, upstairs_neighbour from .util import Context -logger = logging.getLogger(__name__) app = Flask(__name__) +logger = logging.create_logger(app) requests_cache.install_cache("requests_cache", expire_after=timedelta(minutes=10)) @@ -39,9 +38,10 @@ def probabilities(): try: prediction = strategy(context) except Exception as e: - logger.warning("Strategy %s failed: %s", name, e) + logger.warning("Strategy '%s' failed: %s:", name) logger.exception(e) continue + predictions.append({ "name": name, "description": inspect.getdoc(strategy), diff --git a/server/nightr/strategies/iss.py b/server/nightr/strategies/iss.py index 17ed941..e4ca5de 100644 --- a/server/nightr/strategies/iss.py +++ b/server/nightr/strategies/iss.py @@ -9,7 +9,6 @@ from timezonefinder import TimezoneFinder from ..util import Context, Prediction -logger = logging.getLogger(__name__) tf = TimezoneFinder(in_memory=True) From abe4d5186ffccda7b1c7567cd5d9debb0a15dfc9 Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Sat, 6 Apr 2019 17:46:52 +0200 Subject: [PATCH 3/4] Im noob. --- server/nightr/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/nightr/app.py b/server/nightr/app.py index 63f9720..00c41ef 100644 --- a/server/nightr/app.py +++ b/server/nightr/app.py @@ -38,7 +38,7 @@ def probabilities(): try: prediction = strategy(context) except Exception as e: - logger.warning("Strategy '%s' failed: %s:", name) + logger.warning("Strategy '%s' failed:", name) logger.exception(e) continue From 670395c6725bd6937e3460ee42bfabdb66b64423 Mon Sep 17 00:00:00 2001 From: Milo Date: Sat, 6 Apr 2019 17:47:33 +0200 Subject: [PATCH 4/4] better weights --- server/nightr/strategies/miloStrats.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server/nightr/strategies/miloStrats.py b/server/nightr/strategies/miloStrats.py index 3b08314..593c981 100644 --- a/server/nightr/strategies/miloStrats.py +++ b/server/nightr/strategies/miloStrats.py @@ -57,7 +57,10 @@ def tv2newsStrat(context : Context) -> Prediction: avg_delta += d avg_timestamp = avg_delta // len(delta_times) // 60 p = Prediction() - p.weight = 0.7 + if avg_timestamp < 0: + p.weight = 0.0 + else: + p.weight = 0.7 p.probability = 1.0 if avg_timestamp > 50 else 0.0 p.reasons.append('There were ' + ('few' if avg_timestamp > 50 else 'many') + ' recent articles on TV2 News') return p