-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_data.js
132 lines (115 loc) · 3.57 KB
/
load_data.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
var web = false, webmatrix = false, webmatrixpca = false;
function innerProduct(vector1, vector2) {
let result = 0;
for (let i = 0; i < vector1.length; i++) {
result += vector1[i] * vector2[i];
}
result = Math.floor((result + 1) * (255.0 / 2))
return result;
}
// Function to compute the pairwise inner products and organize them into a grid
function computeInnerProductGrid(vectors) {
let grid = [];
for (let i = 0; i < vectors.length; i++) {
grid[i] = new Uint8Array(vectors.length);
for (let j = 0; j < vectors.length; j++) {
if (j < i) {
grid[i][j] = grid[j][i]; // Use previously computed product
} else {
grid[i][j] = innerProduct(vectors[i], vectors[j]);
}
}
}
return grid;
}
async function loadTable(url, sep, castfloat) {
try {
const response = await fetch(url);
const csvData = await response.text();
// Parse the CSV data
const rows = csvData.split(/\r?\n/);
const table = rows.map(row => row.split(sep).map(castfloat ? parseFloat : val => val));
return table
} catch (error) {
throw new Error("Failed to load the table: " + error.message);
}
}
const fetchBinaryArray = () => new Promise((resolve) => {
let xhr = new XMLHttpRequest();
xhr.open('get', './webembed.binary', true);
xhr.responseType = 'arraybuffer';
xhr.onLoad = () => {
if (xhr.status === 200) {
resolve(xhr.response);
}
}
xhr.onError = () => { reject(); }
xhr.send();
})
function loadBinary(path = './webembed.binary', dimensions = 768) {
return new Promise(resolve => {
let xhr = new XMLHttpRequest();
xhr.open('get', path, true);
xhr.responseType = 'arraybuffer';
xhr.onload = (event) => {
const arrayBuffer = xhr.response;
if (arrayBuffer) {
const rawArray = new Float32Array(arrayBuffer)
var out = []
for (let i = 0; i < rawArray.length; i+= dimensions) {
out.push(rawArray.slice(i, i + dimensions))
}
resolve(out)
}
}
xhr.send();
});
}
function loadAll(spoofMatrix = false) {
loadTable("web.csv", "|", false)
.then(table => {web = table.slice(1);});
if (spoofMatrix) {
webmatrix = [[1,2,3]];
} else {
// This is old and slow
// loadTable("webembed.csv", ",", true)
// .then(matrix => {webmatrix = matrix.slice(1);});
// This is smart and fast
// let xhr = new XMLHttpRequest();
// xhr.open('get', './webembed.binary', true);
// xhr.responseType = 'arraybuffer';
// xhr.onload = (event) => {
// const arrayBuffer = xhr.response;
// if (arrayBuffer) {
// const rawArray = new Float32Array(arrayBuffer)
// webmatrix = []
// const dimensions = 768;
// for (let i = 0; i < rawArray.length; i+= dimensions) {
// webmatrix.push(rawArray.slice(i, i + dimensions))
// }
// }
// }
// xhr.send();
loadBinary().then(matrix => {webmatrix = matrix})
// loadBinary("./webembedpca50.binary", 50).then(matrix => {webmatrixpca = matrix})
}
}
var dot = (a, b) => a.map((x, i) => a[i] * b[i]).reduce((m, n) => m + n);
function dotOneToMany(one, matrix) {
const dotProducts = [];
for (let i = 0; i < matrix.length; i++) {
dotProducts.push(dot(one, matrix[i]));
}
return dotProducts;
}
function argsort(array, ascending=false) {
// Create an array of indices [0, 1, 2, ..., n]
const indices = array.map((value, index) => index);
// Sort the indices based on the corresponding values in the array (we want descending order)
if (ascending) {
indices.sort((a, b) => array[a] - array[b]);
} else {
indices.sort((a, b) => array[b] - array[a]);
}
return indices;
}