Skip to content

Commit

Permalink
Port improvements from webworker branch
Browse files Browse the repository at this point in the history
  • Loading branch information
neurolabusc committed Apr 28, 2024
1 parent 4f09e0d commit fbacdca
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 48 deletions.
63 changes: 39 additions & 24 deletions brainchop.js

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions bwlabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ export class BWLabeler {
} // idx()

// determine if voxels below candidate voxel have already been assigned a label
check_previous_slice(bw, il, r, c, sl, dim, conn, tt) {
// const nabo: number[] = [];
const nabo = new Uint32Array(27)
check_previous_slice(bw, il, r, c, sl, dim, conn, tt, nabo, tn) {
let nr_set = 0
if (!sl) {
return 0
Expand Down Expand Up @@ -73,7 +71,7 @@ export class BWLabeler {
}
}
if (nr_set) {
this.fill_tratab(tt, nabo, nr_set)
this.fill_tratab(tt, nabo, nr_set, tn)
return nabo[0]
} else {
return 0
Expand All @@ -82,6 +80,8 @@ export class BWLabeler {

// provisionally label all voxels in volume
do_initial_labelling(bw, dim, conn) {
const naboPS = new Uint32Array(27)
const tn = new Uint32Array(27 + 5)
let label = 1
const kGrowArrayBy = 8192
let ttn = kGrowArrayBy
Expand All @@ -96,7 +96,7 @@ export class BWLabeler {
if (val === 0) {
continue
}
nabo[0] = this.check_previous_slice(bw, il, r, c, sl, dim, conn, tt)
nabo[0] = this.check_previous_slice(bw, il, r, c, sl, dim, conn, tt, naboPS, tn)
if (nabo[0]) {
nr_set += 1
}
Expand Down Expand Up @@ -130,7 +130,7 @@ export class BWLabeler {
}
if (nr_set) {
il[this.idx(r, c, sl, dim)] = nabo[0]
this.fill_tratab(tt, nabo, nr_set)
this.fill_tratab(tt, nabo, nr_set, tn)
} else {
il[this.idx(r, c, sl, dim)] = label
if (label >= ttn) {
Expand All @@ -156,9 +156,8 @@ export class BWLabeler {
} // do_initial_labelling()

// translation table unifies a region that has been assigned multiple classes
fill_tratab(tt, nabo, nr_set) {
fill_tratab(tt, nabo, nr_set, tn) {
let cntr = 0
const tn = new Uint32Array(nr_set + 5).fill(0)
const INT_MAX = 2147483647
let ltn = INT_MAX
for (let i = 0; i < nr_set; i++) {
Expand Down
36 changes: 20 additions & 16 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,33 @@ async function main() {
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) {
nv1.removeVolume(nv1.volumes[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)
let colormap = opts.atlasSelectedColorTable.toLowerCase()
const cmaps = nv1.colormaps()
if (!cmaps.includes(colormap)) {
colormap = 'actc'
if (modelEntry.type === 'Atlas') {
colormap = 'random'
}
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.colormap = colormap
overlayVolume.opacity = opacitySlider.value / 255
nv1.addVolume(overlayVolume)
await nv1.addVolume(overlayVolume)
}
function callbackUI(message = "", progressFrac = -1, modalMessage = "") {
console.log(message)
Expand All @@ -84,12 +88,12 @@ async function main() {
document.getElementById("location").innerHTML = "&nbsp;&nbsp;" + data.string
}
for (let i = 0; i < inferenceModelsList.length; i++) {
var option = document.createElement("option");
var option = document.createElement("option")
option.text = inferenceModelsList[i].modelName
option.value = inferenceModelsList[i].id.toString()
modelSelect.appendChild(option);
modelSelect.appendChild(option)
}
modelSelect.selectedIndex = -1;
modelSelect.selectedIndex = -1
}

main()
6 changes: 6 additions & 0 deletions public/models/model18cls/colormap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"R": [ 0, 245, 205, 120, 196, 220, 230, 0, 122, 236, 12, 204, 42, 119, 220, 103, 255, 165],
"G": [ 0, 245, 62, 18, 58, 248, 148, 118, 186, 13, 48, 182, 204, 159, 216, 255, 165, 42],
"B": [ 0, 245, 78, 134, 250, 164, 34, 14, 220, 176, 255, 142, 164, 176, 20, 255, 0, 42],
"labels": [ "Unknown", "Cerebral-White-Matter", "Cerebral-Cortex", "Lateral-Ventricle", "Inferior-Lateral-Ventricle", "Cerebellum-White-Matter", "Cerebellum-Cortex", "Thalamus", "Caudate", "Putamen", "Pallidum", "3rd-Ventricle", "4th-Ventricle", "Brain-Stem", "Hippocampus", "Amygdala", "Accumbens-area", "VentralDC"]
}
6 changes: 6 additions & 0 deletions public/models/model20chan3cls/colormap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"R": [ 0, 255, 205],
"G": [ 0, 255, 62],
"B": [ 0, 255, 78],
"labels": [ "background", "White Matter", "Grey Matter"]
}
6 changes: 6 additions & 0 deletions public/models/model21_104class/colormap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"R": [ 0, 25, 125, 100, 220, 220, 180, 220, 180, 140, 20, 35, 225, 200, 160, 20, 60, 220, 20, 220, 120, 220, 220, 60, 160, 80, 75, 20, 20, 140, 80, 100, 70, 150, 255, 25, 125, 100, 220, 220, 180, 220, 180, 140, 20, 35, 225, 200, 160, 20, 60, 220, 20, 220, 120, 220, 220, 60, 160, 80, 75, 20, 20, 140, 80, 100, 70, 150, 255, 0, 0, 122, 122, 236, 236, 12, 13, 220, 220, 103, 103, 255, 255, 165, 165, 245, 245, 120, 196, 120, 196, 204, 42, 60, 119, 220, 220, 230, 230, 0, 0, 0, 0, 0],
"G": [ 0, 100, 100, 25, 20, 20, 220, 60, 40, 20, 30, 75, 140, 35, 100, 220, 220, 180, 100, 60, 100, 20, 180, 20, 140, 20, 50, 220, 180, 220, 160, 0, 70, 150, 192, 100, 100, 25, 20, 20, 220, 60, 40, 20, 30, 75, 140, 35, 100, 220, 220, 180, 100, 60, 100, 20, 180, 20, 140, 20, 50, 220, 180, 220, 160, 0, 70, 150, 192, 118, 118, 186, 186, 13, 13, 48, 48, 216, 216, 255, 255, 165, 165, 42, 42, 245, 245, 18, 58, 18, 58, 182, 204, 60, 159, 248, 248, 148, 148, 0, 0, 0, 0, 0],
"B": [ 0, 40, 160, 0, 100, 10, 140, 220, 120, 140, 140, 50, 140, 75, 50, 60, 60, 140, 50, 20, 60, 20, 220, 220, 180, 140, 125, 160, 140, 220, 20, 100, 70, 200, 32, 40, 160, 0, 100, 10, 140, 220, 120, 140, 140, 50, 140, 75, 50, 60, 60, 140, 50, 20, 60, 20, 220, 220, 180, 140, 125, 160, 140, 220, 20, 100, 70, 200, 32, 14, 14, 220, 220, 176, 176, 255, 255, 20, 20, 255, 255, 0, 0, 42, 42, 245, 245, 134, 250, 134, 250, 142, 164, 60, 176, 164, 164, 34, 34, 64, 112, 160, 208, 255],
"labels": [ "BG", "ctx-lh-bankssts", "ctx-lh-caudalanteriorcingulate", "ctx-lh-caudalmiddlefrontal", "ctx-lh-cuneus", "ctx-lh-entorhinal", "ctx-lh-fusiform", "ctx-lh-inferiorparietal", "ctx-lh-inferiortemporal", "ctx-lh-isthmuscingulate", "ctx-lh-lateraloccipital", "ctx-lh-lateralorbitofrontal", "ctx-lh-lingual", "ctx-lh-medialorbitofrontal", "ctx-lh-middletemporal", "ctx-lh-parahippocampal", "ctx-lh-paracentral", "ctx-lh-parsopercularis", "ctx-lh-parsorbitalis", "ctx-lh-parstriangularis", "ctx-lh-pericalcarine", "ctx-lh-postcentral", "ctx-lh-posteriorcingulate", "ctx-lh-precentral", "ctx-lh-precuneus", "ctx-lh-rostralanteriorcingulate", "ctx-lh-rostralmiddlefrontal", "ctx-lh-superiorfrontal", "ctx-lh-superiorparietal", "ctx-lh-superiortemporal", "ctx-lh-supramarginal", "ctx-lh-frontalpole", "ctx-lh-temporalpole", "ctx-lh-transversetemporal", "ctx-lh-insula", "ctx-rh-bankssts", "ctx-rh-caudalanteriorcingulate", "ctx-rh-caudalmiddlefrontal", "ctx-rh-cuneus", "ctx-rh-entorhinal", "ctx-rh-fusiform", "ctx-rh-inferiorparietal", "ctx-rh-inferiortemporal", "ctx-rh-isthmuscingulate", "ctx-rh-lateraloccipital", "ctx-rh-lateralorbitofrontal", "ctx-rh-lingual", "ctx-rh-medialorbitofrontal", "ctx-rh-middletemporal", "ctx-rh-parahippocampal", "ctx-rh-paracentral", "ctx-rh-parsopercularis", "ctx-rh-parsorbitalis", "ctx-rh-parstriangularis", "ctx-rh-pericalcarine", "ctx-rh-postcentral", "ctx-rh-posteriorcingulate", "ctx-rh-precentral", "ctx-rh-precuneus", "ctx-rh-rostralanteriorcingulate", "ctx-rh-rostralmiddlefrontal", "ctx-rh-superiorfrontal", "ctx-rh-superiorparietal", "ctx-rh-superiortemporal", "ctx-rh-supramarginal", "ctx-rh-frontalpole", "ctx-rh-temporalpole", "ctx-rh-transversetemporal", "ctx-rh-insula", "Left-Thalamus-Proper*", "Right-Thalamus-Proper*", "Left-Caudate", "Right-Caudate", "Left-Putamen", "Right-Putamen", "Left-Pallidum", "Right-Pallidum", "Left-Hippocampus", "Right-Hippocampus", "Left-Amygdala", "Right-Amygdala", "Left-Accumbens-area", "Right-Accumbens-area", "Left-VentralDC", "Right-VentralDC", "Left-Cerebral-White-Matter", "Right-Cerebral-White-Matter", "Left-Lateral-Ventricle", "Left-Inf-Lat-Vent", "Right-Lateral-Ventricle", "Right-Inf-Lat-Vent", "3rd-Ventricle", "4th-Ventricle", "CSF", "Brain-Stem", "Left-Cerebellum-White-Matter", "Right-Cerebellum-White-Matter", "Left-Cerebellum-Cortex", "Right-Cerebellum-Cortex", "CC_Posterior", "CC_Mid_Posterior", "CC_Central", "CC_Mid_Anterior", "CC_Anterior"]
}
6 changes: 6 additions & 0 deletions public/models/model30chan18cls/colormap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"R": [ 0, 245, 205, 120, 196, 220, 230, 0, 122, 236, 12, 204, 42, 119, 220, 103, 255, 165],
"G": [ 0, 245, 62, 18, 58, 248, 148, 118, 186, 13, 48, 182, 204, 159, 216, 255, 165, 42],
"B": [ 0, 245, 78, 134, 250, 164, 34, 14, 220, 176, 255, 142, 164, 176, 20, 255, 0, 42],
"labels": [ "Unknown", "Cerebral-White-Matter", "Cerebral-Cortex", "Lateral-Ventricle", "Inferior-Lateral-Ventricle", "Cerebellum-White-Matter", "Cerebellum-Cortex", "Thalamus", "Caudate", "Putamen", "Pallidum", "3rd-Ventricle", "4th-Ventricle", "Brain-Stem", "Hippocampus", "Amygdala", "Accumbens-area", "VentralDC"]
}
6 changes: 6 additions & 0 deletions public/models/model30chan50cls/colormap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"R": [ 0, 245, 196, 220, 230, 0, 122, 236, 12, 119, 220, 103, 60, 255, 165, 0, 25, 125, 100, 220, 220, 180, 220, 180, 140, 20, 35, 225, 200, 160, 20, 60, 220, 20, 220, 120, 220, 220, 60, 160, 80, 75, 20, 20, 140, 80, 100, 70, 150, 255],
"G": [ 0, 245, 58, 248, 148, 118, 186, 13, 48, 159, 216, 255, 60, 165, 42, 0, 100, 100, 25, 20, 20, 220, 60, 40, 20, 30, 75, 140, 35, 100, 220, 220, 180, 100, 60, 100, 20, 180, 20, 140, 20, 50, 220, 180, 220, 160, 0, 70, 150, 192],
"B": [ 0, 245, 250, 164, 34, 14, 220, 176, 255, 176, 20, 255, 60, 0, 42, 208, 40, 160, 0, 100, 10, 140, 220, 120, 140, 140, 50, 140, 75, 50, 60, 60, 140, 50, 20, 60, 20, 220, 220, 180, 140, 125, 160, 140, 220, 20, 100, 70, 200, 32],
"labels": [ "BG", "Cerebral-White-Matter", "Ventricle", "Cerebellum-White-Matter", "Cerebellum", "Thalamus-Proper*", "Caudate", "Putamen", "Pallidum", "Brain-Stem", "Hippocampus", "Amygdala", "CSF", "Accumbens-area", "VentralDC", "CC_Posterior / CC_Mid_Posterior / CC_Central / CC_Mid_Anterior / CC_Anterior", "ctx-bankssts", "ctx-caudalanteriorcingulate", "ctx-caudalmiddlefrontal", "ctx-cuneus", "ctx-entorhinal", "ctx-fusiform", "ctx-inferiorparietal", "ctx-inferiortemporal", "ctx-isthmuscingulate", "ctx-lateraloccipital", "ctx-lateralorbitofrontal", "ctx-lingual", "ctx-medialorbitofrontal", "ctx-middletemporal", "ctx-parahippocampal", "ctx-paracentral", "ctx-parsopercularis", "ctx-parsorbitalis", "ctx-parstriangularis", "ctx-pericalcarine", "ctx-postcentral", "ctx-posteriorcingulate", "ctx-precentral", "ctx-precuneus", "ctx-rostralanteriorcingulate", "ctx-rostralmiddlefrontal", "ctx-superiorfrontal", "ctx-superiorparietal", "ctx-superiortemporal", "ctx-supramarginal", "ctx-frontalpole", "ctx-temporalpole", "ctx-transversetemporal", "ctx-insula"]
}
6 changes: 6 additions & 0 deletions public/models/model5_gw_ae/colormap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"R": [0, 255],
"G": [0, 0],
"B": [0, 0],
"labels": ["background", "brain"]
}
6 changes: 6 additions & 0 deletions public/models/model5_gw_ae/colormap3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"R": [ 0, 255, 205],
"G": [ 0, 255, 62],
"B": [ 0, 255, 78],
"labels": [ "background", "White Matter", "Grey Matter"]
}

0 comments on commit fbacdca

Please sign in to comment.