Skip to content

Commit

Permalink
WIP: ENH: WASM binding output paths are passed as input function argu…
Browse files Browse the repository at this point in the history
…ments
  • Loading branch information
thewtex committed Sep 28, 2023
1 parent 990cd96 commit 2bca22e
Show file tree
Hide file tree
Showing 43 changed files with 147 additions and 305 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"build:wasi:debug": "itk-wasm -i itkwasm/wasi:20230917-38fb6f92-debug build -- -DCMAKE_BUILD_TYPE:STRING=Debug",
"build:testData": "dam download wasm/test/data wasm/test/data.tar.gz bafybeiblzvqdyzykgfy47qfuec3mysd7s4not6t7hgrwkxyuhjmwesqdgi https://data.kitware.com/api/v1/file/64ebf79837a2250d4df1463e/download https://w3s.link/ipfs/bafybeibfwkkoiugogidxetnqor76dzppfrbaspimjuyz2vdqdtqe4s7are",
"bindgen:typescript": "/home/matt/src/itk-wasm/src/itk-wasm-cli.js -b emscripten-build bindgen --output-dir wasm/typescript --package-name @itk-wasm/elastix --package-version 0.1.0 --package-description \"A toolbox for rigid and nonrigid registration of images.\" --repository 'https://github.com/InsightSoftwareConsortium/ITKElastix'",
"bindgen:python": "itk-wasm -b wasi-build bindgen --interface python --output-dir wasm/python --package-name itkwasm-elastix --package-version 0.1.0 --package-description \"A toolbox for rigid and nonrigid registration of images.\" --repository 'https://github.com/InsightSoftwareConsortium/ITKElastix'",
"bindgen:python": "/home/matt/src/itk-wasm/src/itk-wasm-cli.js -b wasi-build bindgen --interface python --output-dir wasm/python --package-name itkwasm-elastix --package-version 0.1.0 --package-description \"A toolbox for rigid and nonrigid registration of images.\" --repository 'https://github.com/InsightSoftwareConsortium/ITKElastix'",
"test": "npm run test:wasi",
"test:wasi": "itk-wasm test --test-dir wasm -- -V",
"clean": "git clean -fdx -e node_modules"
Expand Down
2 changes: 1 addition & 1 deletion wasm/elastix-wasm.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class PipelineFunctor
pipeline.add_option("result", resultImage, "Resampled moving image")->required()->type_name("OUTPUT_IMAGE");

std::string outputTransform;
pipeline.add_option("transform", outputTransform, "Fixed-to-moving transform")
pipeline.add_option("transform", outputTransform, "Fixed-to-moving transform file")
->required()
->type_name("OUTPUT_BINARY_FILE");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@

async def elastix_async(
parameter_object: Any,
transform: str,
fixed: Optional[Image] = None,
moving: Optional[Image] = None,
initial_transform: Optional[os.PathLike] = None,
initial_transform_parameter_object: Optional[Any] = None,
) -> Tuple[Image, os.PathLike, Any]:
) -> Tuple[Image, Any]:
"""Rigid and non-rigid registration of images.
:param parameter_object: Elastix parameter object representation
:type parameter_object: Any
:param transform: Fixed-to-moving transform file
:type transform: str
:param fixed: Fixed image
:type fixed: Image
Expand All @@ -46,9 +50,6 @@ async def elastix_async(
:return: Resampled moving image
:rtype: Image
:return: Fixed-to-moving transform
:rtype: os.PathLike
:return: Elastix optimized transform parameter object representation
:rtype: Any
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@

async def write_parameter_files_async(
parameter_object: Any,
parameter_files: List[str],
) -> os.PathLike:
"""Write an elastix parameter text file from a parameter object.
:param parameter_object: Elastix parameter object representation
:param parameter_object: Elastix parameter object representation.
:type parameter_object: Any
:return: Elastix parameter files
:rtype: os.PathLike
:param parameter_files: Elastix parameter files, must have the same length as the number of parameter maps in the parameter object.
:type parameter_files: List[str]
"""
js_module = await js_package.js_module
web_worker = js_resources.web_worker
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Generated file. To retain edits, remove this comment.

# Generated file. Do not edit.

from pathlib import Path, PurePosixPath
import os
from typing import Dict, Tuple, Optional, List, Any
Expand Down Expand Up @@ -51,8 +49,11 @@ def default_parameter_map(
# Inputs
args.append(str(transform_name))
# Outputs
args.append('0')
parameter_map_name = '0'
args.append(parameter_map_name)

# Options
input_count = len(pipeline_inputs)
if number_of_resolutions:
args.append('--number-of-resolutions')
args.append(str(number_of_resolutions))
Expand Down
38 changes: 22 additions & 16 deletions wasm/python/itkwasm-elastix-wasi/itkwasm_elastix_wasi/elastix.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Generated file. To retain edits, remove this comment.

# Generated file. Do not edit.

from pathlib import Path, PurePosixPath
import os
from typing import Dict, Tuple, Optional, List, Any
Expand All @@ -21,16 +19,20 @@

def elastix(
parameter_object: Any,
transform: str,
fixed: Optional[Image] = None,
moving: Optional[Image] = None,
initial_transform: Optional[os.PathLike] = None,
initial_transform_parameter_object: Optional[Any] = None,
) -> Tuple[Image, os.PathLike, Any]:
) -> Tuple[Image, Any]:
"""Rigid and non-rigid registration of images.
:param parameter_object: Elastix parameter object representation
:type parameter_object: Any
:param transform: Fixed-to-moving transform file
:type transform: str
:param fixed: Fixed image
:type fixed: Image
Expand All @@ -46,9 +48,6 @@ def elastix(
:return: Resampled moving image
:rtype: Image
:return: Fixed-to-moving transform
:rtype: os.PathLike
:return: Elastix optimized transform parameter object representation
:rtype: Any
"""
Expand All @@ -70,21 +69,28 @@ def elastix(
# Inputs
args.append('0')
# Outputs
args.append('0')
args.append(str(PurePosixPath(transform)))
args.append('2')
result_name = '0'
args.append(result_name)

transform_name = str(PurePosixPath(transform))
args.append(transform_name)

transform_parameter_object_name = '2'
args.append(transform_parameter_object_name)

# Options
input_count = len(pipeline_inputs)
if fixed is not None:
input_count_string = str(len(pipeline_inputs))
pipeline_inputs.append(PipelineInput(InterfaceTypes.Image, fixed))
args.append('--fixed')
args.append(input_count_string)
args.append(str(input_count))
input_count += 1

if moving is not None:
input_count_string = str(len(pipeline_inputs))
pipeline_inputs.append(PipelineInput(InterfaceTypes.Image, moving))
args.append('--moving')
args.append(input_count_string)
args.append(str(input_count))
input_count += 1

if initial_transform is not None:
input_file = str(PurePosixPath(initial_transform))
Expand All @@ -93,17 +99,17 @@ def elastix(
args.append(input_file)

if initial_transform_parameter_object is not None:
input_count_string = str(len(pipeline_inputs))
pipeline_inputs.append(PipelineInput(InterfaceTypes.JsonCompatible, initial_transform_parameter_object))
args.append('--initial-transform-parameter-object')
args.append(input_count_string)
args.append(str(input_count))
input_count += 1


print(args, pipeline_outputs, pipeline_inputs)
outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)

result = (
outputs[0].data,
Path(outputs[1].data.path),
outputs[2].data.data,
)
return result
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Generated file. To retain edits, remove this comment.

# Generated file. Do not edit.

from pathlib import Path, PurePosixPath
import os
from typing import Dict, Tuple, Optional, List, Any
Expand Down Expand Up @@ -43,8 +41,11 @@ def read_parameter_files(
args: List[str] = ['--memory-io',]
# Inputs
# Outputs
args.append('0')
parameter_object_name = '0'
args.append(parameter_object_name)

# Options
input_count = len(pipeline_inputs)
if len(parameter_files) < 1:
raise ValueError('"parameter-files" kwarg must have a length > 1')
if len(parameter_files) > 0:
Expand Down
Binary file not shown.
Binary file not shown.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Generated file. To retain edits, remove this comment.

# Generated file. Do not edit.

from pathlib import Path, PurePosixPath
import os
from typing import Dict, Tuple, Optional, List, Any
Expand All @@ -20,22 +18,28 @@

def write_parameter_files(
parameter_object: Any,
parameter_files: List[str],
) -> os.PathLike:
"""Write an elastix parameter text file from a parameter object.
:param parameter_object: Elastix parameter object representation
:param parameter_object: Elastix parameter object representation.
:type parameter_object: Any
:return: Elastix parameter files
:rtype: os.PathLike
:param parameter_files: Elastix parameter files, must have the same length as the number of parameter maps in the parameter object.
:type parameter_files: List[str]
"""
global _pipeline
if _pipeline is None:
_pipeline = Pipeline(file_resources('itkwasm_elastix_wasi').joinpath(Path('wasm_modules') / Path('write-parameter-files.wasi.wasm')))
parameter_files_pipeline_outputs = [PipelineOutput(InterfaceTypes.TextFile, TextFile(PurePosixPath(p))) for p in parameter_files]

pipeline_outputs: List[PipelineOutput] = [
PipelineOutput(InterfaceTypes.TextFile, TextFile(PurePosixPath(parameter_files))),
*parameter_files_pipeline_outputs,
]
output_index = 0
parameter_files_start = output_index
output_index += len(parameter_files)
parameter_files_end = output_index

pipeline_inputs: List[PipelineInput] = [
PipelineInput(InterfaceTypes.JsonCompatible, parameter_object),
Expand All @@ -45,11 +49,11 @@ def write_parameter_files(
# Inputs
args.append('0')
# Outputs
args.append(str(PurePosixPath(parameter_files)))
args.extend([str(PurePosixPath(p)) for p in parameter_files])

# Options
input_count = len(pipeline_inputs)

outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)

result = Path(outputs[0].data.path)
return result

Loading

0 comments on commit 2bca22e

Please sign in to comment.