labeling-window.component.ts 3.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { Component, OnInit } from '@angular/core';
import {CacheService} from '../cache.service';

@Component({
  selector: 'app-labeling-window',
  templateUrl: './labeling-window.component.html',
  styleUrls: ['./labeling-window.component.css']
})
export class LabelingWindowComponent implements OnInit {
  public topk;
  public subplots = [];
  public labels: boolean[] = [];
  private k = 5;

  constructor(private service: CacheService) { }

  ngOnInit(): void {
18
    this.service.onNewSimilarity.subscribe(() => { this.getTopKSimilar(); });
19
20
21
  }

  public changeTables() {
22
23
    this.service.labels = Object.assign({}, this.service.labels, this.labels);
    this.service.updateTables();
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  }

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

  public labelCorrect(index: number) {
    this.labels[index] = true;
  }

  public labelUndefined(index: number) {
    if (this.labels[index] !== undefined) {
      delete this.labels[index];
    }
  }

  public labelIncorrect(index: number) {
    this.labels[index] = false;
  }

44
45
46
47
48
49
50
51
52
  shuffleArray(array) {
    for (var i = array.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      const temp = array[i];
      array[i] = array[j];
      array[j] = temp;
    }
  }

53
54
  public getTopKSimilar() {
    this.labels = [];
55
    let abort = false;
56
57
58
    if (!this.windowSimilarity) {
      return;
    }
59
60
61
    let topk = [];
    let k = this.k;
    const keys = Object.keys(this.windowSimilarity).map(a => Number(a)).sort((a, b) => b - a);
62
63
64
65
66
67
68
69
70
71
72
73
74
    for (let i = 0; i < this.k; i++) {
      const windows = this.windowSimilarity[keys[i]];
      this.shuffleArray(windows);
        for (const index of windows) {
          if (this.service.labels[index] !== undefined) {
            continue;
          }
          topk.push({index, frequency: 100 * keys[i] / this.service.nrOfTables});
          k -= 1;
          if (k < 1) {
            abort = true;
          }
          break;
75
        }
76
        if (abort) {
77
78
          break;
        }
79
80
81
82
83
84
85
86
87
    }
    this.topk = topk;

    this.subplots = [];
    for (const window of this.topk) {
      this.subplots.push(
        {
          index: window.index,
          data: [{
88
89
            x: [...Array(this.service.windowSize).keys()],
            y: this.service.rawValues[0].slice(window.index, window.index + this.service.windowSize),
90
91
92
93
94
95
96
97
98
99
            type: 'line'
          }],
          layout: {
            title: `Index: ${window.index.toString()}      Similarity: ${window.frequency.toString()}%`,
            hovermode: 'closest',
            autosize: true,
            margin: {
              l: 30,
              r: 30,
              t: 30,
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
100
              b: 0,
101
102
              pad: 4
            },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
103
            height: 150,
104
            width: 150,
105
106
107
            titlefont: {
              size: 9
            },
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
108
            xaxis: {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
109
110
111
112
113
114
              showgrid: false,
              zeroline: false,
              showticklabels: false,
            },
            yaxis: {
              zeroline: false,
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
115
116
              showticklabels: false,
            }
117
118
119
120
121
122
          }
        }
      );
    }
  }
}