This commit is contained in:
Christian Lynggaard Jørgensen 2019-04-07 03:30:17 +02:00
commit 53e499c77b
7 changed files with 43 additions and 32 deletions

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.02
p.weight = 0.01
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'}

View File

@ -1,15 +1,11 @@
import itertools
from datetime import datetime
import operator
from datetime import datetime, timezone
from math import pi, sqrt, sin, cos, atan2
import pytz
import requests
from timezonefinder import TimezoneFinder
from ..util import Context, Prediction
tf = TimezoneFinder(in_memory=True)
def night_on_iss(context: Context) -> Prediction:
"""
@ -20,34 +16,43 @@ def night_on_iss(context: Context) -> Prediction:
if not context.flat_earth:
iss_position = requests.get("http://api.open-notify.org/iss-now.json").json()["iss_position"]
the_iss = "The ISS"
at_iss_location = "at the ISS's location"
iss_position_description = "on board the ISS"
else:
p.reasons.append("The ISS is (obviously) located in Hollywood")
p.reasons.append('The "ISS" is obviously a studio in Hollywood')
the_iss = "Hollywood"
iss_position = {'latitude': 34.092808, 'longitude': -118.328659} # Hollywood
at_iss_location = "in Hollywood"
iss_position_description = "in the Hollywood studio"
phone_position = context.position
sun_times = requests.get(f"https://api.sunrise-sunset.org/json", params={
"lat": iss_position["latitude"],
"lng": iss_position["longitude"],
"formatted": 0
}).json()["results"]
iss_position_sunrise = datetime.strptime(sun_times["sunrise"], "%Y-%m-%dT%H:%M:%S%z")
iss_position_sunset = datetime.strptime(sun_times["sunset"], "%Y-%m-%dT%H:%M:%S%z")
p.reasons.append(f"The sun rises and sets at UTC {iss_position_sunrise.strftime('%H:%M')} and {iss_position_sunset.strftime('%H:%M')}, respectively, {at_iss_location}.")
utcnow = datetime.now(timezone.utc)
p.reasons.append(f"It is currently {utcnow.strftime('%H:%M')} UTC")
iss_night = utcnow < iss_position_sunrise or iss_position_sunset < utcnow
iss_time_description = "nighttime" if iss_night else "daytime"
p.reasons.append(f"Therefore, it is {iss_time_description} {iss_position_description}.")
# Calculate ratio: a number between 0 and 1 saying how close we are to the ISS
phone_position = context.position
distance = haversine(iss_position, phone_position)
max_distance = 40075 / 2 # the furthest you can be from any position is half of the earth's circumference
ratio = distance / max_distance
# We're in the same "timezone" as the ISS if we're on the same half of the earth
on_iss_time = ratio < 0.5
side = "same" if on_iss_time else "other"
p.reasons.append(f"{the_iss} is {int(distance)} km away, so we are on the {side} side of the earth.")
for i in itertools.count(1):
iss_tz = tf.closest_timezone_at(lng=float(iss_position["longitude"]), lat=float(iss_position["latitude"]),
delta_degree=i)
if iss_tz is not None:
break
iss_time = datetime.now(pytz.timezone(iss_tz))
iss_night = iss_time.hour < 6 or iss_time.hour >= 22
# iss_night on_iss_time night
# 0 0 1
@ -56,11 +61,10 @@ def night_on_iss(context: Context) -> Prediction:
# 1 1 1
night = iss_night == on_iss_time
iss_time_description = "nighttime" if iss_night else "daytime"
time_description = "nighttime" if night else "daytime"
p.probability = float(night)
p.reasons.append(f"It is {iss_time_description} {iss_position_description}.")
p.reasons.append(f"Therefore, it must be {time_description} where we are.")
op = operator.add if night else operator.sub
p.probability = op(0.5, ratio * 0.5)
p.reasons.append(f"So it must be {time_description} where we are.")
return p

View File

@ -34,7 +34,7 @@ def is_restaurant_open(name, open, close) -> Prediction:
else:
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)
p.probability = round(1 - (1 / 11), 2)
return p

View File

@ -35,12 +35,20 @@ def australiaStrat(context : Context) -> Prediction:
hour = t.hour
p = Prediction()
if hour > 22 or hour < 6:
p.probability = 0.0
p.reasons.append('It\'s night-time in Australia, so it must be day-time here.')
if context.in_australia:
if hour > 22 or hour < 6:
p.probability = 1.0
p.reasons.append('It\'s night-time in Australia, and that\'s where we\'re at.')
else:
p.probability = 0.0
p.reasons.append('It\'s day-time in Australia, and that\'s where we\'re at.')
else:
p.probability = 1.0
p.reasons.append('It\'s day-time in Australia, so it must be night-time here.')
if hour > 22 or hour < 6:
p.probability = 0.0
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, so it must be night-time here.')
return p

View File

@ -55,7 +55,7 @@ def perform_svm_pred(context: Context) -> Prediction:
X = [min(x, 1) for x in X]
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.reasons.append("We are using a SVM. Apparently that's a poor idea.")
p.probability = float(predict(X))
return p

View File

@ -11,7 +11,7 @@ from ..util import Context, Prediction
def is_tide(context: Context) -> Prediction:
"""
Determine whether or not it is night in Aarhus based no the current water level and which month we are in, based
Determine whether or not it is night in Aarhus based on the current water level and which month we are in, based
on number of cars driving across The Storbæltsbro.
"""

View File

@ -5,7 +5,6 @@ pytz
beautifulsoup4
pandas
opencv-python
timezonefinder
scikit-learn
html5lib
xlrd