unroll / tests /livesheet.mjs
SharpMeow's picture
One page scroll bar and American English
757768f verified
Raw History Blame Contribute Delete
9.48 kB
// Live sheet tests (lib/livesheet.js) in Node, with no network: a fake fetch serves a synthetic tifxyz segment and a
// synthetic zarr v2 volume from memory, honoring Range requests. They mirror Scrolls' tests/test_sheetview.py: a
// flat sheet over a volume whose value is its own x index samples exactly that index; a bright slab 6 voxels behind
// the sheet is missed at depth 0 and 5 and found at depth 8 (max), and the mean is 200/17; a planted hole removes
// its cells; a coarser pyramid level divides by its factor; bad inputs throw.
// Usage: node tests/livesheet.mjs (exit 1 on any failure)
import { readSegment, upsample, openVolume, sampleSheet, sheetMesh, toVTK, normals } from '../lib/livesheet.js';
const results = [];
const check = (name, pass, info) => results.push({ name, pass: !!pass, ...(pass ? {} : { info }) });
const throws = async (fn, re) => { try { await fn(); return false; } catch (e) { return re.test(String(e.message)); } };
// ------------------------------------------------------------------ fake bucket
const files = new Map();
let ignoreRange = false, rangeRequests = 0;
globalThis.fetch = async (url, opts = {}) => {
const body = files.get(url);
if (!body) return { ok: false, status: 404, json: async () => null, arrayBuffer: async () => new ArrayBuffer(0) };
const range = opts.headers?.Range && /bytes=(\d+)-(\d+)/.exec(opts.headers.Range);
let out = body, status = 200;
if (range && !ignoreRange) { out = body.subarray(+range[1], +range[2] + 1); status = 206; rangeRequests++; }
const copy = out.slice();
return { ok: true, status, json: async () => JSON.parse(new TextDecoder().decode(copy)),
arrayBuffer: async () => copy.buffer };
};
const putJSON = (url, v) => files.set(url, new TextEncoder().encode(JSON.stringify(v)));
// A classic little-endian float32 TIFF in strips of `rps` rows.
function tiff(h, w, values, rps = 3, compression = 1) {
const nStrips = Math.ceil(h / rps), tags = [];
const entries = 9, ifd = 8, ifdSize = 2 + 12 * entries + 4;
const offArr = ifd + ifdSize, cntArr = offArr + 4 * nStrips, data = cntArr + 4 * nStrips;
const buf = new ArrayBuffer(data + h * w * 4), d = new DataView(buf);
d.setUint16(0, 0x4949, true); d.setUint16(2, 42, true); d.setUint32(4, ifd, true);
tags.push([256, 4, 1, w], [257, 4, 1, h], [258, 3, 1, 32], [259, 3, 1, compression], [262, 3, 1, 1],
[273, 4, nStrips, nStrips === 1 ? data : offArr], [278, 4, 1, rps],
[279, 4, nStrips, nStrips === 1 ? Math.min(rps, h) * w * 4 : cntArr], [339, 3, 1, 3]);
d.setUint16(ifd, entries, true);
tags.forEach(([tag, type, count, value], i) => {
const e = ifd + 2 + 12 * i;
d.setUint16(e, tag, true); d.setUint16(e + 2, type, true); d.setUint32(e + 4, count, true);
if (type === 3) d.setUint16(e + 8, value, true); else d.setUint32(e + 8, value, true);
});
for (let s = 0; s < nStrips; s++) {
d.setUint32(offArr + 4 * s, data + s * rps * w * 4, true);
d.setUint32(cntArr + 4 * s, Math.min(rps, h - s * rps) * w * 4, true);
}
for (let k = 0; k < h * w; k++) d.setFloat32(data + 4 * k, values[k], true);
return new Uint8Array(buf);
}
const STEP = 4, Z0 = 30, SIDE = 64, CH = 16;
function putSegment(base, h = 10, w = 12, hole = null) {
const ch = { x: [], y: [], z: [] };
for (let r = 0; r < h; r++) for (let c = 0; c < w; c++) {
const bad = hole && hole[0] === r && hole[1] === c;
ch.x.push(bad ? -1 : 8 + c * STEP); ch.y.push(bad ? -1 : 8 + r * STEP); ch.z.push(bad ? -1 : Z0);
}
for (const k of 'xyz') files.set(`${base}/${k}.tif`, tiff(h, w, ch[k]));
putJSON(`${base}/meta.json`, { scale: [1 / STEP, 1 / STEP], format: 'tifxyz' });
}
// A two-level OME-Zarr v2 volume, value(z, y, x) at level 0; level 1 is every other voxel.
function putVolume(root, value) {
putJSON(`${root}/.zattrs`, { multiscales: [{ datasets: [
{ path: '0', coordinateTransformations: [{ type: 'scale', scale: [1, 1, 1] }] },
{ path: '1', coordinateTransformations: [{ type: 'scale', scale: [2, 2, 2] }] }] }] });
for (const [lvl, f] of [[0, 1], [1, 2]]) {
const n = SIDE / f;
putJSON(`${root}/${lvl}/.zarray`, { zarr_format: 2, shape: [n, n, n], chunks: [CH, CH, CH], dtype: '|u1',
compressor: null, filters: null, fill_value: 0, order: 'C' });
for (let cz = 0; cz < n / CH; cz++) for (let cy = 0; cy < n / CH; cy++) for (let cx = 0; cx < n / CH; cx++) {
const a = new Uint8Array(CH ** 3);
for (let z = 0; z < CH; z++) for (let y = 0; y < CH; y++) for (let x = 0; x < CH; x++)
a[(z * CH + y) * CH + x] = value((cz * CH + z) * f, (cy * CH + y) * f, (cx * CH + x) * f);
files.set(`${root}/${lvl}/${cz}.${cy}.${cx}`, a);
}
}
}
const B = 'https://bucket.test';
const SEG = `${B}/seg/sheet-on-20260102150214-2.000um.tifxyz`;
putSegment(SEG);
putVolume(`${B}/ramp.zarr`, (z, y, x) => x);
putVolume(`${B}/slab.zarr`, (z) => (z === Z0 + 6 ? 200 : 0));
// ------------------------------------------------------------------ tests
let seg = await readSegment(SEG);
check('segment reads every node with its voxel size', seg.h === 10 && seg.w === 12 && seg.voxelUM === 2 &&
seg.xyz[3 * (2 * 12 + 5)] === 8 + 5 * STEP && seg.xyz[3 * (2 * 12 + 5) + 1] === 8 + 2 * STEP, seg);
check('only Range requests were made', rangeRequests > 0);
const crop = await readSegment(SEG, { crop: [4, 9, 3, 7], stride: 2 });
check('crop and stride thin the grid', crop.h === 3 && crop.w === 2 && crop.xyz[0] === 8 + 3 * STEP &&
crop.xyz[1] === 8 + 4 * STEP && crop.xyz[3 * 3] === 8 + 5 * STEP && crop.xyz[3 * 3 + 1] === 8 + 6 * STEP, crop);
ignoreRange = true;
const whole = await readSegment(SEG, { crop: [4, 9, 3, 7], stride: 2 });
ignoreRange = false;
check('a server that ignores Range gives the same crop', whole.xyz.every((v, i) => v === crop.xyz[i]));
const ramp = await openVolume(`${B}/ramp.zarr`, 0);
let s = await sampleSheet(seg, ramp, { depth: 0 });
const xs = Array.from({ length: seg.h * seg.w }, (_, p) => seg.xyz[3 * p]);
// the sheet spans x 8 to 52, y 8 to 44 at z 30: chunks x 0 to 3, y 0 to 2, z 1
check('only the chunks the samples touch are fetched', s.chunks === 12 && ramp.stats.chunks === 12, ramp.stats);
check('values are the voxels under each vertex', s.values.every((v, p) => v === xs[p]), Array.from(s.values).slice(0, 5));
let mesh = sheetMesh(seg, s.values);
check('one point per node, two triangles per cell', mesh.vertices === 120 && mesh.triangles === 2 * 9 * 11, mesh);
const vtk = toVTK(mesh).split('\n');
check('VTK header counts match', vtk.includes('POINTS 120 float') && vtk.includes(`POLYGONS ${mesh.triangles} ${4 * mesh.triangles}`) &&
vtk.includes('POINT_DATA 120'));
const slab = await openVolume(`${B}/slab.zarr`, 0);
const at = async (depth, reduce = 'max') => (await sampleSheet(seg, slab, { depth, reduce })).values;
check('control: the sheet itself is empty at depth 0', (await at(0)).every((v) => v === 0));
check('the search stops at the depth asked (5 misses a slab 6 away)', (await at(5)).every((v) => v === 0));
check('depth 8 finds the slab behind the sheet', (await at(8)).every((v) => v === 200));
check('the mean over 17 samples is 200/17', (await at(8, 'mean')).every((v) => Math.abs(v - 200 / 17) < 1e-4));
const n = normals(seg);
check('normals of a flat sheet point along z', Math.abs(Math.abs(n[3 * 13 + 2]) - 1) < 1e-12);
const coarse = await openVolume(`${B}/ramp.zarr`, 1);
s = await sampleSheet(seg, coarse, { depth: 0 });
const half = (x) => { const f = Math.floor(x / 2), d = x / 2 - f; return d > 0.5 ? f + 1 : d < 0.5 ? f : (f % 2 ? f + 1 : f); };
check('a coarser level divides by its factor (and rounds halves to even, as numpy)', coarse.factor.join() === '2,2,2' &&
s.values.every((v, p) => v === half(xs[p]) * 2), Array.from(s.values).slice(0, 6));
const HOLED = `${B}/seg/holed-2.000um.tifxyz`;
putSegment(HOLED, 10, 12, [4, 5]);
const holed = await readSegment(HOLED);
mesh = sheetMesh(holed, (await sampleSheet(holed, ramp)).values);
check('a hole removes its point and its four cells', mesh.vertices === 119 && mesh.triangles === 2 * (9 * 11 - 4), mesh);
const up = upsample({ ...(await readSegment(SEG, { crop: [0, 3, 0, 3] })) }, 2);
check('upsample adds midpoints', up.h === 5 && up.w === 5 && up.xyz[3 * 6] === 8 + STEP / 2 && up.xyz[3 * 6 + 1] === 8 + STEP / 2);
const corner = await readSegment(HOLED, { crop: [4, 7, 5, 8] });
const upc = upsample(corner, 2);
const okAt = (i, j) => upc.valid[i * upc.w + j];
check('a refined node is valid only when its cell is', !okAt(0, 0) && !okAt(1, 1) && okAt(2, 2) && okAt(4, 4));
check('bad crop throws', await throws(() => readSegment(SEG, { crop: [0, 50, 0, 5] }), /outside the grid/));
check('bad reduce throws', await throws(() => sampleSheet(seg, ramp, { reduce: 'median' }), /max or mean/));
check('negative depth throws', await throws(() => sampleSheet(seg, ramp, { depth: -1 }), /0 or more/));
check('a missing level throws', await throws(() => openVolume(`${B}/ramp.zarr`, 5), /levels 0 to 1/));
files.set(`${B}/seg/packed-2.000um.tifxyz/x.tif`, tiff(2, 2, [0, 0, 0, 0], 3, 5));
check('a compressed TIFF throws', await throws(() => readSegment(`${B}/seg/packed-2.000um.tifxyz`), /compressed/));
const failed = results.filter((r) => !r.pass);
for (const r of results) console.log(`${r.pass ? 'ok ' : 'FAIL'} ${r.name}${r.pass ? '' : ' ' + JSON.stringify(r.info).slice(0, 300)}`);
console.log(`${results.length - failed.length}/${results.length} passed`);
process.exit(failed.length ? 1 : 0);