main.py 5.35 KB
Newer Older
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
1
from flask import Flask, request
2
3
4
import numpy as np
from flask_cors import CORS
from time import time
5
import orjson
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
6
import os.path
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
7
8
import pseudo
import preprocessing
9

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
10
data_path = 'data/processed-data.npy'
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
11

12
reload = False
13
14
15
16
17
18
19
20

app = Flask(__name__)
CORS(app)

@app.route('/', methods=['GET'])
def index():
    return "hi"

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
21
22
23
24
25
26
27
28
29

"""
Returns raw data

Output: [{
    index: 1d array [x]
    values: 1d array [x]
}]
"""
30
31
@app.route('/read-data', methods=['GET'])
def read_data():
32
    t0 = time()
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
33
    response = preprocessing.read_weather_data()
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
34
    response = orjson.dumps(response)
35
    print('Data read: ' + str(time()-t0))
36
37
    return response

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
38

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
39
40
"""
Creates windows
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
41

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
42
43
44
45
46
Input: {
    parameters: {
        windowssize: int
    }
}
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
47

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
48
49
Output: '1'
"""
50
51
@app.route('/create-windows', methods=['POST'])
def create_windows():
52
    t0 = time()
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
53
54
55
56
    if (not os.path.isfile(data_path)):
        raw_data = request.json
        window_size = int(raw_data['parameters']["windowsize"])
        preprocessing.create_eeg_windows(window_size, 5)
57
    print('Windows created: ' + str(time()-t0))
58
    return '1'
59

60

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""
Does first iteration of LSH and returns a bunch of useful information

Input: {
    query: 2d array [d][t]
}

Output: {
    hash_functions: 3d array [k][l][d]
    candidates: 3d array [k][l][i]
    distances: 3d array [k][l][i]
    average_candidates: 1d array [i]
    average_distances: 1d array [i]
    tables: [{
        bucket: 1d array
    }]
    average_table: {
        bucket: 1d array
    }
    samples: 1d array
    parameters: 1d array
}
"""
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
84
85
86
@app.route('/initialize', methods=['POST'])
def initialize():
    t0 = time()
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
87
    raw_data = orjson.loads(request.data)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
88
    data = np.load(data_path)
89
    data = np.swapaxes(data, 1, 2)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
90
    query = raw_data["query"]
91
92
    query = np.swapaxes(query, 0, 1)
    # parameters = np.load('parameters.npy')
93

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
94
95
96
97
    lsh_data = pseudo.lsh(data, query)

    response = orjson.dumps(lsh_data)
    print('LSH done: ' + str(time()-t0))
98
99
    return response

100
101
102
103
104
105
106
107
108
109
110
@app.route('/get-lsh-parameters', methods=['GET'])
def get_lsh_parameters():
    t0 = time()
    data = np.load(data_path)
    data = np.swapaxes(data, 1, 2)
    parameters = pseudo.get_lsh_parameters(data)

    response = orjson.dumps(parameters)
    print('Parameter calculation done: ' + str(time()-t0))
    return response

111

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Does LSH and returns a bunch of useful information

Input: {
    query: 2d array [d][t]
}

Output: {
    hash_functions: 3d array [k][l][d]
    candidates: 3d array [k][l][i]
    distances: 3d array [k][l][i]
    average_candidates: 1d array [i]
    average_distances: 1d array [i]
    tables: [{
        bucket: 1d array
    }]
    average_table: {
        bucket: 1d array
    }
    samples: 1d array
}
"""
134
135
136
137
@app.route('/update', methods=['POST'])
def update():
    t0 = time()
    raw_data = orjson.loads(request.data)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
138
    data = np.load(data_path)
139
    data = np.swapaxes(data, 1, 2)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
140
    query = raw_data["query"]
141
    query = np.swapaxes(query, 0, 1)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
142
    weights = raw_data["weights"]
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
143
144
    parameters = raw_data["parameters"]

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
145
146
147
    lsh_data = pseudo.lsh(data, query, parameters=parameters, weights=weights)

    response = orjson.dumps(lsh_data)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
148
    print('LSH done: ' + str(time()-t0))
149
150
    return response

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
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

"""
Calculates new weights for LSH algorithm

Input: {
    labels: 1d array [?]
    hash_functions: 2d array [?][d]
    query: 2d array [d][t]
    weights: 1d array [d]
}

Output: 1d array [d]
"""
@app.route('/weights', methods=['POST'])
def weights():
    raw_data = orjson.loads(request.data)
    labels = raw_data["labels"]
    hash_functions = raw_data["hash_functions"]
    query = raw_data["query"]
    old_weights = raw_data["weights"]
    data = np.load(data_path)

    new_weights = pseudo.weights(data, query, old_weights, labels, hash_functions)

    response = orjson.dumps(new_weights)
    return response


"""
Calculates query based on given indices

Input: {
    indices: 1d array [?]
}

Output: 2d array [d][t]
"""
188
189
@app.route('/query', methods=['POST'])
def query():
190
    t0 = time()
191
    raw_data = orjson.loads(request.data)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
192
193
194
    print(raw_data)
    start_index = raw_data['start_index']
    query_size = raw_data['query_size']
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
195
196
    window_indices = raw_data['indices']

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
197
    if start_index is not None:
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
198
199
        preprocessing.create_weather_windows(query_size)
        window_indices = int(start_index)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
200
    data = np.load(data_path)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
201
202
203
204
205
206
207
208
209
    response = pseudo.query(data, window_indices)

    response = orjson.dumps(response)
    print("Query done: " + str(time() - t0))
    return response


"""
Returns values of windows on given indices
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
210

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
211
212
213
214
215
216
Input: {
    indices: 1d array [x]
}

Output: 3d array [x][d][t]
"""
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
217
218
219
220
221
@app.route('/window', methods=['POST'])
def window():
    t0 = time()
    raw_data = orjson.loads(request.data)
    indices = raw_data['indices']
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
222
223
224

    output = np.load(data_path)[indices]

225
    response = orjson.dumps(output.tolist())
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
226
    print("Window(s) done: " + str(time() - t0))
227
228
    return response

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

"""
Returns additional information on given table

Input: {
    table: 2d array [x][?]
}

Output: {
    prototypes: {
        average: 1d array [t]
        max: 1d array [t]
        min: 1d array [t]
    }
    distances: 2d array [x][x]
}
"""
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
246
247
@app.route('/table-info', methods=['POST'])
def table_info():
248
249
    t0 = time()
    raw_data = orjson.loads(request.data)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
250
251
    table = raw_data['table']
    data = np.load(data_path)
252

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
253
    response = pseudo.table_info(data, table)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
254

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
255
256
    print("Averages calculated: " + str(time() - t0))
    return response