-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreferences.py
124 lines (105 loc) · 3.08 KB
/
preferences.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
# preferences.py
"""
Container for clothing preferences.
contains all the the user's current preferences
and updates based on agreement with the aws ml
model. When the model is incorrect, store current conditions
in the weather history db with the user's preferred choice
of layers.
"""
import boto3
import math
import json
import weather
import secure_hash
def predict(user, passw, weather_features):
"""
Invoke the aws ml model to offer a
prediction for the number of layers to
be wearing today. Also check if a previously
stored prediction was made. If a prediction was stored,
then use the stored prediction instead of model
prediction
@rType: int
"""
ml_client = boto3.client('machinelearning')
db_client = boto3.client('dynamodb')
userID = secure_hash.total_hash(user,passw)
# check to see if this user has any applicable stored predictions
get_response = db_client.get_item(
TableName='WeatherDecision',
Key={
'userID':{
'S': userID
},
'Temperature':{
'N':str(math.floor(weather_features['raw_temp']))
}
}
)
# perform a prediction using th
weather_conditions = {
'UserID':userID,
'Temperature': str(math.floor(weather_features['raw_temp'])),
'WindSpeed': str(weather_features['max wind speed']),
'Raining': '1' if int(weather_features['chance of rain']) > 50 else '0',
'Snowing': '1' if int(weather_features['snow']) > 1 else '0',
'Cloudy': '1' if weather_features['general forcast'] == 'Cloudy' else '0'
}
# if we got a match, return the previous setting instead
if len(get_response) >= 2:
return int(get_response['Item']['Subconditions']['M']['Layers']['N']), weather_conditions
response = ml_client.predict(
MLModelId='ml-RgLcS6do8rZ',
Record=weather_conditions,
PredictEndpoint='https://realtime.machinelearning.us-east-1.amazonaws.com'
)
num_layers = math.ceil(response['Prediction']['predictedValue'])
return num_layers, weather_conditions
def updatePrediction(user,passw,desired_layers, weather_conditions):
"""
Save the current weather features with the user's desired
number of layers into the dynamodb. Weather conditions is expected
to be on the form which predict() outputs
"""
client = boto3.client('dynamodb')
weather_conditions['Layers'] = desired_layers
response = client.put_item(
TableName='WeatherDecision',
Item={
'userID':{
'S': total_hash(user,passw)
},
'Temperature':{
'N': weather_conditions['Temperature']
},
'Subconditions':{
'M': {
'Layers':{
'N':str(desired_layers)
},
'WindSpeed':{
'N': weather_conditions['WindSpeed']
},
'Raining':{
'N': weather_conditions['Raining']
},
'Snowing':{
'N': weather_conditions['Snowing']
},
'Cloudy':{
'N': weather_conditions['Cloudy']
}
}
}
}
)
return response
def weather_handler(event, context):
user = event['Username']
passw = event['Password']
state = event['State']
city = event['City']
weather_features = weather.getCurrentWeather(state, city)
layers, conditions = predict(user ,passw, weather_features)
return {"Layers":layers, "Weather":conditions}