main.py 5.04 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_eeg_data(10)
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 100
    return response


Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
"""
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
}
"""
123 124 125 126
@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
127
    data = np.load(data_path)
128
    data = np.swapaxes(data, 1, 2)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
129
    query = raw_data["query"]
130
    query = np.swapaxes(query, 0, 1)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
131
    weights = raw_data["weights"]
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
132 133
    parameters = raw_data["parameters"]

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
134 135 136
    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
137
    print('LSH done: ' + str(time()-t0))
138 139
    return response

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

"""
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]
"""
177 178
@app.route('/query', methods=['POST'])
def query():
179
    t0 = time()
180
    raw_data = orjson.loads(request.data)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
181 182 183
    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
184 185
    window_indices = raw_data['indices']

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
186 187 188 189
    if start_index is not None:
        preprocessing.create_eeg_windows(query_size, 10)
        window_indices = int(start_index / 100)
    data = np.load(data_path)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
190 191 192 193 194 195 196 197 198
    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
199

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
200 201 202 203 204 205
Input: {
    indices: 1d array [x]
}

Output: 3d array [x][d][t]
"""
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
206 207 208 209 210
@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
211 212 213

    output = np.load(data_path)[indices]

214
    response = orjson.dumps(output.tolist())
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
215
    print("Window(s) done: " + str(time() - t0))
216 217
    return response

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

"""
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
235 236
@app.route('/table-info', methods=['POST'])
def table_info():
237 238
    t0 = time()
    raw_data = orjson.loads(request.data)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
239 240
    table = raw_data['table']
    data = np.load(data_path)
241

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

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