labeling-window.component.ts 3.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
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[] = [];
13
  private k = 12;
14
15
16
17

  constructor(private service: CacheService) { }

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

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
21
  public train() {
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
  }

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

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
40
  async getTopKSimilar() {
41
    this.labels = [];
42
    let abort = false;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
43
    const topk = [];
44
    let k = this.k;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
45

46
    const keys = Object.keys(this.windowSimilarity).map(a => Number(a)).sort((a, b) => b - a);
47
48
49
    for (let i = 0; i < this.k; i++) {
      const windows = this.windowSimilarity[keys[i]];
      this.shuffleArray(windows);
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
50
51
52
      for (const index of windows) {
        if (this.service.labels[index] !== undefined) {
          continue;
53
        }
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
54
55
56
57
        topk.push({index, frequency: 100 * keys[i] / this.service.nrOfTables});
        k -= 1;
        if (k < 1) {
          abort = true;
58
59
          break;
        }
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
60
61
62
63
64
        // break;
      }
      if (abort) {
        break;
      }
65
66
    }
    this.topk = topk;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
67
68
    await this.createPlots();
  }
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
69

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
70
  async createPlots() {
71
    this.subplots = [];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
72
73
    const values = await this.service.getWindow(this.topk.map((window) => window.index));
    this.topk.forEach((window, i) => {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
74
75
76
77
      this.subplots.push(
        {
          index: window.index,
          data: [{
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
78
79
            x: [...Array(values[i].length).keys()],
            y: values[i],
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
            type: 'line'
          }],
          layout: {
            title: `Index: ${window.index.toString()}      Similarity: ${window.frequency.toString()}%`,
            hovermode: 'closest',
            autosize: true,
            margin: {
              l: 30,
              r: 30,
              t: 30,
              b: 0,
              pad: 4
            },
            height: 150,
94
            width: 300,
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
95
96
97
98
99
100
101
102
103
104
105
106
            titlefont: {
              size: 9
            },
            xaxis: {
              showgrid: false,
              zeroline: false,
              showticklabels: false,
            },
            yaxis: {
              zeroline: false,
              showticklabels: false,
            }
107
108
          }
        }
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
109
      );
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
110
111
112
113
114
115
116
117
118
    });
  }

  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;
119
120
    }
  }
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
121
122
123
124

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