state.service.ts 5.55 KB
Newer Older
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
1
2
3
4
5
6
7
8
9
import {EventEmitter, Injectable} from '@angular/core';
import {ApiService, LshData, RawData, TableInfoData} from './api.service';

@Injectable({
  providedIn: 'root'
})
export class StateService {
  public loadingProgress: number = 0;

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
10
  private _rawData: RawData[];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
11
12
  private _lshData: LshData;
  private _tableInfo: TableInfoData;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
13
  private _queryWindow: number[][];
14
15
  private _table: {[bucket: string]: number[]}[];
  public _averageTable: {[bucket: string]: number[]};
16
  private _weights: number[];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
17
18
19
20

  private _currentTab: number;
  private _labels = {};
  private _sliderValue;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
21
  private _lshParameters: number[];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
22

23
24
  private states = [];

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
25
26
27
28
29
30
31
32
33
34
35
  public windowSize = 120;
  public nrOfTables = 5;
  public hashSize = 5;
  public stepSize = 200;
  public querySelectionMode = true;

  public onNewData: EventEmitter<void> = new EventEmitter<void>();
  public onNewWindows: EventEmitter<void> = new EventEmitter<void>();
  public onNewQuery: EventEmitter<void> = new EventEmitter<void>();
  public onNewTable: EventEmitter<void> = new EventEmitter<void>();
  public onNewTableInfo: EventEmitter<void> = new EventEmitter<void>();
36
  public onNewLshData: EventEmitter<void> = new EventEmitter<void>();
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

  public onNewLabels: EventEmitter<void> = new EventEmitter<void>();
  public onNewTab: EventEmitter<void> = new EventEmitter<void>();
  public onNewSlider: EventEmitter<number> = new EventEmitter<number>();

  constructor(private api: ApiService) {
     this.initialize();
  }

  async initialize(): Promise<void> {
    this.loadingProgress = 0;
    await this.getRawData();
    this.loadingProgress = 50;
    await this.createWindows();
    this.loadingProgress = 100;
  }

  async reset(): Promise<void> {
    this.loadingProgress = 50;
    await this.createWindows();
    this.loadingProgress = 100;
  }

  async getRawData(): Promise<void> {
    this.rawData = await this.api.readFile();
  }

  async createWindows(): Promise<void> {
    await this.api.createWindows(this.parameters);
    this.onNewWindows.emit();
  }

  async lshInitial(): Promise<void> {
    this.lshData = await this.api.lshInitial(this._queryWindow);
71
    console.log('data loaded');
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
72
    this._lshParameters = this.lshData.parameters;
73
    this._weights = [1, 1, 1];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
74
75
76
    this.createTable();
  }

77
78
79
80
  async update(labels): Promise<void> {
    this._weights = await this.api.getWeights(this._queryWindow, labels, this._weights);
    console.log(this._weights);
    this.lshData = await this.api.lshUpdate(this._queryWindow, this._weights, this._lshParameters);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
81
82
83
    this.createTable();
  }

84
85
86
  async getTableInfo(table: number[][]): Promise<TableInfoData> {
    // console.log(this.tableInfo);
    return await this.api.getTableInfo(table);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
87
88
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
89
  async getQueryWindow(windowIndex: number | {[index: number]: boolean}): Promise<number[][]> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
90
    this.queryWindow = await this.api.getQueryWindow(windowIndex);
91
    console.log(this.queryWindow);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
92
93
94
    return this._queryWindow;
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
95
  async getWindow(indices: number[]): Promise<number[][][]> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
96
97
98
    return await this.api.getWindowByIndices(indices);
  }

99
  async createTable() {
100
    console.log('setting table param');
101
    this.table = this.lshData.tables;
102
    console.log('table param set');
103
104
105
106
107
108
109
    const averageTable = {};
    const length = this.lshData.average_distances.length;
    const median = this.lshData.average_distances[Math.ceil(length / 2)];
    const stepsize = median / 10;
    const indices: number[] = this.lshData.average_distances.map((x) => x > median * 2 ? 19 : Math.floor(x / stepsize));
    this.lshData.average_candidates.forEach((candidate: number, index: number) => {
      if (averageTable[indices[index]] === undefined)
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
110
      {
111
        averageTable[indices[index]] = [];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
112
      }
113
      averageTable[indices[index]].push(candidate);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
114
    });
115
    this._averageTable = averageTable;
116
    console.log('table created');
117
    this.tableInfo = await this.getTableInfo(Object.values(this._averageTable));
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
118
119
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
120
  public set rawData(v: RawData[]) {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
121
    this._rawData = v;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
122
    console.log(this._rawData);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
123
124
125
    this.onNewData.emit();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
126
  public get rawData(): RawData []{
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
127
128
129
130
    return this._rawData;
  }

  public set lshData(v: LshData) {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
131
    console.log(v);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
132
    this._lshData = v;
133
    this.onNewLshData.emit();
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  }

  public get lshData(): LshData {
    return this._lshData;
  }

  public set tableInfo(v: TableInfoData) {
    this._tableInfo = v;
    this.onNewTableInfo.emit();
  }

  public get tableInfo(): TableInfoData {
    return this._tableInfo;
  }

149
  public set table(v: {[bucket: string]: number[]}[]) {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
150
    console.log(v);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
151
    this._table = v;
152
    console.log('emitting onNewTable');
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
153
154
155
    this.onNewTable.emit();
  }

156
  public get table(): {[bucket: string]: number[]}[] {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
    return this._table;
  }

  public set labels(v) {
    this._labels = v;
    this.onNewLabels.emit();
  }

  public get labels() {
    return this._labels;
  }

  public set currentTab(v: number) {
    this._currentTab = v;
    this.onNewTab.emit();
  }

  public get currentTab(): number {
    return this._currentTab;
  }

  public set sliderValue(v: number) {
    this._sliderValue = v;
    this.onNewSlider.emit(v);
  }

  public get sliderValue(): number {
    return this._sliderValue;
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
187
  public set queryWindow(v: number[][]) {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
188
189
190
191
    this._queryWindow = v;
    this.onNewQuery.emit();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
192
  public get queryWindow(): number[][] {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
193
194
195
    return this._queryWindow;
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
196
197
198
199
  public get lshParameters(): number[] {
    return this._lshParameters;
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
200
201
202
203
204
205
206
207
208
  public get parameters(): {[parameter: string]: number} {
    return {
      windowsize: this.windowSize,
      hashsize: this.hashSize,
      tablesize: this.nrOfTables,
      stepsize: this.stepSize
    };
  }
}