labeling-window.component.ts 3.28 KB
Newer Older
1
import { Component, OnInit } from '@angular/core';
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
2
import {StateService} from '../state.service';
3
4
5
6
7
8
9

@Component({
  selector: 'app-labeling-window',
  templateUrl: './labeling-window.component.html',
  styleUrls: ['./labeling-window.component.css']
})
export class LabelingWindowComponent implements OnInit {
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
10
  public topk: number[];
11
12
  public subplots = [];
  public labels: boolean[] = [];
13
  private k = 5;
14

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
15
  constructor(private state: StateService) { }
16
17

  ngOnInit(): void {
18
    this.state.onNewLshData.subscribe(() => this.showSamples());
19
20
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
21
22
23
  async train() {
    this.state.labels = Object.assign({}, this.state.labels, this.labels);
    await this.state.getQueryWindow(this.state.labels);
24
    await this.state.update(Object.assign({}, this.labels));
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
25
26
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
27
28
29
  async updateQuery() {
    this.state.labels = Object.assign({}, this.state.labels, this.labels);
    await this.state.getQueryWindow(this.state.labels);
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  }

  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
46
  async showSamples() {
47
48
49
50
51
52
53
54
    // const allowedCandidates = this.state.lshData.average_candidates.filter((candidate) => this.state.labels[candidate] !== true);
    // this.topk = allowedCandidates.slice(0, this.k);
    // for (let i = 0; i < this.k; i++) {
    //   this.topk.push(allowedCandidates[Math.floor(Math.random() * allowedCandidates.length)]);
    // }
    this.labels = [];
    this.topk = this.state.lshData.samples;

55
    this.subplots = [];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
56
57
    const values: number[][][] = await this.state.getWindow(this.topk);
    for (const idx in this.topk) {
58
      const window = values[idx];
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
      const data = [];
      window.forEach((channel: number[], index: number) => {
        data.push({
          x: [...Array(channel.length).keys()],
          y: channel,
          type: 'line',
          xaxis: 'x',
          yaxis: `y${index + 2}`,
        });
      });
      const subplots = [];
      window.forEach((channel: number[], index: number) => {
        subplots.push([`xy${index + 2}`]);
      });
      const plot = {
        index: this.topk[idx],
        data: data,
        layout: {
          grid: {
78
            rows: this.state.queryWindow.length,
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
            columns: 1,
            subplots: subplots,
          },
          showlegend: false,
          title: `Index: ${this.topk[idx].toString()}`,
          hovermode: 'closest',
          autosize: true,
          margin: {
            l: 30,
            r: 30,
            t: 30,
            b: 0,
            pad: 4
          },
93
          height: 100 * values[0].length,
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
94
95
96
97
98
99
100
101
102
103
104
105
          width: 300,
          titlefont: {
            size: 9
          },
          xaxis: {
            showgrid: false,
            zeroline: false,
            showticklabels: false,
          },
          yaxis: {
            zeroline: false,
            showticklabels: false,
106
107
          }
        }
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
108
109
110
111
112
113
114
115
116
      };
      window.forEach((channel: number[], index: number) => {
        plot.layout[`yaxis${index + 2}`] = {
          zeroline: false,
          showticklabels: false,
        };
      });
      this.subplots.push(plot);
    }
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
117
118
  }

Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
119
120
  public get show() {
    return !!this.state.lshData;
Kruyff,D.L.W. (Dylan)'s avatar
Kruyff,D.L.W. (Dylan) committed
121
  }
122
}