state.service.ts 5.79 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[]};
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
16
17
18
19

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

22
23
  private states = [];

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
24
25
26
27
28
29
30
31
32
33
34
35
36
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
  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>();

  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);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
69
    this._lshParameters = this.lshData.parameters;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
70
71
72
73
    this.createTable();
  }

  async update(): Promise<void> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
74
    this.lshData = await this.api.lshUpdate(this._queryWindow, [], this._lshParameters);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
75
76
77
78
    this.createTable();
  }

  async getTableInfo(): Promise<TableInfoData> {
79
80
    this.tableInfo = await this.api.getTableInfo(Object.values(this._averageTable));
    console.log(this.tableInfo);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
81
82
83
    return this.tableInfo;
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
84
  async getQueryWindow(windowIndex: number | {[index: number]: boolean}): Promise<number[][]> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
85
86
87
88
    this.queryWindow = await this.api.getQueryWindow(windowIndex);
    return this._queryWindow;
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
89
  async getWindow(indices: number[]): Promise<number[][][]> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
90
91
92
93
    return await this.api.getWindowByIndices(indices);
  }

  public createTable() {
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    const table = [];
    this.lshData.candidates.forEach((groupHash: number[][], i: number) => {
      groupHash.forEach((candidates: number[], j: number) => {
        const subTable = {};
        const length = this.lshData.distances[i][j].length;
        const median = this.lshData.distances[i][j][Math.ceil(length / 2)];
        const stepsize = median / 10;
        const indices: number[] = this.lshData.distances[i][j].map((x) => x > median * 2 ? 19 : Math.floor(x / stepsize));
        candidates.forEach((candidate: number, index: number) => {
          if (subTable[indices[index]] === undefined)
          {
            subTable[indices[index]] = [];
          }
          subTable[indices[index]].push(candidate);
        });
        table.push(subTable);
      });
    });
    this.table = table;
    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
120
      {
121
        averageTable[indices[index]] = [];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
122
      }
123
      averageTable[indices[index]].push(candidate);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
124
    });
125
    this._averageTable = averageTable;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
126
127
128
    this.getTableInfo();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
129
  public set rawData(v: RawData[]) {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
130
    this._rawData = v;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
131
    console.log(this._rawData);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
132
133
134
    this.onNewData.emit();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
135
  public get rawData(): RawData []{
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
136
137
138
139
    return this._rawData;
  }

  public set lshData(v: LshData) {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
140
    console.log(v);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
    this._lshData = v;
  }

  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;
  }

157
  public set table(v: {[bucket: string]: number[]}[]) {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
158
    console.log(v);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
159
160
161
162
    this._table = v;
    this.onNewTable.emit();
  }

163
  public get table(): {[bucket: string]: number[]}[] {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    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
194
  public set queryWindow(v: number[][]) {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
195
196
197
198
    this._queryWindow = v;
    this.onNewQuery.emit();
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
199
  public get queryWindow(): number[][] {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
200
201
202
    return this._queryWindow;
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
203
204
205
206
  public get lshParameters(): number[] {
    return this._lshParameters;
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
207
208
209
210
211
212
213
214
215
  public get parameters(): {[parameter: string]: number} {
    return {
      windowsize: this.windowSize,
      hashsize: this.hashSize,
      tablesize: this.nrOfTables,
      stepsize: this.stepSize
    };
  }
}