main.py 4.8 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_mts_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 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 184 185 186 187 188 189 190 191 192
    window_indices = raw_data['indices']
    data = np.load(data_path)

    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
193

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
194 195 196 197 198 199
Input: {
    indices: 1d array [x]
}

Output: 3d array [x][d][t]
"""
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
200 201 202 203 204
@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
205 206 207

    output = np.load(data_path)[indices]

208
    response = orjson.dumps(output.tolist())
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
209
    print("Window(s) done: " + str(time() - t0))
210 211
    return response

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

"""
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
229 230
@app.route('/table-info', methods=['POST'])
def table_info():
231 232
    t0 = time()
    raw_data = orjson.loads(request.data)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
233 234
    table = raw_data['table']
    data = np.load(data_path)
235

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

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