Skip to content

Commit e3a73d0

Browse files
committed
ENH: Addition of an example to use ctk_cli
ctk_cli allows to parse python scripts command lines the same way they would be parsed with SlicerExecutionModel. This commit adds an example to show how to use this package.
1 parent 80a5a9b commit e3a73d0

File tree

10 files changed

+123
-0
lines changed

10 files changed

+123
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import itk
2+
from ctk_cli import *
3+
import sys
4+
import logging
5+
6+
def SmoothingRecursiveGaussianImageFilter(inputVolume, outputVolume, sigma):
7+
reader = itk.ImageFileReader.New(FileName=inputVolume)
8+
filter = itk.SmoothingRecursiveGaussianImageFilter.New(reader)
9+
filter.SetSigma(sigma)
10+
writer = itk.ImageFileWriter.New(filter,FileName=outputVolume)
11+
writer.SetUseCompression(True)
12+
writer.Update()
13+
return 1
14+
15+
16+
def main():
17+
"""Parsing command line arguments and reading input files."""
18+
logging.basicConfig(level=logging.INFO)
19+
args=CLIArgumentParser().parse_args()
20+
# Run processing
21+
SmoothingRecursiveGaussianImageFilter(args.inputVolume,args.outputVolume,args.sigma)
22+
# Compare output with baseline
23+
reader1 = itk.ImageFileReader.New(FileName=args.outputVolume)
24+
reader2 = itk.ImageFileReader.New(FileName=args.baselineVolume)
25+
compareFilter=itk.ComparisonImageFilter.New(reader1)
26+
compareFilter.SetTestInput(reader1)
27+
compareFilter.SetValidInput(reader2)
28+
diff=compareFilter.GetTotalDifference()
29+
if diff < args.tolerance:
30+
return 0
31+
return 1
32+
33+
if __name__ == "__main__":
34+
ret=main()
35+
if ret:
36+
sys.exit(ret)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<executable>
3+
<category>Examples</category>
4+
<title>CLIPythonModuleTemplate</title>
5+
<description><![CDATA[This is a CLI module that can be bundled in an extension]]></description>
6+
<version>0.0.1</version>
7+
<documentation-url>http://www.example.com/Slicer/Modules/CLIPythonModuleTemplate</documentation-url>
8+
<license>Slicer</license>
9+
<contributor>FirstName LastName (Institution), FirstName LastName (Institution)</contributor>
10+
<acknowledgements>This work was partially funded by NIH grant NXNNXXNNNNNN-NNXN</acknowledgements>
11+
<parameters>
12+
<label>IO</label>
13+
<description><![CDATA[Input/output parameters]]></description>
14+
<double>
15+
<name>sigma</name>
16+
<longflag>sigma</longflag>
17+
<flag>s</flag>
18+
<label>Sigma</label>
19+
<description><![CDATA[A double value (in units of mm) passed to the algorithm]]></description>
20+
<default>1.0</default>
21+
</double>
22+
<image>
23+
<name>inputVolume</name>
24+
<label>Input Volume</label>
25+
<channel>input</channel>
26+
<index>0</index>
27+
<description><![CDATA[Input volume]]></description>
28+
</image>
29+
<image>
30+
<name>outputVolume</name>
31+
<label>Output Volume</label>
32+
<channel>output</channel>
33+
<index>1</index>
34+
<description><![CDATA[Output Volume]]></description>
35+
</image>
36+
<image>
37+
<name>baselineVolume</name>
38+
<label>Baseline Volume</label>
39+
<channel>input</channel>
40+
<index>2</index>
41+
<description><![CDATA[Output Volume]]></description>
42+
</image>
43+
<double>
44+
<name>tolerance</name>
45+
<longflag>tolerance</longflag>
46+
<flag>t</flag>
47+
<label>Tolerance</label>
48+
<description><![CDATA[Tolerance between the output image and the baseline (sum)]]></description>
49+
<default>0.0001</default>
50+
</double>
51+
</parameters>
52+
</executable>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
#-----------------------------------------------------------------------------
3+
set(MODULE_NAME CLIModuleTemplate)
4+
5+
#-----------------------------------------------------------------------------
6+
7+
set(PYTHON_MODULE_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/${MODULE_NAME}Python.py)
8+
9+
#-----------------------------------------------------------------------------
10+
if(BUILD_TESTING)
11+
add_subdirectory(Testing)
12+
endif()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fc6170ceeff3d8217a9dd6a1add2ec8c
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0749d4d3f07a217030f9ae33d94c4559
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6e5c289c73e14ba7a1b0f8aaf6ed249a
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3ebd710c9cf9d75750f4569b8caf6d07
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(Python)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#-----------------------------------------------------------------------------
2+
set(BASELINE ${CMAKE_CURRENT_SOURCE_DIR}/../../Data/Baseline)
3+
set(INPUT ${CMAKE_CURRENT_SOURCE_DIR}/../../Data/Input)
4+
set(TEMP "${CMAKE_BINARY_DIR}/Testing/Temporary")
5+
6+
set(CLP ${MODULE_NAME})
7+
8+
#-----------------------------------------------------------------------------
9+
set(testname ${CLP}PythonTest)
10+
ExternalData_add_test(${CLP}PythonData NAME ${testname} COMMAND ${Slicer_LAUNCHER_EXECUTABLE} ${PYTHON_MODULE_SCRIPT}
11+
--sigma 2.5 DATA{${INPUT}/CTHeadAxial.nhdr,CTHeadAxial.raw.gz} ${TEMP}/${CLP}Test.nhdr
12+
DATA{${BASELINE}/${CLP}Test.nhdr,${CLP}Test.raw}
13+
)
14+
set_property(TEST ${testname} PROPERTY LABELS ${CLP})
15+
16+
#-----------------------------------------------------------------------------
17+
ExternalData_add_target(${CLP}PythonData)

Extensions/Testing/CLIExtensionTemplate/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ include(${Slicer_USE_FILE})
1919
#-----------------------------------------------------------------------------
2020
# Extension modules
2121
add_subdirectory(CLIModuleTemplate)
22+
add_subdirectory(CLIModuleTemplatePython)
2223
## NEXT_MODULE
2324

2425
#-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)