More strats

This commit is contained in:
Alexander Munch-Hansen 2019-04-06 14:10:26 +02:00
parent b6c361de97
commit 366b2c3845
4 changed files with 125 additions and 75 deletions

View file

@ -0,0 +1,34 @@
import requests
from server.nightr.util import Prediction, Context
def scrape_traffic(context: Context) -> Prediction:
r = requests.get('https://portal.opendata.dk/api/3/action/datastore_search?resource_id=b3eeb0ff-c8a8-4824-99d6-e0a3747c8b0d')
night_avr = 3.38
day_avr = 6.98
p = Prediction()
data = r.json()
sum = 0
len = 0
for lel in data['result']['records']:
sum += lel['vehicleCount']
len += 1
curr_avg = len / sum
diff = day_avr - night_avr
if curr_avg >= day_avr:
p.reasons.append(f"Because {curr_avg} cars are driving around Aarhus right now and {day_avr} is the expected number for daytime")
p.probability = 0.0
elif curr_avg <= night_avr:
p.reasons.append(f"Because {curr_avg} cars are driving around Aarhus right now and {night_avr} is the expected number for nighttime")
p.probability = 1.0
else:
p.reasons.append(f"Because average for daytime is {day_avr} and average for nighttime is {night_avr}, but the current average is {curr_avg}")
res = 1 - curr_avg / diff
p.probability = res
return p

View file

@ -4,60 +4,10 @@ import pandas as pd
import urllib.request
from datetime import datetime, timedelta
import json
def determine_month():
ds = pd.read_excel(urllib.request.urlopen('https://sundogbaelt.dk/wp-content/uploads/2019/04/trafiktal-maaned.xls'))
cur_year = 2019
amount_of_cur_year = sum([x == cur_year for x in ds['År']])
cur_year_total = sum(ds['Total'][1:amount_of_cur_year+1])
last_year_total = sum(ds['Total'][amount_of_cur_year+1:amount_of_cur_year+13])
return (12/(last_year_total//cur_year_total))+1
def is_tide():
month = determine_month()
tide_data = requests.get('https://www.dmi.dk/fileadmin/user_upload/Bruger_upload/Tidevand/2019/Aarhus.t.txt')
lines = tide_data.text[570:].split('\n')
tuples = [x.split('\t') for x in lines]
lel = [[datetime.strptime(x[0], '%Y%m%d%H%M'), x[1]] for x in tuples[:-1]]
matches = [[x[0], int(x[1])] for x in lel if x[0].month == month]
all_the_data = requests.get('https://www.dmi.dk/NinJo2DmiDk/ninjo2dmidk?cmd=odj&stations=22331&datatype=obs')
current_water_level = json.loads(all_the_data.content)[0]['values'][-1]['value']
# Generate average of when the water is high
last_match = matches[0]
moments = []
for idx, water_level in enumerate(matches[1:]):
#print(last_match[1], water_level[1])
diff = abs(last_match[1]) + abs(water_level[1])
time_diff = (water_level[0] - last_match[0]).seconds
average_inc = time_diff/diff
average_delta = timedelta(seconds=average_inc)
if last_match[1] < 0: # Increasing
time = last_match
while time[1] != current_water_level:
time[0] += average_delta
time[1] += 1
from server.nightr.strategies.strat_utils import determine_month
elif last_match[1] > 0: # Decreasing
time = last_match
while time[1] != current_water_level:
time[0] += average_delta
time[1] -= 1
last_match = water_level
moments.append(time[0])
night = sum([1 for x in moments if 6 >= x.hour or x.hour >= 22])
return night / len(moments)
def tmp():
@ -66,32 +16,13 @@ def tmp():
json.dump(r.json(), f)
def read_tmp():
with open('traffic_data_13_23.json') as f:
data = json.load(f)
number = sum([cars['vehicleCount'] for cars in data['result']['records']])
print(number / len(data['result']['records']))
def scrape_traffic():
r = requests.get('https://portal.opendata.dk/api/3/action/datastore_search?resource_id=b3eeb0ff-c8a8-4824-99d6-e0a3747c8b0d')
night_avr = 3.38
day_avr = None
data = r.json()
sum = 0
len = 0
for lel in data['result']['records']:
sum += lel['vehicleCount']
len += 1
curr_avg = len / sum
diff= day_avr - night_avr
if curr_avg >= day_avr:
return 0.0
elif curr_avg <= night_avr:
return 1.0
res = 1 - curr_avg / diff
assert(res < 1 and res > 0)
return res
def scrape_weather():
@ -112,3 +43,5 @@ def scrape_dmi_aarhus():
return 0.0
#adak_latest_time, adak_latest_temp_aarhus = max(adak_timeserie.items(), key= lambda x : x[0])
read_tmp()

View file

@ -0,0 +1,14 @@
import pandas as pd
import urllib.request
def determine_month():
ds = pd.read_excel(urllib.request.urlopen('https://sundogbaelt.dk/wp-content/uploads/2019/04/trafiktal-maaned.xls'))
cur_year = 2019
amount_of_cur_year = sum([x == cur_year for x in ds['År']])
cur_year_total = sum(ds['Total'][1:amount_of_cur_year+1])
last_year_total = sum(ds['Total'][amount_of_cur_year+1:amount_of_cur_year+13])
return ((12/(last_year_total//cur_year_total))+1), cur_year_total, last_year_total

View file

@ -0,0 +1,69 @@
import datetime
import json
import calendar
import requests
from server.nightr.strategies.strat_utils import determine_month
from server.nightr.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
on number of cars driving across The Storbæltsbro.
"""
p = Prediction()
month, cur_year_total_cars, last_year_total_cars = determine_month()
p.reasons.append(f"Because the month is f{calendar.month_name[month]}")
p.reasons.append(f"Because the number of cars having driven on the Storbæltsbro is f{cur_year_total_cars}")
p.reasons.append(f"And because the number of cars having driven over it in the last year is f{last_year_total_cars}")
tide_data = requests.get('https://www.dmi.dk/fileadmin/user_upload/Bruger_upload/Tidevand/2019/Aarhus.t.txt')
lines = tide_data.text[570:].split('\n')
tuples = [x.split('\t') for x in lines]
lel = [[datetime.strptime(x[0], '%Y%m%d%H%M'), x[1]] for x in tuples[:-1]]
matches = [[x[0], int(x[1])] for x in lel if x[0].month == month]
all_the_data = requests.get('https://www.dmi.dk/NinJo2DmiDk/ninjo2dmidk?cmd=odj&stations=22331&datatype=obs')
current_water_level = json.loads(all_the_data.content)[0]['values'][-1]['value']
# Generate average of when the water is high
last_match = matches[0]
moments = []
for idx, water_level in enumerate(matches[1:]):
#print(last_match[1], water_level[1])
diff = abs(last_match[1]) + abs(water_level[1])
time_diff = (water_level[0] - last_match[0]).seconds
average_inc = time_diff/diff
average_delta = datetime.timedelta(seconds=average_inc)
if last_match[1] < 0: # Increasing
time = last_match
while time[1] != current_water_level:
time[0] += average_delta
time[1] += 1
elif last_match[1] > 0: # Decreasing
time = last_match
while time[1] != current_water_level:
time[0] += average_delta
time[1] -= 1
last_match = water_level
moments.append(time[0])
night = sum([1 for x in moments if 6 >= x.hour or x.hour >= 22])
p.reasons.append(f"And because the number of times the water is at the current level at nighttime is: {night}, compared to the total amount of times in {calendar.month_name[month]}, being {len(moments)}")
p.probability = night / len(moments)
return p