This commit is contained in:
Alexander Munch-Hansen 2019-04-06 17:49:45 +02:00
commit 89d7c07557
4 changed files with 11 additions and 9 deletions

View file

@ -12,7 +12,6 @@ source venv/bin/activate
echo Installing required Python packages echo Installing required Python packages
pip install -Ur requirements.txt pip install -Ur requirements.txt
function run() { function run() {
python -m nightr python -m nightr
} }

View file

@ -1,18 +1,17 @@
import inspect import inspect
import logging
import statistics import statistics
from dataclasses import asdict from dataclasses import asdict
from datetime import timedelta from datetime import timedelta
from typing import List from typing import List
import requests_cache 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 .strategies import miloStrats, iss, cars_in_traffic, tide_strat, upstairs_neighbour
from .util import Context from .util import Context
logger = logging.getLogger(__name__)
app = Flask(__name__) app = Flask(__name__)
logger = logging.create_logger(app)
requests_cache.install_cache("requests_cache", expire_after=timedelta(minutes=10)) requests_cache.install_cache("requests_cache", expire_after=timedelta(minutes=10))
@ -39,9 +38,10 @@ def probabilities():
try: try:
prediction = strategy(context) prediction = strategy(context)
except Exception as e: except Exception as e:
logger.warning("Strategy %s failed: %s", name, e) logger.warning("Strategy '%s' failed:", name)
logger.exception(e) logger.exception(e)
continue continue
predictions.append({ predictions.append({
"name": name, "name": name,
"description": inspect.getdoc(strategy), "description": inspect.getdoc(strategy),

View file

@ -9,7 +9,6 @@ from timezonefinder import TimezoneFinder
from ..util import Context, Prediction from ..util import Context, Prediction
logger = logging.getLogger(__name__)
tf = TimezoneFinder(in_memory=True) tf = TimezoneFinder(in_memory=True)

View file

@ -14,8 +14,9 @@ def camImgStrat(context : Context) -> Prediction:
""" """
img = cv2.imread(str(Path(__file__).parent.joinpath("night.jpg")), 0) img = cv2.imread(str(Path(__file__).parent.joinpath("night.jpg")), 0)
average = img.mean(axis=0).mean(axis=0) average = img.mean(axis=0).mean(axis=0)
print(average)
p = Prediction() p = Prediction()
p.weight = 0.7
if average < 100: if average < 100:
p.probability = 1.0 p.probability = 1.0
p.reasons.append('Image was dark') p.reasons.append('Image was dark')
@ -33,6 +34,7 @@ def australiaStrat(context : Context) -> Prediction:
t = datetime.now().astimezone(australia) t = datetime.now().astimezone(australia)
hour = t.hour hour = t.hour
p = Prediction() p = Prediction()
if hour > 22 or hour < 6: if hour > 22 or hour < 6:
p.probability = 0.0 p.probability = 0.0
p.reasons.append('It\'s night-time in Australia') p.reasons.append('It\'s night-time in Australia')
@ -55,9 +57,11 @@ def tv2newsStrat(context : Context) -> Prediction:
avg_delta += d avg_delta += d
avg_timestamp = avg_delta // len(delta_times) // 60 avg_timestamp = avg_delta // len(delta_times) // 60
p = Prediction() p = Prediction()
print('average time between articles on tv2:', avg_timestamp, 'minutes') 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.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') p.reasons.append('There were ' + ('few' if avg_timestamp > 50 else 'many') + ' recent articles on TV2 News')
print(p.reasons[0])
return p return p