Spaces:
Runtime error
Runtime error
Update functions.py
Browse files- functions.py +86 -13
functions.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
-
|
| 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 |
|
| 13 |
def decode_features(df, feature_view):
|
|
@@ -89,9 +89,9 @@ def get_air_quality_data1():
|
|
| 89 |
def get_air_quality_data():
|
| 90 |
AIR_QUALITY_API_KEY = os.getenv('AIR_QUALITY_API_KEY')
|
| 91 |
json = get_air_json(AIR_QUALITY_API_KEY)
|
| 92 |
-
print(json)
|
| 93 |
iaqi = json['iaqi']
|
| 94 |
-
|
| 95 |
return [
|
| 96 |
json['aqi'], # AQI
|
| 97 |
json['time']['s'][:10], # Date
|
|
@@ -128,7 +128,7 @@ def get_air_quality_df1(data):
|
|
| 128 |
data,
|
| 129 |
columns=col_names
|
| 130 |
)
|
| 131 |
-
new_data.date = new_data.date.apply(
|
| 132 |
|
| 133 |
return new_data
|
| 134 |
|
|
@@ -170,13 +170,13 @@ def get_weather_data_weekly(start_date: datetime) -> pd.DataFrame:
|
|
| 170 |
WEATHER_API_KEY = os.getenv('WEATHER_API_KEY')
|
| 171 |
end_date = f"{start_date + datetime.timedelta(days=6):%Y-%m-%d}"
|
| 172 |
json = requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/Helsinki/{start_date}/{end_date}?unitGroup=metric&include=days&key={WEATHER_API_KEY}&contentType=json').json()
|
| 173 |
-
weather_data =
|
| 174 |
final_df = pd.DataFrame()
|
| 175 |
|
| 176 |
for i in range(7):
|
| 177 |
data = weather_data[i]
|
| 178 |
list_of_data = [
|
| 179 |
-
json['address'].
|
| 180 |
data['datetime'],
|
| 181 |
data['tempmax'],
|
| 182 |
data['tempmin'],
|
|
@@ -202,7 +202,7 @@ def get_weather_data_weekly(start_date: datetime) -> pd.DataFrame:
|
|
| 202 |
data['uvindex'],
|
| 203 |
data['conditions']
|
| 204 |
]
|
| 205 |
-
weather_df =
|
| 206 |
final_df = pd.concat([final_df, weather_df])
|
| 207 |
return final_df
|
| 208 |
|
|
@@ -210,6 +210,7 @@ def get_weather_data(date):
|
|
| 210 |
WEATHER_API_KEY = os.getenv('WEATHER_API_KEY')
|
| 211 |
json = get_weather_json(date, WEATHER_API_KEY)
|
| 212 |
data = json['days'][0]
|
|
|
|
| 213 |
|
| 214 |
return [
|
| 215 |
json['address'].capitalize(),
|
|
@@ -240,6 +241,45 @@ def get_weather_data(date):
|
|
| 240 |
]
|
| 241 |
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
def get_weather_df(data):
|
| 244 |
col_names = [
|
| 245 |
'city',
|
|
@@ -273,16 +313,49 @@ def get_weather_df(data):
|
|
| 273 |
data,
|
| 274 |
columns=col_names
|
| 275 |
)
|
| 276 |
-
new_data.date = new_data.date.apply(
|
| 277 |
|
| 278 |
return new_data
|
| 279 |
|
| 280 |
def timestamp_2_time1(x):
|
| 281 |
-
dt_obj = datetime.strptime(str(x), '%Y-%m-%d')
|
| 282 |
dt_obj = dt_obj.timestamp() * 1000
|
| 283 |
return int(dt_obj)
|
| 284 |
|
| 285 |
def timestamp_2_time(x):
|
| 286 |
-
dt_obj = datetime.strptime(str(x), '%m
|
| 287 |
dt_obj = dt_obj.timestamp() * 1000
|
| 288 |
return int(dt_obj)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import datetime
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
import joblib
|
| 5 |
import pandas as pd
|
|
|
|
| 6 |
import json
|
| 7 |
+
import numpy as np
|
| 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):
|
|
|
|
| 89 |
def get_air_quality_data():
|
| 90 |
AIR_QUALITY_API_KEY = os.getenv('AIR_QUALITY_API_KEY')
|
| 91 |
json = get_air_json(AIR_QUALITY_API_KEY)
|
| 92 |
+
#print(json)
|
| 93 |
iaqi = json['iaqi']
|
| 94 |
+
forecast = json['forecast']['daily']
|
| 95 |
return [
|
| 96 |
json['aqi'], # AQI
|
| 97 |
json['time']['s'][:10], # Date
|
|
|
|
| 128 |
data,
|
| 129 |
columns=col_names
|
| 130 |
)
|
| 131 |
+
new_data.date = new_data.date.apply(timestamp_2_time)
|
| 132 |
|
| 133 |
return new_data
|
| 134 |
|
|
|
|
| 170 |
WEATHER_API_KEY = os.getenv('WEATHER_API_KEY')
|
| 171 |
end_date = f"{start_date + datetime.timedelta(days=6):%Y-%m-%d}"
|
| 172 |
json = requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/Helsinki/{start_date}/{end_date}?unitGroup=metric&include=days&key={WEATHER_API_KEY}&contentType=json').json()
|
| 173 |
+
weather_data = json['days']
|
| 174 |
final_df = pd.DataFrame()
|
| 175 |
|
| 176 |
for i in range(7):
|
| 177 |
data = weather_data[i]
|
| 178 |
list_of_data = [
|
| 179 |
+
#json['address'].lower(),
|
| 180 |
data['datetime'],
|
| 181 |
data['tempmax'],
|
| 182 |
data['tempmin'],
|
|
|
|
| 202 |
data['uvindex'],
|
| 203 |
data['conditions']
|
| 204 |
]
|
| 205 |
+
weather_df = get_weather_df_weekly(list_of_data)
|
| 206 |
final_df = pd.concat([final_df, weather_df])
|
| 207 |
return final_df
|
| 208 |
|
|
|
|
| 210 |
WEATHER_API_KEY = os.getenv('WEATHER_API_KEY')
|
| 211 |
json = get_weather_json(date, WEATHER_API_KEY)
|
| 212 |
data = json['days'][0]
|
| 213 |
+
#print(data)
|
| 214 |
|
| 215 |
return [
|
| 216 |
json['address'].capitalize(),
|
|
|
|
| 241 |
]
|
| 242 |
|
| 243 |
|
| 244 |
+
def get_weather_df_weekly(data):
|
| 245 |
+
col_names = [
|
| 246 |
+
#'city',
|
| 247 |
+
'date',
|
| 248 |
+
'tempmax',
|
| 249 |
+
'tempmin',
|
| 250 |
+
'temp',
|
| 251 |
+
'feelslikemax',
|
| 252 |
+
'feelslikemin',
|
| 253 |
+
'feelslike',
|
| 254 |
+
'dew',
|
| 255 |
+
'humidity',
|
| 256 |
+
'precip',
|
| 257 |
+
'precipprob',
|
| 258 |
+
'precipcover',
|
| 259 |
+
'snow',
|
| 260 |
+
'snowdepth',
|
| 261 |
+
'windgust',
|
| 262 |
+
'windspeed',
|
| 263 |
+
'winddir',
|
| 264 |
+
'pressure',
|
| 265 |
+
'cloudcover',
|
| 266 |
+
'visibility',
|
| 267 |
+
'solarradiation',
|
| 268 |
+
'solarenergy',
|
| 269 |
+
'uvindex',
|
| 270 |
+
'conditions'
|
| 271 |
+
]
|
| 272 |
+
|
| 273 |
+
new_data = pd.DataFrame(
|
| 274 |
+
data
|
| 275 |
+
).T
|
| 276 |
+
new_data.columns = col_names
|
| 277 |
+
for col in col_names:
|
| 278 |
+
if col not in ['name', 'date', 'conditions']:
|
| 279 |
+
new_data[col] = pd.to_numeric(new_data[col])
|
| 280 |
+
|
| 281 |
+
return new_data
|
| 282 |
+
|
| 283 |
def get_weather_df(data):
|
| 284 |
col_names = [
|
| 285 |
'city',
|
|
|
|
| 313 |
data,
|
| 314 |
columns=col_names
|
| 315 |
)
|
| 316 |
+
new_data.date = new_data.date.apply(timestamp_2_time)
|
| 317 |
|
| 318 |
return new_data
|
| 319 |
|
| 320 |
def timestamp_2_time1(x):
|
| 321 |
+
dt_obj = datetime.datetime.strptime(str(x), '%Y-%m-%d')
|
| 322 |
dt_obj = dt_obj.timestamp() * 1000
|
| 323 |
return int(dt_obj)
|
| 324 |
|
| 325 |
def timestamp_2_time(x):
|
| 326 |
+
dt_obj = datetime.datetime.strptime(str(x), '%Y-%m-%d')
|
| 327 |
dt_obj = dt_obj.timestamp() * 1000
|
| 328 |
return int(dt_obj)
|
| 329 |
+
|
| 330 |
+
def data_encoder(X):
|
| 331 |
+
#print(X)
|
| 332 |
+
X.drop(columns=['date'], inplace=True)
|
| 333 |
+
X['conditions'] = OrdinalEncoder().fit_transform(X[['conditions']])
|
| 334 |
+
return X
|
| 335 |
+
|
| 336 |
+
def transform(df):
|
| 337 |
+
df.loc[df["windgust"].isna(),'windgust'] = df['windspeed']
|
| 338 |
+
df['snow'].fillna(0,inplace=True)
|
| 339 |
+
df['snowdepth'].fillna(0, inplace=True)
|
| 340 |
+
df['pressure'].fillna(df['pressure'].mean(), inplace=True)
|
| 341 |
+
return df
|
| 342 |
+
|
| 343 |
+
|
| 344 |
+
def get_aplevel(temps:np.ndarray) -> list:
|
| 345 |
+
boundary_list = np.array([0, 50, 100, 150, 200, 300]) # assert temps.shape == [x, 1]
|
| 346 |
+
redf = np.logical_not(temps<=boundary_list) # temps.shape[0] x boundary_list.shape[0] ndarray
|
| 347 |
+
hift = np.concatenate((np.roll(redf, -1)[:, :-1], np.full((temps.shape[0], 1), False)), axis = 1)
|
| 348 |
+
cat = np.nonzero(np.not_equal(redf,hift))
|
| 349 |
+
|
| 350 |
+
air_pollution_level = ['Good', 'Moderate', 'Unhealthy for sensitive Groups','Unhealthy' ,'Very Unhealthy', 'Hazardous']
|
| 351 |
+
level = [air_pollution_level[el] for el in cat[1]]
|
| 352 |
+
return level
|
| 353 |
+
|
| 354 |
+
def encoder_range(temps):
|
| 355 |
+
boundary_list = np.array([0, 50, 100, 150, 200, 300])
|
| 356 |
+
redf = np.logical_not(temps<=boundary_list)
|
| 357 |
+
hift = np.concatenate((np.roll(redf, -1)[:, :-1], np.full((temps.shape[0], 1), False)), axis = 1)
|
| 358 |
+
cat = np.nonzero(np.not_equal(redf,hift))
|
| 359 |
+
air_pollution_level = ['Good', 'Moderate', 'Unhealthy for sensitive Groups','Unhealthy' ,'Very Unhealthy', 'Hazardous']
|
| 360 |
+
level = [air_pollution_level[el] for el in cat[1]]
|
| 361 |
+
return level
|