Skip to content

Commit

Permalink
change to class
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-perseus committed Dec 1, 2023
1 parent ab2368e commit 1bcf382
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 87 deletions.
82 changes: 0 additions & 82 deletions data/preprocessing/get_features_for_prediction.py

This file was deleted.

84 changes: 84 additions & 0 deletions data/preprocessing/single_prediction_features.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import pandas as pd
import openmeteo_requests
import requests_cache
from datetime import datetime, date, timedelta
from retry_requests import retry

from .prepare_time_features import prepare_time_features

# TODO get these from metadata
feature_columns = ['ferien', 'feiertag', 'covid_19', 'olma_offa', 'temperature_2m_max',
'temperature_2m_min', 'rain_sum', 'snowfall_sum', 'sin_minute',
'cos_minute', 'sin_hour', 'cos_hour', 'sin_weekday', 'cos_weekday',
'sin_day', 'cos_day', 'sin_month', 'cos_month']
weather_api_url = "https://api.open-meteo.com/v1/forecast" # URL API


class SinglePredictionFeatures:
def __init__(self, raw_features_path):
# Import Features
self.calendar_features = pd.read_csv(raw_features_path, sep=",")
self.calendar_features['date'] = pd.to_datetime(self.calendar_features['date'],
format='%Y-%m-%d') # Extract Date

# Get Weather-Data
# Set up Client and define Params
cache_session = requests_cache.CachedSession('.cache', expire_after=3600)
retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
self.openmeteo = openmeteo_requests.Client(session=retry_session)

def get_weather_forecast(self, params):
responses = self.openmeteo.weather_api(weather_api_url, params=params)
response = responses[0]

daily = response.Daily()
daily_temperature_2m_max = daily.Variables(0).ValuesAsNumpy()
daily_temperature_2m_min = daily.Variables(1).ValuesAsNumpy()
daily_rain_sum = daily.Variables(2).ValuesAsNumpy()
daily_snowfall_sum = daily.Variables(3).ValuesAsNumpy()

daily_data = {"date": pd.date_range(
start=pd.to_datetime(daily.Time(), unit="s"),
end=pd.to_datetime(daily.TimeEnd(), unit="s"),
freq=pd.Timedelta(seconds=daily.Interval()),
inclusive="left"
), "temperature_2m_max": daily_temperature_2m_max, "temperature_2m_min": daily_temperature_2m_min,
"rain_sum": daily_rain_sum, "snowfall_sum": daily_snowfall_sum}

daily_dataframe = pd.DataFrame(data=daily_data)
return daily_dataframe

def build_dataframe(self, input_date):
timestamp = datetime.strptime(input_date, '%Y-%m-%d %H:%M')

params = {
"latitude": 47.4239,
"longitude": 9.3748,
"daily": ["temperature_2m_max", "temperature_2m_min", "rain_sum", "snowfall_sum"],
"start_date": timestamp.strftime("%Y-%m-%d"),
"end_date": timestamp.strftime("%Y-%m-%d")
}

# Get Data
df_weather = self.get_weather_forecast(params)

# Merge Weather with other Features
df = pd.merge(df_weather, self.calendar_features, on="date", how="left")
df["datetime"] = timestamp

# Add time-features
df = prepare_time_features(df)

df_filtered = df[feature_columns]

return df_filtered, len(feature_columns)


if __name__ == "__main__":
date_today = date.today()
date_tomorrow = date_today + timedelta(days=1)
single_prediction_features = SinglePredictionFeatures("raw_features_2024.csv")
df_demo, features_length = single_prediction_features.build_dataframe(date_tomorrow.strftime("%Y-%m-%d %H:%M"))
print(df_demo.head())
print(df_demo.columns)
print(features_length)
16 changes: 11 additions & 5 deletions deploy/predict.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import torch
from torch.utils.data import TensorDataset, DataLoader
from data.preprocessing.get_features_for_prediction import build_dataframe
from data.preprocessing.single_prediction_features import SinglePredictionFeatures

batch_size = 1 # Required for model input
parking_data_labels = ["P24", "P44", "P42", "P33", "P23", "P25", "P21", "P31", "P53", "P32", "P22", "P52", "P51",
"P43"] # TODO get these from metadata file
raw_features_path = "data/preprocessing/raw_features_2024.csv"


class Predict:
def __init__(self, model_path):
self.single_prediction_features = SinglePredictionFeatures(raw_features_path)
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model = torch.jit.load(model_path, map_location=self.device)
self.model.eval()

@staticmethod
def build_dataset(df):
def build_dataset(self, df):
features = torch.Tensor(df.values)

dataset = TensorDataset(features)
Expand All @@ -23,13 +24,18 @@ def build_dataset(df):

def predict_with_model(self, dataloader, features_length):
data = next(iter(dataloader))[0]
data = data.view([batch_size, -1, features_length]).to(device)
data = data.view([batch_size, -1, features_length]).to(self.device)
return self.model(data).cpu()

def predict_for_date(self, date):
features_df, features_length = build_dataframe(date)
features_df, features_length = self.single_prediction_features.build_dataframe(date)
dataloader = self.build_dataset(features_df)

output = self.predict_with_model(dataloader, features_length)

return [dict(zip(parking_data_labels, row)) for row in output.tolist()]


if __name__ == "__main__":
predict = Predict("model_scripted.pt")
print(predict.predict_for_date("2023-10-09 00:00"))

0 comments on commit 1bcf382

Please sign in to comment.