api.service.ts 4.49 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
export interface State {
  lshData: LshData;
  queryWindow: number[][];
  weights: number[];
  labels: any;
}

export interface StateNode {
  id: number;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
46
  image: any;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
47
48
49
50
  state: State;
  children: number[];
}

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

  constructor() { }

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

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
69
70
71
72
  /**
   * Split the data into windows (server side)
   */
  async createWindows(parameters: Parameters): Promise<any> {
73
    const postData = {parameters};
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
74
    await fetch('http://127.0.0.1:5000/create-windows', {
75
76
77
78
79
80
81
82
83
      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
84
85
86
87
88
  /**
   * 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', {
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, labels, weights, hash_functions}) ], { 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
102
103
  /**
   * 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', {
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}) ], { 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
  /**
   * Do another iteration of LSH, with weights, and return important information
   */
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
117
  async lshUpdate(query, weights, parameters): Promise<LshData> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
118
    const response = await fetch('http://127.0.0.1:5000/update', {
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: new Blob( [ JSON.stringify({query, weights, parameters}) ], { type: 'text/plain' } )
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 query window based on windows labeled correct
   */
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
132
  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
133
    const response = await fetch('http://127.0.0.1:5000/query', {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
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, query_size, start_index})
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
140
141
142
143
    });
    return await response.json();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
144
145
146
  /**
   * Get data of a window by indices
   */
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
147
  async getWindowByIndices(indices: number[]): Promise<number[][][]> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
148
    const response = await fetch('http://127.0.0.1:5000/window', {
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({indices})
155
156
157
158
    });
    return await response.json();
  }

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