~ubuntu-branches/debian/sid/simpleitk/sid

« back to all changes in this revision

Viewing changes to Wrapping/R/Packaging/SimpleITK/vignettes/SimpleITK_tutorial.Rnw

  • Committer: Package Import Robot
  • Author(s): Ghislain Antony Vaillant
  • Date: 2017-11-02 08:49:18 UTC
  • Revision ID: package-import@ubuntu.com-20171102084918-7hs09ih668xq87ej
Tags: upstream-1.0.1
ImportĀ upstreamĀ versionĀ 1.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%\documentclass{article}
 
2
\documentclass{InsightArticle}
 
3
 
 
4
% to be able to use options in graphics
 
5
\ifx\pdfoutput\undefined
 
6
\usepackage[dvips,
 
7
bookmarks,
 
8
bookmarksopen,
 
9
backref,
 
10
colorlinks,linkcolor={blue},citecolor={blue},urlcolor={blue},
 
11
]{hyperref}
 
12
\usepackage[dvips]{graphicx}
 
13
\else
 
14
\usepackage[bookmarks,
 
15
bookmarksopen,
 
16
backref,
 
17
colorlinks,linkcolor={blue},citecolor={blue},urlcolor={blue},
 
18
]{hyperref}
 
19
\usepackage[pdftex]{graphicx}
 
20
\fi
 
21
\SweaveOpts{keep.source=TRUE}
 
22
% for pseudo code
 
23
\usepackage{listings}
 
24
% subfigures
 
25
\usepackage{subfigure}
 
26
 
 
27
\title{SimpleITK in R}
 
28
%\release{0.00}
 
29
 
 
30
% At minimum, give your name and an email address.  You can include a
 
31
% snail-mail address if you like.
 
32
\author{Richard Beare}
 
33
%\authoraddress{Richard.Beare@monash.edu\\Department of Medicine\\Monash University\\and\\Developmental Imaging\\Murdoch Childrens Research Institute\\Melbourne\\Australia}
 
34
\begin{document}
 
35
\maketitle
 
36
 
 
37
%\ifhtml
 
38
%\chapter*{Front Matter\label{front}}
 
39
%\fi
 
40
\begin{abstract}
 
41
\noindent
 
42
{\em R}, also known as ``Gnu S'' is a widely used, open source,
 
43
language based environment for statistics and computational
 
44
modelling. It will be reasonably familiar to users of other
 
45
interactive, interpreted environments, like Matlab or python. This
 
46
article provides an introduction to the SimpleITK package that has
 
47
been built using the Swig generated wrapping of the SimpleITK
 
48
library. Note that some of the text is written for readers unfamiliar
 
49
with {\em R} and can be skipped by experienced users.
 
50
\end{abstract}
 
51
 
 
52
\tableofcontents
 
53
 
 
54
\section{Introduction}
 
55
{\em R} is an advanced language environment that supports extension
 
56
via an advanced package mechanism and object-oriented and generic
 
57
programming mechanisms. The traditional application domain of {\em R}
 
58
is in interactive statistical analysis, but the language is general
 
59
purpose and facilities are available to support many forms of
 
60
computational work. There are already a number of packages for medical
 
61
imaging and general purpose imaging, but none with the extent of low
 
62
level operators provided by SimpleITK. {\em R} has quite nice features
 
63
that makes interfacing to objects like images quite convenient. This
 
64
package makes extensive use of {\em external references} and language
 
65
operator overloading facilities.
 
66
 
 
67
\section{Very basic {\em R} tutorial}
 
68
{\em R} has extensive online documentation - see the Documentation
 
69
links on the r-project pages. Here are some basic concepts to start
 
70
the project. Skip to the next section if you are already familiar with {\em R}.
 
71
 
 
72
\begin{itemize}
 
73
\item Assignment - traditionally the assignment operator is {\tt <-}, but {\tt =} can be used in most places now:
 
74
<<>>=
 
75
a <- 1  # assign a variable
 
76
b = a
 
77
@@
 
78
  \item Creating vectors - everything in {\em R} is at least a vector, and vectors can contain numbers or strings:
 
79
<<>>=
 
80
a <- c(1,2,34, 20, 10)
 
81
a
 
82
b = c('a', 'k', 'hello')
 
83
b
 
84
d <- 1:10
 
85
d
 
86
@@
 
87
    {\em c} is the concatenate operator and can be used with vectors and lists.
 
88
  \item Displaying objects - as seen above, typing a variable name invokes the generic {\em show} method, which typically provides an informative display of an obje. We'll see how this comes in handy later with images.
 
89
  \item Creating arrays
 
90
<<>>=
 
91
b<-array(1:20, dim=c(5,4))
 
92
b
 
93
@@
 
94
  \item Vector and array subsetting - there are a rich set of these operations with capabilities similar to Matlab. Indexing starts from 1.
 
95
<<>>=
 
96
a[1:2]
 
97
a[3:1]
 
98
a[-1]  # delete first element
 
99
b[1,]  # first row of b
 
100
b[1,c(1,4,2)]
 
101
@@
 
102
    \item Lists - can contain different object types
 
103
<<>>=
 
104
L1 = list('a', 1, 'hello')
 
105
L1
 
106
is.list(L1)
 
107
L1[[2]]
 
108
@@
 
109
Notice that we are using the double bracket operator to access list elements.
 
110
 
 
111
\item Naming components - so far we have been illustrating standard, index-based, access. It is possible to name array, vector and list components which provides options for clear accessing.
 
112
<<>>=
 
113
L1 <- list(first=1, second='hello', third=b)
 
114
L1$second
 
115
L1[["first"]]
 
116
 
 
117
colnames(b) <- c("first", "second", "third", "last")
 
118
b[,"last"]
 
119
@@
 
120
These options provide useful ways of keeping consistency in complex analyses with evolving data structures.
 
121
\item Other data structures. The main structure not discussed here is a special list, called a data frame, that is widely used by the statistical model-fitting procedures. Classes, methods and other language facilities are also available, but used mainly by package developers.
 
122
\end{itemize}
 
123
 
 
124
\section{Getting started with SimpleITK}
 
125
Building and installation instructions are later. Lets jump straight into some examples. In order to display images you need to install ImageJ with the nifti plugin, and be in your path. The results in this docment are displayed slightly differently, using internal {\em R} plotting routines, for compatability with the Sweave document processing.
 
126
 
 
127
\subsection{Image anatomy and access methods}
 
128
\begin{itemize}
 
129
\item Load the SimpleITK library. This may require that the {\tt R\_LIBS} environment variable is set.
 
130
<<>>=
 
131
library(SimpleITK)
 
132
@@
 
133
<<echo=FALSE>>=
 
134
# override show function
 
135
setMethod('show', '_p_itk__simple__Image',
 
136
          function(object)
 
137
          {
 
138
            require(grid)
 
139
            a <- t(as.array(object))
 
140
            rg <- range(a)
 
141
            A <- (a-rg[1])/(rg[2]-rg[1])
 
142
            dd <- dim(a)
 
143
            sp <- object$GetSpacing()
 
144
 
 
145
            grid.raster(A)
 
146
          }
 
147
 
 
148
          )
 
149
 
 
150
@@
 
151
\item Load an image
 
152
<<>>=
 
153
im <- ReadImage(system.file("data/cthead1.png", package="SimpleITK"))
 
154
@@
 
155
\item Display
 
156
<<fig=TRUE>>=
 
157
im
 
158
@@
 
159
\item Get some information about the image
 
160
<<>>=
 
161
print(im)
 
162
im$GetSpacing()
 
163
im$GetSize()
 
164
@@
 
165
These vector quantities are translated directly to R vectors. The same applies to filters, as we'll see later.
 
166
\item Get one pixel value
 
167
<<>>=
 
168
im[100, 120]
 
169
@@
 
170
\item Extract the first 100 columns
 
171
<<fig=TRUE>>=
 
172
im[1:100,]
 
173
@@
 
174
\item First 100 columns, then same data flipped
 
175
<<fig=TRUE>>=
 
176
im[c(1:100,100:1),]
 
177
@@
 
178
\item Remove the first 100 rows
 
179
<<fig=TRUE>>=
 
180
im[,-(1:100)]
 
181
@@
 
182
\item Subsample by 2
 
183
<<fig=TRUE>>=
 
184
im[seq(1,256, by=2), seq(1,256, by=2)]
 
185
@@
 
186
 
 
187
As you can see, we can use array acess techniques to images. The results of each of these operations is an image, not an array. Evidently, we can do some pretty crazy things using this notation, which means that it is very difficult to decide what to do with image metadata, such as spacing and origin. Currently nothing clever is being done with either - spacing is left as per the input, which could easily be wrong. Origin is left as default image constructor.
 
188
\item Lets explore the image class in a little more detail to find out what access methods are available:
 
189
<<>>=
 
190
getMethod('$', class(im))
 
191
@@
 
192
This provides a list of accessor functions that can be used via the \$ notation illustrated above. Most classes create by the swig processing work this way.
 
193
\item Finally, lets allocate an image
 
194
<<>>=
 
195
im2 <- Image(10,10, 20, 'sitkUInt16')
 
196
print(im2)
 
197
@@
 
198
 
 
199
The important points to note here is that the enumerated type describing the pixel type is represented as a string.
 
200
\item Translating images to {\em R} arrays:
 
201
<<>>=
 
202
arr <- as.array(im)
 
203
class(im)
 
204
class(arr)
 
205
# now we can do something crazy
 
206
s <- svd(arr)
 
207
@@
 
208
\item And back again
 
209
<<fig=TRUE>>=
 
210
nim <- as.image(s$u)
 
211
nim
 
212
@@
 
213
 
 
214
Points to note here - {\em R} only supports integer and double types (logical types are also integers). Therefore conversion of any image will end up as an array of one of these types. Similarly, conversion of arrays to images also ends up as one of these types. By default, any {\em R} matrix will be double precision, but can be coerced to integer using the {\em as.integer} or {\em storage.mode} functions. Image pixel types can be converted using the {\em Cast} filters.
 
215
 
 
216
\end{itemize}
 
217
 
 
218
\subsection{Image operations with Simple ITK classes}
 
219
Finally, onto the crux of the matter. Let's look at doing some image filtering. There are two basic approaches with SimpleITK - the procedural and the filter approach
 
220
\begin{itemize}
 
221
\item Gaussian blurring:
 
222
<<fig=TRUE>>=
 
223
res <- SmoothingRecursiveGaussian(im, 3)
 
224
res
 
225
@@
 
226
\item or
 
227
<<fig=TRUE>>=
 
228
filt <- SmoothingRecursiveGaussianImageFilter()
 
229
# check the accessors
 
230
getMethod('$', class(filt))
 
231
filt$SetSigma(5)
 
232
filt$NormalizeAcrossScaleOn()
 
233
res2 <- filt$Execute(im)
 
234
res2
 
235
@@
 
236
 
 
237
Notice that we can explore the accessor functions in the same way as images. Also note that calling the accessor functions without assigning the result to a variable causes the {\em show} method to display a representation of the object.
 
238
\item Cryptic error messages - unfortunately it isn't easy to figure out what arguments are expected by the procedural interface. For example, if we assumed that the sigma parameter was a vector, we'd get the following unhelpful response:
 
239
<<>>=
 
240
try(res3 <- SmoothingRecursiveGaussian(im, c(3, 3)))
 
241
geterrmessage()
 
242
@@
 
243
Note that the {\em try} and {\em geterrmessage} commands are to allow Sweave to complete. They aren't needed in interactive sessions.
 
244
\end{itemize}
 
245
 
 
246
 
 
247
 
 
248
\subsection{Still to come}
 
249
Image arithmetic.
 
250
 
 
251
Testing.
 
252
 
 
253
\subsection{Caveats}
 
254
Beware of images from saved workspaces. External references, which is how images are represented, are not preserved when objects are saved to disk. Thus, attempting to use images from a saved workspace will result in ungraceful crashes.
 
255
 
 
256
\section{Building and Installing}
 
257
Fetch SimpleITK from the git repository. Visit \url{https://www.itk.org/SimpleITKDoxygen/html/Wrapping.html} for the latest instructions on building and installing.
 
258
\section{Development}
 
259
 
 
260
\end{document}