Spaces:
Runtime error
Runtime error
Commit
•
ad4878b
1
Parent(s):
834fd46
Update functions.py
Browse files- functions.py +41 -188
functions.py
CHANGED
@@ -3,111 +3,16 @@ import requests
|
|
3 |
import os
|
4 |
import joblib
|
5 |
import pandas as pd
|
6 |
-
|
7 |
import json
|
8 |
|
9 |
|
10 |
-
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 = {}
|
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(td_transformation_function.transformation_fn), locals()
|
24 |
-
param_dict = dict([(param.name, param.default) for param in sig.parameters.values() if param.default != inspect._empty])
|
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"])
|
32 |
-
elif td_transformation_function.name == "label_encoder":
|
33 |
-
dictionary = param_dict['value_to_index']
|
34 |
-
dictionary_ = {v: k for k, v in dictionary.items()}
|
35 |
-
df_res[feature_name] = df_res[feature_name].map(
|
36 |
-
lambda x: dictionary_[x])
|
37 |
-
return df_res
|
38 |
-
|
39 |
|
40 |
|
41 |
-
def
|
42 |
-
return requests.get(f'https://
|
43 |
|
44 |
def get_weather_json_quick(date):
|
45 |
-
return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/helsinki/
|
46 |
-
|
47 |
-
|
48 |
-
def get_weather_data(json):
|
49 |
-
|
50 |
-
data = json['days'][0]
|
51 |
-
print("data parsed sccessfully")
|
52 |
-
|
53 |
-
return data
|
54 |
-
|
55 |
-
|
56 |
-
def get_weather_df(data):
|
57 |
-
col_names = [
|
58 |
-
'name',
|
59 |
-
'datetime',
|
60 |
-
'tempmax',
|
61 |
-
'tempmin',
|
62 |
-
'temp',
|
63 |
-
'feelslikemax',
|
64 |
-
'feelslikemin',
|
65 |
-
'feelslike',
|
66 |
-
'dew',
|
67 |
-
'humidity',
|
68 |
-
'precip',
|
69 |
-
'precipprob',
|
70 |
-
'precipcover',
|
71 |
-
'snow',
|
72 |
-
'snowdepth',
|
73 |
-
'windgust',
|
74 |
-
'windspeed',
|
75 |
-
'winddir',
|
76 |
-
'sealevelpressure',
|
77 |
-
'cloudcover',
|
78 |
-
'visibility',
|
79 |
-
'solarradiation',
|
80 |
-
'solarenergy',
|
81 |
-
'uvindex',
|
82 |
-
'conditions'
|
83 |
-
]
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
new_data = pd.DataFrame(
|
88 |
-
data,
|
89 |
-
columns=col_names
|
90 |
-
)
|
91 |
-
new_data.datetime = new_data.datetime.apply(timestamp_2_time1)
|
92 |
-
#new_data.rename(columes={'pressure':'sealevelpressure'})
|
93 |
-
return new_data
|
94 |
-
|
95 |
-
def get_air_quality_data1():
|
96 |
-
|
97 |
-
AIR_QUALITY_API_KEY = os.getenv('AIR_QUALITY_API_KEY')
|
98 |
-
json = get_air_json(AIR_QUALITY_API_KEY)
|
99 |
-
|
100 |
-
print(json)
|
101 |
-
# iaqi = json['iaqi']
|
102 |
-
# forecast = json['forecast']['daily']
|
103 |
-
return [
|
104 |
-
json['date'], # AQI
|
105 |
-
json['pm25'],
|
106 |
-
json['pm10'],
|
107 |
-
json['o3'],
|
108 |
-
json['no2'],
|
109 |
-
|
110 |
-
]
|
111 |
|
112 |
def get_air_quality_data():
|
113 |
AIR_QUALITY_API_KEY = os.getenv('AIR_QUALITY_API_KEY')
|
@@ -135,98 +40,44 @@ def get_air_quality_data():
|
|
135 |
forecast['uvi'][0]['avg']
|
136 |
]
|
137 |
|
138 |
-
def get_air_quality_df1(data):
|
139 |
-
col_names = [
|
140 |
-
'aqi',
|
141 |
-
'date',
|
142 |
-
'pm25',
|
143 |
-
'pm10',
|
144 |
-
'o3',
|
145 |
-
'no2',
|
146 |
-
|
147 |
-
]
|
148 |
-
|
149 |
-
new_data = pd.DataFrame(
|
150 |
-
data,
|
151 |
-
columns=col_names
|
152 |
-
)
|
153 |
-
new_data.date = new_data.date.apply(timestamp_2_time)
|
154 |
-
|
155 |
-
return new_data
|
156 |
-
|
157 |
-
def get_air_quality_df(data):
|
158 |
-
col_names = [
|
159 |
-
'aqi',
|
160 |
-
'date',
|
161 |
-
'iaqi_h',
|
162 |
-
'iaqi_p',
|
163 |
-
'iaqi_pm10',
|
164 |
-
'iaqi_t',
|
165 |
-
'o3_avg',
|
166 |
-
'o3_max',
|
167 |
-
'o3_min',
|
168 |
-
'pm10_avg',
|
169 |
-
'pm10_max',
|
170 |
-
'pm10_min',
|
171 |
-
'pm25_avg',
|
172 |
-
'pm25_max',
|
173 |
-
'pm25_min',
|
174 |
-
'uvi_avg',
|
175 |
-
'uvi_max',
|
176 |
-
'uvi_min',
|
177 |
-
]
|
178 |
-
|
179 |
-
new_data = pd.DataFrame(
|
180 |
-
data,
|
181 |
-
columns=col_names
|
182 |
-
)
|
183 |
-
new_data.date = new_data.date.apply(timestamp_2_time1)
|
184 |
-
|
185 |
-
return new_data
|
186 |
-
|
187 |
|
188 |
-
def
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
def get_weather_data(date):
|
193 |
-
WEATHER_API_KEY = os.getenv('WEATHER_API_KEY')
|
194 |
-
json = get_weather_json(date, WEATHER_API_KEY)
|
195 |
data = json['days'][0]
|
196 |
-
|
197 |
-
return [
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
data['conditions']
|
223 |
-
]
|
224 |
|
225 |
|
226 |
def get_weather_df(data):
|
227 |
col_names = [
|
228 |
-
'
|
229 |
-
'
|
230 |
'tempmax',
|
231 |
'tempmin',
|
232 |
'temp',
|
@@ -243,7 +94,7 @@ def get_weather_df(data):
|
|
243 |
'windgust',
|
244 |
'windspeed',
|
245 |
'winddir',
|
246 |
-
'
|
247 |
'cloudcover',
|
248 |
'visibility',
|
249 |
'solarradiation',
|
@@ -252,12 +103,14 @@ def get_weather_df(data):
|
|
252 |
'conditions'
|
253 |
]
|
254 |
|
|
|
|
|
255 |
new_data = pd.DataFrame(
|
256 |
data,
|
257 |
columns=col_names
|
258 |
)
|
259 |
-
new_data.
|
260 |
-
|
261 |
return new_data
|
262 |
|
263 |
def timestamp_2_time1(x):
|
@@ -268,4 +121,4 @@ def timestamp_2_time1(x):
|
|
268 |
def timestamp_2_time(x):
|
269 |
dt_obj = datetime.strptime(str(x), '%m/%d/%Y')
|
270 |
dt_obj = dt_obj.timestamp() * 1000
|
271 |
-
return int(dt_obj)
|
|
|
3 |
import os
|
4 |
import joblib
|
5 |
import pandas as pd
|
|
|
6 |
import json
|
7 |
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
|
11 |
+
def get_weather_csv():
|
12 |
+
return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/helsinki?unitGroup=metric&include=days&key=FYYH5HKD9558HBXD2D6KWXDGH&contentType=csv').csv()
|
13 |
|
14 |
def get_weather_json_quick(date):
|
15 |
+
return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/helsinki/today?include=fcst%2Cobs%2Chistfcst%2Cstats%2Cdays&key=J7TT2WGMUNNHD8JBEDXAJJXB2&contentType=json').json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
def get_air_quality_data():
|
18 |
AIR_QUALITY_API_KEY = os.getenv('AIR_QUALITY_API_KEY')
|
|
|
40 |
forecast['uvi'][0]['avg']
|
41 |
]
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
def get_weather_data(json):
|
45 |
+
#WEATHER_API_KEY = os.getenv('WEATHER_API_KEY')
|
46 |
+
|
47 |
+
#csv = get_weather_csv()
|
|
|
|
|
|
|
48 |
data = json['days'][0]
|
49 |
+
print("data parsed sccessfully")
|
50 |
+
#return [
|
51 |
+
# #json['address'].capitalize(),
|
52 |
+
# data['datetime'],
|
53 |
+
# data['feelslikemax'],
|
54 |
+
# data['feelslikemin'],
|
55 |
+
# data['feelslike'],
|
56 |
+
# data['dew'],
|
57 |
+
# data['humidity'],
|
58 |
+
# data['precip'],
|
59 |
+
# data['precipprob'],
|
60 |
+
# data['precipcover'],
|
61 |
+
# data['snow'],
|
62 |
+
# data['snowdepth'],
|
63 |
+
# data['windgust'],
|
64 |
+
# data['windspeed'],
|
65 |
+
# data['winddir'],
|
66 |
+
# data['pressure'],
|
67 |
+
# data['cloudcover'],
|
68 |
+
# data['visibility'],
|
69 |
+
# data['solarradiation'],
|
70 |
+
# data['solarenergy'],
|
71 |
+
# data['uvindex'],
|
72 |
+
# data['conditions']
|
73 |
+
#]
|
74 |
+
return data
|
|
|
|
|
75 |
|
76 |
|
77 |
def get_weather_df(data):
|
78 |
col_names = [
|
79 |
+
'name',
|
80 |
+
'datetime',
|
81 |
'tempmax',
|
82 |
'tempmin',
|
83 |
'temp',
|
|
|
94 |
'windgust',
|
95 |
'windspeed',
|
96 |
'winddir',
|
97 |
+
'sealevelpressure',
|
98 |
'cloudcover',
|
99 |
'visibility',
|
100 |
'solarradiation',
|
|
|
103 |
'conditions'
|
104 |
]
|
105 |
|
106 |
+
|
107 |
+
|
108 |
new_data = pd.DataFrame(
|
109 |
data,
|
110 |
columns=col_names
|
111 |
)
|
112 |
+
new_data.datetime = new_data.datetime.apply(timestamp_2_time1)
|
113 |
+
#new_data.rename(columes={'pressure':'sealevelpressure'})
|
114 |
return new_data
|
115 |
|
116 |
def timestamp_2_time1(x):
|
|
|
121 |
def timestamp_2_time(x):
|
122 |
dt_obj = datetime.strptime(str(x), '%m/%d/%Y')
|
123 |
dt_obj = dt_obj.timestamp() * 1000
|
124 |
+
return int(dt_obj)
|