from flask import Flask, request import numpy as np from flask_cors import CORS from time import time import orjson import os.path import pseudo import preprocessing data_path = 'data/processed-data.npy' reload = False app = Flask(__name__) CORS(app) @app.route('/', methods=['GET']) def index(): return "hi" """ Returns raw data Output: [{ index: 1d array [x] values: 1d array [x] }] """ @app.route('/read-data', methods=['GET']) def read_data(): t0 = time() response = preprocessing.read_weather_data() response = orjson.dumps(response) print('Data read: ' + str(time()-t0)) return response """ Creates windows Input: { parameters: { windowssize: int } } Output: '1' """ @app.route('/create-windows', methods=['POST']) def create_windows(): t0 = time() 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) print('Windows created: ' + str(time()-t0)) return '1' """ 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 } """ @app.route('/initialize', methods=['POST']) def initialize(): t0 = time() raw_data = orjson.loads(request.data) data = np.load(data_path) data = np.swapaxes(data, 1, 2) query = raw_data["query"] query = np.swapaxes(query, 0, 1) # parameters = np.load('parameters.npy') lsh_data = pseudo.lsh(data, query) response = orjson.dumps(lsh_data) print('LSH done: ' + str(time()-t0)) return response @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 """ 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 } """ @app.route('/update', methods=['POST']) def update(): t0 = time() raw_data = orjson.loads(request.data) data = np.load(data_path) data = np.swapaxes(data, 1, 2) query = raw_data["query"] query = np.swapaxes(query, 0, 1) weights = raw_data["weights"] parameters = raw_data["parameters"] lsh_data = pseudo.lsh(data, query, parameters=parameters, weights=weights) response = orjson.dumps(lsh_data) print('LSH done: ' + str(time()-t0)) return response """ 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] """ @app.route('/query', methods=['POST']) def query(): t0 = time() raw_data = orjson.loads(request.data) print(raw_data) start_index = raw_data['start_index'] query_size = raw_data['query_size'] window_indices = raw_data['indices'] if start_index is not None: preprocessing.create_weather_windows(query_size) window_indices = int(start_index) 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 Input: { indices: 1d array [x] } Output: 3d array [x][d][t] """ @app.route('/window', methods=['POST']) def window(): t0 = time() raw_data = orjson.loads(request.data) indices = raw_data['indices'] output = np.load(data_path)[indices] response = orjson.dumps(output.tolist()) print("Window(s) done: " + str(time() - t0)) return response """ 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] } """ @app.route('/table-info', methods=['POST']) def table_info(): t0 = time() raw_data = orjson.loads(request.data) table = raw_data['table'] data = np.load(data_path) response = pseudo.table_info(data, table) print("Averages calculated: " + str(time() - t0)) return response