Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .binder/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
itk-elastix>=0.24.0
itk-elastix>=0.25.0
itkwidgets>=0.32.0
jupyterlab>=2.2.0
imageio
Expand Down
114 changes: 36 additions & 78 deletions examples/ITK_Example_TomlFileFormatForParameterFiles.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -508,84 +508,42 @@
},
{
"cell_type": "markdown",
"id": "ee767dbf-4998-4c4c-bafa-56ecaa7fc14c",
"metadata": {},
"source": [
"## Bonus: How to convert a dict of TOML values to an elastix ParameterMap\n",
"\n",
"The following cell defines a utility function, `convert_to_parameter_map` to convert a TOML dict to a `ParameterMap`. It shows a little example, loading a TOML dict, converting the dict, and passing the result to a `ParameterObject`."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "5c53f8d7-8398-46e6-87e2-b2586eab9125",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'NumberOfParameters': 2, 'Transform': 'TranslationTransform', 'TransformParameters': [1.2345, -1048]}\n",
"ParameterObject (00000214E1672CF0)\n",
" RTTI typeinfo: class elastix::ParameterObject\n",
" Reference Count: 1\n",
" Modified Time: 2507\n",
" Debug: Off\n",
" Object Name: \n",
" Observers: \n",
" none\n",
"ParameterMap 0: \n",
" (NumberOfParameters 2)\n",
" (Transform \"TranslationTransform\")\n",
" (TransformParameters 1.2345 -1048)\n",
"\n"
]
}
],
"source": [
"# Converts a single TOML dict value (which may be a string, a bool, an integer, or a float) to an elastix parameter value (which is a string).\n",
"def convert_to_parameter_value(value) -> str:\n",
" if isinstance(value, str):\n",
" return value\n",
" if isinstance(value, bool):\n",
" # str(value) returns either \"True\" or \"False\", but elastix prefers lower-case.\n",
" return \"true\" if value else \"false\"\n",
" return str(value)\n",
"\n",
"\n",
"# Converts the specified \"TOML dict\" (which may be produced by tomllib) to a dict that is compatible with the elastix ParameterMap type:\n",
"# mapping strings to tuples of strings.\n",
"def convert_to_parameter_map(toml_dict: dict) -> dict:\n",
" result = {}\n",
" for key, value in toml_dict.items():\n",
" if isinstance(value, (list, tuple)):\n",
" result[key] = tuple(convert_to_parameter_value(item) for item in value)\n",
" else:\n",
" result[key] = (convert_to_parameter_value(value),)\n",
" return result\n",
"\n",
"\n",
"# Example, trying out convert_to_parameter_map:\n",
"\n",
"toml_dict = tomllib.loads(\n",
" \"\"\"\n",
"# Example transformation\n",
"NumberOfParameters = 2\n",
"Transform = \"TranslationTransform\"\n",
"TransformParameters = [ 1.2345, -1048 ]\n",
"\"\"\"\n",
")\n",
"\n",
"print(toml_dict)\n",
"\n",
"parameter_object = itk.ParameterObject.New(\n",
" parameter_map=convert_to_parameter_map(toml_dict)\n",
")\n",
"\n",
"print(parameter_object)"
]
}
"id": "ee767dbf-4998-4c4c-bafa-56ecaa7fc14c",
"metadata": {},
"source": [
"## Bonus: Direct passage of a dict of TOML values\n",
"\n",
"A TOML dict (e.g. produced by `tomllib`) can be passed directly to `itk.dict_to_parameter_object`,\n",
"which converts Python-native types (str, bool, int, float, and lists thereof) to the elastix\n",
"ParameterMap format automatically. The dict can also be passed directly to\n",
"`itk.elastix_registration_method` and `itk.transformix_filter` as the `parameter_object`\n",
"or `transform_parameter_object` argument."
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "5c53f8d7-8398-46e6-87e2-b2586eab9125",
"metadata": {},
"outputs": [],
"source": [
"toml_dict = tomllib.loads(\n",
" \"\"\"\n",
"# Example transformation\n",
"NumberOfParameters = 2\n",
"Transform = \"TranslationTransform\"\n",
"TransformParameters = [ 1.2345, -1048 ]\n",
"\"\"\"\n",
")\n",
"\n",
"print(toml_dict)\n",
"\n",
"# Direct passage: the dict is automatically converted to a ParameterObject\n",
"parameter_object = itk.dict_to_parameter_object(toml_dict)\n",
"\n",
"print(parameter_object)"
]
}
],
"metadata": {
"kernelspec": {
Expand Down
2 changes: 1 addition & 1 deletion examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
itk-elastix>=0.24.0
itk-elastix>=0.25.0
itkwidgets>=0.32.0
jupyterlab>=2.2.0
imageio
Expand Down
Loading