table-overview.component.ts 4.32 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
  averagePlot(average): object {
25
    console.log(average);
26
    return {
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
55
      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
          }
        }
      ],
56
      layout: {
57
        showlegend: false,
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
        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,
      }
    };
  }

79
80
81
82
83
84
85
86
87
88
89
  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('');
  }

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

100
  async createPlots() {
101
    this.subplots = [];
102
103
    this.averages = [];
    const listOfWindows = [];
104
105
    for (const tableIndex in this.tables) {
      const table = this.tables[tableIndex];
106
107
      let opacity = new Array(Object.keys(table.entries).length);
      opacity.fill(1);
108
109
      if (this.service.queryWindow) {
        const queryEntry = this.calculateSignature(table.hash, this.service.queryWindow);
110
        listOfWindows.push(table.entries[queryEntry]);
111
112
113
114
        opacity = Object.keys(table.entries).map((hash: string) => {
            return hash === queryEntry ? 1.0 : 0.5;
          }
        );
115
      }
116
117
118
119
      const orderedHashes = Object.keys(table.entries).sort((hash1, hash2) => {
        return this.distanceMetric(hash1) - this.distanceMetric(hash2);
      });
      console.log(orderedHashes);
120
121
122
      this.subplots.push(
        {
          data: [{
123
            x: orderedHashes.map((hash: string) => {
124
              return Number('0b' + hash);
125
126
              }
            ),
127
            y: orderedHashes.map((hash: string) => table.entries[hash].length / (this.service.rawValues.length - this.service.windowSize)),
128
129
            type: 'bar',
            marker: {
130
              opacity
131
            }
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
          }],
          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,
          }
        }
      );
    }
155
    if (this.service.queryWindow) {
156
157
      console.log(listOfWindows);
      const temp = await this.service.getAverageTableWindows(listOfWindows);
158
159
      this.averages = temp.map(x => this.averagePlot(x));
    }
160
  }
161

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