forked from niivue/niivue-brainchop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
99 lines (93 loc) · 3.21 KB
/
main.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
import { Niivue } from "@niivue/niivue"
import { runInference, inferenceModelsList, brainChopOpts } from "./brainchop.js"
async function main() {
let defaults = {
backColor: [0.4, 0.4, 0.4, 1],
show3Dcrosshair: true,
onLocationChange: handleLocationChange,
}
let nv1 = new Niivue(defaults)
nv1.attachToCanvas(gl1)
nv1.opts.dragMode = nv1.dragModes.pan
nv1.opts.multiplanarForceRender = true
nv1.opts.yoke3Dto2DZoom = true
await nv1.loadVolumes([{ url: "./t1_crop.nii.gz" }])
aboutBtn.onclick = function () {
window.alert("BrainChop models https://github.com/neuroneural/brainchop")
}
opacitySlider.oninput = function () {
nv1.setOpacity(1, opacitySlider.value / 255)
}
async function ensureConformed() {
let nii = nv1.volumes[0]
let isConformed = ((nii.dims[1] === 256) && (nii.dims[2] === 256) && (nii.dims[3] === 256))
if ((nii.permRAS[0] !== -1) || (nii.permRAS[1] !== 3) || (nii.permRAS[2] !== -2))
isConformed = false
if (isConformed)
return
let nii2 = await nv1.conform(nii, false)
nv1.removeVolume(nv1.volumes[0])
nv1.addVolume(nii2)
}
modelSelect.onchange = async function () {
await ensureConformed()
let model = inferenceModelsList[this.selectedIndex]
let opts = brainChopOpts
runInference(opts, model, nv1.volumes[0].hdr, nv1.volumes[0].img, callbackImg, callbackUI)
}
saveBtn.onclick = function () {
nv1.volumes[1].saveToDisk("Custom.nii")
}
async function fetchJSON(fnm) {
const response = await fetch(fnm)
const js = await response.json()
return js
}
async function callbackImg(img, opts, modelEntry) {
while (nv1.volumes.length > 1) {
await nv1.removeVolume(nv1.volumes[1])
}
let overlayVolume = await nv1.volumes[0].clone()
overlayVolume.zeroImage()
overlayVolume.hdr.scl_inter = 0
overlayVolume.hdr.scl_slope = 1
overlayVolume.img = new Uint8Array(img)
if (modelEntry.colormapPath) {
let cmap = await fetchJSON(modelEntry.colormapPath)
overlayVolume.setColormapLabel(cmap)
} else {
let colormap = opts.atlasSelectedColorTable.toLowerCase()
const cmaps = nv1.colormaps()
if (!cmaps.includes(colormap)) {
colormap = 'actc'
}
overlayVolume.colormap = colormap
}
overlayVolume.opacity = opacitySlider.value / 255
await nv1.addVolume(overlayVolume)
}
function callbackUI(message = "", progressFrac = -1, modalMessage = "") {
console.log(message)
document.getElementById("location").innerHTML = message
if (isNaN(progressFrac)) { //memory issue
memstatus.style.color = "red"
memstatus.innerHTML = "Memory Issue"
} else if (progressFrac >= 0) {
modelProgress.value = progressFrac * modelProgress.max
}
if (modalMessage !== "") {
window.alert(modalMessage)
}
}
function handleLocationChange(data) {
document.getElementById("location").innerHTML = " " + data.string
}
for (let i = 0; i < inferenceModelsList.length; i++) {
var option = document.createElement("option")
option.text = inferenceModelsList[i].modelName
option.value = inferenceModelsList[i].id.toString()
modelSelect.appendChild(option)
}
modelSelect.selectedIndex = -1
}
main()