-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
305 lines (276 loc) · 13.6 KB
/
Copy pathmain.py
File metadata and controls
305 lines (276 loc) · 13.6 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""The main module with all API definitions of the Feature-Engineering service"""
from fastapi import FastAPI, Body, HTTPException
import dataclasses
import pandas
import json
from src import features, schema, normalization
app = FastAPI()
class JSONEncoder(json.JSONEncoder):
"""An enhanced version of the JSONEncoder class containing support for dataclasses and DataFrames."""
def default(self, o):
"""Adds JSON encoding support for dataclasses and DataFrames.
This function overrides the default function of JSONEncoder and adds support for encoding dataclasses and
Pandas DataFrames as JSON. Uses the superclass default function for all other types.
Args:
o: The object to serialize into a JSON representation.
Returns:
The JSON representation of the specified object.
"""
if dataclasses.is_dataclass(o):
return dataclasses.asdict(o)
if isinstance(o, pandas.DataFrame):
return o.to_json()
return super().default(o)
@app.get(
"/",
name="Root path",
summary="Returns the routes available through the API",
description="Returns a route list for easier use of API through HATEOAS",
response_description="List of urls to all available routes",
responses={
200: {
"content": {
"application/json": {
"example": {
"payload": [
{
"path": "/examplePath",
"name": "example route"
}
]
}
}
},
}
}
)
async def root():
"""Root API endpoint that lists all available API endpoints.
Returns:
A complete list of all possible API endpoints.
"""
route_filter = ["openapi", "swagger_ui_html", "swagger_ui_redirect", "redoc_html"]
url_list = [{"path": route.path, "name": route.name} for route in app.routes if route.name not in route_filter]
return url_list
@app.post(
"/diff",
name="Creates diff values for all sensors that measure consumption units",
summary="Endpoint for the creation of differential values for all sensors that measure consumption units",
description="Add news columns to the building-dicts dataframe with diff values",
response_description="A JSON representation of the supplied building objects with the added diff columns",
responses={
200: {
"description": "JSON representation of the supplied building objects.",
"content": {
"application/json": {
"example":
"{\"EF 40a_small\": {"
"\"name\": \"EF 40a_small\", "
"\"sensors\": [{\"type\": \"Elektrizit\ät\", \"desc\": \"P Summe\", \"unit\": \"kW\"}], "
"\"dataframe\": \"{"
"\\\"Elektrizit\\\ät\\\":{"
"\\\"1642809600000\\\":4.658038,\\\"1642810500000\\\":4.426662,"
"\\\"1642811400000\\\":4.195286,\\\"1642812300000\\\":4.1157735,"
"\\\"1642813200000\\\":4.036261,\\\"1642814100000\\\":4.0861445,"
"\\\"1642815000000\\\":4.136028,\\\"1642815900000\\\":4.1354125,"
"\\\"1642816800000\\\":4.134797,\\\"1642817700000\\\":4.1507635,"
"\\\"1642818600000\\\":4.16673,\\\"1642819500000\\\":4.1331225,"
"\\\"1642820400000\\\":4.099515,\\\"1642821300000\\\":4.108903,"
"\\\"1642822200000\\\":4.118291,\\\"1642823100000\\\":4.366549,"
"\\\"1642824000000\\\":4.614807,\\\"1642824900000\\\":4.5520855,"
"\\\"1642825800000\\\":4.489364,\\\"1642826700000\\\":4.5500675,"
"\\\"1642827600000\\\":4.610771,\\\"1642828500000\\\":4.618229,"
"\\\"1642829400000\\\":4.625687},"
"\\\"Elektrizit\\\ät Diff\\\":{\\\"1642809600000\\\":null,\\\"1642810500000\\\":-0.231376,"
"\\\"1642811400000\\\":-0.231376,\\\"1642812300000\\\":-0.0795125,\\\"1642813200000\\\":-0.0795125,"
"\\\"1642814100000\\\":0.0498835,\\\"1642815000000\\\":0.0498835,\\\"1642815900000\\\":-0.0006155,"
"\\\"1642816800000\\\":-0.0006155,\\\"1642817700000\\\":0.0159665,\\\"1642818600000\\\":0.0159665,"
"\\\"1642819500000\\\":-0.0336075,\\\"1642820400000\\\":-0.0336075,\\\"1642821300000\\\":0.009388,"
"\\\"1642822200000\\\":0.009388,\\\"1642823100000\\\":0.248258,\\\"1642824000000\\\":0.248258,"
"\\\"1642824900000\\\":-0.0627215,\\\"1642825800000\\\":-0.0627215,\\\"1642826700000\\\":0.0607035,"
"\\\"1642827600000\\\":0.0607035,\\\"1642828500000\\\":0.007458,\\\"1642829400000\\\":0.007458"
"}"
"}\""
"}}"
}
},
},
400: {
"description": "Payload can not be empty.",
"content": {
"application/json": {
"example": {"detail": "Payload can not be empty"}
}
},
},
500: {
"description": "Internal server error.",
"content": {
"application/json": {
"example": {"detail": "Internal server error"}
}
},
}
},
tags=["Features"]
)
def create_diff(payload: str = Body(..., embed=True)):
"""API endpoint for the creation of differential values for all sensors that measure consumption units.
Args:
payload: The JSON representation of building objects.
Returns:
The JSON representation of the updated buildings.
"""
try:
if not payload:
raise HTTPException(status_code=400, detail="Payload can not be empty")
buildings = features.json_to_buildings(json.loads(payload))
features.add_diff_cols_for_consumption_units(buildings)
return json.dumps(buildings, cls=JSONEncoder)
except HTTPException:
raise
except Exception:
raise HTTPException(status_code=500, detail="Internal Server Error")
@app.post(
"/normalize/minmax",
name="Min-Max-Normalization",
summary="Normalizes all columns in the building-dicts dataframe using Min-Max-Normalization",
description="Normalizes all available columns in the building-dicts dataframe using min-max-normalization",
response_description="A JSON representation of the supplied building object",
responses={
200: {
"description": "JSON representation of the supplied building objects.",
"content": {
"application/json": {
"example":
"{\"EF 40a_small\": {"
"\"name\": \"EF 40a_small\", "
"\"sensors\": [{\"type\": \"Elektrizit\ät\", \"desc\": \"P Summe\", \"unit\": \"kW\"}], "
"\"dataframe\": \"{"
"\\\"Elektrizit\\\ät\\\":{"
"\\\"1642809600000\\\":1.0,\\\"1642810500000\\\":0.6278794487,"
"\\\"1642811400000\\\":0.2557588975,\\\"1642812300000\\\":0.1278794487,"
"\\\"1642813200000\\\":0.0,\\\"1642814100000\\\":0.0802273162,"
"\\\"1642815000000\\\":0.1604546324,\\\"1642815900000\\\":0.1594647277,"
"\\\"1642816800000\\\":0.158474823,\\\"1642817700000\\\":0.1841536435,"
"\\\"1642818600000\\\":0.2098324641,\\\"1642819500000\\\":0.1557817353,"
"\\\"1642820400000\\\":0.1017310065,\\\"1642821300000\\\":0.1168296672,"
"\\\"1642822200000\\\":0.131928328,\\\"1642823100000\\\":0.5312000926,"
"\\\"1642824000000\\\":0.9304718573,\\\"1642824900000\\\":0.8295972672,"
"\\\"1642825800000\\\":0.7287226771,\\\"1642826700000\\\":0.8263517306,"
"\\\"1642827600000\\\":0.9239807841,\\\"1642828500000\\\":0.9359754381,"
"\\\"1642829400000\\\":0.9479700922"
"}"
"}\""
"}}"
}
},
},
400: {
"description": "Payload can not be empty.",
"content": {
"application/json": {
"example": {"detail": "Payload can not be empty"}
}
},
},
500: {
"description": "Internal server error.",
"content": {
"application/json": {
"example": {"detail": "Internal server error"}
}
},
}
},
tags=["Normalization"]
)
def normalize_minmax(payload: str = Body(..., embed=True)):
"""API endpoint for normalizing all data in a building objects dataframe to a range from 0 to 1.
Args:
payload: The JSON representation of building objects.
Returns:
The JSON representation of the updated buildings.
"""
try:
if not payload:
raise HTTPException(status_code=400, detail="Payload can not be empty")
buildings = features.json_to_buildings(json.loads(payload))
normalization.min_max_normalization(buildings)
return json.dumps(buildings, cls=JSONEncoder)
except HTTPException:
raise
except Exception:
raise HTTPException(status_code=500, detail="Internal Server Error")
@app.post(
"/normalize/mean",
name="Mean-Normalization",
summary="Normalizes all columns in the building-dicts dataframe using Mean-Normalization",
description="Normalizes all available columns in the building-dicts dataframe using mean-normalization",
response_description="A JSON representation of the supplied building object",
responses={
200: {
"description": "JSON representation of the supplied building objects.",
"content": {
"application/json": {
"example":
"{\"EF 40a_small\": {"
"\"name\": \"EF 40a_small\", "
"\"sensors\": [{\"type\": \"Elektrizit\ät\", \"desc\": \"P Summe\", \"unit\": \"kW\"}], "
"\"dataframe\": \"{"
"\\\"Elektrizit\\\ät\\\":{"
"\\\"1642809600000\\\":1.5355268051,\\\"1642810500000\\\":0.5147979489,"
"\\\"1642811400000\\\":-0.5059309073,\\\"1642812300000\\\":-0.8567049857,"
"\\\"1642813200000\\\":-1.2074790642,\\\"1642814100000\\\":-0.9874150649,"
"\\\"1642815000000\\\":-0.7673510656,\\\"1642815900000\\\":-0.7700663801,"
"\\\"1642816800000\\\":-0.7727816947,\\\"1642817700000\\\":-0.7023445392,"
"\\\"1642818600000\\\":-0.6319073837,\\\"1642819500000\\\":-0.7801688501,"
"\\\"1642820400000\\\":-0.9284303164,\\\"1642821300000\\\":-0.8870146013,"
"\\\"1642822200000\\\":-0.8455988862,\\\"1642823100000\\\":0.2496059077,"
"\\\"1642824000000\\\":1.3448107015,\\\"1642824900000\\\":1.0681111088,"
"\\\"1642825800000\\\":0.7914115162,\\\"1642826700000\\\":1.0592085829,"
"\\\"1642827600000\\\":1.3270056497,\\\"1642828500000\\\":1.3599070561,"
"\\\"1642829400000\\\":1.3928084625"
"}"
"}\""
"}}"
}
},
},
400: {
"description": "Payload can not be empty.",
"content": {
"application/json": {
"example": {"detail": "Payload can not be empty"}
}
},
},
500: {
"description": "Internal server error.",
"content": {
"application/json": {
"example": {"detail": "Internal server error"}
}
},
}
},
tags=["Normalization"]
)
def normalize_mean(payload: str = Body(..., embed=True)):
"""API endpoint for normalizing all data in a building objects dataframe into a standard score.
Args:
payload: The JSON representation of building objects.
Returns:
The JSON representation of the updated buildings.
"""
try:
if not payload:
raise HTTPException(status_code=400, detail="Payload can not be empty")
buildings = features.json_to_buildings(json.loads(payload))
normalization.mean_normalization(buildings)
return json.dumps(buildings, cls=JSONEncoder)
except HTTPException:
raise
except Exception:
raise HTTPException(status_code=500, detail="Internal Server Error")
schema.custom_openapi(app)