gdevenyi commited on
Commit
5c22466
·
verified ·
1 Parent(s): 0465e27

n4: native ANTs 2.6.5 N4 reference on the ds005134 slab mean b0 with the MindGrab mask (#436)

Browse files
n4/validation/ds005134-sub-01-slab/README.md ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # n4 reference: native ANTs N4 on the ds005134 slab mean b0
2
+
3
+ Reference for the `@neurodesk/n4` WebAssembly module (neurodesk/webapps #436): native ANTs 2.6.5 `N4BiasFieldCorrection` on the mean b0 of the ds005134 sub-01 slab, weighted by the MindGrab brain mask that the n4 app makes by default.
4
+
5
+ ## Input
6
+
7
+ The slab is the `noddi`, `dwidenoise` and `n4` example: OpenNeuro ds005134 v1.0.0 sub-01 (CC0, doi:10.18112/openneuro.ds005134.v1.0.0), slices 28-37, 67 × 92 × 10 × 137, 2 mm, 13 b0 volumes. It is `noddi/examples/sub-01_dir-AP_desc-slab_dwi.nii.gz` (and `.bval`) at dataset commit `ce028ecca10e9abe7988f965e03ad51627ca9c52`.
8
+
9
+ ## Files
10
+
11
+ | File | Content | Made by |
12
+ | --- | --- | --- |
13
+ | `meanb0.nii.gz` | float32 mean of the 13 b0 volumes (b < 50), summed in volume order in float32, as the app's `meanVolumes` | `scripts/mean_b0.py` (nibabel) |
14
+ | `mindgrab_mask.nii.gz` | uint8 brain mask, 45 965 voxels: MindGrab 0.1.20260925, model `mindgrab`, `mask: true`, CPU, on `meanb0` (the app's default mask, `@neurodesk/runtime-support` `brainMask`) | `scripts/mindgrab-mask.mjs` (`@neurodesk/node-drivers/mindgrab`) |
15
+ | `native_bias.nii.gz` | float32 bias field | ANTs 2.6.5-gfdce4d2 `N4BiasFieldCorrection` |
16
+ | `native_corrected.nii.gz` | float32 corrected mean b0 | the same run |
17
+
18
+ N4 command (Neurodesk container `ants_2.6.5_20261012`, `ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1`, the options of MRtrix `dwibiascorrect ants`):
19
+
20
+ ```sh
21
+ N4BiasFieldCorrection -d 3 -i meanb0.nii -w mindgrab_mask.nii \
22
+ -o '[native_corrected.nii,native_bias.nii]' -s 4 -b '[100,3]' -c '[1000,0.0]'
23
+ ```
24
+
25
+ `scripts/make_reference.sh <webapps checkout> <out dir>` runs all three steps and gzips each image with `gzip -9n`; on the host that made this folder it rebuilds all four files byte for byte.
26
+
27
+ ## Result
28
+
29
+ The WebAssembly module (`packages/n4`, ANTs 52bc0ab) gives this bias field byte for byte: maximum relative difference 0 over all 61 640 voxels, 2.8 s in Node.
30
+
31
+ An earlier hand check (PR #380) used an FSL `bet -f 0.3` mask (44 521 voxels) and was also exact. The reference uses MindGrab instead, the mask that the app makes (Dice 0.943 against that `bet` mask).
32
+
33
+ ## Licences
34
+
35
+ - `meanb0.nii.gz`, `native_bias.nii.gz`, `native_corrected.nii.gz`: derived from the CC0 slab.
36
+ - `mindgrab_mask.nii.gz`: MindGrab output, MIT (the licence of `@brainchop/mindgrab`), derived from the CC0 slab.
37
+ - `scripts/`, `README.md`, `SHA256SUMS`: Apache-2.0, the licence of `packages/n4` in neurodesk/webapps.
n4/validation/ds005134-sub-01-slab/SHA256SUMS ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ c3b343f348f11b54c3004587b54648ec63653fc85a5532f7a40a4e49edcf99c7 meanb0.nii.gz
2
+ b7b0996f69b61ee708cb234b1333d65292034db2fecf31009cae34551392f42b mindgrab_mask.nii.gz
3
+ b6d67aad8a523b98e57970a2f05e0208c318961db1549c36cca4fa51cd588716 native_bias.nii.gz
4
+ 6ab1f7ba759598733b8edbaf7484463962d64ee7e8a8b15f93a3be810fc71e3b native_corrected.nii.gz
5
+ 200955e37ef4040224b74a7fcf4499d9e80c24de1489fc1aeac6d4be495d32c8 README.md
6
+ 02c5e3bd55f00ddc7dff9bba99e1b61d530232fb46be81dd2763a3fadd9ca045 scripts/make_reference.sh
7
+ 77c5bd3319b856cfa05e2c1b08d1c2b47454cb8e6efc64339a8382b0b7d40843 scripts/mean_b0.py
8
+ 828e18441af81bc54cafa12f5328bc28f5a25ef566ed85c4023ec2a7b59990b5 scripts/mindgrab-mask.mjs
n4/validation/ds005134-sub-01-slab/meanb0.nii.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c3b343f348f11b54c3004587b54648ec63653fc85a5532f7a40a4e49edcf99c7
3
+ size 129879
n4/validation/ds005134-sub-01-slab/mindgrab_mask.nii.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b7b0996f69b61ee708cb234b1333d65292034db2fecf31009cae34551392f42b
3
+ size 1476
n4/validation/ds005134-sub-01-slab/native_bias.nii.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b6d67aad8a523b98e57970a2f05e0208c318961db1549c36cca4fa51cd588716
3
+ size 208139
n4/validation/ds005134-sub-01-slab/native_corrected.nii.gz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6ab1f7ba759598733b8edbaf7484463962d64ee7e8a8b15f93a3be810fc71e3b
3
+ size 222418
n4/validation/ds005134-sub-01-slab/scripts/make_reference.sh ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # Rebuild this folder. Needs Python with nibabel, Node with a neurodesk/webapps checkout
3
+ # (pnpm install), Apptainer and the Neurodesk CVMFS ANTs container.
4
+ # Usage: make_reference.sh <webapps checkout> <out dir>
5
+ set -euo pipefail
6
+ repo=$1
7
+ out=$2
8
+ here=$(cd "$(dirname "$0")" && pwd)
9
+ base=https://huggingface.co/datasets/neurodeskorg/webapps/resolve/ce028ecca10e9abe7988f965e03ad51627ca9c52/noddi/examples
10
+ ants=/cvmfs/neurodesk.ardc.edu.au/containers/ants_2.6.5_20261012/ants_2.6.5_20261012.simg
11
+ mkdir -p "$out"
12
+ cd "$out"
13
+ curl -sfLO "$base/sub-01_dir-AP_desc-slab_dwi.nii.gz"
14
+ curl -sfLO "$base/sub-01_dir-AP_desc-slab_dwi.bval"
15
+ python "$here/mean_b0.py" sub-01_dir-AP_desc-slab_dwi.nii.gz sub-01_dir-AP_desc-slab_dwi.bval meanb0.nii
16
+ node "$here/mindgrab-mask.mjs" "$repo" meanb0.nii mindgrab_mask.nii
17
+ apptainer exec -B "$PWD" --env ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS=1 "$ants" \
18
+ N4BiasFieldCorrection -d 3 -i meanb0.nii -w mindgrab_mask.nii \
19
+ -o '[native_corrected.nii,native_bias.nii]' -s 4 -b '[100,3]' -c '[1000,0.0]'
20
+ for f in meanb0 mindgrab_mask native_bias native_corrected; do gzip -9n -c "$f.nii" > "$f.nii.gz"; done
n4/validation/ds005134-sub-01-slab/scripts/mean_b0.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Mean of the b0 volumes (b < 50) of the slab, as the n4 app's meanVolumes computes it:
2
+ float32 sums in volume order, then one float32 division. Usage: python mean_b0.py DWI BVAL OUT"""
3
+
4
+ import sys
5
+
6
+ import nibabel as nb
7
+ import numpy as np
8
+
9
+ dwi, bval, out = sys.argv[1:]
10
+ img = nb.load(dwi)
11
+ data = np.asanyarray(img.dataobj).astype(np.float32)
12
+ b0 = [i for i, b in enumerate(np.loadtxt(bval)) if b < 50]
13
+ mean = np.zeros(img.shape[:3], np.float32)
14
+ for i in b0:
15
+ mean += data[..., i]
16
+ mean /= np.float32(len(b0))
17
+ header = img.header.copy()
18
+ header.set_data_dtype(np.float32)
19
+ header.set_slope_inter(1, 0)
20
+ nb.Nifti1Image(mean, img.affine, header).to_filename(out)
21
+ print(f"{len(b0)} b0 volumes")
n4/validation/ds005134-sub-01-slab/scripts/mindgrab-mask.mjs ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // MindGrab brain mask of the mean b0 (model 'mindgrab', mask), the n4 app's default mask
2
+ // (@neurodesk/runtime-support brainMask), with the repository's Node driver.
3
+ // Usage, from a neurodesk/webapps checkout after pnpm install:
4
+ // node mindgrab-mask.mjs <repository> meanb0.nii mindgrab_mask.nii
5
+ import { readFile, writeFile } from "node:fs/promises";
6
+ import { createRequire } from "node:module";
7
+ import { join } from "node:path";
8
+ import { pathToFileURL } from "node:url";
9
+
10
+ const [repository, input, output] = process.argv.slice(2);
11
+ const drivers = join(repository, "packages/node-drivers");
12
+ const { loadMindgrabCpu } = await import(pathToFileURL(join(drivers, "src/mindgrab.js")).href);
13
+ const require = createRequire(join(drivers, "package.json"));
14
+ const pkgUrl = pathToFileURL(require.resolve("@brainchop/mindgrab/package.json"));
15
+ const pkg = JSON.parse(await readFile(pkgUrl, "utf8"));
16
+ const mindgrab = await loadMindgrabCpu(pkgUrl);
17
+ const result = await mindgrab.segment(await readFile(input), { model: "mindgrab", mask: true, gzipOutput: false });
18
+ if (!result.mask) throw new Error("MindGrab returned no mask");
19
+ await writeFile(output, new Uint8Array(result.mask));
20
+ console.log(JSON.stringify({ mindgrab: pkg.version, model: "mindgrab", backend: result.backend }));
21
+ process.exit(0);