-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolyp_counter.js
More file actions
311 lines (263 loc) · 10.3 KB
/
polyp_counter.js
File metadata and controls
311 lines (263 loc) · 10.3 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
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
(function () {
"use strict";
const MAX_PATIENTS = 200;
const serratedPolyps = [
"traditional serrated adenoma", "sessile serrated adenoma", "sessile serrated polyp",
"sessile serrated polyps", "sessile serrated lesion", "sessile serrated lesions",
"sessile serrated adenomas", "sessile serrated lesion/polyp",
"sessile serrated lesion \\(sessile serrated polyp\\)",
"sessile serrated lesion \\(sessile serrated adenoma\\)",
"Traditional[\\s\\S]*?Serrated[\\s\\S]*?Adenoma",
];
const adenomatousPolyps = [
"tubular adenoma", "tubular adenomas", "tubulovillous adenoma", "tubular adenomata",
"villous adenoma", "tubulovillous adenoma \\(ta\\)", "villotubular adenoma",
"tubular adenomatous", "tubular adenoma \\(s\\)", "tubular adenoma\\(s\\)", "tubular adenomas",
];
let currentPatient = 1;
let allPatientsData = initializePatientsData();
function initializePatientsData() {
const patients = {};
for (let i = 1; i <= MAX_PATIENTS; i++) {
patients[`Patient ${i}`] = {
serrated: 0,
adenomatous: 0,
total: 0
};
}
return patients;
}
function normalizeText(text) {
return text.replace(/-\s*\n\s*/g, "")
.replace(/\s+/g, " ")
.replace(/[^\x20-\x7E]/g, "")
.toLowerCase()
.trim();
}
function createFloatingPanel() {
if (document.getElementById("polyp-counter-panel")) return;
const panel = document.createElement("div");
panel.id = "polyp-counter-panel";
panel.style.cssText = `
position: fixed;
bottom: 2%;
left: 1px;
width: 123px; /* Further reduced width */
padding: 4px; /* Further reduced padding */
background-color: white;
border: 1px solid #d1d1d1;
border-radius: 4px; /* Further reduced border radius */
z-index: 10000;
font-family: Arial, sans-serif;
font-size: 10px; /* Further reduced font size */
color: #333;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
`;
const title = document.createElement("h2");
title.textContent = "Polyp Counter";
title.style.cssText = `
text-align: center;
margin-bottom: 4px; /* Further reduced margin */
color: #4a4a4a;
font-size: 11px; /* Further reduced font size */
`;
panel.appendChild(title);
const patientNumberDiv = document.createElement("div");
patientNumberDiv.id = "patient-number";
patientNumberDiv.style.cssText = `
text-align: center;
margin-bottom: 6px;
font-weight: bold;
`;
panel.appendChild(patientNumberDiv);
const patientDetails = createPatientDetails();
panel.appendChild(patientDetails);
const navigationDiv = document.createElement("div");
navigationDiv.style.cssText = `
display: flex;
justify-content: space-between;
margin-top: 4px; /* Further reduced margin */
`;
const prevButton = document.createElement("button");
prevButton.textContent = "← Prev";
prevButton.style.cssText = `
font-size: 10px; /* Reduced button font size */
padding: 2px 4px; /* Reduced button padding */
`;
prevButton.onclick = moveToPreviousPatient;
const nextButton = document.createElement("button");
nextButton.textContent = "Next →";
nextButton.style.cssText = `
font-size: 10px; /* Reduced button font size */
padding: 2px 4px; /* Reduced button padding */
`;
nextButton.onclick = moveToNextPatient;
navigationDiv.appendChild(prevButton);
navigationDiv.appendChild(nextButton);
panel.appendChild(navigationDiv);
document.body.appendChild(panel);
updatePatientDetails(currentPatient);
}
function createPatientDetails() {
const patientDetails = document.createElement("div");
patientDetails.id = "current-patient-details";
patientDetails.style.cssText = `
background-color: #f8f8f8;
border-radius: 3px;
padding: 4px; /* Further reduced padding */
`;
const totalCount = createPolypDetail("Total:", `total-count`, "#2c3e50");
const serratedCount = createPolypDetail("Serrated:", `serrated-count`, "#c0392b");
const adenomatousCount = createPolypDetail("Adenomatous:", `adenomatous-count`, "#27ae60");
patientDetails.appendChild(totalCount);
patientDetails.appendChild(serratedCount);
patientDetails.appendChild(adenomatousCount);
return patientDetails;
}
function createPolypDetail(label, id, color) {
const detail = document.createElement("div");
detail.style.cssText = `
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 2px; /* Further reduced margin */
font-size: 10px; /* Further reduced font size */
`;
const labelText = document.createElement("span");
labelText.textContent = label;
labelText.style.fontWeight = "normal";
detail.appendChild(labelText);
const countSpan = document.createElement("span");
countSpan.id = id;
countSpan.style.cssText = `
font-weight: bold;
color: ${color};
font-size: 11px; /* Further reduced font size */
min-width: 16px; /* Further reduced min-width */
text-align: right;
`;
countSpan.textContent = "0";
detail.appendChild(countSpan);
return detail;
}
function updatePatientDetails(patientIndex) {
const patientData = allPatientsData[`Patient ${patientIndex}`];
document.getElementById("patient-number").textContent = `Patient ${patientIndex}`;
document.getElementById(`serrated-count`).textContent = patientData.serrated;
document.getElementById(`adenomatous-count`).textContent = patientData.adenomatous;
document.getElementById(`total-count`).textContent = patientData.total;
}
function saveAllData() {
// Your save logic here, if needed
}
function updateFloatingPanel() {
updatePatientDetails(currentPatient);
}
function countMatches(doc, patterns) {
let count = 0;
const regex = new RegExp(patterns.map((p, i) => `(?<p${i}>${p.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')})`).join('|'), 'gi');
let match;
while ((match = regex.exec(doc.text())) !== null) {
count += 1;
}
return count;
}
function countPolyps(patient) {
const text = document.body.innerText;
const normalizedText = normalizeText(text);
const doc = nlp(normalizedText);
const serratedCount = countMatches(doc, serratedPolyps);
const adenomatousCount = countMatches(doc, adenomatousPolyps);
const patientData = allPatientsData[`Patient ${patient}`];
patientData.serrated += serratedCount;
patientData.adenomatous += adenomatousCount;
patientData.total += serratedCount + adenomatousCount;
saveAllData();
updateFloatingPanel();
}
function incrementPolypCount(type, patient) {
const patientData = allPatientsData[`Patient ${patient}`];
if (type === "adenomatous") {
patientData.adenomatous += 1;
patientData.total += 1;
} else if (type === "serrated") {
patientData.serrated += 1;
patientData.total += 1;
}
saveAllData();
updateFloatingPanel();
}
function resetCounts(patient) {
const patientData = allPatientsData[`Patient ${patient}`];
patientData.serrated = 0;
patientData.adenomatous = 0;
patientData.total = 0;
saveAllData();
updateFloatingPanel();
}
function resetAllCounts() {
allPatientsData = initializePatientsData();
saveAllData();
currentPatient = 1;
updateFloatingPanel();
}
function handleValueChange(name, old_value, new_value, remote) {
if (name === 'currentPatient') {
currentPatient = new_value;
updatePatientDetails(new_value);
} else if (name === 'allPatientsData') {
allPatientsData = new_value;
updatePatientDetails(currentPatient);
}
}
createFloatingPanel();
updateFloatingPanel();
function updateCurrentPatient(patient) {
currentPatient = patient;
updatePatientDetails(patient);
}
function moveToNextPatient() {
currentPatient = currentPatient < MAX_PATIENTS ? currentPatient + 1 : 1;
updateCurrentPatient(currentPatient);
}
function moveToPreviousPatient() {
currentPatient = currentPatient > 1 ? currentPatient - 1 : MAX_PATIENTS;
updateCurrentPatient(currentPatient);
}
document.addEventListener("keydown", (event) => {
switch (event.key) {
case "F12":
if (event.shiftKey) {
moveToPreviousPatient();
} else {
moveToNextPatient();
}
break;
case "F1":
incrementPolypCount("adenomatous", currentPatient);
break;
case "F2":
incrementPolypCount("serrated", currentPatient);
break;
case "F3":
countPolyps(currentPatient);
break;
case "F4":
if (event.ctrlKey) {
resetAllCounts();
updateCurrentPatient(1); // Explicitly update to Patient 1
} else {
resetCounts(currentPatient);
}
break;
default:
// Add any additional shortcut handlers here
}
});
window.addEventListener("load", updateFloatingPanel);
document.addEventListener("visibilitychange", () => {
if (!document.hidden) {
updateFloatingPanel();
}
});
})();