diff --git a/CMake/PackageDepends/MITK_Numpy_Config.cmake b/CMake/PackageDepends/MITK_Numpy_Config.cmake index adf8728aac..f2243e1c04 100644 --- a/CMake/PackageDepends/MITK_Numpy_Config.cmake +++ b/CMake/PackageDepends/MITK_Numpy_Config.cmake @@ -1,5 +1,4 @@ -if(${Numpy_DIR}) +if(NOT MITK_USE_SYSTEM_PYTHON) set(Numpy_INCLUDE_DIR ${Numpy_DIR}/core/include) list(APPEND ALL_INCLUDE_DIRECTORIES ${Numpy_INCLUDE_DIR}) - include_directories(${Numpy_INCLUDE_DIR}) endif() diff --git a/CMakeExternals/Python.cmake b/CMakeExternals/Python.cmake index 44d443a1fd..b9b28d619d 100644 --- a/CMakeExternals/Python.cmake +++ b/CMakeExternals/Python.cmake @@ -1,175 +1,175 @@ #---------------------------------------------------------------------- # Python #---------------------------------------------------------------------- if( MITK_USE_Python AND NOT MITK_USE_SYSTEM_PYTHON ) # Sanity checks if(DEFINED Python_DIR AND NOT EXISTS ${Python_DIR}) message(FATAL_ERROR "Python_DIR variable is defined but corresponds to non-existing directory") endif() if(NOT DEFINED Python_DIR) set(proj Python) - set(proj_DEPENDENCIES ZLIB) + set(Python_DEPENDENCIES ZLIB Python-src) set(Python_DEPENDS ${proj}) set(MITK_PYTHON_MAJOR_VERSION 2) set(MITK_PYTHON_MINOR_VERSION 7) set(MITK_PYTHON_PATCH_VERSION 3) set(PYTHON_SOURCE_PACKAGE Python-${MITK_PYTHON_MAJOR_VERSION}.${MITK_PYTHON_MINOR_VERSION}.${MITK_PYTHON_PATCH_VERSION}) set(PYTHON_SOURCE_DIR "${CMAKE_BINARY_DIR}/${PYTHON_SOURCE_PACKAGE}") # patch the VS compiler config if(WIN32) set(PYTHON_PATCH_COMMAND PATCH_COMMAND ${CMAKE_COMMAND} -DPYTHON_SOURCE_DIR:PATH=${PYTHON_SOURCE_DIR} -P ${CMAKE_CURRENT_LIST_DIR}/Patch${proj}.cmake) endif() # download the source code ExternalProject_Add(Python-src URL "https://dl.dropboxusercontent.com/u/8367205/ExternalProjects/${PYTHON_SOURCE_PACKAGE}.tgz" URL_MD5 "2cf641732ac23b18d139be077bd906cd" PREFIX ${CMAKE_BINARY_DIR}/${PYTHON_SOURCE_PACKAGE}-cmake SOURCE_DIR "${PYTHON_SOURCE_DIR}" ${PYTHON_PATCH_COMMAND} CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" ) set(additional_cmake_cache_args ) list(APPEND additional_cmake_cache_args -DBUILTIN_ARRAY:BOOL=ON -DBUILTIN_AUDIOOP:BOOL=ON -DBUILTIN_BINASCII:BOOL=ON -DBUILTIN_BISECT:BOOL=ON -DBUILTIN_BSDB:BOOL=ON -DBUILTIN_BSSDB:BOOL=ON -DBUILTIN_BZ2:BOOL=ON -DBUILTIN_CMATH:BOOL=ON -DBUILTIN_COLLECTIONS:BOOL=ON -DBUILTIN_CODECS_CN:BOOL=ON -DBUILTIN_CODECS_HK:BOOL=ON -DBUILTIN_CODECS_ISO2022:BOOL=ON -DBUILTIN_CODECS_JP:BOOL=ON -DBUILTIN_CODECS_KR:BOOL=ON -DBUILTIN_CODECS_TW:BOOL=ON -DBUILTIN_CPICKLE:BOOL=ON -DBUILTIN_CRYPT:BOOL=ON -DBUILTIN_CSTRINGIO:BOOL=ON -DBUILTIN_CSV:BOOL=ON -DBUILTIN_CTYPES:BOOL=OFF #-DBUILTIN_CTYPES_TEST:BOOL=OFF #-DBUILTIN_CURSES:BOOL=ON -DBUILTIN_DATETIME:BOOL=ON -DBUILTIN_DBM:BOOL=ON -DBUILTIN_ELEMENTTREE:BOOL=ON -DBUILTIN_FCNTL:BOOL=ON -DBUILTIN_FUNCTOOLS:BOOL=ON -DBUILTIN_FUTURE_BUILTINS:BOOL=ON -DBULTIN_GDBM:BOOL=ON -DBUILTIN_GRP:BOOL=ON -DBUILTIN_HASHLIB:BOOL=ON -DBUILTIN_HEAPQ:BOOL=ON -DBUILTIN_HOTSHOT:BOOL=ON -DBUILTIN_IO:BOOL=ON -DBUILTIN_ITERTOOLS:BOOL=ON -DBUILTIN_JSON:BOOL=ON -DBUILTIN_LOCALE:BOOL=ON -DBUILTIN_LSPROF:BOOL=ON -DBUILTIN_MATH:BOOL=ON -DBUILTIN_MMAP:BOOL=ON -DBUILTIN_MULTIBYTECODEC:BOOL=ON -DBUILTIN_MD5:BOOL=ON -DBUILTIN_MULTIPROCESSING:BOOL=ON -DBUILTIN_NIS:BOOL=ON -DBUILTIN_NIT:BOOL=ON -DBUILTIN_OPERATOR:BOOL=ON -DBUILTIN_PARSER:BOOL=ON -DBUILTIN_POSIX:BOOL=ON -DBUILTIN_PWD:BOOL=ON -DBUILTIN_PYEXPAT:BOOL=ON -DBUILTIN_READLINE:BOOL=ON -DBUILTIN_RESOURCE:BOOL=ON -DBULTIN_RANDOM:BOOL=ON -DBUILTIN_SCPROXY:BOOL=OFF -DBUILTIN_SELECT:BOOL=ON -DBUILTIN_SHA:BOOL=ON -DBUILTIN_SHA256:BOOL=ON -DBUILTIN_SHA512:BOOL=ON -DBUILTIN_SOCKET:BOOL=ON -DBUILTIN_SPWD:BOOL=ON -DBUILTIN_SQLITE3:BOOL=OFF -DBUILTIN_SSL:BOOL=ON -DBUILTIN_STROP:BOOL=ON -DBUILTIN_STRUCT:BOOL=ON -DBUILTIN_SUBPROCESS:BOOL=ON -DBUILTIN_SYSLOG:BOOL=ON -DBUILTIN_TERMIOS:BOOL=ON #-DBUILTIN_TESTCAPI:BOOL=OFF -DBUILTIN_TIME:BOOL=ON -DBUILTIN_TKINTER:BOOL=ON -DBUILTIN_UNICODEDATA:BOOL=ON -DBUILTIN_WINREG:BOOL=ON -DBUILTIN_ZLIB:BOOL=OFF -DUSE_SYSTEM_ZLIB:BOOL=ON ) # CMake build environment for python from: # https://github.com/davidsansome/python-cmake-buildsystem ExternalProject_Add(${proj} URL "https://dl.dropboxusercontent.com/u/8367205/ExternalProjects/python-cmake-buildsystem-47845c55.tar.gz" URL_MD5 "6e49d1ed93a5a0fff7621430c163d2d1" SOURCE_DIR ${CMAKE_BINARY_DIR}/${proj}-src PREFIX ${proj}-cmake BINARY_DIR ${proj}-build INSTALL_DIR ${proj}-install CMAKE_ARGS ${ep_common_args} -DCMAKE_INSTALL_PREFIX:PATH= CMAKE_CACHE_ARGS -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER} -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER} #-DBUILD_TESTING:BOOL=OFF -DBUILD_SHARED:BOOL=ON -DBUILD_STATIC:BOOL=OFF -DUSE_SYSTEM_LIBRARIES:BOOL=OFF ${additional_cmake_cache_args} -DZLIB_INCLUDE_DIR:PATH=${ZLIB_INCLUDE_DIR} -DZLIB_LIBRARY:FILEPATH=${ZLIB_LIBRARY} DEPENDS - Python-src ${${proj}_DEPENDENCIES} + ${Python_DEPENDENCIES} ) set(Python_DIR "${CMAKE_BINARY_DIR}/${proj}-install") if(UNIX) set(PYTHON_EXECUTABLE "${Python_DIR}/bin/python${CMAKE_EXECUTABLE_SUFFIX}") set(PYTHON_INCLUDE_DIR "${Python_DIR}/include/python${MITK_PYTHON_MAJOR_VERSION}.${MITK_PYTHON_MINOR_VERSION}") set(PYTHON_LIBRARY "${Python_DIR}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}python${MITK_PYTHON_MAJOR_VERSION}.${MITK_PYTHON_MINOR_VERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}") set(MITK_PYTHON_SITE_DIR "${Python_DIR}/lib/python${MITK_PYTHON_MAJOR_VERSION}.${MITK_PYTHON_MINOR_VERSION}/site-packages") else() set(PYTHON_EXECUTABLE "${Python_DIR}/bin/python${CMAKE_EXECUTABLE_SUFFIX}") set(PYTHON_INCLUDE_DIR "${Python_DIR}/include") set(PYTHON_LIBRARY "${Python_DIR}/libs/python${MITK_PYTHON_MAJOR_VERSION}${MITK_PYTHON_MINOR_VERSION}.lib") set(MITK_PYTHON_SITE_DIR "${Python_DIR}/Lib/site-packages") endif() # pre compile all *.py files in the runtime after install step ExternalProject_Add_Step(${proj} compile_step COMMAND ${PYTHON_EXECUTABLE} -m compileall DEPENDEES install ) # use the python executable in the build dir for unix systems. The stripped # ones will cause conflicts if system libraries are present during the build/configure process # of opencv, since they will try to lookup the sys path first if no lib is directly # linked with it s path into the executable if(UNIX) set(PYTHON_EXECUTABLE "${CMAKE_BINARY_DIR}/${proj}-build/bin/python${CMAKE_EXECUTABLE_SUFFIX}") endif() else() mitkMacroEmptyExternalProject(${proj} "${proj_DEPENDENCIES}") endif() endif() diff --git a/Modules/Python/Testing/files.cmake b/Modules/Python/Testing/files.cmake index 70ed5dac94..fb6dc38d7e 100644 --- a/Modules/Python/Testing/files.cmake +++ b/Modules/Python/Testing/files.cmake @@ -1,4 +1,13 @@ set(MODULE_TESTS mitkPythonTest.cpp - mitkPythonWrappingTest.cpp + mitkVtkPythonTest.cpp ) + +if(MITK_USE_OpenCV) + set(MODULE_TESTS ${MODULE_TESTS} mitkCvPythonTest.cpp) +endif() + +if(MITK_USE_SimpleITK) + set(MODULE_TESTS ${MODULE_TESTS} mitkSimpleItkPythonTest.cpp) +endif() + diff --git a/Modules/Python/Testing/mitkCommonPythonTest.h b/Modules/Python/Testing/mitkCommonPythonTest.h new file mode 100644 index 0000000000..420e97bcb7 --- /dev/null +++ b/Modules/Python/Testing/mitkCommonPythonTest.h @@ -0,0 +1,72 @@ + +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace mitk +{ + +class CommonPythonTestSuite : public mitk::TestFixture +{ + +protected: + mitk::PythonService* m_PythonService; + mitk::Image::Pointer m_Image; + mitk::Image::Pointer m_Image2D; + mitk::Surface::Pointer m_Surface; + QMap m_Snippets; + +public: + + void setUp() + { + //get the context of the python module + us::Module* module = us::ModuleRegistry::GetModule("MitkPython"); + us::ModuleContext* context = module->GetModuleContext(); + //get the service which is generated in the PythonModuleActivator + us::ServiceReference serviceRef = context->GetServiceReference(); + m_PythonService = dynamic_cast( context->GetService(serviceRef) ); + + m_Image = mitk::IOUtil::LoadImage(GetTestDataFilePath("Pic3D.nrrd")); + m_Image2D = mitk::IOUtil::LoadImage(GetTestDataFilePath("Png2D-bw.png")); + m_Surface = mitk::IOUtil::LoadSurface(GetTestDataFilePath("binary.stl")); + + QmitkPythonSnippets::LoadStringMap(QmitkPythonSnippets::DEFAULT_SNIPPET_FILE,m_Snippets); + } + + void tearDown() + { + m_Image = NULL; + m_Image2D = NULL; + m_Surface = NULL; + } +}; + +} diff --git a/Modules/Python/Testing/mitkCvPythonTest.cpp b/Modules/Python/Testing/mitkCvPythonTest.cpp new file mode 100644 index 0000000000..6f41448807 --- /dev/null +++ b/Modules/Python/Testing/mitkCvPythonTest.cpp @@ -0,0 +1,58 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include + +class mitkCvPythonTestSuite : public mitk::CommonPythonTestSuite +{ + CPPUNIT_TEST_SUITE(mitkCvPythonTestSuite); + MITK_TEST(testCVImageTransfer); + MITK_TEST(testOpenCVMedianFilter); + CPPUNIT_TEST_SUITE_END(); + +public: + + void testCVImageTransfer() + { + std::string varName("mitkImage"); + CPPUNIT_ASSERT_MESSAGE ( "Is OpenCV Python Wrapping available?", + m_PythonService->IsOpenCvPythonWrappingAvailable() == true ); + + CPPUNIT_ASSERT_MESSAGE( "Valid image copied to python import should return true.", + m_PythonService->CopyToPythonAsCvImage( m_Image2D, varName) == true ); + + mitk::Image::Pointer pythonImage = m_PythonService->CopyCvImageFromPython(varName); + + // todo pixeltypes do not match, cv is changing it + //CPPUNIT_ASSERT_MESSAGE( "Compare if images are equal after transfer.", + // mitk::Equal(pythonImage,m_Image2D) ); + } + + //TODO opencv median filter, add cpp test code + void testOpenCVMedianFilter() + { + // simple itk median filter in python + CPPUNIT_ASSERT_MESSAGE ( "Is OpenCV Python Wrapping available?", m_PythonService->IsOpenCvPythonWrappingAvailable() == true ); + + CPPUNIT_ASSERT_MESSAGE( "Valid image copied to python import should return true.", m_PythonService->CopyToPythonAsCvImage(m_Image2D, "mitkImage") == true ); + + m_PythonService->Execute( m_Snippets["opencv median filter"].toStdString(), mitk::IPythonService::MULTI_LINE_COMMAND ); + + CPPUNIT_ASSERT_MESSAGE( "Python execute error occured.", !m_PythonService->PythonErrorOccured()); + } +}; + +MITK_TEST_SUITE_REGISTRATION(mitkCvPython) diff --git a/Modules/Python/Testing/mitkPythonWrappingTest.cpp b/Modules/Python/Testing/mitkSimpleItkPythonTest.cpp similarity index 55% rename from Modules/Python/Testing/mitkPythonWrappingTest.cpp rename to Modules/Python/Testing/mitkSimpleItkPythonTest.cpp index 339df84530..e2d6cce2cb 100644 --- a/Modules/Python/Testing/mitkPythonWrappingTest.cpp +++ b/Modules/Python/Testing/mitkSimpleItkPythonTest.cpp @@ -1,342 +1,201 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include #include - -#include -// vtk cone sample snippet -#include -// vtk decimate pro snippet -#include +#include #include "SimpleITK.h" namespace sitk = itk::simple; namespace mitk { static mitk::Image::Pointer SimpleItkToMitkImage( sitk::Image& sitkImage ); static sitk::Image MitkToSimpleItkImage( mitk::Image* image ); } sitk::Image mitk::MitkToSimpleItkImage( mitk::Image* image ) { const mitk::Vector3D spacing = image->GetGeometry()->GetSpacing(); mitk::Point3D origin = image->GetGeometry()->GetOrigin(); mitk::PixelType pixelType = image->GetPixelType(); mitk::ImageReadAccessor ra(image); void* buffer = (void*) ra.GetData(); sitk::ImportImageFilter importer; std::vector sitkSpacing; sitkSpacing.push_back(spacing[0]); sitkSpacing.push_back(spacing[1]); sitkSpacing.push_back(spacing[2]); std::vector sitkOrigin; sitkOrigin.push_back(origin[0]); sitkOrigin.push_back(origin[1]); sitkOrigin.push_back(origin[2]); std::vector sitkSize; for ( unsigned int i = 0; i < image->GetDimension(); ++i ) sitkSize.push_back(image->GetDimensions()[i]); importer.SetSpacing(sitkSpacing); importer.SetSize(sitkSize); importer.SetOrigin(sitkOrigin); if( pixelType.GetComponentType() == itk::ImageIOBase::DOUBLE ) { importer.SetBufferAsDouble((double*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::FLOAT ) { importer.SetBufferAsFloat((float*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::SHORT) { importer.SetBufferAsInt16((int16_t*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::CHAR ) { importer.SetBufferAsInt8((int8_t*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::INT ) { importer.SetBufferAsInt32((int32_t*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::LONG ) { importer.SetBufferAsInt64((int64_t*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::UCHAR ) { importer.SetBufferAsUInt8((uint8_t*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::UINT ) { importer.SetBufferAsUInt32((uint32_t*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::ULONG ) { importer.SetBufferAsUInt64((uint64_t*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::USHORT ) { importer.SetBufferAsUInt16((uint16_t*) buffer); } return importer.Execute(); } mitk::Image::Pointer mitk::SimpleItkToMitkImage( sitk::Image& sitkImage ) { mitk::Image::Pointer image = mitk::Image::New(); void* buffer = NULL; mitk::PixelType pixelType = MakeScalarPixelType(); std::vector sitkSpacing = sitkImage.GetSpacing(); double spacing[3] = { sitkSpacing[0], sitkSpacing[1], sitkSpacing[2] }; std::vector sitkOrigin = sitkImage.GetOrigin(); double origin[3] = { sitkOrigin[0], sitkOrigin[1], sitkOrigin[2] }; std::vector sitkSize = sitkImage.GetSize(); unsigned int dimensions[4] = { 1,1,1,1}; for ( size_t i = 0; i < sitkSize.size(); ++i ) dimensions[i] = sitkSize[i]; size_t size = 0; if ( sitkImage.GetPixelIDValue() == sitk::sitkInt8 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsInt8(); size = sizeof(char); } else if( sitkImage.GetPixelIDValue() == sitk::sitkInt16 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsInt16(); size = sizeof(short); } else if( sitkImage.GetPixelIDValue() == sitk::sitkInt32 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsInt32(); size = sizeof(int); } else if( sitkImage.GetPixelIDValue() == sitk::sitkInt64 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsInt64(); size = sizeof(long); } else if( sitkImage.GetPixelIDValue() == sitk::sitkUInt8 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsUInt8(); size = sizeof(unsigned char); } else if( sitkImage.GetPixelIDValue() == sitk::sitkUInt16 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsUInt16(); size = sizeof(unsigned short); } else if( sitkImage.GetPixelIDValue() == sitk::sitkUInt32 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsUInt32(); size = sizeof(unsigned int); } else if( sitkImage.GetPixelIDValue() == sitk::sitkUInt64 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsUInt64(); size = sizeof(unsigned long); } else if( sitkImage.GetPixelIDValue() == sitk::sitkFloat32 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsFloat(); size = sizeof(float); } else if( sitkImage.GetPixelIDValue() == sitk::sitkFloat64 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsDouble(); size = sizeof(double); } image->Initialize(pixelType,sitkImage.GetDimension(),dimensions); image->SetSpacing(spacing); image->SetOrigin(origin); for(size_t i = 0; i < sitkSize.size(); ++i ) size *= sitkSize[i]; mitk::ImageWriteAccessor wa(image); memcpy(wa.GetData(),buffer, size); return image; } -class mitkPythonWrappingTestSuite : public mitk::TestFixture +class mitkSimpleItkPythonTestSuite : public mitk::CommonPythonTestSuite { - CPPUNIT_TEST_SUITE(mitkPythonWrappingTestSuite); + CPPUNIT_TEST_SUITE(mitkSimpleItkPythonTestSuite); MITK_TEST(testSimpleItkImageTransfer); - MITK_TEST(testCVImageTransfer); - MITK_TEST(testSurfaceTransfer); - MITK_TEST(testVtkCreateConePythonSnippet); - MITK_TEST(testVtkDecimateProPythonSnippet); MITK_TEST(testSimpleITKMedianFilterSnippet); - MITK_TEST(testOpenCVMedianFilter); CPPUNIT_TEST_SUITE_END(); -private: - mitk::PythonService* m_PythonService; - mitk::Image::Pointer m_Image; - mitk::Image::Pointer m_Image2D; - mitk::Surface::Pointer m_Surface; - QMap m_Snippets; public: - void setUp() - { - //get the context of the python module - us::Module* module = us::ModuleRegistry::GetModule("MitkPython"); - us::ModuleContext* context = module->GetModuleContext(); - //get the service which is generated in the PythonModuleActivator - us::ServiceReference serviceRef = context->GetServiceReference(); - m_PythonService = dynamic_cast( context->GetService(serviceRef) ); - - m_Image = mitk::IOUtil::LoadImage(GetTestDataFilePath("Pic3D.nrrd")); - m_Image2D = mitk::IOUtil::LoadImage(GetTestDataFilePath("Png2D-bw.png")); - m_Surface = mitk::IOUtil::LoadSurface(GetTestDataFilePath("binary.stl")); - - QmitkPythonSnippets::LoadStringMap(QmitkPythonSnippets::DEFAULT_SNIPPET_FILE,m_Snippets); - } - - void tearDown() - { - m_Image = NULL; - m_Image2D = NULL; - m_Surface = NULL; - } - void testSimpleItkImageTransfer() { std::string varName("mitkImage"); CPPUNIT_ASSERT_MESSAGE ( "Is SimpleITK Python Wrapping available?", m_PythonService->IsSimpleItkPythonWrappingAvailable() == true ); CPPUNIT_ASSERT_MESSAGE( "Valid image copied to python import should return true.", m_PythonService->CopyToPythonAsSimpleItkImage( m_Image, varName) == true ); mitk::Image::Pointer pythonImage = m_PythonService->CopySimpleItkImageFromPython(varName); CPPUNIT_ASSERT_MESSAGE( "Compare if images are equal after transfer.", mitk::Equal(*pythonImage.GetPointer(),*m_Image.GetPointer(), mitk::eps,true) ); } - void testCVImageTransfer() - { - std::string varName("mitkImage"); - CPPUNIT_ASSERT_MESSAGE ( "Is OpenCV Python Wrapping available?", - m_PythonService->IsOpenCvPythonWrappingAvailable() == true ); - - CPPUNIT_ASSERT_MESSAGE( "Valid image copied to python import should return true.", - m_PythonService->CopyToPythonAsCvImage( m_Image2D, varName) == true ); - - mitk::Image::Pointer pythonImage = m_PythonService->CopyCvImageFromPython(varName); - - // todo pixeltypes do not match, cv is changing it - //CPPUNIT_ASSERT_MESSAGE( "Compare if images are equal after transfer.", - // mitk::Equal(pythonImage,m_Image2D) ); - } - - void testSurfaceTransfer() - { - std::string varName("mitkSurface"); - CPPUNIT_ASSERT_MESSAGE ( "Is VTK Python Wrapping available?", m_PythonService->IsVtkPythonWrappingAvailable() == true ); - - CPPUNIT_ASSERT_MESSAGE( "Valid surface copied to python import should return true.", - m_PythonService->CopyToPythonAsVtkPolyData( m_Surface, varName) == true ); - - mitk::Surface::Pointer pythonSurface = m_PythonService->CopyVtkPolyDataFromPython(varName); - - CPPUNIT_ASSERT_MESSAGE( "Compare if surfaces are equal after transfer.", mitk::Equal(*pythonSurface.GetPointer(),*m_Surface.GetPointer(),mitk::eps,true) ); - } - - void testVtkCreateConePythonSnippet() - { - // cone in cpp - mitk::Surface::Pointer mitkSurface = mitk::Surface::New(); - vtkSmartPointer coneSrc = vtkSmartPointer::New(); - coneSrc->SetResolution(60); - coneSrc->SetCenter(-2,0,0); - coneSrc->Update(); - mitkSurface->SetVtkPolyData(coneSrc->GetOutput()); - - // run python code - CPPUNIT_ASSERT_MESSAGE ( "Is VTK Python Wrapping available?", m_PythonService->IsVtkPythonWrappingAvailable() == true ); - - m_PythonService->Execute( m_Snippets["vtk: create cone"].toStdString(), mitk::IPythonService::MULTI_LINE_COMMAND ); - CPPUNIT_ASSERT_MESSAGE( "Python execute error occured.", !m_PythonService->PythonErrorOccured()); - - mitk::Surface::Pointer pythonSurface = m_PythonService->CopyVtkPolyDataFromPython("cone"); - - CPPUNIT_ASSERT_MESSAGE( "Compare if cones are equal.", mitk::Equal(*pythonSurface.GetPointer(), *mitkSurface.GetPointer(), mitk::eps,true) ); - } - - void testVtkDecimateProPythonSnippet() - { - // decimate pro in cpp - mitk::Surface::Pointer mitkSurface = mitk::Surface::New(); - vtkSmartPointer deci = vtkSmartPointer::New(); - deci->SetInputData(m_Surface->GetVtkPolyData()); - deci->SetTargetReduction(0.9); - deci->PreserveTopologyOn(); - deci->Update(); - mitkSurface->SetVtkPolyData(deci->GetOutput()); - - // decimate pro in python - CPPUNIT_ASSERT_MESSAGE ( "Is VTK Python Wrapping available?", m_PythonService->IsVtkPythonWrappingAvailable() == true ); - - CPPUNIT_ASSERT_MESSAGE( "Valid surface copied to python import should return true.", m_PythonService->CopyToPythonAsVtkPolyData( m_Surface, "mitkSurface") == true ); - - m_PythonService->Execute( m_Snippets["vtk.vtkDecimatePro"].toStdString(), mitk::IPythonService::MULTI_LINE_COMMAND ); - CPPUNIT_ASSERT_MESSAGE( "Python execute error occured.", !m_PythonService->PythonErrorOccured()); - - mitk::Surface::Pointer pythonSurface = m_PythonService->CopyVtkPolyDataFromPython("mitkSurface_new"); - - CPPUNIT_ASSERT_MESSAGE( "Compare if surfaces are equal.", mitk::Equal(*pythonSurface.GetPointer(), *mitkSurface.GetPointer(), mitk::eps,true) ); - } void testSimpleITKMedianFilterSnippet() { // simple itk median filter in cpp sitk::MedianImageFilter medianFilter; medianFilter.SetRadius(1); sitk::Image sitkImage = medianFilter.Execute(mitk::MitkToSimpleItkImage(m_Image)); mitk::Image::Pointer mitkImage = mitk::SimpleItkToMitkImage(sitkImage); // simple itk median filter in python CPPUNIT_ASSERT_MESSAGE ( "Is SimpleItk Python Wrapping available?", m_PythonService->IsSimpleItkPythonWrappingAvailable() == true ); CPPUNIT_ASSERT_MESSAGE( "Valid image copied to python import should return true.", m_PythonService->CopyToPythonAsSimpleItkImage(m_Image, "mitkImage") == true ); m_PythonService->Execute( m_Snippets["medianfilter"].toStdString(), mitk::IPythonService::MULTI_LINE_COMMAND ); CPPUNIT_ASSERT_MESSAGE( "Python execute error occured.", !m_PythonService->PythonErrorOccured()); mitk::Image::Pointer pythonImage = m_PythonService->CopySimpleItkImageFromPython("mitkImage_new"); CPPUNIT_ASSERT_MESSAGE( "Compare if images are equal.", mitk::Equal(*pythonImage.GetPointer(), *mitkImage.GetPointer(),mitk::eps,true) ); } - - //TODO opencv median filter, add cpp test code - void testOpenCVMedianFilter() - { - // simple itk median filter in python - CPPUNIT_ASSERT_MESSAGE ( "Is OpenCV Python Wrapping available?", m_PythonService->IsOpenCvPythonWrappingAvailable() == true ); - - CPPUNIT_ASSERT_MESSAGE( "Valid image copied to python import should return true.", m_PythonService->CopyToPythonAsCvImage(m_Image2D, "mitkImage") == true ); - - m_PythonService->Execute( m_Snippets["opencv median filter"].toStdString(), mitk::IPythonService::MULTI_LINE_COMMAND ); - - CPPUNIT_ASSERT_MESSAGE( "Python execute error occured.", !m_PythonService->PythonErrorOccured()); - } }; -MITK_TEST_SUITE_REGISTRATION(mitkPythonWrapping) +MITK_TEST_SUITE_REGISTRATION(mitkSimpleItkPython) diff --git a/Modules/Python/Testing/mitkVtkPythonTest.cpp b/Modules/Python/Testing/mitkVtkPythonTest.cpp new file mode 100644 index 0000000000..3fb2662006 --- /dev/null +++ b/Modules/Python/Testing/mitkVtkPythonTest.cpp @@ -0,0 +1,95 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include +#include +// vtk cone sample snippet +#include +// vtk decimate pro snippet +#include + +#include + +class mitkVtkPythonTestSuite : public mitk::CommonPythonTestSuite +{ + CPPUNIT_TEST_SUITE(mitkVtkPythonTestSuite); + MITK_TEST(testSurfaceTransfer); + MITK_TEST(testVtkCreateConePythonSnippet); + MITK_TEST(testVtkDecimateProPythonSnippet); + CPPUNIT_TEST_SUITE_END(); + +public: + + void testSurfaceTransfer() + { + std::string varName("mitkSurface"); + CPPUNIT_ASSERT_MESSAGE ( "Is VTK Python Wrapping available?", m_PythonService->IsVtkPythonWrappingAvailable() == true ); + + CPPUNIT_ASSERT_MESSAGE( "Valid surface copied to python import should return true.", + m_PythonService->CopyToPythonAsVtkPolyData( m_Surface, varName) == true ); + + mitk::Surface::Pointer pythonSurface = m_PythonService->CopyVtkPolyDataFromPython(varName); + + CPPUNIT_ASSERT_MESSAGE( "Compare if surfaces are equal after transfer.", mitk::Equal(*pythonSurface.GetPointer(),*m_Surface.GetPointer(),mitk::eps,true) ); + } + + void testVtkCreateConePythonSnippet() + { + // cone in cpp + mitk::Surface::Pointer mitkSurface = mitk::Surface::New(); + vtkSmartPointer coneSrc = vtkSmartPointer::New(); + coneSrc->SetResolution(60); + coneSrc->SetCenter(-2,0,0); + coneSrc->Update(); + mitkSurface->SetVtkPolyData(coneSrc->GetOutput()); + + // run python code + CPPUNIT_ASSERT_MESSAGE ( "Is VTK Python Wrapping available?", m_PythonService->IsVtkPythonWrappingAvailable() == true ); + + m_PythonService->Execute( m_Snippets["vtk: create cone"].toStdString(), mitk::IPythonService::MULTI_LINE_COMMAND ); + CPPUNIT_ASSERT_MESSAGE( "Python execute error occured.", !m_PythonService->PythonErrorOccured()); + + mitk::Surface::Pointer pythonSurface = m_PythonService->CopyVtkPolyDataFromPython("cone"); + + CPPUNIT_ASSERT_MESSAGE( "Compare if cones are equal.", mitk::Equal(*pythonSurface.GetPointer(), *mitkSurface.GetPointer(), mitk::eps,true) ); + } + + void testVtkDecimateProPythonSnippet() + { + // decimate pro in cpp + mitk::Surface::Pointer mitkSurface = mitk::Surface::New(); + vtkSmartPointer deci = vtkSmartPointer::New(); + deci->SetInputData(m_Surface->GetVtkPolyData()); + deci->SetTargetReduction(0.9); + deci->PreserveTopologyOn(); + deci->Update(); + mitkSurface->SetVtkPolyData(deci->GetOutput()); + + // decimate pro in python + CPPUNIT_ASSERT_MESSAGE ( "Is VTK Python Wrapping available?", m_PythonService->IsVtkPythonWrappingAvailable() == true ); + + CPPUNIT_ASSERT_MESSAGE( "Valid surface copied to python import should return true.", m_PythonService->CopyToPythonAsVtkPolyData( m_Surface, "mitkSurface") == true ); + + m_PythonService->Execute( m_Snippets["vtk.vtkDecimatePro"].toStdString(), mitk::IPythonService::MULTI_LINE_COMMAND ); + CPPUNIT_ASSERT_MESSAGE( "Python execute error occured.", !m_PythonService->PythonErrorOccured()); + + mitk::Surface::Pointer pythonSurface = m_PythonService->CopyVtkPolyDataFromPython("mitkSurface_new"); + + CPPUNIT_ASSERT_MESSAGE( "Compare if surfaces are equal.", mitk::Equal(*pythonSurface.GetPointer(), *mitkSurface.GetPointer(), mitk::eps,true) ); + } +}; + +MITK_TEST_SUITE_REGISTRATION(mitkVtkPython) diff --git a/Modules/Python/files.cmake b/Modules/Python/files.cmake index 66fb3a3ce7..c4fe74b329 100644 --- a/Modules/Python/files.cmake +++ b/Modules/Python/files.cmake @@ -1,28 +1,34 @@ SET(CPP_FILES mitkIPythonService.cpp mitkPythonActivator.cpp mitkPythonService.cpp QmitkCtkPythonShell.cpp QmitkPythonVariableStackTableModel.cpp QmitkPythonVariableStackTableView.cpp QmitkPythonScriptEditorHighlighter.cpp QmitkPythonTextEditor.cpp QmitkPythonSnippets.cpp ) +if(BUILD_TESTING) + SET(H_FILES + Testing/mitkCommonPythonTest.h + ) +endif() + #SET(UI_FILES # QmitkPythonSnippets.ui #) SET(MOC_H_FILES QmitkCtkPythonShell.h QmitkPythonVariableStackTableModel.h QmitkPythonVariableStackTableView.h QmitkPythonScriptEditorHighlighter.h QmitkPythonTextEditor.h QmitkPythonSnippets.h ) set(QRC_FILES resources/mitkPython.qrc )