Spaces:
Build error
Build error
Update functions.py
Browse files- functions.py +46 -95
functions.py
CHANGED
@@ -1,89 +1,54 @@
|
|
|
|
1 |
import requests
|
2 |
import os
|
3 |
import joblib
|
4 |
import pandas as pd
|
5 |
-
import datetime
|
6 |
-
import numpy as np
|
7 |
-
import time
|
8 |
-
from sklearn.preprocessing import OrdinalEncoder
|
9 |
-
from dotenv import load_dotenv
|
10 |
-
load_dotenv(override=True)
|
11 |
-
|
12 |
-
|
13 |
-
def decode_features(df, feature_view):
|
14 |
-
"""Decodes features in the input DataFrame using corresponding Hopsworks Feature Store transformation functions"""
|
15 |
-
df_res = df.copy()
|
16 |
-
|
17 |
-
import inspect
|
18 |
-
|
19 |
-
|
20 |
-
td_transformation_functions = feature_view._batch_scoring_server._transformation_functions
|
21 |
-
|
22 |
-
res = {}
|
23 |
-
for feature_name in td_transformation_functions:
|
24 |
-
if feature_name in df_res.columns:
|
25 |
-
td_transformation_function = td_transformation_functions[feature_name]
|
26 |
-
sig, foobar_locals = inspect.signature(td_transformation_function.transformation_fn), locals()
|
27 |
-
param_dict = dict([(param.name, param.default) for param in sig.parameters.values() if param.default != inspect._empty])
|
28 |
-
if td_transformation_function.name == "min_max_scaler":
|
29 |
-
df_res[feature_name] = df_res[feature_name].map(
|
30 |
-
lambda x: x * (param_dict["max_value"] - param_dict["min_value"]) + param_dict["min_value"])
|
31 |
-
|
32 |
-
elif td_transformation_function.name == "standard_scaler":
|
33 |
-
df_res[feature_name] = df_res[feature_name].map(
|
34 |
-
lambda x: x * param_dict['std_dev'] + param_dict["mean"])
|
35 |
-
elif td_transformation_function.name == "label_encoder":
|
36 |
-
dictionary = param_dict['value_to_index']
|
37 |
-
dictionary_ = {v: k for k, v in dictionary.items()}
|
38 |
-
df_res[feature_name] = df_res[feature_name].map(
|
39 |
-
lambda x: dictionary_[x])
|
40 |
-
return df_res
|
41 |
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
|
82 |
-
return df_weather
|
83 |
|
84 |
def get_weather_df(data):
|
85 |
col_names = [
|
86 |
-
'
|
87 |
'date',
|
88 |
'tempmax',
|
89 |
'tempmin',
|
@@ -111,28 +76,14 @@ def get_weather_df(data):
|
|
111 |
]
|
112 |
|
113 |
new_data = pd.DataFrame(
|
114 |
-
data
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
if col not in ['name', 'date', 'conditions']:
|
119 |
-
new_data[col] = pd.to_numeric(new_data[col])
|
120 |
|
121 |
return new_data
|
122 |
|
123 |
-
|
124 |
-
|
125 |
-
def get_aplevel(temps:np.ndarray) -> list:
|
126 |
-
boundary_list = np.array([0, 50, 100, 150, 200, 300]) # assert temps.shape == [x, 1]
|
127 |
-
redf = np.logical_not(temps<=boundary_list) # temps.shape[0] x boundary_list.shape[0] ndarray
|
128 |
-
hift = np.concatenate((np.roll(redf, -1)[:, :-1], np.full((temps.shape[0], 1), False)), axis = 1)
|
129 |
-
cat = np.nonzero(np.not_equal(redf,hift))
|
130 |
-
|
131 |
-
air_pollution_level = ['Good', 'Moderate', 'Unhealthy for sensitive Groups','Unhealthy' ,'Very Unhealthy', 'Hazardous']
|
132 |
-
level = [air_pollution_level[el] for el in cat[1]]
|
133 |
-
return level
|
134 |
-
|
135 |
def timestamp_2_time(x):
|
136 |
-
dt_obj = datetime.
|
137 |
dt_obj = dt_obj.timestamp() * 1000
|
138 |
return int(dt_obj)
|
|
|
1 |
+
from datetime import datetime
|
2 |
import requests
|
3 |
import os
|
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 [
|
21 |
+
json['address'].capitalize(),
|
22 |
+
data['datetime'],
|
23 |
+
data['tempmax'],
|
24 |
+
data['tempmin'],
|
25 |
+
data['temp'],
|
26 |
+
data['feelslikemax'],
|
27 |
+
data['feelslikemin'],
|
28 |
+
data['feelslike'],
|
29 |
+
data['dew'],
|
30 |
+
data['humidity'],
|
31 |
+
data['precip'],
|
32 |
+
data['precipprob'],
|
33 |
+
data['precipcover'],
|
34 |
+
data['snow'],
|
35 |
+
data['snowdepth'],
|
36 |
+
data['windgust'],
|
37 |
+
data['windspeed'],
|
38 |
+
data['winddir'],
|
39 |
+
data['pressure'],
|
40 |
+
data['cloudcover'],
|
41 |
+
data['visibility'],
|
42 |
+
data['solarradiation'],
|
43 |
+
data['solarenergy'],
|
44 |
+
data['uvindex'],
|
45 |
+
data['conditions']
|
46 |
+
]
|
47 |
|
|
|
48 |
|
49 |
def get_weather_df(data):
|
50 |
col_names = [
|
51 |
+
'city',
|
52 |
'date',
|
53 |
'tempmax',
|
54 |
'tempmin',
|
|
|
76 |
]
|
77 |
|
78 |
new_data = pd.DataFrame(
|
79 |
+
data,
|
80 |
+
columns=col_names
|
81 |
+
)
|
82 |
+
new_data.date = new_data.date.apply(timestamp_2_time)
|
|
|
|
|
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)
|