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

export interface RawData {
4
5
  index: string[];
  values: number[];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
6
  name?: string;
7
8
}

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

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

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

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
37
38
39
40
41
42
43
44
45
46
47
48
49
export interface State {
  lshData: LshData;
  queryWindow: number[][];
  weights: number[];
  labels: any;
}

export interface StateNode {
  id: number;
  state: State;
  children: number[];
}

50
51
52
@Injectable({
  providedIn: 'root'
})
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
53
54
55
/**
 * This service acts as the interface between the client and server side.
 */
56
57
58
59
export class ApiService {

  constructor() { }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
60
61
62
  /**
   * 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
63
  async readFile(): Promise<RawData[]> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
64
    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
65
    return await response.json();
66
67
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
68
69
70
71
  /**
   * Split the data into windows (server side)
   */
  async createWindows(parameters: Parameters): Promise<any> {
72
    const postData = {parameters};
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
73
    await fetch('http://127.0.0.1:5000/create-windows', {
74
75
76
77
78
79
80
81
82
      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
83
84
85
86
87
  /**
   * 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', {
88
89
90
91
92
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
93
      body: new Blob( [ JSON.stringify({query, labels, weights, hash_functions}) ], { type: 'text/plain' } )
94
95
96
97
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
98
99
100
101
102
  /**
   * 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', {
103
104
105
106
107
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
108
      body: new Blob( [ JSON.stringify({query}) ], { type: 'text/plain' } )
109
110
111
112
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
113
114
115
  /**
   * Do another iteration of LSH, with weights, and return important information
   */
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
116
  async lshUpdate(query, weights, parameters): Promise<LshData> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
117
    const response = await fetch('http://127.0.0.1:5000/update', {
118
119
120
121
122
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
123
      body: new Blob( [ JSON.stringify({query, weights, parameters}) ], { type: 'text/plain' } )
124
125
126
127
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
128
129
130
  /**
   * Get query window based on windows labeled correct
   */
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
131
  async getQueryWindow(indices: number | {[index: number]: boolean}, query_size: number, start_index:number): Promise<number[][]> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
132
    const response = await fetch('http://127.0.0.1:5000/query', {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
133
134
135
136
137
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
138
      body: JSON.stringify({indices, query_size, start_index})
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
139
140
141
142
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
143
144
145
  /**
   * Get data of a window by indices
   */
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
146
  async getWindowByIndices(indices: number[]): Promise<number[][][]> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
147
    const response = await fetch('http://127.0.0.1:5000/window', {
148
149
150
151
152
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
153
      body: JSON.stringify({indices})
154
155
156
157
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
158
159
160
161
  /**
   * 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
162
    const response = await fetch('http://127.0.0.1:5000/table-info', {
163
164
165
166
167
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
168
      body: JSON.stringify({table})
169
170
171
    });
    return await response.json();
  }
172
}