[Insight-users] Wrapping R with SimpleITK

Brandon Whitcher bwhitcher at gmail.com
Tue Jan 24 09:48:53 EST 2012


Wes and Bradley,

I know we've gone silent on the R <-> ITK front, I'm still trying to find
someone who will do the heavy lifting.

Just to let you (and the other ITK list users) that the annual R users
meeting will be at Vanderbilt University, Nashville, from 12-15 June.

     http://biostat.mc.vanderbilt.edu/wiki/Main/UseR-2012

Abstracts are being accepted until 12 March.  It would be a great way of
promoting ITK to the R community!

What are your thoughts?

cheers...

Brandon

On 4 January 2012 17:42, Wes Turner <wes.turner at kitware.com> wrote:

> I agree with Bradley that it should be part of the SimpleITK Package.
>  IMHO, it is too integral to the full functioning of the ITK to R interface
> to keep separate.
>
> Here are the pointers to the VTK versions of the routines.  We will need
> to change it to use ITK arrays instead, but that should not be too
> difficult.  I am not sure how optimized these are, but once we get the
> functionality encapsulated and into SimpleITK, we can work on optimization.
>
> Basic data conversion:
>
> http://vtk.org/gitweb?p=VTK.git;a=blob;f=Graphics/vtkRAdapter.cxx;h=d6049a5eb748f878df56d40b4c82aeec7fc51471;hb=HEAD
>
> http://vtk.org/gitweb?p=VTK.git;a=blob;f=Graphics/vtkRAdapter.h;h=83a2a833c18d8d8ce36d1f4e77e5cf12b5d2c217;hb=HEAD
>
> Interface to the R Interpreter (probably not needed)
>
> http://vtk.org/gitweb?p=VTK.git;a=blob;f=Graphics/vtkRAdapter.h;h=83a2a833c18d8d8ce36d1f4e77e5cf12b5d2c217;hb=HEAD
>
> http://vtk.org/gitweb?p=VTK.git;a=blob;f=Graphics/vtkRInterface.h;h=e98ce921b5a16c5299fdb8cccbb29793fcd8989b;hb=HEAD
>
> Pipeline encapsulation (probably not needed)
>
> http://vtk.org/gitweb?p=VTK.git;a=blob;f=Graphics/vtkRCalculatorFilter.cxx;h=bdc99183fb2a6b949228fcadbed63e4a9f0e9d9a;hb=HEAD
>
> http://vtk.org/gitweb?p=VTK.git;a=blob;f=Graphics/vtkRCalculatorFilter.h;h=08f43d42b724a608cc37fdba1268875dab7ceba8;hb=HEAD
>
> - Wes
>
>
> On Wed, Jan 4, 2012 at 10:41 AM, Bradley Lowekamp <blowekamp at mail.nih.gov>wrote:
>
>>
>> On Jan 4, 2012, at 10:07 AM, Brandon Whitcher wrote:
>>
>> Wes, thanks for your input here.  That would be great.  Do your methods
>> depend upon any ITK/SimpleITK routines?  I'm wondering if it would be
>> better to keep SimpleITK (the R wrapper version) as a separate R package
>> and then develop additional packages that add features, or just put
>> everything in one package (hmm).
>>
>>
>> For python we have added interface methods to numpy as part of the
>> SimpleITK package. I would think that converting sitk::Images to R arrays
>> would be a very common usage, so it sounds like it should be fairly tightly
>> coupled with the SimpleITK package. Is the multi-dimensional array part of
>> the standard R interface or a separate package.
>>
>> The swig documentation for R is fairly poor:
>> http://www.swig.org/Doc2.0/SWIGDocumentation.html#R
>>
>> Swig has to support more standard features then are listed there.
>>
>> If you are wondering how we did the python numpy interface, you can begin
>> by looking at the Wrapping/Python.i file:
>>
>>
>> http://itk.org/gitweb?p=SimpleITK.git;a=blob;f=Wrapping/Python.i;h=ca732fc3e219e3ec6f76974adc9e015282b52562;hb=HEAD
>>
>> I that with this information and the way VTK interfaces with R can help
>> get you started.
>>
>> Brad
>>
>>
>> For me, I've developed (with others) a lot of mathematical modelling and
>> statistical inference methods in R that operate on multidimensional arrays
>> (usually coming from one of the medical imaging data formats); e.g., for
>> functional MRI, DTI, HARDI or DCE-MRI.  What we lack is the so-called
>> pre-processing techniques needed in medical image analysis: segmentation,
>> filtering, registration, etc.  Hence, SimpleITK (the R wrapper version)
>> would be a great way to access the techniques listed previously on data
>> that have been read it via R, and stored as multidimensional arrays, then
>> take the output back into R for further analysis and visualization.
>>
>> I'm looking for the best way to incorporate the impressive variety of ITK
>> routines into my R-based data analysis pipeline.
>>
>> cheers...
>>
>> Brandon
>> On 3 January 2012 18:28, Wes Turner <wes.turner at kitware.com> wrote:
>>
>>>
>>>
>>> On Tue, Jan 3, 2012 at 10:37 AM, Bradley Lowekamp <
>>> blowekamp at mail.nih.gov> wrote:
>>>
>>>> Hello,
>>>>
>>>> It is great to hear that you are approaching ITK for the first time
>>>> with SimpleITK. Your feed back is very much appreciated so that we can
>>>> improve the documentations and the compilation procedures for a easier
>>>> experience.
>>>>
>>>>
>>>> I have added the source of your Data problem to the FAQ here:
>>>>
>>>>
>>>> http://www.itk.org/Wiki/ITK/Release_4/SimpleITK/FAQ#Where_is_the_Input_Data.3F_or_Why_is_my_Testing.2FData_directory_empty.3F
>>>>
>>>> The output you have attached looks great! I hope you have had success
>>>> trying additional filters by now.
>>>>
>>>> You said that you want to read Nifti and ANALYZE data with R. ITK  and
>>>> SimpleITK also have good support for these image formats, so you may be
>>>> able to read directly with SimpleITK's ReadImage method or the
>>>> ImageFileReader object.
>>>>
>>>> There is currently no optimized method for exporting a SimpleITK image
>>>> into an R array. So you are likely must use the slow SetPixel and GetPixel
>>>> methods.
>>>>
>>>
>>>  There are methods currently in VTK for conversion between VTK and R
>>> structures for some subset of formats.  I would be willing to provide some
>>> help and guidance.  It is only two classes and should be a short putt.
>>>
>>> - Wes
>>>
>>>
>>>> For visualization in SimpleITK we have a primitive sitk.Show method.
>>>> This method currently just writes the image out to disk and then loads is
>>>> up in ImageJ. To use the, you need a recent version of ImageJ. We hope to
>>>> make this methods more configurable in the future, and take advantage built
>>>> in language specific visualization tools. However, with SimpleITK
>>>> supporting 2D, 3D image, along with all the vector pixel types, and complex
>>>> pixel types etc... many existing tools don't have the needs support for all
>>>> these possibilities.
>>>>
>>>> Enjoy,
>>>>
>>>> Brad
>>>>
>>>>
>>>> ========================================================
>>>> Bradley Lowekamp
>>>> Medical Science and Computing for
>>>> Office of High Performance Computing and Communications
>>>> National Library of Medicine
>>>> blowekamp at mail.nih.gov
>>>>
>>>> On Jan 2, 2012, at 3:46 AM, Brandon Whitcher wrote:
>>>>
>>>> Following up on my previous message I found a copy of cthead1.png at
>>>>
>>>>
>>>> http://itk.org/gitweb?p=SimpleITKData.git;a=commitdiff;h=f7d86582b91a35e5c8d30f1b58320bc47cc658fa
>>>>
>>>> ...and downloaded it.  I have attached it and the "out.png" file from
>>>> your example code.
>>>>
>>>> > source("load.R")
>>>>
>>>> Creating a generic function for ‘print’ from package ‘base’ in the
>>>> global environment
>>>> > img <- ReadImage( "Input/cthead1.png" )
>>>> > (pid <- Image_GetPixelIDValue( img ))
>>>> [1] 1
>>>>
>>>> > simg <- SmoothingRecursiveGaussian( img, 4 )
>>>> > cimg <- Cast( simg, pid )
>>>> > WriteImage( cimg, "out.png" )
>>>>
>>>> Success!
>>>>
>>>> Now I need to read up on ITK functions and how to take an array in R
>>>> (containing ANALYZE or NIfTI data) and pass it into ITK, process it and
>>>> bring it back in order to visualize/manipulate in R.  I have a package that
>>>> performs basic I/O for ANALYZE and NIfTI using S4 objects in R (called
>>>> oro.nifti) and it would be great to pass these arrays (along with the
>>>> metadata) back-and-forth with SimpleITK.
>>>>
>>>> cheers...
>>>>
>>>> Brandon
>>>>
>>>>
>>>>
>>>> On 2 January 2012 08:37, Brandon Whitcher <bwhitcher at gmail.com> wrote:
>>>>
>>>>> Brad, as you can tell I'm completely new to ITK... thank-you for your
>>>>> patience.  My SimpleITK (source) Testing/Data directory is empty from my
>>>>> "clone-ing" via Git (I am also a Git novice, sorry).  Looking at the
>>>>> version control tree at
>>>>>
>>>>>
>>>>> http://itk.org/gitweb?p=SimpleITK.git;a=tree;f=Testing;h=eb50d756edee129fc26beb6d4c00f288ea49309a;hb=HEAD
>>>>>
>>>>> ...I get...
>>>>>
>>>>> m--------- - Data history
>>>>>
>>>>> ...which doesn't look too promising from my knowledge of file
>>>>> permissions.  How can I overcome this?  May I ask for "cthead1.png" via
>>>>> email to try this example ASAP?
>>>>>
>>>>> cheers...
>>>>>
>>>>> Brandon
>>>>>
>>>>>
>>>>> On 2 January 2012 04:21, Bradley Lowekamp <blowekamp at mail.nih.gov>wrote:
>>>>>
>>>>>> Hello Brandon,
>>>>>>
>>>>>> As is frequently the case the first exception is the cause of the
>>>>>> remainder. To understand the first error message some explanation of the
>>>>>> how SimpleITK manages image types is needed.
>>>>>>
>>>>>> There may be some relevant information in the SimpleITK presentation
>>>>>> from MICCAI 2011:
>>>>>>
>>>>>> https://github.com/SimpleITK/SimpleITK-MICCAI-2011-Tutorial/blob/master/Presentation/SimpleITK-MICCAI-2011.pdf?raw=true
>>>>>>
>>>>>>
>>>>>> The sitk::Image class is a typeless image class which wraps numerous
>>>>>> ITK image types. Externally the sitk::Image class presents the same
>>>>>> interface for all images, wether it is a scalar itk::Image, a
>>>>>> multicomponent itk::VectorImage, or even a run-line compressed
>>>>>> itk::LabelMap. And then each of these ITK image types are templated over
>>>>>> the dimension and the pixel type. So SimpleITK presents dozens of itk
>>>>>> images types through a single sitk::Image object.
>>>>>>
>>>>>> So SimpelITK is not strongly typed on these individual image types,
>>>>>> but it is type aware.
>>>>>>
>>>>>> ITK  filters are also templated on the input, and output image types.
>>>>>> But SimpleITK again type not expose the details of all the templated types,
>>>>>> and instead just exposes a generic interface to the image filter, and
>>>>>> internally is instantiates the image filter with all the plausible input
>>>>>> image types, and the sensible corresponding output image types.
>>>>>>
>>>>>> There are a few methods which can be used to interrogate the image
>>>>>> after it's loaded:
>>>>>>
>>>>>> First there is the ToString member function which just dumps out
>>>>>>  bunch of information about the internal ITK object:
>>>>>>
>>>>>> >cat( Image_ToString( myimage ) )
>>>>>>
>>>>>> And more specific methods:
>>>>>>
>>>>>> > Image_GetDimension( myimage )
>>>>>> > Image_GetPixelIDTypeAsString( myimage )
>>>>>> > Image_GetPixelIDValue( myimage )
>>>>>>
>>>>>> So lets look at what happens with the image you have chosen to run
>>>>>> this script with
>>>>>>
>>>>>> On Jan 1, 2012, at 9:03 AM, Brandon Whitcher wrote:
>>>>>>
>>>>>> Brad,
>>>>>>
>>>>>> I have attempted to run SimpleGaussian.R in an interactive R session
>>>>>> by executing
>>>>>>
>>>>>>
>>>>>> using the "spine1.png" image (attached, along with load.R).
>>>>>>
>>>>>>
>>>>>> The data used for the tests can be found in the
>>>>>> {SIMPLEITK_SOURCE}/Testing/Data.
>>>>>>
>>>>>>
>>>>>> The code fails to produce a smoothed image, here is the output
>>>>>>
>>>>>> > source("load.R") # library(SimpleITK)
>>>>>> >
>>>>>> > args <- c("spine1.png", 5, "spine1_smooth.png") # commandArgs(TRUE)
>>>>>> >
>>>>>> > myreader <- ImageFileReader()
>>>>>> > myreader <- ImageFileReader_SetFileName(myreader, args[[1]])
>>>>>> > myimage <- ImageFileReader_Execute(myreader)
>>>>>> >
>>>>>>
>>>>>>
>>>>>> if you run:
>>>>>> >cat( Image_ToString( myimage ) )
>>>>>>
>>>>>> You should see the following line:
>>>>>>
>>>>>> RTTI typeinfo:   itk::VectorImage< unsigned char, 2u>
>>>>>>
>>>>>> Or if you run:
>>>>>>
>>>>>> > Image_GetPixelIDTypeAsString( myimage )
>>>>>> [1] "vector of 8-bit unsigned integer"
>>>>>>
>>>>>> So with either of these methods you see that this file is not a black
>>>>>> and white image that it first appears, but actually a RGB image loaded into
>>>>>> an Itk::VectorImage
>>>>>>
>>>>>> > pixeltype <- Image_GetPixelIDValue(myimage)
>>>>>> >
>>>>>> > myfilter <- SmoothingRecursiveGaussianImageFilter()
>>>>>> > myfilter <-
>>>>>> SmoothingRecursiveGaussianImageFilter_SetSigma(myfilter, as.double(args[2]))
>>>>>> > smoothedimage <-
>>>>>> SmoothingRecursiveGaussianImageFilter_Execute(myfilter, myimage)
>>>>>> Warning message:
>>>>>> In f(...) :
>>>>>>   Exception thrown in SimpleITK
>>>>>> SmoothingRecursiveGaussianImageFilter_Execute:
>>>>>> /srv/SimpleITK/Code/Common/include/sitkMemberFunctionFactory.txx:173:
>>>>>> sitk::ERROR: Pixel type: vector of 8-bit unsigned integer is not
>>>>>> supported in 2D byN3itk6simple37SmoothingRecursiveGaussianImageFilterE
>>>>>>
>>>>>>
>>>>>> While this error message could use improvement  ( and will be
>>>>>> improved ). It basically says that this filter doesn't work with this
>>>>>> particular image type  ( it should also list the types of images it does
>>>>>> work with ). This particular filter currently only works with scalar image
>>>>>> type. So this image with sitkVectorUInt8 pixels is not going to work.
>>>>>>
>>>>>>
>>>>>>
>>>>>> >
>>>>>> > mycaster <- CastImageFilter()
>>>>>> > mycaster <- CastImageFilter_SetOutputPixelType(mycaster, pixeltype)
>>>>>> > castedimage <- CastImageFilter_Execute(mycaster, soothedimage)
>>>>>> Error in CastImageFilter_Execute(mycaster, soothedimage) :
>>>>>>   object 'soothedimage' not found
>>>>>>
>>>>>>
>>>>>> There appears to be a typeo "soothedimage" should be "smoothedimage".
>>>>>> However, I think the same error message should be generated because the
>>>>>> smoothedimage was not assigned due to the previous exception.
>>>>>>
>>>>>> >
>>>>>> > mywriter <- ImageFileWriter()
>>>>>> > mywriter <- ImageFileWriter_SetFileName(mywriter, args[[3]])
>>>>>> > mywriter <- ImageFileWriter_Execute(mywriter, castedimage)
>>>>>> Error in ImageFileWriter_Execute(mywriter, castedimage) :
>>>>>>   object 'castedimage' not found
>>>>>>
>>>>>> I am not sure what to make of the warnings or errors (sorry).  Any
>>>>>> advice or modifications would be helpful.  I am using spine1.png since I
>>>>>> cannot locate STAPLE1.png in either the ITK or SimpleITK directories.
>>>>>>
>>>>>>
>>>>>> Using the functional interface the following may be simpler:
>>>>>>
>>>>>> img <- ReadImage( "/src/SimpleITK/Testing/Data/Input/cthead1.png" )
>>>>>> pid <- Image_GetPixelIDValue( img )
>>>>>> simg <- SmoothingRecursiveGaussian( img, 4 )
>>>>>> cimg <- Cast( simg, pid )
>>>>>> WriteImage( cimg, "out.png" )
>>>>>>
>>>>>> Good luck,
>>>>>> Brad
>>>>>>
>>>>>>  thanks,
>>>>>>
>>>>>> Brandon
>>>>>>
>>>>>>
>>>>>> On 31 December 2011 18:34, Bradley Lowekamp <blowekamp at mail.nih.gov>wrote:
>>>>>>
>>>>>>> Hello again,
>>>>>>>
>>>>>>> I was not able to get the R packaging to work as described in the
>>>>>>> link provided. I did find a manual way to load the SimpleITK R library.
>>>>>>>
>>>>>>> Copy the SimpleITK.R file and the libSimpleITK.so ( renamed to
>>>>>>> SimpleITK.so ) into your current path, such as with the following commands:
>>>>>>>
>>>>>>> cp SimpleITK/SimpleITK-build/Wrapping/SimpleITK.R
>>>>>>> cp SimpleITK/SimpleITK-build/Wrapping/SimpleITK.R ./
>>>>>>>
>>>>>>>
>>>>>>> The fire up R and type:
>>>>>>>
>>>>>>> $ R
>>>>>>>
>>>>>>> R version 2.14.0 (2011-10-31)
>>>>>>> ...
>>>>>>>
>>>>>>> > dyn.load( paste("SimpleITK", .Platform$dynlib.ext, sep="" ))
>>>>>>> > source ( "SimpleITK.R" )
>>>>>>> Creating a generic function for ‘print’ from package ‘base’ in the
>>>>>>> global environment
>>>>>>> > cacheMetaData( 1 )
>>>>>>>
>>>>>>> Here is an example that was working at one point:
>>>>>>>
>>>>>>>
>>>>>>> http://www.itk.org/SimpleITKDoxygen/html/SimpleGaussian_8R-example.html
>>>>>>>
>>>>>>> However the "library( SimpleITK )" call is used for when SimpleITK
>>>>>>> is installed as a package not the adhoc approach used above.
>>>>>>>
>>>>>>> Good Luck,
>>>>>>> Brad
>>>>>>>
>>>>>>> On Dec 31, 2011, at 1:11 PM, Lowekamp, Bradley (NIH/NLM/LHC) [C]
>>>>>>> wrote:
>>>>>>>
>>>>>>> Hello Brandon,
>>>>>>>
>>>>>>> The particular files you looking for are in the SimpleITK source
>>>>>>> directory not the build. You will need both the DESCRIPTION and the zzz.R
>>>>>>> files from the {SIMPLEITK_SOURCE}/Wrapping/R_Package directory.
>>>>>>>
>>>>>>> We have been calling R a Tier 2 or Tier 3 language in SimpleITK
>>>>>>> because is not as well supported as the other wrapped languages such as
>>>>>>> Python, Java, CSharp or even Ruby, or TCL. The primary reason for this is
>>>>>>> that we have not gotten the automatic testing working for R, so that it
>>>>>>> will show up on the nightly dashboard. Additionally, we also are hoping for
>>>>>>> contribution from the community to get the R language specific features
>>>>>>> polished and up to the expected R standard conventions.
>>>>>>>
>>>>>>> I hope this helps you with getting SimpleITK working R,
>>>>>>>
>>>>>>> Brad
>>>>>>>
>>>>>>> ========================================================
>>>>>>> Bradley Lowekamp
>>>>>>> Medical Science and Computing for
>>>>>>> Office of High Performance Computing and Communications
>>>>>>> National Library of Medicine
>>>>>>> blowekamp at mail.nih.gov
>>>>>>>
>>>>>>> On Dec 31, 2011, at 9:28 AM, Brandon Whitcher wrote:
>>>>>>>
>>>>>>> Dear ITK/SimpleITK experts,
>>>>>>>
>>>>>>> I have installed SimpleITK by turning off all wrapping facilities
>>>>>>> except WRAP_R.  I am now following the instructions in
>>>>>>>
>>>>>>> http://www.itk.org/SimpleITKDoxygen/html/Wrapping.html
>>>>>>>
>>>>>>> Specifically,
>>>>>>>
>>>>>>>
>>>>>>>    - 6) Copy the file
>>>>>>>
>>>>>>>        SimpleITK/Wrapping/R_Package/DESCRIPTION
>>>>>>>
>>>>>>> into the current
>>>>>>>
>>>>>>>        SimpleITK/DESCRIPTION
>>>>>>>
>>>>>>> ...but I cannot find the file Wrapping/R_package/DESCRIPTION in my
>>>>>>> SimpleITK-binary installation.  Where is this file?
>>>>>>>
>>>>>>> thank-you,
>>>>>>>
>>>>>>> Brandon
>>>>>>>
>>>>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> _____________________________________
>>>> Powered by www.kitware.com
>>>>
>>>> Visit other Kitware open-source projects at
>>>> http://www.kitware.com/opensource/opensource.html
>>>>
>>>> Kitware offers ITK Training Courses, for more information visit:
>>>> http://www.kitware.com/products/protraining.html
>>>>
>>>> Please keep messages on-topic and check the ITK FAQ at:
>>>> http://www.itk.org/Wiki/ITK_FAQ
>>>>
>>>> Follow this link to subscribe/unsubscribe:
>>>> http://www.itk.org/mailman/listinfo/insight-users
>>>>
>>>>
>>>
>>>
>>> --
>>> Wesley D. Turner, Ph.D.
>>> Kitware, Inc.
>>> Technical Leader
>>> 28 Corporate Drive
>>> Clifton Park, NY 12065-8662
>>> Phone: 518-881-4920
>>>
>>
>>
>>  ========================================================
>>
>> Bradley Lowekamp
>>
>> Medical Science and Computing for
>>
>> Office of High Performance Computing and Communications
>>
>> National Library of Medicine
>>
>> blowekamp at mail.nih.gov
>>
>>
>>
>>
>
>
> --
> Wesley D. Turner, Ph.D.
> Kitware, Inc.
> Technical Leader
> 28 Corporate Drive
> Clifton Park, NY 12065-8662
> Phone: 518-881-4920
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20120124/1ca1a87f/attachment.htm>


More information about the Insight-users mailing list