Skip to content

Commit 63c9384

Browse files
committed
test: migrate read-image-file-series test to image-io package
Add BinaryFile[] support, fix tests. Following #1010.
1 parent 269d582 commit 63c9384

File tree

5 files changed

+127
-124
lines changed

5 files changed

+127
-124
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import { demoServer } from './common.ts'
2+
3+
function verifyImage (image) {
4+
cy.expect(image.imageType.dimension).to.equal(3)
5+
cy.expect(image.imageType.componentType).to.equal('uint16')
6+
cy.expect(image.imageType.pixelType).to.equal('Scalar')
7+
cy.expect(image.imageType.components).to.equal(1)
8+
cy.expect(image.origin[0]).to.equal(0.0)
9+
cy.expect(image.origin[1]).to.equal(0.0)
10+
cy.expect(image.origin[2]).to.equal(2.0)
11+
cy.expect(image.spacing[0]).to.equal(0.85935)
12+
cy.expect(image.spacing[1]).to.equal(0.85935)
13+
cy.expect(image.spacing[2]).to.equal(3.0)
14+
cy.expect(image.size[0]).to.equal(256)
15+
cy.expect(image.size[1]).to.equal(256)
16+
cy.expect(image.size[2]).to.equal(3)
17+
cy.expect(image.data.length).to.equal(3 * 65536)
18+
cy.expect(image.data[1000]).to.equal(0)
19+
}
20+
21+
const zSpacing = 3.0
22+
const zOrigin = 2.0
23+
const testImageFiles = [
24+
'mri3D_01.png',
25+
'mri3D_02.png',
26+
'mri3D_03.png',
27+
]
28+
29+
describe('meta-image', () => {
30+
beforeEach(function() {
31+
cy.visit(demoServer)
32+
33+
const testPathPrefix = '../test/data/input/PNGSeries/'
34+
35+
cy.window().then((win) => {
36+
win.testImageFiles = {}
37+
})
38+
testImageFiles.forEach((fileName) => {
39+
cy.readFile(`${testPathPrefix}${fileName}`, null).as(fileName)
40+
cy.window().then((win) => {
41+
win.testImageFiles[fileName] = this[fileName]
42+
})
43+
})
44+
})
45+
46+
it('Reads a sorted PNG file series', function () {
47+
cy.window().then(async (win) => {
48+
const files = testImageFiles.map((fileName) => {
49+
return { data: new Uint8Array(win.testImageFiles[fileName]), path: fileName }
50+
})
51+
const sortedSeries = true
52+
53+
const { image, webWorkerPool } = await win.imageIo.readImageFileSeries(files, { zSpacing, zOrigin, sortedSeries })
54+
webWorkerPool.terminateWorkers()
55+
verifyImage(image)
56+
})
57+
})
58+
59+
it('Reads sorted PNG file series, specify componentType, pixelType', function () {
60+
cy.window().then(async (win) => {
61+
const files = testImageFiles.map((fileName) => {
62+
return { data: new Uint8Array(win.testImageFiles[fileName]), path: fileName }
63+
})
64+
const sortedSeries = true
65+
const componentType = win.itk.IntTypes.Int32
66+
const pixelType = win.itk.PixelTypes.Vector
67+
68+
const { image, webWorkerPool } = await win.imageIo.readImageFileSeries(files, { zSpacing, zOrigin, sortedSeries, componentType, pixelType })
69+
webWorkerPool.terminateWorkers()
70+
cy.expect(image.imageType.componentType).to.equal('int32')
71+
cy.expect(image.imageType.pixelType).to.equal('Vector')
72+
})
73+
})
74+
75+
it('Reads an unsorted PNG file series', function () {
76+
cy.window().then(async (win) => {
77+
const files = testImageFiles.map((fileName) => {
78+
return { data: new Uint8Array(win.testImageFiles[fileName]), path: fileName }
79+
})
80+
files.reverse()
81+
82+
const { image, webWorkerPool } = await win.imageIo.readImageFileSeries(files, { zSpacing, zOrigin })
83+
webWorkerPool.terminateWorkers()
84+
verifyImage(image)
85+
})
86+
})
87+
88+
it('Reads an unsorted PNG file series, specifying componentType, pixelType', function () {
89+
cy.window().then(async (win) => {
90+
const files = testImageFiles.map((fileName) => {
91+
return { data: new Uint8Array(win.testImageFiles[fileName]), path: fileName }
92+
})
93+
files.reverse()
94+
const componentType = win.itk.IntTypes.Int32
95+
const pixelType = win.itk.PixelTypes.Vector
96+
97+
const { image, webWorkerPool } = await win.imageIo.readImageFileSeries(files, { zSpacing, zOrigin, componentType, pixelType })
98+
webWorkerPool.terminateWorkers()
99+
cy.expect(image.imageType.componentType).to.equal('int32')
100+
cy.expect(image.imageType.pixelType).to.equal('Vector')
101+
})
102+
})
103+
})

packages/image-io/typescript/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"esbuild": "^0.19.8",
6161
"shx": "^0.3.4",
6262
"start-server-and-test": "^2.0.1",
63-
"typescript": "^5.0.4",
63+
"typescript": "^5.3.3",
6464
"vite": "^4.5.0",
6565
"vite-plugin-static-copy": "^0.17.0"
6666
},
@@ -74,4 +74,4 @@
7474
"!test/node/common.js"
7575
]
7676
}
77-
}
77+
}

packages/image-io/typescript/src/read-image-file-series.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { stackImages, WorkerPool, castImage } from 'itk-wasm'
1+
import { stackImages, WorkerPool, castImage, BinaryFile } from 'itk-wasm'
22

33
import readImage from './read-image.js'
44

@@ -9,7 +9,7 @@ const numberOfWorkers = typeof globalThis.navigator?.hardwareConcurrency === 'nu
99
const workerPool = new WorkerPool(numberOfWorkers, readImage)
1010

1111
async function readImageFileSeries (
12-
fileList: File[] | FileList,
12+
fileList: File[] | FileList | BinaryFile[],
1313
options?: ReadImageFileSeriesOptions,
1414
): Promise<ReadImageFileSeriesResult> {
1515
let zSpacing = 1.0
@@ -26,34 +26,33 @@ async function readImageFileSeries (
2626
sortedSeries = options.sortedSeries
2727
}
2828
}
29+
// @ts-ignore error TS2769: No overload matches this call.
2930
const fetchFileDescriptions = Array.from(fileList, async function (file) {
30-
return await file.arrayBuffer().then(function (
31-
arrayBuffer
32-
) {
33-
const fileDescription = {
34-
name: file.name,
35-
type: file.type,
36-
data: arrayBuffer
31+
if (file instanceof File) {
32+
const arrayBuffer = await file.arrayBuffer()
33+
return {
34+
path: file.name,
35+
data: new Uint8Array(arrayBuffer)
3736
}
38-
return fileDescription
39-
})
37+
}
38+
return file as BinaryFile
4039
})
4140

42-
const fileDescriptions = await Promise.all(fetchFileDescriptions)
41+
const fileDescriptions = await Promise.all(fetchFileDescriptions) as BinaryFile[]
4342
if (!sortedSeries) {
4443
fileDescriptions.sort((a, b) => {
45-
if (a.name < b.name) {
44+
if (a.path < b.path) {
4645
return -1
4746
}
48-
if (a.name > b.name) {
47+
if (a.path > b.path) {
4948
return 1
5049
}
5150
return 0
5251
})
5352
}
5453
const taskArgsArray = []
5554
for (let index = 0; index < fileDescriptions.length; index++) {
56-
taskArgsArray.push([fileDescriptions[index].data, fileDescriptions[index].name])
55+
taskArgsArray.push([fileDescriptions[index],])
5756
}
5857
const results = await workerPool.runTasks(taskArgsArray).promise
5958
const images = results.map((result) => {

pnpm-lock.yaml

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/browser/io/readImageFileSeriesTest.js

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)