forked from kaivalyar/ox_wip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviz8.html
621 lines (486 loc) · 23.8 KB
/
viz8.html
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Viz. Token-wise Importances</title>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-cloud/1.2.5/d3.layout.cloud.min.js"></script>
<style>
body {
background: whitesmoke;
font-family: Arial, sans-serif;
}
h1 {
text-align: center;
font-weight: bold;
}
h2 {
text-align: center;
}
.itw {
color: green;
}
.teacher {
color: red;
}
.container {
display: flex;
justify-content: space-between;
}
.container div {
padding: 10px;
max-width: 45%;
}
.container img {
max-width: 100%;
padding: 10px;
}
form {
margin-bottom: 20px;
}
form label {
margin-right: 10px;
}
form input[type="number"] {
width: 50px;
margin-right: 10px;
}
form button {
margin-right: 10px;
}
#cloud {
background: white;
/* border: thin solid black; */
padding: 30px;
}
</style>
</head>
<body style="background: whitesmoke;">
<script>
/*document.addEventListener('DOMContentLoaded', function() {
const files = document.querySelectorAll('#file-list li');
files.forEach(fileItem => {
const fileName = fileItem.getAttribute('data-file');
fetchFile(fileName);
});
});*/
/*document.addEventListener('DOMContentLoaded', function() {
for (let i = 0; i <= 4007; i++) {
const fileName = `DeNorms/${i}.csv`;
fetchFile(fileName);
}
});*/
/*
document.addEventListener('DOMContentLoaded', function() {
async function fetchFilesInBatches(start, end, batchSize) {
for (let i = start; i <= end; i++) {
const fileName = `DATA/DeNorms/${i}.csv`;
fetchFile(fileName);
if ((i + 1) % batchSize === 0) {
await new Promise(resolve => setTimeout(resolve, 50)); // Pause for 5ms
}
}
}
fetchFilesInBatches(0, 5, 1);
//fetchFilesInBatches(0, 4007, 100);
});
*/
let use_denorms = false;
function setUseDenorms(value) {
use_denorms = Boolean(value);
// alert(`Value set to: ${use_denorms}`);
}
async function fetchFilesInBatches(start, end, batchSize) {
for (let i = start; i <= end; i++) {
const fileName = use_denorms ? `DATA/DeNorms/${i}.csv` : `DATA/DIMPS/${i}.csv`;
// For use_denorms=true, the file path will be DATA/DeNorms/${i}.csv
// For use_denorms=false, the file path will be DATA/DIMPS/${i}.csv
fetchFile(fileName);
if ((i + 1) % batchSize === 0) {
await new Promise(resolve => setTimeout(resolve, 50)); // Pause for 5ms
}
}
}
let poscloud = [];
let negcloud = [];
function fetchFiles() {
const useDenormsCheckbox = document.getElementById('use_denorms');
const use_denorms = Boolean(useDenormsCheckbox.checked) || false;
setUseDenorms(use_denorms);
const start = parseInt(document.getElementById('start').value);
const end = parseInt(document.getElementById('end').value);
d3.select("#output").innerHTML = "";
d3.select("#output").selectAll("*").remove();
// d3.select("#cloud").innerHTML = "";
// d3.select("#cloud").selectAll("*").remove();
poscloud = [];
negcloud = [];
fetchFilesInBatches(start, end, 1);
}
function fetchFile(fileName) {
fetch(fileName)
.then(response => response.text())
.then(text => {
const data = d3.csvParse(text);
// console.log(fileName);
// console.log(data);
// console.log();
displayData(data, fileName.replace('/', '_').replace('/', '_').replace('.csv', ''));
})
.catch(error => console.error('Error fetching file:', error));
}
let csvData = null;
function readCSVFile() {
const fileName = `DATA/final_rot.csv`;
fetch(fileName)
.then(response => response.text())
.then(text => {
csvData = d3.csvParse(text);
})
.catch(error => console.error('Error fetching file:', error));
}
function extractDataFromCSV(rowNumber) {
if (!csvData) {
readCSVFile();
}
const dataRow = csvData[rowNumber];
// console.log(rowNumber)
// console.log(dataRow)
// console.log()
const groundTruth = dataRow['Ground_truth'];
const prediction = dataRow['Prediction'];
const rotPredictions = dataRow['ROT_Predictions'];
return {groundTruth, prediction, rotPredictions};
}
function displayData(data, fileName) {
//console.log(`Data from ${fileName}:`, data);
/*const fileName = `sd/${fn}.csv`;
fetch(fileName)
.then(response => response.text())
.then(text => {
const newData = d3.csvParse(text);
const value = newData[ri][cn];
})
.catch(error => console.error('Error fetching file:', error));*/
const index = fileName.split('_')[2];
//console.log(extractDataFromCSV(index));
const {groundTruth, prediction, rotPredictions} = extractDataFromCSV(index);
const values = data.map(row => parseFloat(row[Object.keys(row)[1]]));
const sumOfValues = values.reduce((acc, curr) => acc + curr, 0);
const negativeVals = values.filter(value => value < 0);
const positiveVals = values.filter(value => value >= 0);
const minNegativeACT = Math.min(...negativeVals);
const maxNegativeACT = Math.max(...negativeVals);
const minPositiveACT = Math.min(...positiveVals);
const maxPositiveACT = Math.max(...positiveVals);
const numNegativeACT = negativeVals.length;
const numPositiveACT = positiveVals.length;
//document.getElementById('meta').innerHTML += `File: ${fileName}<br>Range of positive importances: ${minPositiveACT} to ${maxPositiveACT}, total ${numPositiveACT} tokens<br>Range of negative importances: ${minNegativeACT} to ${maxNegativeACT}, total ${numNegativeACT} tokens<br><br>`;
let minNegative = Math.min(...negativeVals);
const maxNegative = 0;
const minPositive = 0;
let maxPositive = Math.max(...positiveVals);
if (maxPositive + minNegative > 0) {
minNegative = 0 - maxPositive;
} else {
maxPositive = 0 - minNegative;
}
function normalizeNegative(value, min, max) {
return (((value - min) / (max - min)) * 100) - 100;
}
// -30, -40, 0
// -30 - -40 / 0 - -40
// 10 / 40
// 25%
// -75%
function normalizePositive(value, min, max) {
return ((value - min) / (max - min)) * 100;
}
// 30, 0, 40
// 30-0 / 40-0
// 75%
function normalize(value) {
if (value < 0) {
return normalizeNegative(value, minNegative, maxNegative);
} else {
return normalizePositive(value, minPositive, maxPositive);
}
}
const sumOfNormalizedValues = data.reduce((acc, row) => {
const originalValue = parseFloat(row[Object.keys(row)[1]]);
const normalizedValue = normalize(originalValue);
return acc + normalizedValue;
}, 0);
d3.select("#output").append("p").attr("class", "file-data").attr("id", `output-${fileName}`);
const outputDiv = d3.select(`#output-${fileName}`);
outputDiv.append("hr")
outputDiv.append("span").text(`${fileName}` + '.csv')
outputDiv.append("br")
outputDiv.append("span").text(`Range of positive importances: `).style("color", "green")
outputDiv.append("span").text(`${minPositiveACT}`).style("background-color", `rgba(0, 255, 0, ${Math.round(normalize(minPositiveACT)) / 100})`)
outputDiv.append("span").text(` to `)
outputDiv.append("span").text(`${maxPositiveACT}`).style("background-color", `rgba(0, 255, 0, ${Math.round(normalize(maxPositiveACT)) / 100})`)
outputDiv.append("span").text(`, total `)
outputDiv.append("span").text(`${numPositiveACT}`).style("color", "green")
outputDiv.append("span").text(` tokens`)
outputDiv.append("br")
outputDiv.append("span").text(`Range of negative importances: `).style("color", "red")
outputDiv.append("span").text(`${minNegativeACT}`).style("background-color", `rgba(255, 0, 0, ${Math.round(normalize(Math.abs(minNegativeACT))) / 100})`)
outputDiv.append("span").text(` to `)
outputDiv.append("span").text(`${maxNegativeACT}`).style("background-color", `rgba(255, 0, 0, ${Math.round(normalize(Math.abs(maxNegativeACT))) / 100})`)
outputDiv.append("span").text(`, total `)
outputDiv.append("span").text(`${numNegativeACT}`).style("color", "red")
outputDiv.append("span").text(` tokens`)
/*outputDiv.append("span").text(`Range of negative importances: ${minNegativeACT} to ${maxNegativeACT}, total ${numNegativeACT} tokens`)
outputDiv.append("span").text(`Range of negative importances: ${minNegativeACT} to ${maxNegativeACT}, total ${numNegativeACT} tokens`)
outputDiv.append("span").text(`Range of negative importances: ${minNegativeACT} to ${maxNegativeACT}, total ${numNegativeACT} tokens`)
outputDiv.append("span").text(`Range of negative importances: ${minNegativeACT} to ${maxNegativeACT}, total ${numNegativeACT} tokens`)
outputDiv.append("span").text(`Range of negative importances: ${minNegativeACT} to ${maxNegativeACT}, total ${numNegativeACT} tokens`)
outputDiv.append("span").text(`Range of negative importances: ${minNegativeACT} to ${maxNegativeACT}, total ${numNegativeACT} tokens`)
outputDiv.append("span").text(`Range of negative importances: ${minNegativeACT} to ${maxNegativeACT}, total ${numNegativeACT} tokens`)*/
outputDiv.append("br")
outputDiv.append("span").html(`DATA: <b> ${groundTruth} </b>`)
outputDiv.append("br")
outputDiv.append("span").html(` GPT: <b> ${prediction} </b>`)
outputDiv.append("br")
outputDiv.append("span").html(` ROT: <b> ${rotPredictions} </b>`)
outputDiv.append("br")
outputDiv.append("span").html(` sum: <b> ${sumOfValues} </b>`)
outputDiv.append("br")
outputDiv.append("span").html(`nsum: <b> ${sumOfNormalizedValues} </b>`)
outputDiv.append("br")
outputDiv.append("br")
outputDiv.append("br")
data.forEach(row => {
const originalValue = parseFloat(row[Object.keys(row)[1]]);
let tempNormalizedValue = normalize(originalValue);
if (row[Object.keys(row)[0]] === 'RACE=0' ||
row[Object.keys(row)[0]] === 'RACE=1' ||
row[Object.keys(row)[0]] === 'PARTY=0' ||
row[Object.keys(row)[0]] === 'PARTY=1' ||
row[Object.keys(row)[0]] === 'GENDER=0' ||
row[Object.keys(row)[0]] === 'GENDER=1') {
tempNormalizedValue = 100 * originalValue;
}
const normalizedValue = tempNormalizedValue;
const titleString = `OG: ${originalValue} | Viz: ${normalizedValue}`;
let backgroundColor;
if (normalizedValue >= 0) {
backgroundColor = `rgba(0, 255, 0, ${Math.round(normalizedValue) / 100})`;
} else {
backgroundColor = `rgba(255, 0, 0, ${Math.abs(Math.round(normalizedValue)) / 100})`;
}
outputDiv.append("span").text(' ');
outputDiv.append("span")
.text(row[Object.keys(row)[0]])
// .attr("title", normalizedValue.toFixed(2))
.attr("title", titleString)
.style("background-color", backgroundColor);
});
outputDiv.append("br")
outputDiv.append("hr")
// WORDCLOUD - BOOKKEEPING
// Separate positive and negative values
const positiveValues = [];
const negativeValues = [];
data.forEach(row => {
const key = Object.keys(row)[1];
const value = parseFloat(row[key]);
const token = row[Object.keys(row)[0]];
if (token.length > 2) {
if (value >= 0) {
positiveValues.push({ token, value: normalize(value) });
} else {
negativeValues.push({ token, value: normalize(value) });
}
}
});
// Function to get top N percentile values
function getTopPercentile(arr, percentile) {
const sortedArr = arr.sort((a, b) => b.value - a.value);
const topCount = Math.ceil(sortedArr.length * (percentile / 100));
return sortedArr.slice(0, topCount);
}
// Get top 10 percentile of largest positive values
const top10PercentilePositive = getTopPercentile(positiveValues, 10);
// Get top 10 percentile of largest magnitude negative values
const top10PercentileNegative = getTopPercentile(negativeValues.map(v => ({ token: v.token, value: Math.abs(v.value) })), 10);
// Logging results
// console.log("Top 10 Percentile Positive Values:");
// top10PercentilePositive.forEach(item => {
// console.log(`Token: ${item.token}, Value: ${item.value}`);
// });
// console.log("Top 10 Percentile Negative Values:");
// top10PercentileNegative.forEach(item => {
// console.log(`Token: ${item.token}, Value: ${-item.value}`); // Negate to get the original negative value
// });
poscloud.push(...top10PercentilePositive);
negcloud.push(...top10PercentileNegative);
// WORDCLOUD START
// Combine both sets of data, negating the negative values
}
function add_word_cloud() {
const mergedCloud = poscloud.concat(negcloud.map(item => ({ token: item.token, value: -item.value })));
console.log(negcloud)
const uniqueCloud = mergedCloud.reduce((acc, current) => {
const x = acc.find(item => item.token === current.token);
if (!x) {
return acc.concat([current]);
} else {
x.value += current.value;
return acc;
}
}, []);
console.log(mergedCloud)
console.log(uniqueCloud)
const uniquePoscloud = uniqueCloud.filter(item => item.value >= 0);
const uniqueNegcloud = uniqueCloud.filter(item => item.value < 0);
const sortedTop50PercentilePositive = uniquePoscloud.sort((a, b) => b.value - a.value).slice(0, 15);
const min_positive = Math.min(...sortedTop50PercentilePositive.map(i => i.value));
const max_positive = Math.max(...sortedTop50PercentilePositive.map(i => i.value));
const scaledTop50PercentilePositive = sortedTop50PercentilePositive.map(item => ({
token: item.token,
value: (item.value - min_positive) / (max_positive - min_positive) * 25 + 25
}));
// const scaledTop10PercentilePositive = uniquePoscloud.map(item => ({
// token: item.token,
// value: (item.value - min_positive) / (max_positive - min_positive) * 99 + 1
// }));
// const sortedScaledTop10PercentilePositive = scaledTop10PercentilePositive.sort((a, b) => Math.abs(b.value) - Math.abs(a.value)).slice(0, 50);
const sortedTop50PercentileNegative = uniqueNegcloud.sort((a, b) => a.value - b.value).slice(0, 15);
const min_negative = Math.min(...sortedTop50PercentileNegative.map(i => Math.abs(i.value)));
const max_negative = Math.max(...sortedTop50PercentileNegative.map(i => Math.abs(i.value)));
const scaledTop50PercentileNegative = sortedTop50PercentileNegative.map(item => ({
token: item.token,
value: ((Math.abs(item.value) - min_negative) / (max_negative - min_negative) * 25 + 25) * -1
}));
console.log(scaledTop50PercentilePositive);
console.log(scaledTop50PercentileNegative);
const combinedData = scaledTop50PercentilePositive.concat(
scaledTop50PercentileNegative.map(item => ({
token: item.token,
value: item.value
}))
);
console.log(combinedData);
console.log('-----')
d3.select("#cloud").innerHTML = "";
d3.select("#cloud").selectAll("*").remove();
// Set the dimensions for the word cloud
const width = 800;
const height = 400;
// Create the word cloud layout
const layout = d3.layout.cloud()
.size([width, height])
.words(combinedData.map(d => ({ text: d.token, size: d.value })))
.padding(10)
.rotate(() => ~~(Math.random() * 2) * 90)
.font("Arial")
.fontSize(d => d.size)
.on("end", draw);
layout.start();
// Draw the word cloud
function draw(words) {
d3.select("#cloud").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", d => Math.abs(d.size) + "px")
.style("font-family", "Arial")
.style("fill", d => d.size > 0 ? "green" : "red") // Set color based on positive or negative value
.attr("text-anchor", "middle")
.attr("transform", d => "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")")
.text(d => d.text);
}
// WORDCLOUD END
}
</script>
<h1 style="text-align: center; font-weight: bold;">Does ChatGPT think your resume is like <span class="itw" style="color: green;">IT worker</span> or more like <span class="teacher" style="color: red;">teacher</span></h1>
<div>
<div style="display: flex; justify-content: space-between;">
<div style="padding: 10px; max-width: 45%;">
<h2 style="text-align: center;">Words commonly associated with <span class="itw"> IT workers </span></h2>
<img src="DATA/wordclouds/positive_weights_wordcloud.png" style="max-width: 100%; padding: 10px;">
</div>
<div style="padding: 10px; max-width: 45%;">
<h2 style="text-align: center;">Words commonly associated with <span class="teacher">teachers</span> </h2>
<img src="DATA/wordclouds/negative_weights_wordcloud.png" style="max-width: 100%; padding: 10px;">
</div>
</div>
<div style="padding: 10px; max-width: 95%;">
<h2 style="text-align: center;"> Common WordCloud </h2>
<img src="DATA/wordclouds/all_wordcloud.png" style="max-width: 100%; padding: 10px;">
</div>
<p>
<a href="DATA/input_data_stats.txt" >Data Description</a>
<br>
<a href="DATA/summary_stats.txt" >RoT fit quality</a>
<br>
<a href="DATA/logreg-stats.txt" >LogReg fit quality</a>
</p>
<div style="clear: both;"></div>
<p>
<form onsubmit="fetchFiles(); return false;">
<label for="start">Start at Resume: </label>
<input type="number" id="start" name="start" value="0">
<label for="end">Go until Resume: </label>
<input type="number" id="end" name="end" value="5">
<label for="use_denorms">Use (Legacy) "DeNorms":</label>
<input type="checkbox" id="use_denorms" name="use_denorms">
<button type="submit">Visualise!</button>
<button style="float: right;" onclick="add_word_cloud()">Word Cloud</button>
</form>
<br>
</p>
</div>
<div style="background: white; border: thin solid black; padding: 30px;">
<div id="cloud"></div>
<br>
<div id="output"></div>
</div>
<div>
<p id="meta"></p>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
var csvFileName = '';
fetch('./INPUTS/')
.then(response => response.text())
.then(data => {
const parser = new DOMParser();
const doc = parser.parseFromString(data, 'text/html');
const links = doc.querySelectorAll('a');
links.forEach(link => {
if (link.href.endsWith('.csv')) {
csvFileName = link.href.split('/').pop();
}
});
if (csvFileName) {
console.log(`CSV file found: ${csvFileName}`);
console.log(csvFileName.split('_'));
let [part1, part2, part3] = csvFileName.split('_');
console.log(part1);
console.log(part2);
console.log(part3);
const itwElements = document.querySelectorAll('.itw');
itwElements.forEach(element => {
element.innerHTML = '"' + part1 + '"';
});
const teacherElements = document.querySelectorAll('.teacher');
teacherElements.forEach(element => {
element.innerHTML = '"' + part2 + '"';
});
} else {
console.log('No CSV file found in the ./INPUTS folder.');
}
})
.catch(error => console.error('Error fetching the directory:', error));
});
</script>
</body>
</html>