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 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 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
44 changes: 44 additions & 0 deletions Extensions/PythonCLIExtensionTemplate.s4ext
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# First token of each non-comment line is the keyword and the rest of the line
# (including spaces) is the value.
# - the value can be blank
#

# This is source code manager (i.e. svn)
scm local
scmurl Testing/PythonCLIExtensionTemplate
scmrevision NA

# list dependencies
# - These should be names of other modules that have .s4ext files
# - The dependencies will be built first
depends NA

# Inner build directory (default is ".")
build_subdirectory .

# homepage
homepage https://slicer.org/slicerWiki/index.php/Documentation/Nightly/Extensions/PythonCLIExtensionTemplate

# Firstname1 Lastname1 ([SubOrg1, ]Org1), Firstname2 Lastname2 ([SubOrg2, ]Org2)
# For example: Jane Roe (Superware), John Doe (Lab1, Nowhere), Joe Bloggs (Noware)
contributors John Doe (AnyWare Corp.)

# Match category in the xml description of the module (where it shows up in Modules menu)
category Examples

# url to icon (png, size 128x128 pixels)
iconurl https://www.example.com/Slicer/Extensions/PythonCLIExtensionTemplate.png

# Give people an idea what to expect from this code
# - Is it just a test or something you stand behind?
status

# One line stating what the module does
description This is an example of a simple extension

# Space separated list of urls
screenshoturls https://www.example.com/Slicer/Extensions/PythonCLIExtensionTemplate/Screenshots/1.png

# 0 or 1: Define if the extension should be enabled after its installation.
enabled 1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace
{

template <typename TPixel>
int DoIt( int argc, char * argv[], T )
int DoIt( int argc, char * argv[], TPixel )
{
PARSE_ARGS;

Expand Down
25 changes: 25 additions & 0 deletions Extensions/Testing/PythonCLIExtensionTemplate/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 2.8.9)

project(PythonCLIExtensionTemplate)

#-----------------------------------------------------------------------------
# Extension meta-information
set(EXTENSION_HOMEPAGE "https://slicer.org/slicerWiki/index.php/Documentation/Nightly/Extensions/PythonCLIExtensionTemplate")
set(EXTENSION_CATEGORY "Examples")
set(EXTENSION_CONTRIBUTORS "John Doe (AnyWare Corp.)")
set(EXTENSION_DESCRIPTION "This is an example of a simple extension")
set(EXTENSION_ICONURL "https://www.example.com/Slicer/Extensions/PythonCLIExtensionTemplate.png")
set(EXTENSION_SCREENSHOTURLS "https://www.example.com/Slicer/Extensions/PythonCLIExtensionTemplate/Screenshots/1.png")

#-----------------------------------------------------------------------------
# Extension dependencies
find_package(Slicer REQUIRED)
include(${Slicer_USE_FILE})

#-----------------------------------------------------------------------------
# Extension modules
add_subdirectory(PythonCLIModuleTemplate)
## NEXT_MODULE

#-----------------------------------------------------------------------------
include(${Slicer_EXTENSION_CPACK})
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import itk
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)
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>https://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)
2 changes: 1 addition & 1 deletion SuperBuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ if(Slicer_USE_SimpleITK)
endif()

if(Slicer_BUILD_CLI_SUPPORT)
list(APPEND Slicer_DEPENDENCIES SlicerExecutionModel)
list(APPEND Slicer_DEPENDENCIES SlicerExecutionModel python-ctk_cli)
endif()

if(Slicer_BUILD_EXTENSIONMANAGER_SUPPORT)
Expand Down
36 changes: 36 additions & 0 deletions SuperBuild/External_python-ctk_cli.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
set(proj python-ctk_cli)

# Set dependency list
set(${proj}_DEPENDENCIES python python-setuptools)

# Include dependent projects if any
ExternalProject_Include_Dependencies(${proj} PROJECT_VAR proj DEPENDS_VAR ${proj}_DEPENDENCIES)

if(${CMAKE_PROJECT_NAME}_USE_SYSTEM_${proj})
# XXX - Add a test checking if <proj> is available
endif()

if(NOT DEFINED ${CMAKE_PROJECT_NAME}_USE_SYSTEM_${proj})
set(${CMAKE_PROJECT_NAME}_USE_SYSTEM_${proj} ${${CMAKE_PROJECT_NAME}_USE_SYSTEM_python})
endif()

set(ctk_cli_REPOSITORY ${git_protocol}:https://github.com/commontk/ctk-cli.git)
set(ctk_cli_GIT_TAG 4346a22618b299c8eff84c6a179067ca68db43b9)

if(NOT ${CMAKE_PROJECT_NAME}_USE_SYSTEM_${proj})

ExternalProject_Add(${proj}
${${proj}_EP_ARGS}
GIT_REPOSITORY ${ctk_cli_REPOSITORY}
GIT_TAG ${ctk_cli_GIT_TAG}
SOURCE_DIR ${proj}
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ${PYTHON_EXECUTABLE} setup.py install
DEPENDS
${${proj}_DEPENDENCIES}
)
else()
ExternalProject_Add_Empty(${proj} DEPENDS ${${proj}_DEPENDENCIES})
endif()
2 changes: 1 addition & 1 deletion Utilities/Templates/Modules/CLI/TemplateKey.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace
{

template <typename TPixel>
int DoIt( int argc, char * argv[], T )
int DoIt( int argc, char * argv[], TPixel )
{
PARSE_ARGS;

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)
Loading