labeling-window.component.ts 2.51 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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 {
    this.service.onQuery.subscribe(() => { this.getTopKSimilar(); });
  }

  public changeTables() {
    console.log(this.labels);
    this.service.updateTables(this.labels);
  }

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

  public getTopKSimilar() {
    this.labels = [];
    let topk = [];
    let k = this.k;
    const keys = Object.keys(this.windowSimilarity).map(a => Number(a)).sort((a, b) => b - a);
    for (const key of keys) {
      const windows = this.windowSimilarity[key];
      const nrOfWindows = windows.length;
      if (nrOfWindows >= k) {
        const temp = windows.slice(0, k).map((index) => ({index, frequency: 100 * key / this.service.nrOfTables}));
        topk = topk.concat(temp);
        break;
      } else {
        k -= nrOfWindows;
        const temp = windows.map(index => ({index, frequency: 100 * key / this.service.nrOfTables}));
        topk = topk.concat(temp);
      }
    }
    this.topk = topk;

    this.subplots = [];
    for (const window of this.topk) {
      this.subplots.push(
        {
          index: window.index,
          data: [{
            x: this.service.rawIndices.slice(window.index, window.index + this.service.windowSize),
            y: this.service.rawValues.slice(window.index, window.index + this.service.windowSize),
            type: 'line'
          }],
          layout: {
            title: `Index: ${window.index.toString()}      Similarity: ${window.frequency.toString()}%`,
            hovermode: 'closest',
            autosize: true,
            margin: {
              l: 30,
              r: 30,
              t: 30,
              pad: 4
            },
            height: 200,
            width: 200,
            titlefont: {
              size: 9
            },
          }
        }
      );
    }
  }
}