-
Notifications
You must be signed in to change notification settings - Fork 449
/
index.js
152 lines (136 loc) · 4.3 KB
/
index.js
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
151
152
/**
*
* This demo is part of Burn project: https://github.com/tracel-ai/burn
*
* Released under a dual license:
* https://github.com/tracel-ai/burn/blob/main/LICENSE-MIT
* https://github.com/tracel-ai/burn/blob/main/LICENSE-APACHE
*
*/
/**
* Looks up element by an id.
* @param {string} - Element id.
*/
function $(id) {
return document.getElementById(id);
}
/**
* Truncates number to a given decimal position
* @param {number} num - Number to truncate.
* @param {number} fixed - Decimal positions.
* src: https://stackoverflow.com/a/11818658
*/
function toFixed(num, fixed) {
const re = new RegExp('^-?\\d+(?:\.\\d{0,' + (fixed || -1) + '})?');
return num.toString().match(re)[0];
}
/**
* Helper function that builds a chart using Chart.js library.
* @param {object} chartEl - Chart canvas element.
*
* NOTE: Assumes chart.js is loaded into the global.
*/
function chartConfigBuilder(chartEl) {
Chart.register(ChartDataLabels);
return new Chart(chartEl, {
plugins: [ChartDataLabels],
type: "bar",
data: {
labels: ["", "", "", "", "",],
datasets: [
{
data: [0.0, 0.0, 0.0, 0.0, 0.0], // Added one more data point to make it 10
borderWidth: 0,
fill: true,
backgroundColor: "#247ABF",
axis: 'y',
},
],
},
options: {
responsive: false,
maintainAspectRatio: false,
animation: true,
plugins: {
legend: {
display: false,
},
tooltip: {
enabled: true,
},
datalabels: {
color: "white",
formatter: function (value, context) {
return toFixed(value, 2);
},
},
},
indexAxis: 'y',
scales: {
y: {
},
x: {
suggestedMin: 0.0,
suggestedMax: 1.0,
beginAtZero: true,
},
},
},
});
}
/** Helper function that extracts labels and probabilities from the data.
* @param {object} data - Data object.
* @returns {object} - Object with labels and probabilities.
*/
function extractLabelsAndProbabilities(data) {
const labels = [];
const probabilities = [];
for (let item of data) {
if (item.hasOwnProperty('label') && item.hasOwnProperty('probability')) {
labels.push(item.label);
probabilities.push(item.probability);
}
}
return {
labels,
probabilities
};
}
/**
* Helper function that extracts RGB values from a canvas.
* @param {object} canvas - Canvas element.
* @param {object} ctx - Canvas context.
* @returns {object} - Flattened array of RGB values.
*/
function extractRGBValuesFromCanvas(canvas, ctx) {
// Get image data from the canvas
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
// Get canvas dimensions
const height = canvas.height;
const width = canvas.width;
// Create a flattened array to hold the RGB values in channel-first order
const flattenedArray = new Float32Array(3 * height * width);
// Initialize indices for R, G, B channels in the flattened array
let kR = 0,
kG = height * width,
kB = 2 * height * width;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
// Compute the index for the image data array
const index = (y * width + x) * 4;
// Fill in the R, G, B channels in the flattened array
flattenedArray[kR++] = imageData.data[index] / 255.0; // Red
flattenedArray[kG++] = imageData.data[index + 1] / 255.0; // Green
flattenedArray[kB++] = imageData.data[index + 2] / 255.0; // Blue
}
}
return flattenedArray;
}
/** Detect if browser is safari
* @returns {boolean} - True if browser is safari.
*/
function isSafari() {
// https://stackoverflow.com/questions/7944460/detect-safari-browser
let isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
return isSafari;
}