-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestViz2Script.js
186 lines (137 loc) · 6.63 KB
/
TestViz2Script.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
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
var population = 10000;
var prevalence = 0.5;
var sensitivity = 0.8;
var specificity = 0.8;
function updateDiagram() {
// Calculating values based on prevalence, sensitivity, and specificity
var positive = population * prevalence;
var negative = population - positive;
var truePositive = positive * sensitivity;
var falseNegative = positive - truePositive;
var trueNegative = negative * specificity;
var falsePositive = negative - trueNegative;
// Update Confusion Matrix
document.getElementById("truePositive").textContent = Math.round(truePositive);
document.getElementById("falsePositive").textContent = Math.round(falsePositive);
document.getElementById("trueNegative").textContent = Math.round(trueNegative);
document.getElementById("falseNegative").textContent = Math.round(falseNegative);
document.getElementById("totalPositive").textContent = Math.round(truePositive + falsePositive);
document.getElementById("totalNegative").textContent = Math.round(trueNegative + falseNegative);
document.getElementById("totalDisease").textContent = Math.round(positive);
document.getElementById("totalNoDisease").textContent = Math.round(negative);
// Calculate metrics based on Confusion Matrix
var calculatedSensitivity = truePositive / (truePositive + falseNegative);
var calculatedSpecificity = trueNegative / (trueNegative + falsePositive);
var PPV = truePositive / (truePositive + falsePositive);
var NPV = trueNegative / (trueNegative + falseNegative);
// Update calculations
document.getElementById("calculations").innerHTML = `
<tr>
<td>Sensitivity</td>
<td>${truePositive.toFixed()} / (${truePositive.toFixed()} + ${falseNegative.toFixed()}) = ${sensitivity.toFixed(2)}</td>
</tr>
<tr>
<td>Specificity</td>
<td>${trueNegative.toFixed()} / (${trueNegative.toFixed()} + ${falsePositive.toFixed()}) = ${specificity.toFixed(2)}</td>
</tr>
<tr>
<td>Positive Predictive Value (PPV)</td>
<td>${truePositive.toFixed()} / (${truePositive.toFixed()} + ${falsePositive.toFixed()}) = ${PPV.toFixed(2)}</td>
</tr>
<tr>
<td>Negative Predictive Value (NPV))</td>
<td>${trueNegative.toFixed()} / (${trueNegative.toFixed()} + ${falseNegative.toFixed()}) = ${NPV.toFixed(2)}</td>
</tr>
`;
// Update PPV and NPV visualizations
document.getElementById('PPVBar').value = PPV;
document.getElementById('NPVBar').value = NPV;
document.getElementById('PPVValue').textContent = PPV.toFixed(2);
document.getElementById('NPVValue').textContent = NPV.toFixed(2);
//new code
var relativeSize = document.getElementById('relativePopulationSize').checked;
var diseaseCanvasSize, nonDiseaseCanvasSize;
if (relativeSize) {
diseaseCanvasSize = prevalence;
nonDiseaseCanvasSize = 1 - prevalence;
} else {
diseaseCanvasSize = nonDiseaseCanvasSize = 0.5; // Same size for both
}
drawPieChart("diseaseCanvas", [truePositive, falseNegative], ["red", "pink"], diseaseCanvasSize);
drawPieChart("nonDiseaseCanvas", [trueNegative, falsePositive], ["green", "yellow"], nonDiseaseCanvasSize);
updateROC();
}
function updateROC() {
var TPR = sensitivity;
var FPR = 1 - specificity;
var canvas = document.getElementById('ROCCanvas');
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.moveTo(0, canvas.height);
ctx.lineTo(canvas.width, 0);
ctx.stroke();
ctx.beginPath();
ctx.arc(FPR * canvas.width, (1 - TPR) * canvas.height, 5, 0, 2 * Math.PI, false);
ctx.fill();
// Add labels
ctx.font = '14px Arial';
ctx.fillText('1 - Specificity', canvas.width / 2, canvas.height - 5);
ctx.save();
ctx.rotate(-Math.PI / 2);
ctx.textAlign = 'center';
ctx.fillText('Sensitivity', -canvas.height / 2, 15);
ctx.restore();
}
function drawPieChart(canvasId, data, colors, relativeSize) {
var canvas = document.getElementById(canvasId);
canvas.width = canvas.height = 200; // Keeping the canvas size constant
var ctx = canvas.getContext("2d");
var total = data.reduce((a, b) => a + b, 0);
var angleStart = 0;
// The center of the pie chart should be the center of the canvas
var centerX = canvas.width / 2;
var centerY = canvas.height / 2;
// Radius of pie chart relative to the size of the canvas
var radius = relativeSize * centerX;
for (var i in data) {
ctx.beginPath();
// Draw the pie chart around the center of the canvas
ctx.arc(centerX, centerY, radius, angleStart, angleStart + Math.PI * 2 * (data[i] / total), false);
ctx.lineTo(centerX, centerY);
ctx.fillStyle = colors[i];
ctx.fill();
angleStart += Math.PI * 2 * (data[i] / total);
}
}
//nee
//nee
function setupSliders() {
var prevalenceRange = document.getElementById('prevalenceRange');
var prevalenceValue = document.getElementById('prevalenceValue');
prevalenceValue.textContent = prevalenceRange.value;
prevalenceRange.oninput = function() {
prevalenceValue.textContent = this.value;
prevalence = this.value / 100;
updateDiagram();
}
var sensitivityRange = document.getElementById('sensitivityRange');
var sensitivityValue = document.getElementById('sensitivityValue');
sensitivityValue.textContent = sensitivityRange.value;
sensitivityRange.oninput = function() {
sensitivityValue.textContent = this.value;
sensitivity = this.value / 100;
updateDiagram();
}
var specificityRange = document.getElementById('specificityRange');
var specificityValue = document.getElementById('specificityValue');
specificityValue.textContent = specificityRange.value;
specificityRange.oninput = function() {
specificityValue.textContent = this.value;
specificity = this.value / 100;
updateDiagram();
}
}
document.getElementById('relativePopulationSize').addEventListener('change', updateDiagram);
setupSliders();
updateDiagram();