Spaces:
Runtime error
Runtime error
Update functions.py
Browse files- functions.py +7 -100
functions.py
CHANGED
|
@@ -4,109 +4,17 @@ import os
|
|
| 4 |
import joblib
|
| 5 |
import pandas as pd
|
| 6 |
|
|
|
|
|
|
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
load_dotenv()
|
| 9 |
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
def
|
| 12 |
-
"""Decodes features in the input DataFrame using corresponding Hopsworks Feature Store transformation functions"""
|
| 13 |
-
df_res = df.copy()
|
| 14 |
-
|
| 15 |
-
import inspect
|
| 16 |
-
|
| 17 |
-
td_transformation_functions = feature_view._batch_scoring_server._transformation_functions
|
| 18 |
-
|
| 19 |
-
res = {}
|
| 20 |
-
for feature_name in td_transformation_functions:
|
| 21 |
-
if feature_name in df_res.columns:
|
| 22 |
-
td_transformation_function = td_transformation_functions[feature_name]
|
| 23 |
-
sig, foobar_locals = inspect.signature(
|
| 24 |
-
td_transformation_function.transformation_fn), locals()
|
| 25 |
-
param_dict = dict([(param.name, param.default) for param in sig.parameters.values(
|
| 26 |
-
) if param.default != inspect._empty])
|
| 27 |
-
if td_transformation_function.name == "min_max_scaler":
|
| 28 |
-
df_res[feature_name] = df_res[feature_name].map(
|
| 29 |
-
lambda x: x * (param_dict["max_value"] - param_dict["min_value"]) + param_dict["min_value"])
|
| 30 |
-
|
| 31 |
-
elif td_transformation_function.name == "standard_scaler":
|
| 32 |
-
df_res[feature_name] = df_res[feature_name].map(
|
| 33 |
-
lambda x: x * param_dict['std_dev'] + param_dict["mean"])
|
| 34 |
-
elif td_transformation_function.name == "label_encoder":
|
| 35 |
-
dictionary = param_dict['value_to_index']
|
| 36 |
-
dictionary_ = {v: k for k, v in dictionary.items()}
|
| 37 |
-
df_res[feature_name] = df_res[feature_name].map(
|
| 38 |
-
lambda x: dictionary_[x])
|
| 39 |
-
return df_res
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
def get_model(project, model_name, evaluation_metric, sort_metrics_by):
|
| 43 |
-
"""Retrieve desired model or download it from the Hopsworks Model Registry.
|
| 44 |
-
In second case, it will be physically downloaded to this directory"""
|
| 45 |
-
TARGET_FILE = "model.pkl"
|
| 46 |
-
list_of_files = [os.path.join(dirpath, filename) for dirpath, _, filenames
|
| 47 |
-
in os.walk('.') for filename in filenames if filename == TARGET_FILE]
|
| 48 |
-
|
| 49 |
-
if list_of_files:
|
| 50 |
-
model_path = list_of_files[0]
|
| 51 |
-
model = joblib.load(model_path)
|
| 52 |
-
else:
|
| 53 |
-
if not os.path.exists(TARGET_FILE):
|
| 54 |
-
mr = project.get_model_registry()
|
| 55 |
-
# get best model based on custom metrics
|
| 56 |
-
model = mr.get_best_model(model_name,
|
| 57 |
-
evaluation_metric,
|
| 58 |
-
sort_metrics_by)
|
| 59 |
-
model_dir = model.download()
|
| 60 |
-
model = joblib.load(model_dir + "/model.pkl")
|
| 61 |
-
|
| 62 |
-
return model
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def get_air_json(city_name, AIR_QUALITY_API_KEY):
|
| 66 |
-
return requests.get(f'https://api.waqi.info/feed/{city_name}/?token={AIR_QUALITY_API_KEY}').json()['data']
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
def get_air_quality_data(city_name):
|
| 70 |
-
AIR_QUALITY_API_KEY = os.getenv('AIR_QUALITY_API_KEY')
|
| 71 |
-
json = get_air_json(city_name, AIR_QUALITY_API_KEY)
|
| 72 |
-
iaqi = json['iaqi']
|
| 73 |
-
forecast = json['forecast']['daily']
|
| 74 |
-
return [
|
| 75 |
-
city_name,
|
| 76 |
-
json['aqi'], # AQI
|
| 77 |
-
json['time']['s'][:10], # Date
|
| 78 |
-
forecast['pm10'][0]['avg'],
|
| 79 |
-
forecast['pm25'][0]['avg'],
|
| 80 |
-
forecast['o3'][0]['avg'],
|
| 81 |
-
]
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
def get_air_quality_df(data):
|
| 85 |
-
col_names = [
|
| 86 |
-
'city',
|
| 87 |
-
'aqi',
|
| 88 |
-
'date',
|
| 89 |
-
'pm10',
|
| 90 |
-
'pm25',
|
| 91 |
-
'o3',
|
| 92 |
-
]
|
| 93 |
-
|
| 94 |
-
new_data = pd.DataFrame(
|
| 95 |
-
data,
|
| 96 |
-
columns=col_names
|
| 97 |
-
)
|
| 98 |
-
new_data.date = new_data.date.apply(timestamp_2_time)
|
| 99 |
-
|
| 100 |
-
return new_data
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
def get_weather_json(city, date, WEATHER_API_KEY):
|
| 104 |
-
return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/{city.lower()}/{date}?unitGroup=metric&include=days&key={WEATHER_API_KEY}&contentType=json').json()
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
def get_weather_data(city_name, date):
|
| 108 |
WEATHER_API_KEY = os.getenv('WEATHER_API_KEY')
|
| 109 |
-
json = get_weather_json(
|
| 110 |
data = json['days'][0]
|
| 111 |
|
| 112 |
return [
|
|
@@ -175,8 +83,7 @@ def get_weather_df(data):
|
|
| 175 |
|
| 176 |
return new_data
|
| 177 |
|
| 178 |
-
|
| 179 |
def timestamp_2_time(x):
|
| 180 |
dt_obj = datetime.strptime(str(x), '%Y-%m-%d')
|
| 181 |
dt_obj = dt_obj.timestamp() * 1000
|
| 182 |
-
return int(dt_obj)
|
|
|
|
| 4 |
import joblib
|
| 5 |
import pandas as pd
|
| 6 |
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
load_dotenv()
|
| 11 |
|
| 12 |
+
def get_weather_json(date, WEATHER_API_KEY):
|
| 13 |
+
return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/london/{date}?unitGroup=metric&include=days&key={WEATHER_API_KEY}&contentType=json').json()
|
| 14 |
|
| 15 |
+
def get_weather_data(date):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
WEATHER_API_KEY = os.getenv('WEATHER_API_KEY')
|
| 17 |
+
json = get_weather_json(date, WEATHER_API_KEY)
|
| 18 |
data = json['days'][0]
|
| 19 |
|
| 20 |
return [
|
|
|
|
| 83 |
|
| 84 |
return new_data
|
| 85 |
|
|
|
|
| 86 |
def timestamp_2_time(x):
|
| 87 |
dt_obj = datetime.strptime(str(x), '%Y-%m-%d')
|
| 88 |
dt_obj = dt_obj.timestamp() * 1000
|
| 89 |
+
return int(dt_obj)
|