-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
150 lines (121 loc) · 4.4 KB
/
script.js
File metadata and controls
150 lines (121 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
let analysisData = [];
let chart = null; // Global variable for the chart
// Function to initialize the table with sample data
function initializeTableWithSampleData() {
const sampleValues = [1];
sampleValues.forEach((bias) => {
addDataToTable(bias);
});
}
// Function to generate a random bit vector
function generateBitVector(length, probabilityOfOne) {
const bitVector = new Uint8Array(length);
const threshold = probabilityOfOne * 256;
for (let i = 0; i < length; i++) {
bitVector[i] = Math.random() * 256 < threshold ? 1 : 0;
}
return bitVector;
}
// Call the function to initialize the table with sample data
initializeTableWithSampleData();
function calculateBitVectorAnalysis() {
const coinBiasInput = document.getElementById("coin-bias");
let coinBias = parseFloat(coinBiasInput.value.trim());
if (isNaN(coinBias) || coinBias < 0 || coinBias > 100) {
document.getElementById("result").textContent = "Please enter a valid probability of 1 (between 0 and 100%).";
return;
}
coinBias = Math.round(coinBias * 100) / 100;
coinBiasInput.value = coinBias.toFixed(2);
const exists = analysisData.some(data => data.bias === coinBias);
if (exists) {
document.getElementById("result").textContent = "This value already exists in the table.";
return;
}
addDataToTable(coinBias);
document.getElementById("result").textContent = ""; // Clear any previous messages
coinBiasInput.value = ""; // Clear the input field after adding to the table
}
function addDataToTable(bias) {
const probabilityOfOne = bias / 100;
const probabilityOfZero = 1 - probabilityOfOne;
// Calculate the number of bits required to represent the bit vector
const numBits = 1 << 20;
const shannonEntropy = (-(probabilityOfOne * Math.log2(probabilityOfOne) + probabilityOfZero * Math.log2(probabilityOfZero))) * numBits / 8;
// Generate the bit vector
const bitVector = generateBitVector(numBits, probabilityOfOne);
// Gzip compress the bit vector with maximum compression (level 9)
const compressedData = pako.gzip(bitVector, { level: 9 });
// Get the size of the compressed data in bytes
const compressedSizeInBytes = compressedData.length;
const asr = numBits * probabilityOfOne * 128 / 8; // Convert bits to bytes
analysisData.push({ bias: bias, entropy: shannonEntropy, compressedSize: compressedSizeInBytes, asr: asr });
// Sort the table by Shannon entropy in ascending order
analysisData.sort((a, b) => a.entropy - b.entropy);
// Update the table
updateTable();
}
function updateTable() {
const tableBody = document.getElementById("analysis-table");
tableBody.innerHTML = "";
analysisData.forEach((data) => {
const row = document.createElement("tr");
const biasCell = document.createElement("td");
const entropyCell = document.createElement("td");
const compressedSizeCell = document.createElement("td");
const asrCell = document.createElement("td");
biasCell.textContent = data.bias.toFixed(2) + "%";
entropyCell.textContent = Math.round(data.entropy).toLocaleString();
compressedSizeCell.textContent = data.compressedSize.toLocaleString();
asrCell.textContent = Math.round(data.asr).toLocaleString();
row.appendChild(biasCell);
row.appendChild(entropyCell);
row.appendChild(compressedSizeCell);
row.appendChild(asrCell);
tableBody.appendChild(row);
});
generateGraph(); // Call generateGraph after updating the table
}
function generateGraph() {
if (chart) {
chart.destroy();
}
const biases = [];
const compressedSizes = [];
// Extract relevant data for the chart
analysisData.forEach((data) => {
biases.push(data.bias);
compressedSizes.push(data.compressedSize);
});
const ctx = document.getElementById("analysis-chart").getContext("2d");
chart = new Chart(ctx, {
type: "bar",
data: {
labels: biases,
datasets: [
{
label: "Gzip Compressed ASL Size (bytes)",
data: compressedSizes,
backgroundColor: "rgba(75, 192, 192, 0.6)",
borderColor: "rgba(75, 192, 192, 1)",
borderWidth: 1,
},
],
},
options: {
responsive: true,
scales: {
x: {
type: 'linear',
beginAtZero: true,
max: Math.max(...biases),
},
y: {
beginAtZero: true,
},
},
},
});
}
// Show an empty table when the page loads
updateTable();