state.service.ts 5.35 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
  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>();
35
  public onNewLshData: EventEmitter<void> = new EventEmitter<void>();
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
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
69

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

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

  async getTableInfo(): Promise<TableInfoData> {
81
82
    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
83
84
85
    return this.tableInfo;
  }

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

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

  public createTable() {
97
98
99
    console.log('setting table param');
    this.table = this.lshData.candidates;
    console.log('table param set');
100
101
102
103
104
105
106
    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
107
      {
108
        averageTable[indices[index]] = [];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
109
      }
110
      averageTable[indices[index]].push(candidate);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
111
    });
112
    this._averageTable = averageTable;
113
    console.log('table created');
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
114
115
116
    this.getTableInfo();
  }

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

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

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

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

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

153
  public get table(): {[bucket: string]: number[]}[] {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
154
155
156
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
    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
184
  public set queryWindow(v: number[][]) {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
185
186
187
188
    this._queryWindow = v;
    this.onNewQuery.emit();
  }

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

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

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