1
## Test image slicing operators. They need to conform to rules that are slightly
2
## different to those for R matrices.
4
## These rules are: spacing needs to be uniform per dimension
5
## i.e. im[c(1,2,11),] is not accepted
6
## This means that creating reflected images isn't possible
7
## with slicing - use slicing + Tile.
8
## Dropping dimensions is the default, but SimpleITK does not
9
## support 1D images. The slicing operator will issue a warning if
12
## The bracket operator used for slicing now constructs calls to Slice and Extract,
13
## rather than using special C routines. This means that the operation is multithreaded
14
## and preserves spacing, origin and direction.
16
slicingOperatorUnitSpacingTest<- function()
18
## Cropping test - unit spacing
19
im <- Image(15,15,"sitkFloat64")
20
im$SetSpacing(c(0.3,0.6))
24
im$SetDirection(c(ct,-st,st,ct))
28
ef <- ExtractImageFilter()
33
## meta-data for slicing operator should match the metadata for the extract
34
## filter which we know to be correct.
35
if(!all(e1$GetOrigin() == e2$GetOrigin()))
37
cat("slicing operator failed, origins don't match")
38
quit(save="no", status=1)
40
if(!all(e1$GetSpacing() == e2$GetSpacing()))
42
cat("slicing operator failed, spacings don't match")
43
quit(save="no", status=1)
45
if(!all(e1$GetDirection() == e2$GetDirection()))
47
cat("slicing operator failed, directions don't match")
48
quit(save="no", status=1)
53
dirToMat <- function(im)
55
## produce an R formatted direction matrix
56
d1 <- im$GetDirection()
61
slicingOperatorNegativeSpacingTest<- function()
63
## Cropping test - unit spacing
64
im <- Image(15,15,"sitkFloat64")
65
im$SetSpacing(c(0.3,0.6))
69
im$SetDirection(c(ct,-st,st,ct))
74
## meta-data for slicing operator should match the metadata for the extract
75
## filter which we know to be correct.
76
if(!all(e1$GetOrigin() == im$TransformIndexToPhysicalPoint(c(1,4))))
78
cat("slicing operator failed, origins don't match")
79
quit(save="no", status=1)
83
## reversed the second dimension, so d2[,2] == -d1[,2]
84
## negate the second column and compare to original
88
cat("slicing operator failed, directions incorrect")
89
quit(save="no", status=1)
93
slicingPositionChecks <- function()
95
x<-rep(1:10, rep(12, 10))
100
row3 <- as.array(xim[,3,drop=FALSE])
101
if (!(all(row3 == 3))) {
102
cat("Incorrect pixel values - should be all 3\n")
103
quit(save="no", status=1)
107
b <- as.array(xim[5:9, 6:9])
109
cat("Failed block extract - voxels should have value 21\n")
110
quit(save="no", status=1)
113
slicingOperatorWarningsErrors <- function()
115
im <- Image(15,15,"sitkFloat64")
117
## this should issue a warning
119
try(msg <- tools::assertWarning(im[1, 2:3]), silent=TRUE)
120
if (length(msg) == 0) {
121
cat("Slicing operator should have issued a warning, but did not.\n")
122
quit(save="no", status=1)
124
## Turning drop off - no warning
126
try(msg <- tools::assertCondition(im[1, 2:3, ,drop=FALSE]), silent=TRUE)
127
if (length(msg) != 0) {
128
cat("Slicing operator issued a warning/error, but should not.\n")
129
quit(save="no", status=1)
132
## Irregular spacing - should issue an error
134
try(msg <- tools::assertError(im[c(1,2, 4), 2:3]), silent=TRUE)
135
if (length(msg) == 0) {
136
cat("Slicing operator should have issued an error, but did not.\n")
137
quit(save="no", status=1)
141
slicingSyntaxTrials <- function()
143
im2 <- Image(15,15,"sitkFloat64")
144
im3 <- Image(15,15, 15, "sitkFloat64")
146
## slicing requires all dimensions to be included - these should produce an
149
try(msg <- tools::assertError(im3[1:2, 2:3]), silent=TRUE)
150
if (length(msg) == 0) {
151
cat("Slicing operator should have issued an error, but did not.\n")
152
quit(save="no", status=1)
156
try(msg <- tools::assertError(im3[1, 2:3,drop=TRUE]), silent=TRUE)
157
if (length(msg) == 0) {
158
cat("Slicing operator should have issued an error, but did not.\n")
159
quit(save="no", status=1)
163
try(msg <- tools::assertCondition(im3[1:2, 2:3,]), silent=TRUE)
164
if (length(msg) != 0) {
165
cat("Slicing issued an error/warning, but should not.\n")
166
quit(save="no", status=1)
169
try(msg <- tools::assertCondition(im3[1:2, 2:3,,drop=FALSE]), silent=TRUE)
170
if (length(msg) != 0) {
171
cat("Slicing issued an error/warning, but should not.\n")
172
quit(save="no", status=1)
176
try(msg <- tools::assertCondition(im3[1:2, ,2:3,]), silent=TRUE)
177
if (length(msg) != 0) {
178
cat("Slicing issued an error/warning, but should not.\n")
179
quit(save="no", status=1)
183
j<-im3[,,3,drop=FALSE]
184
if (any(j$GetSize() != c(15, 15, 1))) {
185
cat("Image slicing error - incorrect dimensions returned\n")
186
cat("Expected [15, 15, 1] - got [", j$GetSize(), "]\n")
187
quit(save="no", status=1)
189
j<-im3[,,3,drop=TRUE]
190
if (any(j$GetSize() != c(15, 15))) {
191
cat("Image slicing error - incorrect dimensions returned\n")
192
cat("Expected [15, 15] - got [", j$GetSize(), "]\n")
193
quit(save="no", status=1)
195
## logical indexing, with variables
196
xidx <- (1:im3$GetWidth()) > 5
198
try(msg <- tools::assertCondition(im3[xidx, ,2:3]), silent=TRUE)
199
if (length(msg) != 0) {
200
cat("Slicing using logical issued an error/warning, but should not.\n")
201
quit(save="no", status=1)
205
slicingZeroIndexTrials <- function()
207
## various forms of indexes are illegal because we can't support
208
## an "empty" image in the way R supports an "empty" array
209
im3 <- Image(15,15, 15, "sitkFloat64")
211
## error caused by calling with c()
213
try(msg <- tools::assertError(im3[c(), 2:3,]), silent=TRUE)
214
if (length(msg) == 0) {
215
cat("Slicing operator should have issued an error, but did not.\n")
216
quit(save="no", status=1)
218
## check the 3rd dimension, which is handled differently to the first 2
220
try(msg <- tools::assertError(im3[2:3, 2:3, c()]), silent=TRUE)
221
if (length(msg) == 0) {
222
cat("Slicing operator should have issued an error, but did not.\n")
223
quit(save="no", status=1)
227
try(msg <- tools::assertError(im3[2:3, 0, 2:3]), silent=TRUE)
228
if (length(msg) == 0) {
229
cat("Slicing operator should have issued an error, but did not.\n")
230
quit(save="no", status=1)
233
try(msg <- tools::assertError(im3[2:3, 2:3, 0]), silent=TRUE)
234
if (length(msg) == 0) {
235
cat("Slicing operator should have issued an error, but did not.\n")
236
quit(save="no", status=1)
241
slicingOperatorUnitSpacingTest()
242
slicingOperatorNegativeSpacingTest()
243
slicingPositionChecks()
244
slicingSyntaxTrials()
245
slicingZeroIndexTrials()