api.service.ts 4.3 KB
Newer Older
1 2 3
import { Injectable } from '@angular/core';

export interface RawData {
4 5
  index: string[];
  values: number[];
6 7
}

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
8
export interface LshData {
9
  candidates: number[][][];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
10
  distances: number[][][];
11 12
  average_candidates: number[];
  average_distances: number[];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
13 14
  tables: {[bucket: string]: number[]}[];
  average_table: {[bucket: string]: number[]};
15
  samples: number[];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
16
  hash_functions: number[][];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
17 18 19 20 21
  parameters?: number[];
}

export interface TableInfoData {
  prototypes: {
22 23 24
    average: number[][];
    min: number[][];
    max: number[][];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
25 26 27 28
  }[];
  distances: number[][];
}

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
29 30 31 32 33 34 35
export interface Parameters {
  windowsize: number;
  hashsize: number;
  tablesize: number;
  stepsize: number;
}

36 37 38
@Injectable({
  providedIn: 'root'
})
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
39 40 41
/**
 * This service acts as the interface between the client and server side.
 */
42 43 44 45
export class ApiService {

  constructor() { }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
46 47 48
  /**
   * Read input data. The format is a list of channels, where each channel is an object of type RawData
   */
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
49
  async readFile(): Promise<RawData[]> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
50
    const response = await fetch('http://127.0.0.1:5000/read-data');
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
51
    return await response.json();
52 53
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
54 55 56 57
  /**
   * Split the data into windows (server side)
   */
  async createWindows(parameters: Parameters): Promise<any> {
58
    const postData = {parameters};
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
59
    await fetch('http://127.0.0.1:5000/create-windows', {
60 61 62 63 64 65 66 67 68
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(postData)
    });
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
69 70 71 72 73
  /**
   * Get weights which will be applied to the LSH hash functions
   */
  async getWeights(query: number[][], labels: {[index: number]: boolean}, weights: number[], hash_functions: number[][]): Promise<number[]> {
    const response = await fetch('http://127.0.0.1:5000/weights', {
74 75 76 77 78
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
79
      body: new Blob( [ JSON.stringify({query, labels, weights, hash_functions}) ], { type: 'text/plain' } )
80 81 82 83
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
84 85 86 87 88
  /**
   * Do the first iteration of LSH and return important information
   */
  async lshInitial(query: number[][]): Promise<LshData> {
    const response = await fetch('http://127.0.0.1:5000/initialize', {
89 90 91 92 93
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
94
      body: new Blob( [ JSON.stringify({query}) ], { type: 'text/plain' } )
95 96 97 98
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
99 100 101
  /**
   * Do another iteration of LSH, with weights, and return important information
   */
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
102
  async lshUpdate(query, weights, parameters): Promise<LshData> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
103
    const response = await fetch('http://127.0.0.1:5000/update', {
104 105 106 107 108
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
109
      body: new Blob( [ JSON.stringify({query, weights, parameters}) ], { type: 'text/plain' } )
110 111 112 113
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
114 115 116
  /**
   * Get query window based on windows labeled correct
   */
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
117 118
  async getQueryWindow(indices: number | {[index: number]: boolean}, query_size: number, start_index:number): Promise<number[][]> {
    console.log('sending query')
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
119
    const response = await fetch('http://127.0.0.1:5000/query', {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
120 121 122 123 124
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
125
      body: JSON.stringify({indices, query_size, start_index})
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
126 127 128 129
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
130 131 132
  /**
   * Get data of a window by indices
   */
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
133
  async getWindowByIndices(indices: number[]): Promise<number[][][]> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
134
    const response = await fetch('http://127.0.0.1:5000/window', {
135 136 137 138 139
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
140
      body: JSON.stringify({indices})
141 142 143 144
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
145 146 147 148
  /**
   * Get additional information for a given table
   */
  async getTableInfo(table: number[][]): Promise<TableInfoData> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
149
    const response = await fetch('http://127.0.0.1:5000/table-info', {
150 151 152 153 154
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
155
      body: JSON.stringify({table})
156 157 158
    });
    return await response.json();
  }
159
}