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.onNewSimilarity.subscribe(() => { this.getTopKSimilar(); }); } public changeTables() { this.service.labels = Object.assign({}, this.service.labels, this.labels); this.service.updateTables(); } 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; } 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; } } public getTopKSimilar() { this.labels = []; let abort = false; if (!this.windowSimilarity) { return; } let topk = []; let k = this.k; const keys = Object.keys(this.windowSimilarity).map(a => Number(a)).sort((a, b) => b - a); 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; } if (abort) { break; } } this.topk = topk; this.subplots = []; 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, } } }; window.forEach((channel, index) => { plot.layout[`yaxis${index + 2}`] = { zeroline: false, showticklabels: false, }; }); this.subplots.push(plot); } } }