main.py 2.7 KB
Newer Older
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
from flask import Flask, jsonify, request
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from flask_cors import CORS
from collections import defaultdict
from time import time

app = Flask(__name__)
CORS(app)

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

@app.route('/read-data', methods=['GET'])
def read_data():
    df = pd.read_csv("DailyDelhiClimateTrain.csv", index_col=0)
    df.index = pd.to_datetime(df.index)
    df.sort_index(inplace=True)
    meantemp = df.loc[:, 'meantemp'].copy()
    response = {
        "index": meantemp.index.values.astype(str).tolist(),
        "values": meantemp.values.tolist()
    }
    response = jsonify(response)
    return response


@app.route('/create-windows', methods=['POST'])
def create_windows():
    raw_data = request.json
    values = raw_data["values"]
34
    window_size = int(raw_data["windowsize"])
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    data = []
    for index in range(len(values) - window_size):
        window = values[index:index + window_size]
        norm = np.linalg.norm(window)
        if norm == 0:
            data.append(window)
        else:
            data.append((window / norm).tolist())
    response = jsonify(data)
    return response

@app.route('/create-tables', methods=['POST'])
def create_tables():
    t0 = time()
49
50
51
52
53
    raw_data = request.json
    data = raw_data["windows"]
    window_size = int(raw_data["windowsize"])
    hash_size = int(raw_data["hashsize"])
    table_size = int(raw_data["tablesize"])
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
    data = np.array(data)
    tables = [defaultdict(list) for _ in range(table_size)]
    tables_hash_function = [np.random.randn(window_size, hash_size) for _ in range(table_size)]

    for table_index in range(table_size):
        table = tables[table_index]
        hash_function = tables_hash_function[table_index]
        for window_index in range(data.shape[0]):
            signature = (np.dot(data[window_index], hash_function) > 0).astype('int')
            table[str(signature)].append(window_index)

    print(time() - t0)
    response = {
        "hash_functions": np.array(tables_hash_function).tolist(),
        "tables": tables
    }
    response = jsonify(response)
    return response

@app.route('/query', methods=['POST'])
def query():
    raw_data = request.json
    window = raw_data["window"]
    tables = raw_data["tables"]
    hash_functions = raw_data["hash_functions"]
    neighbours = []

    for t in range(len(tables)):
        table = tables[t]
        hash_function = hash_functions[t]
        row = window
        signature = (np.dot(row, hash_function) > 0).astype('int')
        neighbours.append(table[str(signature)])
    neighbours = list(set.intersection(*map(set, neighbours)))

    response = jsonify(neighbours)
    return response