Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ctk cli2 #497

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 CMake/SlicerExtensionTemplatesGenerator.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ macro(_append_extension_template_generator_commands module_type)
endmacro()

# Loop over module type and add template generators
foreach(type IN ITEMS CLI Loadable ScriptedLoadable)
foreach(type IN ITEMS CLI PythonCLI Loadable ScriptedLoadable)
_append_extension_template_generator_commands(${type})
endforeach()

Expand Down
24 changes: 24 additions & 0 deletions Utilities/Templates/Modules/PythonCLI/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#-----------------------------------------------------------------------------
set(MODULE_NAME PythonCLIModuleTemplate)

#-----------------------------------------------------------------------------
set(MODULE_PYTHON_SCRIPTS
${MODULE_NAME}.py
)

set(MODULE_PYTHON_RESOURCES
${MODULE_NAME}.xml
)

#-----------------------------------------------------------------------------
slicerMacroBuildScriptedModule(
NAME ${MODULE_NAME}
SCRIPTS ${MODULE_PYTHON_SCRIPTS}
RESOURCES ${MODULE_PYTHON_RESOURCES}
)

#-----------------------------------------------------------------------------
if(BUILD_TESTING)
# Additional build-time testing
add_subdirectory(Testing)
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fc6170ceeff3d8217a9dd6a1add2ec8c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0749d4d3f07a217030f9ae33d94c4559
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
6e5c289c73e14ba7a1b0f8aaf6ed249a
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3ebd710c9cf9d75750f4569b8caf6d07
36 changes: 36 additions & 0 deletions Utilities/Templates/Modules/PythonCLI/PythonCLIModuleTemplate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import itk
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See https://pypi.python.org/pypi/flake8-import-order

import logging
import sys

from ctk_cli import CLIArgumentParser

import itk

Useful resrouces: https://pypi.python.org/pypi/flake8-import-order and

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, would it be possible to use SimpleITK, wrapped ITK is not yet enabled by default.

Thanks for your help

from ctk_cli import *
import sys
import logging

def SmoothingRecursiveGaussianImageFilter(inputVolume, outputVolume, sigma):
reader = itk.ImageFileReader.New(FileName=inputVolume)
filter = itk.SmoothingRecursiveGaussianImageFilter.New(reader)
filter.SetSigma(sigma)
writer = itk.ImageFileWriter.New(filter,FileName=outputVolume)
writer.SetUseCompression(True)
writer.Update()
return 1


def main():
"""Parsing command line arguments and reading input files."""
logging.basicConfig(level=logging.INFO)
args=CLIArgumentParser().parse_args()
# Run processing
SmoothingRecursiveGaussianImageFilter(args.inputVolume,args.outputVolume,args.sigma)
# Compare output with baseline
reader1 = itk.ImageFileReader.New(FileName=args.outputVolume)
reader2 = itk.ImageFileReader.New(FileName=args.baselineVolume)
compareFilter=itk.ComparisonImageFilter.New(reader1)
compareFilter.SetTestInput(reader1)
compareFilter.SetValidInput(reader2)
diff=compareFilter.GetTotalDifference()
if diff < args.tolerance:
return 0
return 1

if __name__ == "__main__":
ret=main()
if ret:
sys.exit(ret)
52 changes: 52 additions & 0 deletions Utilities/Templates/Modules/PythonCLI/PythonCLIModuleTemplate.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<executable>
<category>Examples</category>
<title>PythonCLIModuleTemplate</title>
<description><![CDATA[This is a Python CLI module that can be bundled in an extension]]></description>
<version>0.0.1</version>
<documentation-url>http://www.example.com/Slicer/Modules/PythonCLIModuleTemplate</documentation-url>
<license>Slicer</license>
<contributor>FirstName LastName (Institution), FirstName LastName (Institution)</contributor>
<acknowledgements>This work was partially funded by NIH grant NXNNXXNNNNNN-NNXN</acknowledgements>
<parameters>
<label>IO</label>
<description><![CDATA[Input/output parameters]]></description>
<double>
<name>sigma</name>
<longflag>sigma</longflag>
<flag>s</flag>
<label>Sigma</label>
<description><![CDATA[A double value (in units of mm) passed to the algorithm]]></description>
<default>1.0</default>
</double>
<image>
<name>inputVolume</name>
<label>Input Volume</label>
<channel>input</channel>
<index>0</index>
<description><![CDATA[Input volume]]></description>
</image>
<image>
<name>outputVolume</name>
<label>Output Volume</label>
<channel>output</channel>
<index>1</index>
<description><![CDATA[Output Volume]]></description>
</image>
<image>
<name>baselineVolume</name>
<label>Baseline Volume</label>
<channel>input</channel>
<index>2</index>
<description><![CDATA[Output Volume]]></description>
</image>
<double>
<name>tolerance</name>
<longflag>tolerance</longflag>
<flag>t</flag>
<label>Tolerance</label>
<description><![CDATA[Tolerance between the output image and the baseline (sum)]]></description>
<default>0.0001</default>
</double>
</parameters>
</executable>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(Python)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#-----------------------------------------------------------------------------
set(BASELINE ${CMAKE_CURRENT_SOURCE_DIR}/../../Data/Baseline)
set(INPUT ${CMAKE_CURRENT_SOURCE_DIR}/../../Data/Input)
set(TEMP "${CMAKE_BINARY_DIR}/Testing/Temporary")

set(CLP ${MODULE_NAME})

#-----------------------------------------------------------------------------
set(testname ${CLP}Test)
ExternalData_add_test(${CLP}Data NAME ${testname} COMMAND ${Slicer_LAUNCHER_EXECUTABLE} ${MODULE_NAME}.py
--sigma 2.5 DATA{${INPUT}/CTHeadAxial.nhdr,CTHeadAxial.raw.gz} ${TEMP}/${CLP}Test.nhdr
DATA{${BASELINE}/${CLP}Test.nhdr,${CLP}Test.raw}
)
set_property(TEST ${testname} PROPERTY LABELS ${CLP})

#-----------------------------------------------------------------------------
ExternalData_add_target(${CLP}Data)