Spaces:
Runtime error
Runtime error
Commit ·
82326ea
1
Parent(s): c7ce2ca
Update functions.py
Browse files- functions.py +44 -106
functions.py
CHANGED
|
@@ -3,7 +3,6 @@ import requests
|
|
| 3 |
import os
|
| 4 |
import joblib
|
| 5 |
import pandas as pd
|
| 6 |
-
|
| 7 |
import json
|
| 8 |
|
| 9 |
|
|
@@ -11,9 +10,9 @@ def decode_features(df, feature_view):
|
|
| 11 |
"""Decodes features in the input DataFrame using corresponding Hopsworks Feature Store transformation functions"""
|
| 12 |
df_res = df.copy()
|
| 13 |
|
|
|
|
| 14 |
import inspect
|
| 15 |
|
| 16 |
-
|
| 17 |
td_transformation_functions = feature_view._batch_scoring_server._transformation_functions
|
| 18 |
|
| 19 |
res = {}
|
|
@@ -25,7 +24,6 @@ def decode_features(df, feature_view):
|
|
| 25 |
if td_transformation_function.name == "min_max_scaler":
|
| 26 |
df_res[feature_name] = df_res[feature_name].map(
|
| 27 |
lambda x: x * (param_dict["max_value"] - param_dict["min_value"]) + param_dict["min_value"])
|
| 28 |
-
|
| 29 |
elif td_transformation_function.name == "standard_scaler":
|
| 30 |
df_res[feature_name] = df_res[feature_name].map(
|
| 31 |
lambda x: x * param_dict['std_dev'] + param_dict["mean"])
|
|
@@ -36,115 +34,53 @@ def decode_features(df, feature_view):
|
|
| 36 |
lambda x: dictionary_[x])
|
| 37 |
return df_res
|
| 38 |
|
| 39 |
-
|
| 40 |
-
def get_model1(project, model_name, evaluation_metric, sort_metrics_by):
|
| 41 |
-
"""Retrieve desired model or download it from the Hopsworks Model Registry.
|
| 42 |
-
In second case, it will be physically downloaded to this directory"""
|
| 43 |
-
TARGET_FILE = "model_tempmax.pkl"
|
| 44 |
-
list_of_files = [os.path.join(dirpath,filename) for dirpath, _, filenames \
|
| 45 |
-
in os.walk('.') for filename in filenames if filename == TARGET_FILE]
|
| 46 |
-
|
| 47 |
-
if list_of_files:
|
| 48 |
-
model_path = list_of_files[0]
|
| 49 |
-
model = joblib.load(model_path)
|
| 50 |
-
else:
|
| 51 |
-
if not os.path.exists(TARGET_FILE):
|
| 52 |
-
mr = project.get_model_registry()
|
| 53 |
-
# get best model based on custom metrics
|
| 54 |
-
model = mr.get_best_model(model_name,
|
| 55 |
-
evaluation_metric,
|
| 56 |
-
sort_metrics_by)
|
| 57 |
-
model_dir = model.download()
|
| 58 |
-
model = joblib.load(model_dir + "/model_tempmax.pkl")
|
| 59 |
-
|
| 60 |
-
return model
|
| 61 |
-
def get_model2(project, model_name, evaluation_metric, sort_metrics_by):
|
| 62 |
-
"""Retrieve desired model or download it from the Hopsworks Model Registry.
|
| 63 |
-
In second case, it will be physically downloaded to this directory"""
|
| 64 |
-
TARGET_FILE = "model_tempmin.pkl"
|
| 65 |
-
list_of_files = [os.path.join(dirpath,filename) for dirpath, _, filenames \
|
| 66 |
-
in os.walk('.') for filename in filenames if filename == TARGET_FILE]
|
| 67 |
-
|
| 68 |
-
if list_of_files:
|
| 69 |
-
model_path = list_of_files[0]
|
| 70 |
-
model = joblib.load(model_path)
|
| 71 |
-
else:
|
| 72 |
-
if not os.path.exists(TARGET_FILE):
|
| 73 |
-
mr = project.get_model_registry()
|
| 74 |
-
# get best model based on custom metrics
|
| 75 |
-
model = mr.get_best_model(model_name,
|
| 76 |
-
evaluation_metric,
|
| 77 |
-
sort_metrics_by)
|
| 78 |
-
model_dir = model.download()
|
| 79 |
-
model = joblib.load(model_dir + "/model_tempmin.pkl")
|
| 80 |
-
|
| 81 |
-
return model
|
| 82 |
-
def get_model(project, model_name, evaluation_metric, sort_metrics_by):
|
| 83 |
-
"""Retrieve desired model or download it from the Hopsworks Model Registry.
|
| 84 |
-
In second case, it will be physically downloaded to this directory"""
|
| 85 |
-
TARGET_FILE = "model_temp.pkl"
|
| 86 |
-
list_of_files = [os.path.join(dirpath,filename) for dirpath, _, filenames \
|
| 87 |
-
in os.walk('.') for filename in filenames if filename == TARGET_FILE]
|
| 88 |
-
|
| 89 |
-
if list_of_files:
|
| 90 |
-
model_path = list_of_files[0]
|
| 91 |
-
model = joblib.load(model_path)
|
| 92 |
-
else:
|
| 93 |
-
if not os.path.exists(TARGET_FILE):
|
| 94 |
-
mr = project.get_model_registry()
|
| 95 |
-
# get best model based on custom metrics
|
| 96 |
-
model = mr.get_best_model(model_name,
|
| 97 |
-
evaluation_metric,
|
| 98 |
-
sort_metrics_by)
|
| 99 |
-
model_dir = model.download()
|
| 100 |
-
model = joblib.load(model_dir + "/model_temp.pkl")
|
| 101 |
-
|
| 102 |
-
return model
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
def get_weather_json(date, WEATHER_API_KEY):
|
| 107 |
return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/helsinki/{date}?unitGroup=metric&include=days&key={WEATHER_API_KEY}&contentType=json').json()
|
| 108 |
|
|
|
|
|
|
|
| 109 |
|
| 110 |
-
def
|
| 111 |
-
|
| 112 |
-
json = get_weather_json(date, WEATHER_API_KEY)
|
| 113 |
-
data = json['days'][0]
|
| 114 |
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
|
| 143 |
|
| 144 |
def get_weather_df(data):
|
| 145 |
col_names = [
|
| 146 |
-
'
|
| 147 |
-
'
|
| 148 |
'tempmax',
|
| 149 |
'tempmin',
|
| 150 |
'temp',
|
|
@@ -161,7 +97,7 @@ def get_weather_df(data):
|
|
| 161 |
'windgust',
|
| 162 |
'windspeed',
|
| 163 |
'winddir',
|
| 164 |
-
'
|
| 165 |
'cloudcover',
|
| 166 |
'visibility',
|
| 167 |
'solarradiation',
|
|
@@ -170,12 +106,14 @@ def get_weather_df(data):
|
|
| 170 |
'conditions'
|
| 171 |
]
|
| 172 |
|
|
|
|
|
|
|
| 173 |
new_data = pd.DataFrame(
|
| 174 |
data,
|
| 175 |
columns=col_names
|
| 176 |
)
|
| 177 |
-
new_data.
|
| 178 |
-
|
| 179 |
return new_data
|
| 180 |
|
| 181 |
def timestamp_2_time1(x):
|
|
|
|
| 3 |
import os
|
| 4 |
import joblib
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
import json
|
| 7 |
|
| 8 |
|
|
|
|
| 10 |
"""Decodes features in the input DataFrame using corresponding Hopsworks Feature Store transformation functions"""
|
| 11 |
df_res = df.copy()
|
| 12 |
|
| 13 |
+
print(df_res)
|
| 14 |
import inspect
|
| 15 |
|
|
|
|
| 16 |
td_transformation_functions = feature_view._batch_scoring_server._transformation_functions
|
| 17 |
|
| 18 |
res = {}
|
|
|
|
| 24 |
if td_transformation_function.name == "min_max_scaler":
|
| 25 |
df_res[feature_name] = df_res[feature_name].map(
|
| 26 |
lambda x: x * (param_dict["max_value"] - param_dict["min_value"]) + param_dict["min_value"])
|
|
|
|
| 27 |
elif td_transformation_function.name == "standard_scaler":
|
| 28 |
df_res[feature_name] = df_res[feature_name].map(
|
| 29 |
lambda x: x * param_dict['std_dev'] + param_dict["mean"])
|
|
|
|
| 34 |
lambda x: dictionary_[x])
|
| 35 |
return df_res
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def get_weather_json(date, WEATHER_API_KEY):
|
| 38 |
return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/helsinki/{date}?unitGroup=metric&include=days&key={WEATHER_API_KEY}&contentType=json').json()
|
| 39 |
|
| 40 |
+
def get_weather_csv():
|
| 41 |
+
return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/shanghai?unitGroup=metric&include=days&key=FYYH5HKD9558HBXD2D6KWXDGH&contentType=csv').csv()
|
| 42 |
|
| 43 |
+
def get_weather_json_quick(date):
|
| 44 |
+
return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/shanghai/{date}?unitGroup=metric&include=days&key=FYYH5HKD9558HBXD2D6KWXDGH&contentType=json').json()
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
|
| 47 |
+
def get_weather_data(json):
|
| 48 |
+
#WEATHER_API_KEY = os.getenv('WEATHER_API_KEY')
|
| 49 |
+
|
| 50 |
+
#csv = get_weather_csv()
|
| 51 |
+
data = json['days'][0]
|
| 52 |
+
print("data parsed sccessfully")
|
| 53 |
+
#return [
|
| 54 |
+
# #json['address'].capitalize(),
|
| 55 |
+
# data['datetime'],
|
| 56 |
+
# data['feelslikemax'],
|
| 57 |
+
# data['feelslikemin'],
|
| 58 |
+
# data['feelslike'],
|
| 59 |
+
# data['dew'],
|
| 60 |
+
# data['humidity'],
|
| 61 |
+
# data['precip'],
|
| 62 |
+
# data['precipprob'],
|
| 63 |
+
# data['precipcover'],
|
| 64 |
+
# data['snow'],
|
| 65 |
+
# data['snowdepth'],
|
| 66 |
+
# data['windgust'],
|
| 67 |
+
# data['windspeed'],
|
| 68 |
+
# data['winddir'],
|
| 69 |
+
# data['pressure'],
|
| 70 |
+
# data['cloudcover'],
|
| 71 |
+
# data['visibility'],
|
| 72 |
+
# data['solarradiation'],
|
| 73 |
+
# data['solarenergy'],
|
| 74 |
+
# data['uvindex'],
|
| 75 |
+
# data['conditions']
|
| 76 |
+
#]
|
| 77 |
+
return data
|
| 78 |
|
| 79 |
|
| 80 |
def get_weather_df(data):
|
| 81 |
col_names = [
|
| 82 |
+
'name',
|
| 83 |
+
'datetime',
|
| 84 |
'tempmax',
|
| 85 |
'tempmin',
|
| 86 |
'temp',
|
|
|
|
| 97 |
'windgust',
|
| 98 |
'windspeed',
|
| 99 |
'winddir',
|
| 100 |
+
'sealevelpressure',
|
| 101 |
'cloudcover',
|
| 102 |
'visibility',
|
| 103 |
'solarradiation',
|
|
|
|
| 106 |
'conditions'
|
| 107 |
]
|
| 108 |
|
| 109 |
+
|
| 110 |
+
|
| 111 |
new_data = pd.DataFrame(
|
| 112 |
data,
|
| 113 |
columns=col_names
|
| 114 |
)
|
| 115 |
+
new_data.datetime = new_data.datetime.apply(timestamp_2_time1)
|
| 116 |
+
#new_data.rename(columes={'pressure':'sealevelpressure'})
|
| 117 |
return new_data
|
| 118 |
|
| 119 |
def timestamp_2_time1(x):
|