cache.service.ts 4.86 KB
Newer Older
1
2
3
4
5
6
7
import {EventEmitter, Injectable} from '@angular/core';
import {ApiService, RawData} from './api.service';

@Injectable({
  providedIn: 'root'
})
export class CacheService {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
8
9
  public rawValues: number[];
  public rawIndices: string[];
10
  public loadingProgress: number = 0;
11

12
  private _currentTab: number;
13
  private _query = undefined;
14
15
16
  private _labels = {};
  private _tables;
  private _windowSimilarity;
17
  private _sliderValue;
18
  private _queryWindow;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
19
20
  private _candidates: number[];
  private _parameters: number[];
21

22
  public windowSize = 120;
23
  public nrOfTables = 5;
24
  public hashSize = 5;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
25
  public stepSize = 200;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
26
  public querySelectionMode = true;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
27
  public distances = [];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
28
  public _dtwdistances: number[] = [];
29

30
  public onNewData: EventEmitter<void> = new EventEmitter<void>();
31
32
33
  public onNewSimilarity: EventEmitter<void> = new EventEmitter<void>();
  public onNewLabels: EventEmitter<void> = new EventEmitter<void>();
  public onNewQuery: EventEmitter<void> = new EventEmitter<void>();
34
35
  public onNewTables: EventEmitter<void> = new EventEmitter<void>();
  public onNewWindows: EventEmitter<void> = new EventEmitter<void>();
36
  public onNewTab: EventEmitter<void> = new EventEmitter<void>();
37
  public onNewSlider: EventEmitter<void> = new EventEmitter<void>();
38
39

  constructor(private api: ApiService) {
40
     this.initialize();
41
42
43
  }

  async initialize(): Promise<void> {
44
    this.loadingProgress = 0;
45
    await this.getRawData();
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
46
    this.loadingProgress = 50;
47
    await this.createWindows();
48
    this.loadingProgress = 100;
49
50
51
52
  }

  async reset(): Promise<void> {
    this.windowSimilarity = undefined;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
53
    this.loadingProgress = 50;
54
    await this.createWindows();
55
    this.loadingProgress = 100;
56
57
58
  }

  async getRawData(): Promise<void> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
59
60
61
    const rawData: RawData = await this.api.readFile();
    this.rawIndices = rawData.index;
    this.rawValues = rawData.values;
62
    this.onNewData.emit();
63
64
  }

65
  async createWindows(): Promise<void> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
66
    await this.api.createWindows(this.parameters);
67
    this.onNewWindows.emit();
68
69
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
70
71
72
73
  async initial(): Promise<void> {
    const output = await this.api.initialize(this._queryWindow);
    this.dtwdistances = output.distances;
    this.candidates = output.candidates;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
74
    this.createTable();
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
75
76
    this._parameters = output.parameters;
    console.log(output);
77
78
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
79
80
81
82
83
  async update(): Promise<void> {
    console.log(this.distances);
    const output = await this.api.update(this._queryWindow, this.distances, this._parameters);
    this.dtwdistances = output.distances;
    this.candidates = output.candidates;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
84
    this.createTable();
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
85
    console.log(output);
86
87
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
88
89
  async getAverageWindows(windows): Promise<any> {
    return await this.api.getAverageWindows(windows);
90
91
  }

92
  async getQueryWindow(window): Promise<number[]> {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
93
94
95
    const output = await this.api.getQueryWindow(window);
    this.distances = output.distances;
    this.queryWindow = output.average;
96
    return this._queryWindow;
97
98
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
99
100
101
102
  async getWindow(indices: number[]): Promise<number[][]> {
    return await this.api.getWindow(indices);
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  public createTable() {
    const indices = this.dtwdistances.map((x) => x > 500 ? 100 : Math.floor(x / 5));
    const table = {};
    this.candidates.forEach((candidate, index) => {
      if (table[indices[index]] === undefined)
      {
        table[indices[index]] = [];
      }
      table[indices[index]].push(candidate);
    });
    this.tables = table;
  }

  public set dtwdistances(v) {
    this._dtwdistances = v;
  }

  public get dtwdistances(): number[] {
    return this._dtwdistances;
  }

124
125
126
127
128
129
130
131
132
  public set query(v) {
    this._query = v;
  }

  public get query(): number {
    return this._query;
  }

  public get windows(): number[][] {
133
    return [];
134
135
136
137
  }

  public set tables(v) {
    this._tables = v;
138
139
140
    this.onNewTables.emit();
  }

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  public get tables() {
    return this._tables;
  }

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

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

  public set windowSimilarity(v) {
    this._windowSimilarity = v;
    this.onNewSimilarity.emit();
  }

  public get windowSimilarity() {
    return this._windowSimilarity;
  }

163
164
165
166
167
168
169
170
171
  public set currentTab(v) {
    this._currentTab = v;
    this.onNewTab.emit();
  }

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

172
173
174
175
176
177
178
179
180
  public set sliderValue(v) {
    this._sliderValue = v;
    this.onNewSlider.emit(v);
  }

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

181
182
183
184
185
  public set queryWindow(v) {
    this._queryWindow = v;
    this.onNewQuery.emit();
  }

186
187
188
189
  public get queryWindow() {
    return this._queryWindow;
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
190
191
192
193
194
195
196
197
  public set candidates(v) {
    this._candidates = v;
  }

  public get candidates() {
    return this._candidates;
  }

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