Spaces:
Sleeping
Sleeping
Shengyi Zhou commited on
Commit ·
cf12e36
1
Parent(s): d5da71b
refactor: clean up static web demo
Browse files
README.md
CHANGED
|
@@ -1,45 +1,46 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
-
short_description:
|
| 9 |
---
|
| 10 |
|
| 11 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 12 |
-
|
| 13 |
# SAMI Web Demo
|
| 14 |
|
| 15 |
-
|
| 16 |
-
python -m http.server 8080
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
- supports loading local `.nrrd` files
|
| 26 |
-
- shows `axial`, `coronal`, and `sagittal` slice views
|
| 27 |
-
- supports `point` prompts (Add/Remove)
|
| 28 |
-
- applies each point interaction immediately (Slicer-like point workflow)
|
| 29 |
-
- runs a local demo mask directly in the browser
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
|
| 35 |
-
|
| 36 |
|
| 37 |
-
## Example data
|
| 38 |
|
| 39 |
-
The
|
| 40 |
|
| 41 |
-
|
| 42 |
-
-
|
| 43 |
|
| 44 |
-
|
| 45 |
|
|
|
|
| 1 |
---
|
| 2 |
+
title: SAMI Web Demo
|
| 3 |
+
emoji: 🧠
|
| 4 |
colorFrom: yellow
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
+
short_description: Browser-based prototype for 3D medical image interaction
|
| 9 |
---
|
| 10 |
|
|
|
|
|
|
|
| 11 |
# SAMI Web Demo
|
| 12 |
|
| 13 |
+
A static browser prototype demonstrating a lightweight 3D medical image workflow without 3D Slicer or a backend service.
|
|
|
|
| 14 |
|
| 15 |
+
## Features
|
| 16 |
|
| 17 |
+
- Loads the included brain MRI example by default.
|
| 18 |
+
- Loads local 3D `.nrrd` volumes.
|
| 19 |
+
- Displays axial, coronal, and sagittal slices.
|
| 20 |
+
- Supports positive and negative point prompts.
|
| 21 |
+
- Generates a simple local mask immediately after each point.
|
| 22 |
+
- Keeps all image processing inside the browser.
|
| 23 |
|
| 24 |
+
The generated mask is an intensity-based demonstration. It does not call the SAMI inference backend and is not intended for medical use.
|
| 25 |
|
| 26 |
+
## Run locally
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
From this directory, start a static file server:
|
| 29 |
+
|
| 30 |
+
```bash
|
| 31 |
+
python -m http.server 8080
|
| 32 |
+
```
|
| 33 |
|
| 34 |
+
Then open <http://localhost:8080/>.
|
| 35 |
|
| 36 |
+
A modern browser with `DecompressionStream` support is required for gzip-compressed NRRD files.
|
| 37 |
|
| 38 |
+
## Example data
|
| 39 |
|
| 40 |
+
The files in `examples/` come from [3D Slicer Sample Data](https://www.slicer.org/wiki/SampleData):
|
| 41 |
|
| 42 |
+
- `MRBrainTumor2.nrrd`
|
| 43 |
+
- `Panoramix-cropped.nrrd`
|
| 44 |
|
| 45 |
+
Refer to the dataset-specific acknowledgements in 3D Slicer Sample Data for original-source attribution.
|
| 46 |
|
app.js
CHANGED
|
@@ -10,25 +10,18 @@ const EXAMPLE_VOLUMES = {
|
|
| 10 |
};
|
| 11 |
|
| 12 |
const state = {
|
| 13 |
-
rawVolume: null,
|
| 14 |
volume: null,
|
| 15 |
-
rawType: "uint8",
|
| 16 |
dims: [96, 96, 64],
|
| 17 |
-
spacing: [1, 1, 1.6],
|
| 18 |
slices: {
|
| 19 |
axial: 32,
|
| 20 |
coronal: 48,
|
| 21 |
sagittal: 48,
|
| 22 |
},
|
| 23 |
-
activeTool: "point",
|
| 24 |
polarity: "positive",
|
| 25 |
prompts: [],
|
| 26 |
promptsVisible: true,
|
| 27 |
maskVisible: true,
|
| 28 |
mask: null,
|
| 29 |
-
drawing: null,
|
| 30 |
-
boxStart: null,
|
| 31 |
-
boxCurrent: null,
|
| 32 |
busy: false,
|
| 33 |
};
|
| 34 |
|
|
@@ -72,8 +65,6 @@ function init() {
|
|
| 72 |
}
|
| 73 |
|
| 74 |
function bindUI() {
|
| 75 |
-
state.activeTool = "point";
|
| 76 |
-
|
| 77 |
document.querySelectorAll("[data-polarity]").forEach((button) => {
|
| 78 |
button.addEventListener("click", () => {
|
| 79 |
document.querySelectorAll("[data-polarity]").forEach((node) => {
|
|
@@ -114,9 +105,6 @@ function bindUI() {
|
|
| 114 |
if (clearAllButton) {
|
| 115 |
clearAllButton.addEventListener("click", () => {
|
| 116 |
state.prompts = [];
|
| 117 |
-
state.boxStart = null;
|
| 118 |
-
state.boxCurrent = null;
|
| 119 |
-
state.drawing = null;
|
| 120 |
state.mask = new Uint8Array(totalVoxelCount());
|
| 121 |
renderPromptLog();
|
| 122 |
updateSegSummary();
|
|
@@ -146,15 +134,6 @@ function bindCanvas(viewName, canvas) {
|
|
| 146 |
canvas.addEventListener("mousedown", (event) => {
|
| 147 |
onPointerDown(viewName, canvas, event);
|
| 148 |
});
|
| 149 |
-
canvas.addEventListener("mousemove", (event) => {
|
| 150 |
-
onPointerMove(viewName, canvas, event);
|
| 151 |
-
});
|
| 152 |
-
canvas.addEventListener("mouseup", (event) => {
|
| 153 |
-
onPointerUp(viewName, canvas, event);
|
| 154 |
-
});
|
| 155 |
-
canvas.addEventListener("mouseleave", (event) => {
|
| 156 |
-
onPointerUp(viewName, canvas, event);
|
| 157 |
-
});
|
| 158 |
}
|
| 159 |
|
| 160 |
async function loadExampleVolume(example) {
|
|
@@ -171,11 +150,8 @@ async function loadExampleVolume(example) {
|
|
| 171 |
|
| 172 |
setVolume({
|
| 173 |
name: example.label,
|
| 174 |
-
|
| 175 |
-
displayData: normalizeToUint8(parsed.data),
|
| 176 |
-
rawType: parsed.type,
|
| 177 |
dims: parsed.sizes,
|
| 178 |
-
spacing: parsed.spacings,
|
| 179 |
});
|
| 180 |
setBackendMessage(`${example.label} loaded from the examples folder.`);
|
| 181 |
} catch (error) {
|
|
@@ -187,12 +163,9 @@ async function loadExampleVolume(example) {
|
|
| 187 |
}
|
| 188 |
}
|
| 189 |
|
| 190 |
-
function setVolume({ name,
|
| 191 |
-
state.
|
| 192 |
-
state.volume = displayData;
|
| 193 |
-
state.rawType = rawType || inferRawType(rawData);
|
| 194 |
state.dims = dims;
|
| 195 |
-
state.spacing = spacing || [1, 1, 1];
|
| 196 |
state.mask = new Uint8Array(totalVoxelCount(dims));
|
| 197 |
state.prompts = [];
|
| 198 |
state.promptsVisible = true;
|
|
@@ -241,11 +214,8 @@ async function handleVolumeUpload(event) {
|
|
| 241 |
const parsed = await parseNrrdFile(file);
|
| 242 |
setVolume({
|
| 243 |
name: file.name,
|
| 244 |
-
|
| 245 |
-
displayData: normalizeToUint8(parsed.data),
|
| 246 |
-
rawType: parsed.type,
|
| 247 |
dims: parsed.sizes,
|
| 248 |
-
spacing: parsed.spacings,
|
| 249 |
});
|
| 250 |
setBackendMessage(`Loaded ${file.name}. Ready for mock or live segmentation.`);
|
| 251 |
} catch (error) {
|
|
@@ -268,102 +238,17 @@ function onPointerDown(viewName, canvas, event) {
|
|
| 268 |
return;
|
| 269 |
}
|
| 270 |
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
setBackendMessage("Point prompt applied immediately (Slicer-like flow).");
|
| 283 |
-
return;
|
| 284 |
-
}
|
| 285 |
-
|
| 286 |
-
if (state.activeTool === "box") {
|
| 287 |
-
state.boxStart = { view: viewName, xyz: voxel };
|
| 288 |
-
state.boxCurrent = voxel;
|
| 289 |
-
drawAll();
|
| 290 |
-
return;
|
| 291 |
-
}
|
| 292 |
-
|
| 293 |
-
if (state.activeTool === "scribble") {
|
| 294 |
-
state.drawing = {
|
| 295 |
-
view: viewName,
|
| 296 |
-
polarity: state.polarity,
|
| 297 |
-
points: [voxel],
|
| 298 |
-
};
|
| 299 |
-
drawAll();
|
| 300 |
-
}
|
| 301 |
-
}
|
| 302 |
-
|
| 303 |
-
function onPointerMove(viewName, canvas, event) {
|
| 304 |
-
const voxel = canvasEventToVoxel(viewName, canvas, event);
|
| 305 |
-
|
| 306 |
-
if (
|
| 307 |
-
state.activeTool === "box" &&
|
| 308 |
-
state.boxStart &&
|
| 309 |
-
state.boxStart.view === viewName &&
|
| 310 |
-
voxel
|
| 311 |
-
) {
|
| 312 |
-
state.boxCurrent = voxel;
|
| 313 |
-
drawAll();
|
| 314 |
-
return;
|
| 315 |
-
}
|
| 316 |
-
|
| 317 |
-
if (!state.drawing || state.drawing.view !== viewName || !voxel) {
|
| 318 |
-
return;
|
| 319 |
-
}
|
| 320 |
-
|
| 321 |
-
const points = state.drawing.points;
|
| 322 |
-
const last = points[points.length - 1];
|
| 323 |
-
if (!last || voxelDistance(last, voxel) >= 1) {
|
| 324 |
-
points.push(voxel);
|
| 325 |
-
drawAll();
|
| 326 |
-
}
|
| 327 |
-
}
|
| 328 |
-
|
| 329 |
-
function onPointerUp(viewName, canvas, event) {
|
| 330 |
-
const voxel = canvasEventToVoxel(viewName, canvas, event);
|
| 331 |
-
|
| 332 |
-
if (state.activeTool === "box" && state.boxStart && state.boxStart.view === viewName) {
|
| 333 |
-
if (voxel) {
|
| 334 |
-
pushPrompt({
|
| 335 |
-
type: "box",
|
| 336 |
-
polarity: state.polarity,
|
| 337 |
-
view: viewName,
|
| 338 |
-
start: state.boxStart.xyz,
|
| 339 |
-
end: voxel,
|
| 340 |
-
});
|
| 341 |
-
}
|
| 342 |
-
state.boxStart = null;
|
| 343 |
-
state.boxCurrent = null;
|
| 344 |
-
drawAll();
|
| 345 |
-
return;
|
| 346 |
-
}
|
| 347 |
-
|
| 348 |
-
if (
|
| 349 |
-
state.activeTool === "scribble" &&
|
| 350 |
-
state.drawing &&
|
| 351 |
-
state.drawing.view === viewName
|
| 352 |
-
) {
|
| 353 |
-
if (voxel) {
|
| 354 |
-
state.drawing.points.push(voxel);
|
| 355 |
-
}
|
| 356 |
-
if (state.drawing.points.length > 1) {
|
| 357 |
-
pushPrompt({
|
| 358 |
-
type: "scribble",
|
| 359 |
-
polarity: state.drawing.polarity,
|
| 360 |
-
view: viewName,
|
| 361 |
-
points: densifyPath(state.drawing.points.slice()),
|
| 362 |
-
});
|
| 363 |
-
}
|
| 364 |
-
state.drawing = null;
|
| 365 |
-
drawAll();
|
| 366 |
-
}
|
| 367 |
}
|
| 368 |
|
| 369 |
function pushPrompt(prompt) {
|
|
@@ -391,13 +276,7 @@ function renderPromptLog() {
|
|
| 391 |
row.className = "prompt-row";
|
| 392 |
const index = state.prompts.length - reverseIndex;
|
| 393 |
|
| 394 |
-
|
| 395 |
-
row.textContent = `#${index} ${prompt.polarity} point on ${prompt.view} at (${prompt.xyz.join(", ")})`;
|
| 396 |
-
} else if (prompt.type === "box") {
|
| 397 |
-
row.textContent = `#${index} ${prompt.polarity} box on ${prompt.view}`;
|
| 398 |
-
} else {
|
| 399 |
-
row.textContent = `#${index} ${prompt.polarity} scribble on ${prompt.view} with ${prompt.points.length} points`;
|
| 400 |
-
}
|
| 401 |
|
| 402 |
promptLogEl.appendChild(row);
|
| 403 |
});
|
|
@@ -465,61 +344,12 @@ function drawPromptOverlay(context, viewName, width, height, sourceWidth, source
|
|
| 465 |
? "rgba(72, 245, 194, 0.22)"
|
| 466 |
: "rgba(255, 117, 107, 0.18)";
|
| 467 |
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
context.stroke();
|
| 474 |
-
} else if (prompt.type === "box") {
|
| 475 |
-
drawOverlayBox(context, viewName, prompt.start, prompt.end, scaleX, scaleY);
|
| 476 |
-
} else {
|
| 477 |
-
drawOverlayPath(context, viewName, prompt.points, scaleX, scaleY);
|
| 478 |
-
}
|
| 479 |
});
|
| 480 |
-
|
| 481 |
-
if (state.boxStart && state.boxCurrent && state.boxStart.view === viewName) {
|
| 482 |
-
context.lineWidth = 2;
|
| 483 |
-
context.strokeStyle = state.polarity === "positive" ? "#48f5c2" : "#ff756b";
|
| 484 |
-
context.fillStyle =
|
| 485 |
-
state.polarity === "positive"
|
| 486 |
-
? "rgba(72, 245, 194, 0.14)"
|
| 487 |
-
: "rgba(255, 117, 107, 0.12)";
|
| 488 |
-
drawOverlayBox(context, viewName, state.boxStart.xyz, state.boxCurrent, scaleX, scaleY);
|
| 489 |
-
}
|
| 490 |
-
|
| 491 |
-
if (state.drawing && state.drawing.view === viewName && state.drawing.points.length > 0) {
|
| 492 |
-
context.lineWidth = 2;
|
| 493 |
-
context.strokeStyle = state.drawing.polarity === "positive" ? "#48f5c2" : "#ff756b";
|
| 494 |
-
drawOverlayPath(context, viewName, state.drawing.points, scaleX, scaleY);
|
| 495 |
-
}
|
| 496 |
-
}
|
| 497 |
-
|
| 498 |
-
function drawOverlayBox(context, viewName, start, end, scaleX, scaleY) {
|
| 499 |
-
const [u1, v1] = voxelToViewCoords(viewName, start);
|
| 500 |
-
const [u2, v2] = voxelToViewCoords(viewName, end);
|
| 501 |
-
const left = Math.min(u1, u2) * scaleX;
|
| 502 |
-
const top = Math.min(v1, v2) * scaleY;
|
| 503 |
-
const boxWidth = (Math.abs(u1 - u2) + 1) * scaleX;
|
| 504 |
-
const boxHeight = (Math.abs(v1 - v2) + 1) * scaleY;
|
| 505 |
-
|
| 506 |
-
context.fillRect(left, top, boxWidth, boxHeight);
|
| 507 |
-
context.strokeRect(left, top, boxWidth, boxHeight);
|
| 508 |
-
}
|
| 509 |
-
|
| 510 |
-
function drawOverlayPath(context, viewName, points, scaleX, scaleY) {
|
| 511 |
-
context.beginPath();
|
| 512 |
-
points.forEach((point, index) => {
|
| 513 |
-
const [u, v] = voxelToViewCoords(viewName, point);
|
| 514 |
-
const px = (u + 0.5) * scaleX;
|
| 515 |
-
const py = (v + 0.5) * scaleY;
|
| 516 |
-
if (index === 0) {
|
| 517 |
-
context.moveTo(px, py);
|
| 518 |
-
} else {
|
| 519 |
-
context.lineTo(px, py);
|
| 520 |
-
}
|
| 521 |
-
});
|
| 522 |
-
context.stroke();
|
| 523 |
}
|
| 524 |
|
| 525 |
function extractSlice(viewName, sliceIndex) {
|
|
@@ -610,48 +440,6 @@ function applyPointPrompt(mask, prompt) {
|
|
| 610 |
}
|
| 611 |
}
|
| 612 |
|
| 613 |
-
function applyBoxPrompt(mask, prompt) {
|
| 614 |
-
const [minX, maxX] = sortPair(prompt.start[0], prompt.end[0]);
|
| 615 |
-
const [minY, maxY] = sortPair(prompt.start[1], prompt.end[1]);
|
| 616 |
-
const [minZ, maxZ] = sortPair(prompt.start[2], prompt.end[2]);
|
| 617 |
-
|
| 618 |
-
let expandedMinX = minX;
|
| 619 |
-
let expandedMaxX = maxX;
|
| 620 |
-
let expandedMinY = minY;
|
| 621 |
-
let expandedMaxY = maxY;
|
| 622 |
-
let expandedMinZ = minZ;
|
| 623 |
-
let expandedMaxZ = maxZ;
|
| 624 |
-
|
| 625 |
-
if (prompt.view === "axial") {
|
| 626 |
-
expandedMinZ = Math.max(0, minZ - 4);
|
| 627 |
-
expandedMaxZ = Math.min(state.dims[2] - 1, maxZ + 4);
|
| 628 |
-
} else if (prompt.view === "coronal") {
|
| 629 |
-
expandedMinY = Math.max(0, minY - 4);
|
| 630 |
-
expandedMaxY = Math.min(state.dims[1] - 1, maxY + 4);
|
| 631 |
-
} else {
|
| 632 |
-
expandedMinX = Math.max(0, minX - 4);
|
| 633 |
-
expandedMaxX = Math.min(state.dims[0] - 1, maxX + 4);
|
| 634 |
-
}
|
| 635 |
-
|
| 636 |
-
for (let z = expandedMinZ; z <= expandedMaxZ; z += 1) {
|
| 637 |
-
for (let y = expandedMinY; y <= expandedMaxY; y += 1) {
|
| 638 |
-
for (let x = expandedMinX; x <= expandedMaxX; x += 1) {
|
| 639 |
-
mask[xyzToIndex(x, y, z)] = prompt.polarity === "positive" ? 1 : 0;
|
| 640 |
-
}
|
| 641 |
-
}
|
| 642 |
-
}
|
| 643 |
-
}
|
| 644 |
-
|
| 645 |
-
function applyScribblePrompt(mask, prompt) {
|
| 646 |
-
prompt.points.forEach((point) => {
|
| 647 |
-
applyPointPrompt(mask, {
|
| 648 |
-
type: "point",
|
| 649 |
-
polarity: prompt.polarity,
|
| 650 |
-
xyz: point,
|
| 651 |
-
});
|
| 652 |
-
});
|
| 653 |
-
}
|
| 654 |
-
|
| 655 |
function updateSegSummary() {
|
| 656 |
let count = 0;
|
| 657 |
for (let i = 0; i < state.mask.length; i += 1) {
|
|
@@ -745,8 +533,6 @@ async function parseNrrdBytes(bytes) {
|
|
| 745 |
|
| 746 |
return {
|
| 747 |
sizes: header.sizes,
|
| 748 |
-
spacings: header.spacings || [1, 1, 1],
|
| 749 |
-
type: header.type,
|
| 750 |
data: decodeTypedData(payload.buffer, header.type, header.endian || "little"),
|
| 751 |
};
|
| 752 |
}
|
|
@@ -774,14 +560,6 @@ function parseNrrdHeader(text, delimiter, bytes) {
|
|
| 774 |
result.encoding = value.toLowerCase();
|
| 775 |
} else if (key === "endian") {
|
| 776 |
result.endian = value.toLowerCase();
|
| 777 |
-
} else if (key === "space directions") {
|
| 778 |
-
result.spacings = value.split(/\)\s+\(/).map((token) => {
|
| 779 |
-
const cleaned = token.replace(/[()]/g, "");
|
| 780 |
-
const parts = cleaned.split(",").map((part) => Number(part.trim()) || 0);
|
| 781 |
-
return Math.sqrt(parts.reduce((sum, item) => sum + item * item, 0)) || 1;
|
| 782 |
-
});
|
| 783 |
-
} else if (key === "spacings") {
|
| 784 |
-
result.spacings = value.split(/\s+/).map((item) => Number(item) || 1);
|
| 785 |
} else if (key === "data file") {
|
| 786 |
result.detached = true;
|
| 787 |
}
|
|
@@ -888,28 +666,6 @@ function normalizeToUint8(data) {
|
|
| 888 |
return output;
|
| 889 |
}
|
| 890 |
|
| 891 |
-
function inferRawType(data) {
|
| 892 |
-
if (data instanceof Uint8Array) {
|
| 893 |
-
return "uint8";
|
| 894 |
-
}
|
| 895 |
-
if (data instanceof Int8Array) {
|
| 896 |
-
return "int8";
|
| 897 |
-
}
|
| 898 |
-
if (data instanceof Int16Array) {
|
| 899 |
-
return "int16";
|
| 900 |
-
}
|
| 901 |
-
if (data instanceof Uint16Array) {
|
| 902 |
-
return "uint16";
|
| 903 |
-
}
|
| 904 |
-
if (data instanceof Float32Array) {
|
| 905 |
-
return "float32";
|
| 906 |
-
}
|
| 907 |
-
if (data instanceof Float64Array) {
|
| 908 |
-
return "float64";
|
| 909 |
-
}
|
| 910 |
-
throw new Error("Unsupported typed array");
|
| 911 |
-
}
|
| 912 |
-
|
| 913 |
function normalizeNrrdType(type) {
|
| 914 |
const mapping = {
|
| 915 |
uchar: "uint8",
|
|
@@ -952,137 +708,6 @@ function createTypedArray(type, count) {
|
|
| 952 |
}
|
| 953 |
}
|
| 954 |
|
| 955 |
-
function createNpyBuffer(typedArray, dtype, shape) {
|
| 956 |
-
const descr = numpyDescr(dtype);
|
| 957 |
-
const shapeText = `(${shape.join(", ")}${shape.length === 1 ? "," : ""})`;
|
| 958 |
-
let header = `{'descr': '${descr}', 'fortran_order': False, 'shape': ${shapeText}, }`;
|
| 959 |
-
const magic = new Uint8Array([0x93, 0x4e, 0x55, 0x4d, 0x50, 0x59, 0x01, 0x00]);
|
| 960 |
-
const encoder = new TextEncoder();
|
| 961 |
-
let headerBytes = encoder.encode(`${header}\n`);
|
| 962 |
-
const padLength = (16 - ((magic.length + 2 + headerBytes.length) % 16)) % 16;
|
| 963 |
-
header = `${header}${" ".repeat(padLength)}\n`;
|
| 964 |
-
headerBytes = encoder.encode(header);
|
| 965 |
-
|
| 966 |
-
const headerLen = headerBytes.length;
|
| 967 |
-
const dataBytes = new Uint8Array(
|
| 968 |
-
typedArray.buffer,
|
| 969 |
-
typedArray.byteOffset,
|
| 970 |
-
typedArray.byteLength
|
| 971 |
-
);
|
| 972 |
-
const output = new Uint8Array(magic.length + 2 + headerLen + dataBytes.length);
|
| 973 |
-
|
| 974 |
-
output.set(magic, 0);
|
| 975 |
-
output[8] = headerLen & 0xff;
|
| 976 |
-
output[9] = (headerLen >> 8) & 0xff;
|
| 977 |
-
output.set(headerBytes, 10);
|
| 978 |
-
output.set(dataBytes, 10 + headerLen);
|
| 979 |
-
|
| 980 |
-
return output;
|
| 981 |
-
}
|
| 982 |
-
|
| 983 |
-
function numpyDescr(dtype) {
|
| 984 |
-
switch (dtype) {
|
| 985 |
-
case "int8":
|
| 986 |
-
return "|i1";
|
| 987 |
-
case "uint8":
|
| 988 |
-
return "|u1";
|
| 989 |
-
case "int16":
|
| 990 |
-
return "<i2";
|
| 991 |
-
case "uint16":
|
| 992 |
-
return "<u2";
|
| 993 |
-
case "float32":
|
| 994 |
-
return "<f4";
|
| 995 |
-
case "float64":
|
| 996 |
-
return "<f8";
|
| 997 |
-
default:
|
| 998 |
-
throw new Error(`Unsupported dtype for NPY export: ${dtype}`);
|
| 999 |
-
}
|
| 1000 |
-
}
|
| 1001 |
-
|
| 1002 |
-
async function gzipBytes(bytes) {
|
| 1003 |
-
if (typeof CompressionStream === "undefined") {
|
| 1004 |
-
throw new Error("This browser cannot gzip data for live backend mode.");
|
| 1005 |
-
}
|
| 1006 |
-
|
| 1007 |
-
const stream = new Blob([bytes]).stream().pipeThrough(new CompressionStream("gzip"));
|
| 1008 |
-
return new Uint8Array(await new Response(stream).arrayBuffer());
|
| 1009 |
-
}
|
| 1010 |
-
|
| 1011 |
-
function unpackBinarySegmentation(buffer) {
|
| 1012 |
-
const source = new Uint8Array(buffer);
|
| 1013 |
-
const total = totalVoxelCount();
|
| 1014 |
-
const mask = new Uint8Array(total);
|
| 1015 |
-
let writeIndex = 0;
|
| 1016 |
-
|
| 1017 |
-
for (let i = 0; i < source.length && writeIndex < total; i += 1) {
|
| 1018 |
-
const byte = source[i];
|
| 1019 |
-
for (let bit = 7; bit >= 0 && writeIndex < total; bit -= 1) {
|
| 1020 |
-
mask[writeIndex] = (byte >> bit) & 1;
|
| 1021 |
-
writeIndex += 1;
|
| 1022 |
-
}
|
| 1023 |
-
}
|
| 1024 |
-
|
| 1025 |
-
return mask;
|
| 1026 |
-
}
|
| 1027 |
-
|
| 1028 |
-
function createScribbleMask(prompt) {
|
| 1029 |
-
const mask = new Uint8Array(totalVoxelCount());
|
| 1030 |
-
|
| 1031 |
-
prompt.points.forEach((point) => {
|
| 1032 |
-
const radius = 2;
|
| 1033 |
-
const [cx, cy, cz] = point;
|
| 1034 |
-
for (let z = Math.max(0, cz - 1); z <= Math.min(state.dims[2] - 1, cz + 1); z += 1) {
|
| 1035 |
-
for (
|
| 1036 |
-
let y = Math.max(0, cy - radius);
|
| 1037 |
-
y <= Math.min(state.dims[1] - 1, cy + radius);
|
| 1038 |
-
y += 1
|
| 1039 |
-
) {
|
| 1040 |
-
for (
|
| 1041 |
-
let x = Math.max(0, cx - radius);
|
| 1042 |
-
x <= Math.min(state.dims[0] - 1, cx + radius);
|
| 1043 |
-
x += 1
|
| 1044 |
-
) {
|
| 1045 |
-
const dx = x - cx;
|
| 1046 |
-
const dy = y - cy;
|
| 1047 |
-
if (dx * dx + dy * dy <= radius * radius) {
|
| 1048 |
-
mask[xyzToIndex(x, y, z)] = 1;
|
| 1049 |
-
}
|
| 1050 |
-
}
|
| 1051 |
-
}
|
| 1052 |
-
}
|
| 1053 |
-
});
|
| 1054 |
-
|
| 1055 |
-
return mask;
|
| 1056 |
-
}
|
| 1057 |
-
|
| 1058 |
-
function densifyPath(points) {
|
| 1059 |
-
if (points.length < 2) {
|
| 1060 |
-
return points;
|
| 1061 |
-
}
|
| 1062 |
-
|
| 1063 |
-
const output = [points[0]];
|
| 1064 |
-
for (let i = 1; i < points.length; i += 1) {
|
| 1065 |
-
const start = points[i - 1];
|
| 1066 |
-
const end = points[i];
|
| 1067 |
-
const steps = Math.max(
|
| 1068 |
-
Math.abs(end[0] - start[0]),
|
| 1069 |
-
Math.abs(end[1] - start[1]),
|
| 1070 |
-
Math.abs(end[2] - start[2])
|
| 1071 |
-
);
|
| 1072 |
-
|
| 1073 |
-
for (let step = 1; step <= steps; step += 1) {
|
| 1074 |
-
const t = step / Math.max(1, steps);
|
| 1075 |
-
output.push([
|
| 1076 |
-
Math.round(start[0] + (end[0] - start[0]) * t),
|
| 1077 |
-
Math.round(start[1] + (end[1] - start[1]) * t),
|
| 1078 |
-
Math.round(start[2] + (end[2] - start[2]) * t),
|
| 1079 |
-
]);
|
| 1080 |
-
}
|
| 1081 |
-
}
|
| 1082 |
-
|
| 1083 |
-
return output;
|
| 1084 |
-
}
|
| 1085 |
-
|
| 1086 |
async function ensureOk(response, endpointName) {
|
| 1087 |
if (response.ok) {
|
| 1088 |
return;
|
|
@@ -1141,14 +766,3 @@ function clamp(value, min, max) {
|
|
| 1141 |
function clampToByte(value) {
|
| 1142 |
return clamp(Math.round(value), 0, 255);
|
| 1143 |
}
|
| 1144 |
-
|
| 1145 |
-
function sortPair(a, b) {
|
| 1146 |
-
return a <= b ? [a, b] : [b, a];
|
| 1147 |
-
}
|
| 1148 |
-
|
| 1149 |
-
function voxelDistance(a, b) {
|
| 1150 |
-
const dx = a[0] - b[0];
|
| 1151 |
-
const dy = a[1] - b[1];
|
| 1152 |
-
const dz = a[2] - b[2];
|
| 1153 |
-
return Math.sqrt(dx * dx + dy * dy + dz * dz);
|
| 1154 |
-
}
|
|
|
|
| 10 |
};
|
| 11 |
|
| 12 |
const state = {
|
|
|
|
| 13 |
volume: null,
|
|
|
|
| 14 |
dims: [96, 96, 64],
|
|
|
|
| 15 |
slices: {
|
| 16 |
axial: 32,
|
| 17 |
coronal: 48,
|
| 18 |
sagittal: 48,
|
| 19 |
},
|
|
|
|
| 20 |
polarity: "positive",
|
| 21 |
prompts: [],
|
| 22 |
promptsVisible: true,
|
| 23 |
maskVisible: true,
|
| 24 |
mask: null,
|
|
|
|
|
|
|
|
|
|
| 25 |
busy: false,
|
| 26 |
};
|
| 27 |
|
|
|
|
| 65 |
}
|
| 66 |
|
| 67 |
function bindUI() {
|
|
|
|
|
|
|
| 68 |
document.querySelectorAll("[data-polarity]").forEach((button) => {
|
| 69 |
button.addEventListener("click", () => {
|
| 70 |
document.querySelectorAll("[data-polarity]").forEach((node) => {
|
|
|
|
| 105 |
if (clearAllButton) {
|
| 106 |
clearAllButton.addEventListener("click", () => {
|
| 107 |
state.prompts = [];
|
|
|
|
|
|
|
|
|
|
| 108 |
state.mask = new Uint8Array(totalVoxelCount());
|
| 109 |
renderPromptLog();
|
| 110 |
updateSegSummary();
|
|
|
|
| 134 |
canvas.addEventListener("mousedown", (event) => {
|
| 135 |
onPointerDown(viewName, canvas, event);
|
| 136 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
}
|
| 138 |
|
| 139 |
async function loadExampleVolume(example) {
|
|
|
|
| 150 |
|
| 151 |
setVolume({
|
| 152 |
name: example.label,
|
| 153 |
+
data: normalizeToUint8(parsed.data),
|
|
|
|
|
|
|
| 154 |
dims: parsed.sizes,
|
|
|
|
| 155 |
});
|
| 156 |
setBackendMessage(`${example.label} loaded from the examples folder.`);
|
| 157 |
} catch (error) {
|
|
|
|
| 163 |
}
|
| 164 |
}
|
| 165 |
|
| 166 |
+
function setVolume({ name, data, dims }) {
|
| 167 |
+
state.volume = data;
|
|
|
|
|
|
|
| 168 |
state.dims = dims;
|
|
|
|
| 169 |
state.mask = new Uint8Array(totalVoxelCount(dims));
|
| 170 |
state.prompts = [];
|
| 171 |
state.promptsVisible = true;
|
|
|
|
| 214 |
const parsed = await parseNrrdFile(file);
|
| 215 |
setVolume({
|
| 216 |
name: file.name,
|
| 217 |
+
data: normalizeToUint8(parsed.data),
|
|
|
|
|
|
|
| 218 |
dims: parsed.sizes,
|
|
|
|
| 219 |
});
|
| 220 |
setBackendMessage(`Loaded ${file.name}. Ready for mock or live segmentation.`);
|
| 221 |
} catch (error) {
|
|
|
|
| 238 |
return;
|
| 239 |
}
|
| 240 |
|
| 241 |
+
const prompt = {
|
| 242 |
+
type: "point",
|
| 243 |
+
polarity: state.polarity,
|
| 244 |
+
view: viewName,
|
| 245 |
+
xyz: voxel,
|
| 246 |
+
};
|
| 247 |
+
pushPrompt(prompt);
|
| 248 |
+
applyPointPrompt(state.mask, prompt);
|
| 249 |
+
updateSegSummary();
|
| 250 |
+
drawAll();
|
| 251 |
+
setBackendMessage("Point prompt applied immediately (Slicer-like flow).");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
}
|
| 253 |
|
| 254 |
function pushPrompt(prompt) {
|
|
|
|
| 276 |
row.className = "prompt-row";
|
| 277 |
const index = state.prompts.length - reverseIndex;
|
| 278 |
|
| 279 |
+
row.textContent = `#${index} ${prompt.polarity} point on ${prompt.view} at (${prompt.xyz.join(", ")})`;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
promptLogEl.appendChild(row);
|
| 282 |
});
|
|
|
|
| 344 |
? "rgba(72, 245, 194, 0.22)"
|
| 345 |
: "rgba(255, 117, 107, 0.18)";
|
| 346 |
|
| 347 |
+
const [u, v] = voxelToViewCoords(viewName, prompt.xyz);
|
| 348 |
+
context.beginPath();
|
| 349 |
+
context.arc((u + 0.5) * scaleX, (v + 0.5) * scaleY, 3, 0, Math.PI * 2);
|
| 350 |
+
context.fill();
|
| 351 |
+
context.stroke();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
}
|
| 354 |
|
| 355 |
function extractSlice(viewName, sliceIndex) {
|
|
|
|
| 440 |
}
|
| 441 |
}
|
| 442 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 443 |
function updateSegSummary() {
|
| 444 |
let count = 0;
|
| 445 |
for (let i = 0; i < state.mask.length; i += 1) {
|
|
|
|
| 533 |
|
| 534 |
return {
|
| 535 |
sizes: header.sizes,
|
|
|
|
|
|
|
| 536 |
data: decodeTypedData(payload.buffer, header.type, header.endian || "little"),
|
| 537 |
};
|
| 538 |
}
|
|
|
|
| 560 |
result.encoding = value.toLowerCase();
|
| 561 |
} else if (key === "endian") {
|
| 562 |
result.endian = value.toLowerCase();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
} else if (key === "data file") {
|
| 564 |
result.detached = true;
|
| 565 |
}
|
|
|
|
| 666 |
return output;
|
| 667 |
}
|
| 668 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 669 |
function normalizeNrrdType(type) {
|
| 670 |
const mapping = {
|
| 671 |
uchar: "uint8",
|
|
|
|
| 708 |
}
|
| 709 |
}
|
| 710 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 711 |
async function ensureOk(response, endpointName) {
|
| 712 |
if (response.ok) {
|
| 713 |
return;
|
|
|
|
| 766 |
function clampToByte(value) {
|
| 767 |
return clamp(Math.round(value), 0, 255);
|
| 768 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
style.css
CHANGED
|
@@ -59,8 +59,7 @@ label {
|
|
| 59 |
.eyebrow,
|
| 60 |
.slice-subtitle,
|
| 61 |
.status-label,
|
| 62 |
-
.hint
|
| 63 |
-
.field-label {
|
| 64 |
color: var(--muted);
|
| 65 |
}
|
| 66 |
|
|
@@ -173,10 +172,6 @@ button:disabled {
|
|
| 173 |
margin-top: 12px;
|
| 174 |
}
|
| 175 |
|
| 176 |
-
.two-up {
|
| 177 |
-
grid-template-columns: repeat(2, minmax(0, 1fr));
|
| 178 |
-
}
|
| 179 |
-
|
| 180 |
.polarity-grid {
|
| 181 |
grid-template-columns: repeat(2, minmax(0, 1fr));
|
| 182 |
}
|
|
@@ -209,24 +204,6 @@ button:disabled {
|
|
| 209 |
margin-top: 12px;
|
| 210 |
}
|
| 211 |
|
| 212 |
-
.compact-stack {
|
| 213 |
-
gap: 8px;
|
| 214 |
-
}
|
| 215 |
-
|
| 216 |
-
.field-label {
|
| 217 |
-
font-size: 0.92rem;
|
| 218 |
-
}
|
| 219 |
-
|
| 220 |
-
.text-input {
|
| 221 |
-
width: 100%;
|
| 222 |
-
min-height: 44px;
|
| 223 |
-
border-radius: 14px;
|
| 224 |
-
border: 1px solid var(--line);
|
| 225 |
-
background: rgba(255, 255, 255, 0.78);
|
| 226 |
-
padding: 0 14px;
|
| 227 |
-
color: var(--ink);
|
| 228 |
-
}
|
| 229 |
-
|
| 230 |
.hint {
|
| 231 |
font-size: 0.92rem;
|
| 232 |
line-height: 1.4;
|
|
@@ -320,8 +297,4 @@ canvas {
|
|
| 320 |
.status-bar {
|
| 321 |
grid-template-columns: repeat(2, minmax(0, 1fr));
|
| 322 |
}
|
| 323 |
-
|
| 324 |
-
.two-up {
|
| 325 |
-
grid-template-columns: 1fr;
|
| 326 |
-
}
|
| 327 |
}
|
|
|
|
| 59 |
.eyebrow,
|
| 60 |
.slice-subtitle,
|
| 61 |
.status-label,
|
| 62 |
+
.hint {
|
|
|
|
| 63 |
color: var(--muted);
|
| 64 |
}
|
| 65 |
|
|
|
|
| 172 |
margin-top: 12px;
|
| 173 |
}
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
.polarity-grid {
|
| 176 |
grid-template-columns: repeat(2, minmax(0, 1fr));
|
| 177 |
}
|
|
|
|
| 204 |
margin-top: 12px;
|
| 205 |
}
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
.hint {
|
| 208 |
font-size: 0.92rem;
|
| 209 |
line-height: 1.4;
|
|
|
|
| 297 |
.status-bar {
|
| 298 |
grid-template-columns: repeat(2, minmax(0, 1fr));
|
| 299 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
}
|