Skip to content

Commit

Permalink
Merge pull request #1267 from thewtex/test-transform-io-ts
Browse files Browse the repository at this point in the history
test(transform-io): add node tests
  • Loading branch information
thewtex authored Nov 8, 2024
2 parents ec64423 + c02cd85 commit e47e2c7
Show file tree
Hide file tree
Showing 132 changed files with 723 additions and 310 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/javascript-typescript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
strategy:
max-parallel: 3
matrix:
package: [compress-stringify, compare-images, dicom, mesh-io, image-io, downsample]
package: [compress-stringify, compare-images, dicom, mesh-io, image-io, transform-io, downsample]

steps:
- uses: actions/checkout@v4
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const interfaceJsonTypeToInterfaceType = new Map([
['OUTPUT_POINT_SET', 'PointSet'],
['INPUT_POLYDATA', 'PolyData'],
['OUTPUT_POLYDATA', 'PolyData'],
['INPUT_TRANSFORM', 'Transform'],
['OUTPUT_TRANSFORM', 'Transform'],
['INPUT_TRANSFORM', 'TransformList'],
['OUTPUT_TRANSFORM', 'TransformList'],
['INPUT_JSON', 'JsonCompatible'],
['OUTPUT_JSON', 'JsonCompatible']
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const interfaceJsonTypeToPythonType = new Map([
['OUTPUT_POINT_SET', 'PointSet'],
['INPUT_POLYDATA', 'PolyData'],
['OUTPUT_POLYDATA', 'PolyData'],
['INPUT_TRANSFORM', 'Transform'],
['OUTPUT_TRANSFORM', 'Transform'],
['INPUT_TRANSFORM', 'TransformList'],
['OUTPUT_TRANSFORM', 'TransformList'],
['BOOL', 'bool'],
['TEXT', 'str'],
['INT', 'int'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ function functionModule(
functionContent += ` inputs.push({ type: InterfaceTypes.${interfaceType}, data: { data: value } })\n`
functionContent += ' args.push(inputCountString)\n\n'
} else {
// Image, Mesh, PointSet, PolyData, Transform, JsonCompatible
// Image, Mesh, PointSet, PolyData, TransformList, JsonCompatible
functionContent +=
' const inputCountString = inputs.length.toString()\n'
functionContent += ` inputs.push({ type: InterfaceTypes.${interfaceType}, data: value as ${interfaceType} })\n`
Expand Down Expand Up @@ -511,7 +511,7 @@ function functionModule(
functionContent += ` inputs.push({ type: InterfaceTypes.${interfaceType}, data: { data: options.${camel} } })\n`
functionContent += ` args.push('--${parameter.name}', inputCountString)\n\n`
} else {
// Image, Mesh, PointSet, PolyData, Transform, JsonCompatible
// Image, Mesh, PointSet, PolyData, TransformList, JsonCompatible
functionContent +=
' const inputCountString = inputs.length.toString()\n'
functionContent += ` inputs.push({ type: InterfaceTypes.${interfaceType}, data: options.${camel} as ${interfaceType} })\n`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const interfaceJsonTypeToTypeScriptType = new Map([
['OUTPUT_POINT_SET', 'PointSet'],
['INPUT_POLYDATA', 'PolyData'],
['OUTPUT_POLYDATA', 'PolyData'],
['INPUT_TRANSFORM', 'Transform'],
['OUTPUT_TRANSFORM', 'Transform'],
['INPUT_TRANSFORM', 'TransformList'],
['OUTPUT_TRANSFORM', 'TransformList'],
['BOOL', 'boolean'],
['TEXT', 'string'],
['INT', 'number'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const typesRequireImport = [
'Mesh',
'PointSet',
'PolyData',
'Transform',
'TransformList',
'TextFile',
'BinaryFile',
'TextFile',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const defaultImageTag = '20241030-6c93a1be'
const defaultImageTag = '20241107-53b18ede'
export default defaultImageTag
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ export { default as PolyDataType } from './poly-data-type.js'
export { default as TransformParameterizations } from './transform-parameterizations.js'
export { default as TransformType } from './transform-type.js'
export { default as Transform } from './transform.js'
export type { default as TransformList } from './transform-list.js'

export { default as InterfaceTypes } from './interface-types.js'
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const InterfaceTypes = {
PointSet: 'PointSet',
Mesh: 'Mesh',
PolyData: 'PolyData',
Transform: 'Transform',
TransformList: 'TransformList',
JsonCompatible: 'JsonCompatible'
} as const

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Transform from './transform.js'

type TransformList = Transform[]

export default TransformList
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import TransformParameterizations from './transform-parameterizations.js'
import FloatTypes from './float-types.js'

class TransformType {
constructor (
public readonly transformParameterization: (typeof TransformParameterizations)[keyof typeof TransformParameterizations] = TransformParameterizations.Identity,
public readonly parametersValueType: typeof FloatTypes[keyof typeof FloatTypes] = FloatTypes.Float64,
public readonly inputDimension: number = 3,
public readonly outputDimension: number = 3,
public readonly transformParameterization: (typeof TransformParameterizations)[keyof typeof TransformParameterizations] = TransformParameterizations.Identity
public readonly outputDimension: number = 3
) {}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import TransformType from './transform-type.js'
import type TypedArray from '../typed-array.js'

class Transform {
transformType: TransformType
Expand All @@ -10,8 +11,8 @@ class Transform {
inputSpaceName: string
outputSpaceName: string

parameters: number[]
fixedParameters: number[]
parameters: TypedArray
fixedParameters: TypedArray

constructor (transformType = new TransformType()) {
this.transformType = transformType
Expand All @@ -21,8 +22,8 @@ class Transform {
this.inputSpaceName = ''
this.outputSpaceName = ''

this.parameters = new Array(0)
this.fixedParameters = new Array(0)
this.parameters = new Uint8Array(0)
this.fixedParameters = new Uint8Array(0)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Image from '../../interface-types/image.js'
import Mesh from '../../interface-types/mesh.js'
import PointSet from '../../interface-types/point-set.js'
import PolyData from '../../interface-types/poly-data.js'
// import Transform from '../../interface-types/transform.js'
import TransformList from '../../interface-types/transform-list.js'
import FloatTypes from '../../interface-types/float-types.js'
import IntTypes from '../../interface-types/int-types.js'

Expand All @@ -22,7 +22,7 @@ const haveSharedArrayBuffer = typeof globalThis.SharedArrayBuffer === 'function'
const encoder = new TextEncoder()
const decoder = new TextDecoder('utf-8')

function readFileSharedArray (
function readFileSharedArray(
emscriptenModule: PipelineEmscriptenModule,
path: string
): Uint8Array {
Expand All @@ -42,7 +42,7 @@ function readFileSharedArray (
return array
}

function memoryUint8SharedArray (
function memoryUint8SharedArray(
emscriptenModule: PipelineEmscriptenModule,
byteOffset: number,
length: number
Expand All @@ -63,7 +63,7 @@ function memoryUint8SharedArray (
return array
}

function setPipelineModuleInputArray (
function setPipelineModuleInputArray(
emscriptenModule: PipelineEmscriptenModule,
dataArray: TypedArray | null,
inputIndex: number,
Expand All @@ -82,7 +82,7 @@ function setPipelineModuleInputArray (
return dataPtr
}

function setPipelineModuleInputJSON (
function setPipelineModuleInputJSON(
emscriptenModule: PipelineEmscriptenModule,
dataObject: object,
inputIndex: number
Expand All @@ -98,13 +98,13 @@ function setPipelineModuleInputJSON (
emscriptenModule.stringToUTF8(dataJSON, jsonPtr, length)
}

function getPipelineModuleOutputArray (
function getPipelineModuleOutputArray(
emscriptenModule: PipelineEmscriptenModule,
outputIndex: number,
subIndex: number,
componentType:
| (typeof IntTypes)[keyof typeof IntTypes]
| (typeof FloatTypes)[keyof typeof FloatTypes]
| (typeof IntTypes)[keyof typeof IntTypes]
| (typeof FloatTypes)[keyof typeof FloatTypes]
): TypedArray | Float32Array | Uint32Array | null {
const dataPtr = emscriptenModule.ccall(
'itk_wasm_output_array_address',
Expand All @@ -123,7 +123,7 @@ function getPipelineModuleOutputArray (
return data
}

function getPipelineModuleOutputJSON (
function getPipelineModuleOutputJSON(
emscriptenModule: PipelineEmscriptenModule,
outputIndex: number
): object {
Expand All @@ -138,7 +138,7 @@ function getPipelineModuleOutputJSON (
return dataObject
}

function runPipelineEmscripten (
function runPipelineEmscripten(
pipelineModule: PipelineEmscriptenModule,
args: string[],
outputs: PipelineOutput[] | null,
Expand Down Expand Up @@ -310,6 +310,42 @@ function runPipelineEmscripten (
setPipelineModuleInputJSON(pipelineModule, pointSetJSON, index)
break
}
case InterfaceTypes.TransformList: {
const transformList = input.data as TransformList
const transformListJSON: any = []
transformList.forEach((transform, transformIndex) => {
const fixedParameterPtr = setPipelineModuleInputArray(
pipelineModule,
transform.fixedParameters,
index,
transformIndex * 2
)
const fixedParameters = `data:application/vnd.itk.address,0:${fixedParameterPtr}`
const parameterPtr = setPipelineModuleInputArray(
pipelineModule,
transform.parameters,
index,
transformIndex * 2 + 1
)
const parameters = `data:application/vnd.itk.address,0:${parameterPtr}`
const transformJSON = {
transformType: transform.transformType,
numberOfFixedParameters: transform.numberOfFixedParameters,
numberOfParameters: transform.numberOfParameters,

name: transform.name,

inputSpaceName: transform.inputSpaceName,
outputSpaceName: transform.outputSpaceName,

parameters,
fixedParameters
}
transformListJSON.push(transformJSON)
})
setPipelineModuleInputJSON(pipelineModule, transformListJSON, index)
break
}
case InterfaceTypes.PolyData: {
const polyData = input.data as PolyData
const pointsPtr = setPipelineModuleInputArray(
Expand Down Expand Up @@ -618,6 +654,34 @@ function runPipelineEmscripten (
outputData = pointSet
break
}
case InterfaceTypes.TransformList: {
const transformList = getPipelineModuleOutputJSON(
pipelineModule,
index
) as TransformList
transformList.forEach((transform, transformIndex) => {
if (transform.numberOfFixedParameters > 0) {
transformList[transformIndex].fixedParameters =
getPipelineModuleOutputArray(
pipelineModule,
index,
transformIndex * 2,
transform.transformType.parametersValueType
) as TypedArray
}
if (transform.numberOfFixedParameters > 0) {
transformList[transformIndex].parameters =
getPipelineModuleOutputArray(
pipelineModule,
index,
transformIndex * 2 + 1,
transform.transformType.parametersValueType
) as TypedArray
}
})
outputData = transformList
break
}
case InterfaceTypes.PolyData: {
const polyData = getPipelineModuleOutputJSON(
pipelineModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Image from '../interface-types/image.js'
import Mesh from '../interface-types/mesh.js'
import PointSet from '../interface-types/point-set.js'
import PolyData from '../interface-types/poly-data.js'
import Transform from '../interface-types/transform.js'
import TransformList from '../interface-types/transform-list.js'
import JsonCompatible from '../interface-types/json-compatible.js'

interface PipelineInput {
Expand All @@ -24,7 +24,7 @@ interface PipelineInput {
| Mesh
| PointSet
| PolyData
| Transform
| TransformList
}

export default PipelineInput
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Image from '../interface-types/image.js'
import Mesh from '../interface-types/mesh.js'
import PointSet from '../interface-types/point-set.js'
import PolyData from '../interface-types/poly-data.js'
import Transform from '../interface-types/transform.js'
import TransformList from '../interface-types/transform-list.js'
import JsonCompatible from '../interface-types/json-compatible.js'

interface PipelineOutput {
Expand All @@ -24,7 +24,7 @@ interface PipelineOutput {
| Mesh
| PointSet
| PolyData
| Transform
| TransformList
}

export default PipelineOutput
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import test from 'ava'

import { Transform, TransformType } from '../../../dist/index-node.js'

test('transformType should have the same transformType passed to the constructor', t => {
const transform = new Transform()
const defaultTransformType = new TransformType()
t.deepEqual(transform.transformType, defaultTransformType)
})

test('name should have the default value of "Transform"', t => {
const transform = new Transform()
t.deepEqual(transform.name, 'Transform')
})

test('numberOfFixedParameters should have a default value of 0', t => {
const transform = new Transform()
t.is(transform.numberOfFixedParameters, 0)
})

test('numberOfParameters should have a default value of 0', t => {
const transform = new Transform()
t.is(transform.numberOfParameters, 0)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import test from 'ava'

import {
TransformType,
TransformParameterizations,
FloatTypes
} from '../../../dist/index-node.js'

test('inputDimension should have a default value of 3', (t) => {
const transformType = new TransformType()
t.is(transformType.inputDimension, 3)
})

test('outputDimension should have a default value of 3', (t) => {
const transformType = new TransformType()
t.is(transformType.outputDimension, 3)
})

test('transformParameterization should have a default value of Identity', (t) => {
const transformType = new TransformType()
t.is(
transformType.transformParameterization,
TransformParameterizations.Identity
)
})
test('parametersValueType should have the same value passed to the constructor', (t) => {
const transformType = new TransformType()
t.is(
transformType.parametersValueType,
FloatTypes.Float64
)
})
Loading

0 comments on commit e47e2c7

Please sign in to comment.