Reasons and battery.

This commit is contained in:
Casper V. Kristensen 2019-04-06 23:00:08 +02:00
parent 64108f67f9
commit 319c03067f
Signed by: caspervk
GPG key ID: 289CA03790535054
9 changed files with 41 additions and 21 deletions

View file

@ -10,7 +10,7 @@ from typing import List
import requests_cache
from flask import Flask, jsonify, logging, request
from .strategies import miloStrats, iss, cars_in_traffic, tide_strat, upstairs_neighbour, bing
from .strategies import miloStrats, iss, cars_in_traffic, tide_strat, upstairs_neighbour, bing, battery
from .util import Context
app = Flask(__name__)
@ -30,6 +30,7 @@ strategies = {
"tide": tide_strat.is_tide,
"upstairs_neighbour": upstairs_neighbour.check_games,
"bing": bing.clock,
"battery_level": battery.battery_level,
}

View file

@ -0,0 +1,21 @@
from ..util import Context, Prediction
def battery_level(context: Context) -> Prediction:
"""
If the battery is low, it's probably bedtime soon.
"""
p = Prediction()
if context.battery > 60:
p.reasons.append("Battery level's good, so it's probably still early in the day.")
elif context.battery > 30:
p.reasons.append("Battery level's getting low, so it's probably around dinnertime.")
elif context.battery > 10:
p.reasons.append("Your phone is dying, so it's bedtime soon?")
else:
p.reasons.append("Your phone's practically dead, so it's probably around four in the morning.")
p.probability = 1 - (context.battery / 100) # night is inverse proportional to battery level
return p

View file

@ -11,7 +11,7 @@ def clock(context: Context) -> Prediction:
It's nighttime if Bing says it's daytime.
"""
p = Prediction()
p.weight = 0.05
p.weight = 0.02
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'}
@ -22,7 +22,7 @@ def clock(context: Context) -> Prediction:
time = datetime.strptime(time_str, "%H:%M")
night = time.hour < 6 or time.hour >= 22
time_description = "" if night else "daytime"
time_description = "nighttime" if night else "daytime"
time_description_oppersite = "daytime" if night else "nighttime"
p.reasons.append(f"Bing says its {time_description}.")

View file

@ -1,5 +1,4 @@
import itertools
import logging
from datetime import datetime
from math import pi, sqrt, sin, cos, atan2
@ -14,7 +13,7 @@ tf = TimezoneFinder(in_memory=True)
def night_on_iss(context: Context) -> Prediction:
"""
It is night if it is night on the ISS and it is currently orbiting above us.
It is night if it is night on the ISS and it is currently orbiting above us. http://www.isstracker.com/
"""
p = Prediction()

View file

@ -17,7 +17,7 @@ def is_restaurant_open(name, open, close) -> Prediction:
soup = BeautifulSoup(r.content, features='html5lib')
listing_groups = soup.find_all('div', {'class': 'listing-group'})
p.reasons.append("Hopefully we are not banned from Just-eat ..")
#p.reasons.append("Hopefully we are not banned from Just-eat ..")
nice_group = None
for x in listing_groups:
@ -32,10 +32,12 @@ def is_restaurant_open(name, open, close) -> Prediction:
all_listings = nice_group.find_all('a', {'class': 'mediaElement'})
if any(name in x['href'] for x in all_listings):
p.reasons.append(f"{name} is currently open. We conclude from this, that there is {1 / 11}% chance of it being night outside!")
p.reasons.append(f"Our favorite pizza place, {name}, is currently open.")
p.reasons.append(f"We conclude from this, that there is {1 / 11}% chance of it being night outside")
p.probability = 1 / 11
else:
p.reasons.append(f"{name} is not open. We can conclude from this, that there is {1 - (1/11)}% chance of it currently being night outside! ")
p.reasons.append(f"Our favorite pizza place, {name}, is closed.")
p.reasons.append(f"We can conclude from this, that there is {1 - (1/11)}% chance of it currently being night outside!")
p.probability = 1 - (1 / 11)
return p

View file

@ -18,11 +18,11 @@ def camImgStrat(context : Context) -> Prediction:
p.probability = 1 - round((average/255),3)
if average < 128:
p.weight = round(1 - (average/255), 3)
p.reasons.append('Image was dark')
p.reasons.append('Camera image was dark, so the sun has probably set.')
else:
p.weight = round(average / 255, 3)
p.reasons.append('Image was light')
p.reasons.append('Camera image was light, so the sun is still shining.')
return p
@ -37,10 +37,10 @@ def australiaStrat(context : Context) -> Prediction:
if hour > 22 or hour < 6:
p.probability = 0.0
p.reasons.append('It\'s night-time in Australia')
p.reasons.append('It\'s night-time in Australia, so it must be day-time here.')
else:
p.probability = 1.0
p.reasons.append('It\'s day-time in Australia')
p.reasons.append('It\'s day-time in Australia, so it must be night-time here.')
return p
@ -48,10 +48,7 @@ def tv2newsStrat(context : Context) -> Prediction:
"""
The number of articles releases in the last few hours on TV2.dk
"""
print('before')
r = requests.get('http://mpx.services.tv2.dk/api/latest')
print('after')
data = r.json()
publish_dates = [(x['pubDate'])//1000 for x in data][:10]

View file

@ -50,9 +50,9 @@ def perform_svm_pred(context: Context) -> Prediction:
records = data.json()['result']['records']
X = [house['vehicleCount'] / house['totalSpaces'] for house in records]
X = [min(x, 1) for x in X]
p.reasons.append("Since we only have two data points")
p.reasons.append("Since our only two data points have 11 dimensions")
p.reasons.append("Since we are using a SVM")
p.reasons.append("We only have two data points")
p.reasons.append("Our only two data points have 11 dimensions")
p.reasons.append("We are using a SVM")
p.probability = predict(X)
return p

View file

@ -34,7 +34,7 @@ def check_games(context: Context) -> Prediction:
last_game_in_hours = (((datetime.now() - last_played_game).seconds)/60/60)
if last_game_in_hours < 2:
p.reasons.append("Alexanders upstairs neighbour is currently playing league")
p.reasons.append("Alexander's upstairs neighbour is currently playing league")
p.probability = 0.8
else:
last_game_in_hours = min(24.0, last_game_in_hours)

View file

@ -9,8 +9,8 @@ import numpy as np
@dataclass
class Context:
battery: int = 100
position: Dict[str, float] = field(default_factory=lambda: {'latitude': 53.0, 'longitude': 9.0})
battery: int = 55
position: Dict[str, float] = field(default_factory=lambda: {'latitude': 53.0, 'longitude': 9.0}) # Denmark somewhere
image: np.ndarray = None
# App settings