table-overview.component.ts 4.39 KB
Newer Older
1
2
import { Component, OnInit } from '@angular/core';
import {CacheService} from '../cache.service';
3
import {element} from 'protractor';
4
5
6
7
8
9
10
11

@Component({
  selector: 'app-table-overview',
  templateUrl: './table-overview.component.html',
  styleUrls: ['./table-overview.component.css']
})
export class TableOverviewComponent implements OnInit {
  public subplots;
12
  public averages;
13
14
15
16
  constructor(private service: CacheService) { }

  ngOnInit(): void {
    this.service.onNewTables.subscribe(() => this.createPlots());
17
    this.service.onNewSimilarity.subscribe(() => this.createPlots());
18
19
20
21
22
23
  }

  public get tables() {
    return this.service.tables;
  }

24
25
  averagePlot(average): object {
    return {
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
      data: [
        {
          x: [...Array(average.average.length).keys()],
          y: average.average,
          type: 'line',
        },
        {
          x: [...Array(average.average.length).keys()],
          y: average.max,
          type: 'scatter',
          fill: null,
          mode: 'lines',
          line: {
            color: 'rgb(55, 128, 191)',
            width: 3
          }
        },
        {
          x: [...Array(average.average.length).keys()],
          y: average.min,
          type: 'scatter',
          fill: 'tonexty',
          mode: 'lines',
          line: {
            color: 'rgb(55, 128, 191)',
            width: 3
          }
        }
      ],
55
      layout: {
56
        showlegend: false,
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
        hovermode: 'closest',
        autosize: true,
        margin: {
          l: 10,
          r: 10,
          t: 10,
          b: 10,
          pad: 4
        },
        xaxis: {
          showticklabels: false
        },
        yaxis: {
          showticklabels: false
        },
        height: 100,
        width: screen.width * 0.1,
      }
    };
  }

78
79
80
81
82
83
84
85
86
87
88
  calculateSignature(hashFunction, window): string {
    const output = new Array(hashFunction[0].length);
    output.fill(0);
    for (const hash of hashFunction) {
      for (let i = 0; i < hash.length; i++) {
        output[i] += window[i] * hash[i];
      }
    }
    return output.map((x) => x > 0 ? '1' : '0').join('');
  }

89
90
91
92
93
94
95
96
97
98
  distanceMetric(hash) {
    let sum = 0;
    for (let i = 0; i < hash.length; i++) {
      if (hash[i] === '1') {
        sum++;
      }
    }
    return sum;
  }

99
  async createPlots() {
100
    this.subplots = [];
101
102
    this.averages = [];
    const listOfWindows = [];
103
104
    for (const tableIndex in this.tables) {
      const table = this.tables[tableIndex];
105
      let rank = undefined;
106
107
      let opacity = new Array(Object.keys(table.entries).length);
      opacity.fill(1);
108
109
110
      const orderedHashes = Object.keys(table.entries).sort((hash1, hash2) => {
        return this.distanceMetric(hash1) - this.distanceMetric(hash2);
      });
111
112
      if (this.service.queryWindow) {
        const queryEntry = this.calculateSignature(table.hash, this.service.queryWindow);
113
        listOfWindows.push(table.entries[queryEntry]);
114
        opacity = orderedHashes.map((hash: string) => {
115
116
117
            return hash === queryEntry ? 1.0 : 0.5;
          }
        );
118
        rank = table.entries[queryEntry].length;
119
      }
120
121
      this.subplots.push(
        {
122
          rank: rank,
123
          data: [{
124
125
            x: orderedHashes.map((hash: string, i) => {
              return i; // Number('0b' + hash);
126
127
              }
            ),
128
            y: orderedHashes.map((hash: string) => table.entries[hash].length ), // / (this.service.rawValues.length - this.service.windowSize)),
129
130
            type: 'bar',
            marker: {
131
              opacity
132
            }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
          }],
          layout: {
            hovermode: 'closest',
            autosize: true,
            margin: {
              l: 10,
              r: 10,
              t: 10,
              b: 10,
              pad: 4
            },
            xaxis: {
              showticklabels: false
            },
            yaxis: {
              showticklabels: false
            },
            height: 100,
            width: screen.width * 0.1,
          }
        }
      );
    }
156
    this.subplots.sort((plot1, plot2) => plot1.rank - plot2.rank);
157
    if (this.service.queryWindow) {
158
      const temp = await this.service.getAverageTableWindows(listOfWindows);
159
160
      this.averages = temp.map(x => this.averagePlot(x));
    }
161
  }
162

163
164
  async setQuery(data) {
    console.log('clicked for query');
165
    this.service.queryWindow = await this.service.getQueryWindow(data[0].y);
166
    await this.service.getSimilarWindows();
167
  }
168
}