labeling-window.component.ts 3.67 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
    }
    this.topk = topk;
    this.subplots = [];
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
    for (const windowIndex of this.topk) {
      const window = this.service.rawValues.map((channel) => channel.slice(windowIndex.index, windowIndex.index + this.service.windowSize));
      const data = [];
      window.forEach((channel, index) => {
        data.push({
          x: [...Array(channel.length).keys()],
          y: channel,
          type: 'line',
          xaxis: 'x',
          yaxis: `y${index + 2}`,
        });
      });
      const subplots = [];
      window.forEach((channel, index) => {
        subplots.push([`xy${index + 2}`]);
      });
      const plot = {
        index: windowIndex.index,
        data: data,
        layout: {
          grid: {
            rows: this.service.queryWindow.length,
            columns: 1,
            subplots: subplots,
          },
          showlegend: false,
          title: `Index: ${windowIndex.index.toString()}      Similarity: ${windowIndex.frequency.toString()}%`,
          hovermode: 'closest',
          autosize: true,
          margin: {
            l: 30,
            r: 30,
            t: 30,
            b: 0,
            pad: 4
          },
          height: 300,
          width: 150,
          titlefont: {
            size: 9
          },
          xaxis: {
            showgrid: false,
            zeroline: false,
            showticklabels: false,
          },
          yaxis: {
            zeroline: false,
            showticklabels: false,
131
132
          }
        }
133
134
135
136
137
138
139
140
      };
      window.forEach((channel, index) => {
        plot.layout[`yaxis${index + 2}`] = {
          zeroline: false,
          showticklabels: false,
        };
      });
      this.subplots.push(plot);
141
142
143
    }
  }
}