Skip to content
This repository has been archived by the owner on Apr 7, 2022. It is now read-only.

Commit

Permalink
Merge pull request #20
Browse files Browse the repository at this point in the history
McToel
  • Loading branch information
McToel authored Oct 4, 2020
2 parents 9f73802 + 817bad3 commit a18b4b5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 41 deletions.
1 change: 1 addition & 0 deletions helpers/StreckennetzSteffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def eva_route_length(self, waypoints) -> float:
self.get_name(eva=waypoints[i + 1]))
except KeyError:
pass
return length

@functools.lru_cache(maxsize=8000)
def distance(self, u: str, v: str) -> float:
Expand Down
36 changes: 3 additions & 33 deletions server/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import os
import subprocess

# self-writen stuff
from helpers.StationPhillip import StationPhillip
from server.connection import get_connection, clean_data, get_trips_of_trains
from server.predictor import Predictor
Expand All @@ -21,7 +20,6 @@
logger = logging.getLogger(__name__)
basepath = os.path.dirname(os.path.realpath(__file__))

# make a new random-forest-predictor instance
stations = StationPhillip()
pred = Predictor()

Expand Down Expand Up @@ -65,10 +63,10 @@ def analysis(connection):
# add adelay5 to first and last station
fs_data = pred.get_pred_data(connection[index1])
ls_data = pred.get_pred_data(connection[-index2])
_x, _x, _x, _x, _x, connection[index1]["ddelay5"], _x, _x = pred.predict(fs_data, 0)
_x, _x, _x, _x, _x, connection[index1]["ddelay5"], _x, _x = pred.predict(fs_data, 0).values()
_x, connection[-index2]["adelay5"], _x, _x, _x, _x, _x, _x = pred.predict(
ls_data, 1
)
).values()

# there are two segments more than connections (= overall info at the end - 1 bc it is like that)
for i in range(index1, len(connection) - index2):
Expand All @@ -85,13 +83,6 @@ def analysis(connection):
).seconds
// 60
) % 60 # we just want minutes
logger.debug(
str(fromUnix(connection[i + 1]["departure"][time]))
+ " - "
+ str(fromUnix(connection[i]["arrival"][time]))
+ " = "
+ str(transtime)
)
(
connection[i]["con_score"],
connection[i]["adelay5"],
Expand All @@ -107,32 +98,11 @@ def analysis(connection):
).seconds
// 60
) % 60 # we just want minutes
logger.debug(
str(fromUnix(connection[i + 2]["departure"][time]))
+ " - "
+ str(fromUnix(connection[i]["arrival"][time]))
+ " = "
+ str(transtime)
)
(
connection[i]["con_score"],
connection[i]["adelay5"],
connection[i + 2]["ddelay5"],
) = pred.predict_con(data1, data2, transtime)

logger.debug(data1)
logger.debug(data2)

logger.debug(
"Score for connection["
+ connection[i]["train"]["name"]
+ " to "
+ connection[i + 1]["train"]["name"]
+ " in "
+ connection[i]["segmentDestination"]["title"]
+ "] = "
+ str(connection[i]["con_score"])
)
if total_score == 0:
total_score = connection[i]["con_score"]
else:
Expand All @@ -151,7 +121,7 @@ def analysis(connection):
else:
connection[-1]["total_score"] = int(total_score * 100)

logger.debug("Verbindungsscore:" + str(total_score))
# logger.debug("Verbindungsscore:" + str(total_score))

for i in range(len(connection) - 1):
if "ICE" in connection[i]["train"]["name"]:
Expand Down
22 changes: 14 additions & 8 deletions server/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from functools import lru_cache
import requests
from datetime import datetime, timedelta
from pytz import timezone
Expand Down Expand Up @@ -60,19 +61,24 @@ def get_trips_of_trains(connection):
for n in range(len(connection[i]["segments"])):
try:
jid = connection[i]["segments"][n]["jid"]
r = requests.get(
"https://marudor.de/api/hafas/v1/journeyDetails?jid={}?profile=db".format(
jid
)
)
full_trip = r.json()["stops"]
full_trip = [int(part["station"]["id"]) for part in full_trip]
connection[i]["segments"][n]["full_trip"] = full_trip
connection[i]["segments"][n]["full_trip"] = get_trip_of_train(jid)
except KeyError:
pass # Fußweg has no jid
return connection


@lru_cache
def get_trip_of_train(jid):
r = requests.get(
"https://marudor.de/api/hafas/v1/journeyDetails?jid={}?profile=db".format(
jid
)
)
trip = r.json()["stops"]
trip = [int(stop["station"]["id"]) for stop in trip]
return trip


def clean_data(connection):
""" Remove unneded content"""
for i in range(len(connection)):
Expand Down

0 comments on commit a18b4b5

Please sign in to comment.