/
predict.py
252 lines (220 loc) · 9.35 KB
/
predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import update
import pickle
import numpy as np
import pandas as pd
import logging
import requests
import json
from datetime import timedelta
from datetime import timezone
import config
def download_file(url) :
response = requests.get(url)
with open(f'/root/{url.split("/")[-1]}', "wb") as file:
file.write(response.content)
def feature_extraction(timestep, previous):
'''
PURPOSE: Calculate the features for a machine learning model within the context of hurricane-net
METHOD: Use the predictors and the calculation methodology defined in Knaff 2013
INPUT: timestep - current dictionary of features in the hurricane object format
previous - previous timestep dictionary of features in the hurricane object format
OUTPUT: Dictionary of features
timestep = {
'lat' : float,
'long' : float,
'max-wind' : float,
'entry-time' : datetime
}
'''
features = {
'lat': timestep['lat'],
'long': timestep['lon'],
'max_wind': timestep['wind'],
'delta_wind': (timestep['wind'] - previous[
'wind']) / # Calculated from track (12h)
((timestep['time'] - previous[
'time']).total_seconds() / 43200),
'min_pressure': timestep['pressure'],
'zonal_speed': (timestep['lat'] - previous[
'lat']) / # Calculated from track (per hour)
((timestep['time'] - previous[
'time']).total_seconds() / 3600),
'meridonal_speed': (timestep['lon'] - previous[
'lon']) / # Calculated from track (per hour)
((timestep['time'] - previous[
'time']).total_seconds() / 3600),
'year': timestep['time'].year,
'month': timestep['time'].month,
'day': timestep['time'].day,
'hour': timestep['time'].hour,
}
return features
def predict_json(project, model, instances, version=None):
"""Send json data to a deployed model for prediction.
Args:
project (str): project where the Cloud ML Engine Model is deployed.
model (str): model name.
instances ([Mapping[str: Any]]): Keys should be the names of Tensors
your deployed model expects as inputs. Values should be datatypes
convertible to Tensors, or (potentially nested) lists of datatypes
convertible to tensors.
version: str, version of the model to target.
Returns:
Mapping[str: any]: dictionary of prediction results defined by the
model.
"""
'''
# Create the ML Engine service object.
# To authenticate set the environment variable
# GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account_file>
service = googleapiclient.discovery.build('ml', 'v1')
name = 'projects/{}/models/{}'.format(project, model)
if version is not None:
name += '/versions/{}'.format(version)
response = service.projects().predict(
name=name,
body={'instances': instances}
).execute()
if 'error' in response:
raise RuntimeError(response['error'])
return response
'''
# make request to hurricane ai
headers = {"content-type": "application/json"}
data = json.dumps({"instances" : instances})
json_response = requests.post(f'http://localhost:9000/v1/models/{model}:predict',
data = data,
headers = headers)
print(json_response.text)
# return results
return json.loads(json_response.text)["predictions"]
def predict_universal(data = None) :
# get the update
if data :
raw = data
else :
raw = update.nhc()
# read in the scaler
download_file(config.feature_scaler_path)
with open('/root/feature_scaler.pkl', 'rb') as f :
scaler = pickle.load(f)
# generate predictions
results = []
for storm in raw :
print(f'Processing {storm["id"]}. . . ')
# create prescale data structure
df = pd.DataFrame(storm['entries']).sort_values('time', ascending = False)
# set reference time and geometric pattern recognition
reference = df['time'].max().replace(tzinfo = timezone.utc)
reference_count = 0
print(f"Reference time is: {reference}")
while reference.hour not in [0, 6, 12, 18] : # not a regular timezone
reference_count += 1
reference = df.iloc[reference_count]['time']
print(f"Reference time is: {reference}")
input = df[df['time'].isin(
[reference - timedelta(hours = delta)
for delta in [0, 6, 12, 18, 24, 30]])
].sort_values('time', ascending = False).reindex()
# flag for if input is not long enough
if (len(input) < 6) :
logging.warning(
f"{storm['id']}"
f" does not have enough data, does not follow the input"
f" pattern for the AI, or an unknown error. Skipping.")
results.append({'error': f'{storm["id"]} did not have enough records'})
continue
# generate input
input = [list(feature_extraction(input.iloc[i + 1], input.iloc[i]).values())
for i in range(5)]
# scale our input
input = np.expand_dims(scaler.transform(input), axis = 0)
# get our prediction
prediction_json = predict_json('cyclone-ai', 'hurricane', input.tolist())
prediction = prediction_json[0]
# inverse transform the prediction
lat = [output[0] for output in scaler.inverse_transform(
[[lat[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for lat in
prediction])]
lon = [output[1] for output in scaler.inverse_transform(
[[0, lon[0], 0, 0, 0, 0, 0, 0, 0, 0, 0] for lon in
prediction])]
wind = [output[2] for output in scaler.inverse_transform(
[[0, 0, wind[0], 0, 0, 0, 0, 0, 0, 0, 0] for wind in
prediction])]
output = dict()
for index, value in enumerate([12, 18, 24, 30, 36]):
output[reference + timedelta(hours = value)] = {
'lat': lat[index],
'lon': lon[index],
'max_wind(mph)': wind[index] * 1.15078
}
output['id'] = storm['id']
results.append(output)
print(f'Done with {storm["id"]}, results:\n{output}')
return results
def predict_singular(data = None) :
# get the update
if data :
raw = data
else :
raw = update.nhc()
# read in the scaler
download_file('model_artifacts/universal/feature_scaler.pkl')
with open('/root/feature_scaler.pkl', 'rb') as f :
scaler = pickle.load(f)
# generate predictions
results = []
for storm in raw:
print(f'Processing {storm["id"]}. . . ')
# create prescale data structure
df = pd.DataFrame(storm['entries']).sort_values('time', ascending=False)
# set reference time and geometric pattern recognition
reference = df['time'].max().replace(tzinfo=timezone.utc)
reference_count = 0
print(f"Reference time is: {reference}")
while reference.hour not in [0, 6, 12, 18]: # not a regular timezone
reference_count += 1
reference = df.iloc[reference_count]['time']
print(f"Reference time is: {reference}")
input = df[df['time'].isin(
[reference - timedelta(hours=delta)
for delta in [0, 24, 48, 72, 96, 120]])
].sort_values('time', ascending=False).reindex()
# if input is not long enough
if (len(input) < 6):
logging.warning(
f"{storm['id']}"
f" does not have enough data, does not follow the input"
f" pattern for the AI, or an unknown error. Skipping.")
continue
input = [list(feature_extraction(input.iloc[i + 1], input.iloc[i]).values())
for i in range(5)]
# scale our input
input = np.expand_dims(scaler.transform(input), axis=0)
# get our prediction
prediction = predict_json(
'cyclone-ai', 'universal', input.tolist())[
"predictions"][0]["time_distributed"]
print(prediction)
# inverse transform the prediction
lat = [output[0] for output in scaler.inverse_transform(
[[lat[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] for lat in
prediction])]
lon = [output[1] for output in scaler.inverse_transform(
[[0, lon[0], 0, 0, 0, 0, 0, 0, 0, 0, 0] for lon in
prediction])]
wind = [output[2] for output in scaler.inverse_transform(
[[0, 0, wind[0], 0, 0, 0, 0, 0, 0, 0, 0] for wind in
prediction])]
output = dict()
for index, value in enumerate([24, 48, 72, 96, 120]):
output[reference + timedelta(hours=value)] = {
'lat': lat[index],
'long': lon[index],
'max_wind(mph)': wind[index] * 1.15078
}
output['id'] = storm['id']
results.append(output)
print(f'Done with {storm["id"]}, results:\n{output}')
return results