From 9e5e12df7e48fe0f6bcc67cff0da2a0acfbb41ce Mon Sep 17 00:00:00 2001 From: Matt McCormick Date: Sun, 19 Jan 2020 19:40:10 -0500 Subject: [PATCH] DOC: Notebook content for serializing ParameterObject's --- .gitignore | 1 + examples/2_RegistrationParameters.ipynb | 266 +++++++++++++++++++++--- 2 files changed, 234 insertions(+), 33 deletions(-) diff --git a/.gitignore b/.gitignore index 9420924f..e1e5d52a 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ tools/ zstd-1.2.0-linux.tar zstd-1.2.0-linux/ .ipynb_checkpoints/ +examples/Parameters.*.txt diff --git a/examples/2_RegistrationParameters.ipynb b/examples/2_RegistrationParameters.ipynb index aec6e4e1..e1354c95 100644 --- a/examples/2_RegistrationParameters.ipynb +++ b/examples/2_RegistrationParameters.ipynb @@ -4,12 +4,12 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Examine and Modify Registration Parameters" + "# Examine, Modify, and Save Registration Parameters" ] }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -23,12 +23,12 @@ "source": [ "[Elastix registration parameters](http://elastix.isi.uu.nl/doxygen/parameter.html) are stored in a *ParameterObject*.\n", "\n", - "Let's find out how to examine default registration parameters and modify their values." + "Let's find out how to examine default registration parameters, modify their values, and serialize them." ] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -37,17 +37,17 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Self (0x1d64050)\n", + "Self (0x55e28544c5e0)\n", " RTTI typeinfo: elastix::ParameterObject\n", " Reference Count: 1\n", - " Modified Time: 67\n", + " Modified Time: 56\n", " Debug: Off\n", " Object Name: \n", " Observers: \n", @@ -106,7 +106,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -115,13 +115,13 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "19343049a8924fe58728b36450e4d93c", + "model_id": "85298f1bfb5d40bcb220771e8f155595", "version_major": 2, "version_minor": 0 }, @@ -139,7 +139,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ @@ -148,13 +148,13 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "9d154703982645b3b90061a0a21e371a", + "model_id": "9511aa67abc14a0d97c3fe7e29607618", "version_major": 2, "version_minor": 0 }, @@ -172,13 +172,13 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "b7c66cb3e4b344dab22a4c22f98b68d1", + "model_id": "a67097d976ae47adb5365419b32d2b9e", "version_major": 2, "version_minor": 0 }, @@ -196,7 +196,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -205,13 +205,13 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { - "model_id": "437243537e33433094d574fd2da5b1cb", + "model_id": "c379640b0f854cd09f40facd01151aff", "version_major": 2, "version_minor": 0 }, @@ -240,17 +240,17 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Self (0x1d64050)\n", + "Self (0x55e28544c5e0)\n", " RTTI typeinfo: elastix::ParameterObject\n", " Reference Count: 1\n", - " Modified Time: 24855\n", + " Modified Time: 24781\n", " Debug: Off\n", " Object Name: \n", " Observers: \n", @@ -322,17 +322,17 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Self (0x1d64050)\n", + "Self (0x55e28544c5e0)\n", " RTTI typeinfo: elastix::ParameterObject\n", " Reference Count: 1\n", - " Modified Time: 24856\n", + " Modified Time: 24782\n", " Debug: Off\n", " Object Name: \n", " Observers: \n", @@ -432,7 +432,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -441,7 +441,7 @@ "3" ] }, - "execution_count": 28, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } @@ -452,17 +452,17 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Self (0x1d64050)\n", + "Self (0x55e28544c5e0)\n", " RTTI typeinfo: elastix::ParameterObject\n", " Reference Count: 1\n", - " Modified Time: 24856\n", + " Modified Time: 24782\n", " Debug: Off\n", " Object Name: \n", " Observers: \n", @@ -560,17 +560,17 @@ }, { "cell_type": "code", - "execution_count": 34, + "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Self (0x1d64050)\n", + "Self (0x55e28544c5e0)\n", " RTTI typeinfo: elastix::ParameterObject\n", " Reference Count: 1\n", - " Modified Time: 24856\n", + " Modified Time: 24782\n", " Debug: Off\n", " Object Name: \n", " Observers: \n", @@ -664,6 +664,206 @@ "print(parameters)" ] }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Self (0x55e28544c5e0)\n", + " RTTI typeinfo: elastix::ParameterObject\n", + " Reference Count: 1\n", + " Modified Time: 24782\n", + " Debug: Off\n", + " Object Name: \n", + " Observers: \n", + " none\n", + "ParameterMap 0: \n", + " (AutomaticParameterEstimation \"true\")\n", + " (AutomaticTransformInitialization \"true\")\n", + " (CheckNumberOfSamples \"true\")\n", + " (DefaultPixelValue 0)\n", + " (FinalBSplineInterpolationOrder 3)\n", + " (FixedImagePyramid \"FixedSmoothingImagePyramid\")\n", + " (ImageSampler \"RandomCoordinate\")\n", + " (Interpolator \"LinearInterpolator\")\n", + " (MaximumNumberOfIterations 256)\n", + " (MaximumNumberOfSamplingAttempts 8)\n", + " (Metric \"AdvancedMattesMutualInformation\")\n", + " (MovingImagePyramid \"MovingSmoothingImagePyramid\")\n", + " (NewSamplesEveryIteration \"true\")\n", + " (NumberOfResolutions 5)\n", + " (NumberOfSamplesForExactGradient 4096)\n", + " (NumberOfSpatialSamples 2048)\n", + " (Optimizer \"AdaptiveStochasticGradientDescent\")\n", + " (Registration \"MultiResolutionRegistration\")\n", + " (ResampleInterpolator \"FinalBSplineInterpolator\")\n", + " (Resampler \"DefaultResampler\")\n", + " (Transform \"TranslationTransform\")\n", + " (WriteIterationInfo \"false\")\n", + " (WriteResultImage \"true\")\n", + "ParameterMap 1: \n", + " (AutomaticParameterEstimation \"true\")\n", + " (AutomaticScalesEstimation \"true\")\n", + " (CheckNumberOfSamples \"true\")\n", + " (DefaultPixelValue 0)\n", + " (FinalBSplineInterpolationOrder 3)\n", + " (FixedImagePyramid \"FixedSmoothingImagePyramid\")\n", + " (ImageSampler \"RandomCoordinate\")\n", + " (Interpolator \"LinearInterpolator\")\n", + " (MaximumNumberOfIterations 256)\n", + " (MaximumNumberOfSamplingAttempts 8)\n", + " (Metric \"AdvancedMattesMutualInformation\")\n", + " (MovingImagePyramid \"MovingSmoothingImagePyramid\")\n", + " (NewSamplesEveryIteration \"true\")\n", + " (NumberOfResolutions 5)\n", + " (NumberOfSamplesForExactGradient 4096)\n", + " (NumberOfSpatialSamples 2048)\n", + " (Optimizer \"AdaptiveStochasticGradientDescent\")\n", + " (Registration \"MultiResolutionRegistration\")\n", + " (ResampleInterpolator \"FinalBSplineInterpolator\")\n", + " (Resampler \"DefaultResampler\")\n", + " (Transform \"AffineTransform\")\n", + " (WriteIterationInfo \"false\")\n", + " (WriteResultImage \"true\")\n", + "ParameterMap 2: \n", + " (AutomaticParameterEstimation \"true\")\n", + " (CheckNumberOfSamples \"true\")\n", + " (DefaultPixelValue 0)\n", + " (FinalBSplineInterpolationOrder 3)\n", + " (FinalGridSpacingInPhysicalUnits 20)\n", + " (FixedImagePyramid \"FixedSmoothingImagePyramid\")\n", + " (GridSpacingSchedule 1)\n", + " (ImageSampler \"RandomCoordinate\")\n", + " (Interpolator \"LinearInterpolator\")\n", + " (MaximumNumberOfIterations 256)\n", + " (MaximumNumberOfSamplingAttempts 8)\n", + " (Metric \"AdvancedMattesMutualInformation\" \"TransformBendingEnergyPenalty\")\n", + " (Metric0Weight 1)\n", + " (Metric1Weight 1)\n", + " (MovingImagePyramid \"MovingSmoothingImagePyramid\")\n", + " (NewSamplesEveryIteration \"true\")\n", + " (NumberOfResolutions 5)\n", + " (NumberOfSamplesForExactGradient 4096)\n", + " (NumberOfSpatialSamples 2048)\n", + " (Optimizer \"AdaptiveStochasticGradientDescent\")\n", + " (Registration \"MultiMetricMultiResolutionRegistration\")\n", + " (ResampleInterpolator \"FinalBSplineInterpolator\")\n", + " (Resampler \"DefaultResampler\")\n", + " (Transform \"BSplineTransform\")\n", + " (WriteIterationInfo \"false\")\n", + " (WriteResultImage \"true\")\n", + "\n" + ] + } + ], + "source": [ + "# Remove a parameter\n", + "parameters.RemoveParameter(\"ResultImageFormat\")\n", + "\n", + "print(parameters)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(AutomaticParameterEstimation \"true\")\n", + "(AutomaticScalesEstimation \"true\")\n", + "(CheckNumberOfSamples \"true\")\n", + "(DefaultPixelValue 0.000000)\n", + "(FinalBSplineInterpolationOrder 3.000000)\n", + "(FixedImagePyramid \"FixedSmoothingImagePyramid\")\n", + "(ImageSampler \"RandomCoordinate\")\n", + "(Interpolator \"LinearInterpolator\")\n", + "(MaximumNumberOfIterations 256.000000)\n", + "(MaximumNumberOfSamplingAttempts 8.000000)\n", + "(Metric \"AdvancedMattesMutualInformation\")\n", + "(MovingImagePyramid \"MovingSmoothingImagePyramid\")\n", + "(NewSamplesEveryIteration \"true\")\n", + "(NumberOfResolutions 5.000000)\n", + "(NumberOfSamplesForExactGradient 4096.000000)\n", + "(NumberOfSpatialSamples 2048.000000)\n", + "(Optimizer \"AdaptiveStochasticGradientDescent\")\n", + "(Registration \"MultiResolutionRegistration\")\n", + "(ResampleInterpolator \"FinalBSplineInterpolator\")\n", + "(Resampler \"DefaultResampler\")\n", + "(Transform \"AffineTransform\")\n", + "(WriteIterationInfo \"false\")\n", + "(WriteResultImage \"true\")\n" + ] + } + ], + "source": [ + "# Each parameter map can be serialized to a file.\n", + "for index in range(parameters.GetNumberOfParameterMaps()):\n", + " parameter_map = parameters.GetParameterMap(index)\n", + " parameters.WriteParameterFile(parameter_map, \"Parameters.{0}.txt\".format(index))\n", + " \n", + "%cat Parameters.1.txt" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Self (0x55e287b14970)\n", + " RTTI typeinfo: elastix::ParameterObject\n", + " Reference Count: 1\n", + " Modified Time: 24796\n", + " Debug: Off\n", + " Object Name: \n", + " Observers: \n", + " none\n", + "ParameterMap 0: \n", + " (AutomaticParameterEstimation \"true\")\n", + " (AutomaticTransformInitialization \"true\")\n", + " (CheckNumberOfSamples \"true\")\n", + " (DefaultPixelValue 0)\n", + " (FinalBSplineInterpolationOrder 3)\n", + " (FixedImagePyramid \"FixedSmoothingImagePyramid\")\n", + " (ImageSampler \"RandomCoordinate\")\n", + " (Interpolator \"LinearInterpolator\")\n", + " (MaximumNumberOfIterations 256)\n", + " (MaximumNumberOfSamplingAttempts 8)\n", + " (Metric \"AdvancedMattesMutualInformation\")\n", + " (MovingImagePyramid \"MovingSmoothingImagePyramid\")\n", + " (NewSamplesEveryIteration \"true\")\n", + " (NumberOfResolutions 5)\n", + " (NumberOfSamplesForExactGradient 4096)\n", + " (NumberOfSpatialSamples 2048)\n", + " (Optimizer \"AdaptiveStochasticGradientDescent\")\n", + " (Registration \"MultiResolutionRegistration\")\n", + " (ResampleInterpolator \"FinalBSplineInterpolator\")\n", + " (Resampler \"DefaultResampler\")\n", + " (Transform \"TranslationTransform\")\n", + " (WriteIterationInfo \"false\")\n", + " (WriteResultImage \"true\")\n", + "\n" + ] + } + ], + "source": [ + "# To deserialize the parameters\n", + "parameter_files = [\"Parameters.{0}.txt\".format(i) for i in range(3)]\n", + "restored_parameters = itk.ParameterObject.New()\n", + "restored_parameters.ReadParameterFile(\"Parameters.{0}.txt\".format(index))\n", + "print(restored_parameters)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -688,7 +888,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.9" + "version": "3.7.6" } }, "nbformat": 4,