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

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

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
144 145 146 147
  /**
   * 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
148
    const response = await fetch('http://127.0.0.1:5000/table-info', {
149 150 151 152 153
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
154
      body: JSON.stringify({table})
155 156 157
    });
    return await response.json();
  }
158
}