cache.service.ts 4.54 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
28
  public distances = [];
  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
74
75
76
  async initial(): Promise<void> {
    const output = await this.api.initialize(this._queryWindow);
    this.dtwdistances = output.distances;
    this.candidates = output.candidates;
    this.tables = output.hash_functions;
    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
84
85
  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;
    this.tables = output.hash_functions;
    console.log(output);
86
87
  }

88
89
90
91
92
93
94
95
  async getAverageProgressWindows(windows): Promise<number[][]> {
    return await this.api.getAverageProgressWindows(windows);
  }

  async getAverageTableWindows(windows): Promise<number[][]> {
    return await this.api.getAverageTableWindows(windows);
  }

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

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
103
104
105
106
  async getWindow(indices: number[]): Promise<number[][]> {
    return await this.api.getWindow(indices);
  }

107
108
109
110
111
112
113
114
115
  public set query(v) {
    this._query = v;
  }

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

  public get windows(): number[][] {
116
    return [];
117
118
119
120
  }

  public set tables(v) {
    this._tables = v;
121
122
123
    this.onNewTables.emit();
  }

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  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;
  }

146
147
148
149
150
151
152
153
154
  public set currentTab(v) {
    this._currentTab = v;
    this.onNewTab.emit();
  }

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

155
156
157
158
159
160
161
162
163
  public set sliderValue(v) {
    this._sliderValue = v;
    this.onNewSlider.emit(v);
  }

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

164
165
166
167
168
  public set queryWindow(v) {
    this._queryWindow = v;
    this.onNewQuery.emit();
  }

169
170
171
172
  public get queryWindow() {
    return this._queryWindow;
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
173
174
175
176
177
178
179
180
  public set candidates(v) {
    this._candidates = v;
  }

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

181
182
183
184
  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
185
186
      tablesize: this.nrOfTables,
      stepsize: this.stepSize
187
188
189
    };
  }
}