-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestsK.R
320 lines (305 loc) · 11.3 KB
/
testsK.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#'
#' Header for all (concatenated) test files
#'
#' Require spatstat.explore
#' Obtain environment variable controlling tests.
#'
#' $Revision: 1.5 $ $Date: 2020/04/30 05:31:37 $
require(spatstat.explore)
FULLTEST <- (nchar(Sys.getenv("SPATSTAT_TEST", unset="")) > 0)
ALWAYS <- TRUE
cat(paste("--------- Executing",
if(FULLTEST) "** ALL **" else "**RESTRICTED** subset of",
"test code -----------\n"))
#'
#' tests/kernelstuff.R
#'
#' $Revision: 1.2 $ $Date: 2023/11/05 01:49:45 $
local({
if(FULLTEST) {
#' test all cases in kernels.R
kernames <- c("gaussian", "rectangular", "triangular",
"epanechnikov", "biweight", "cosine", "optcosine")
X <- rnorm(20)
U <- runif(20)
for(ker in kernames) {
dX <- dkernel(X, ker)
fX <- pkernel(X, ker)
qU <- qkernel(U, ker)
m0 <- kernel.moment(0, 0, ker)
m1 <- kernel.moment(1, 0, ker)
m2 <- kernel.moment(2, 0, ker)
m3 <- kernel.moment(3, 0, ker)
}
}
})
#'
#' tests/Kfuns.R
#'
#' Various K and L functions and pcf
#'
#' $Revision: 1.43 $ $Date: 2022/06/17 01:47:08 $
#'
#' Assumes 'EveryStart.R' was run
if(FULLTEST) {
Cells <- cells
Amacrine <- amacrine
Redwood <- redwood
} else {
## reduce numbers of data + dummy points
spatstat.options(npixel=32, ndummy.min=16)
Cells <- cells[c(FALSE, TRUE)]
Amacrine <- amacrine[c(FALSE, TRUE)]
Redwood <- redwood[c(FALSE, TRUE)]
}
myfun <- function(x,y){(x+1) * y } # must be outside
local({
if(FULLTEST) {
#' supporting code
rmax.rule("Kscaled", owin(), 42)
implemented.for.K(c("border", "bord.modif", "translate", "good", "best"),
"polygonal", TRUE)
implemented.for.K(c("border", "bord.modif", "translate", "good", "best"),
"mask", TRUE)
implemented.for.K(c("border", "isotropic"), "mask", TRUE)
implemented.for.K(c("border", "isotropic"), "mask", FALSE)
#' shortcuts
D <- density(Cells)
K <- Kborder.engine(Cells, rmax=0.4, weights=D, ratio=TRUE)
K <- Knone.engine(Cells, rmax=0.4, weights=D, ratio=TRUE)
allcor <- c("none", "border", "bord.modif","isotropic", "translate")
K <- Krect.engine(Cells, rmax=0.4, ratio=TRUE, correction=allcor)
K <- Krect.engine(Cells, rmax=0.4, ratio=TRUE, correction=allcor,
weights=D)
K <- Krect.engine(Cells, rmax=0.4, ratio=TRUE, correction=allcor,
use.integers=FALSE)
#' Kest special code blocks
K <- Kest(Cells, var.approx=TRUE, ratio=FALSE)
Z <- distmap(Cells) + 1
Kb <- Kest(Cells, correction=c("border","bord.modif"),
weights=Z, ratio=TRUE)
Kn <- Kest(Cells, correction="none",
weights=Z, ratio=TRUE)
Knb <- Kest(Cells, correction=c("border","bord.modif","none"),
weights=Z, ratio=TRUE)
}
if(ALWAYS) {
bigint <- 50000 # This is only "big" on a 32-bit system where
# sqrt(.Machine$integer.max) = 46340.9
X <- runifpoint(bigint)
Z <- as.im(1/bigint, owin())
Kb <- Kest(X, correction=c("border","bord.modif"),
rmax=0.02, weights=Z, ratio=TRUE)
}
if(FULLTEST) {
Kn <- Kest(X, correction="none",
rmax=0.02, weights=Z, ratio=TRUE)
Knb <- Kest(X, correction=c("border","bord.modif","none"),
rmax=0.02, weights=Z, ratio=TRUE)
#' pcf.ppp special code blocks
pr <- pcf(Cells, ratio=TRUE, var.approx=TRUE)
pc <- pcf(Cells, domain=square(0.5))
pcr <- pcf(Cells, domain=square(0.5), ratio=TRUE)
pw <- pcf(Redwood, correction="none")
pwr <- pcf(Redwood, correction="none", ratio=TRUE)
pv <- pcf(Redwood, kernel="rectangular")
p1 <- pcf(Redwood[1])
#' pcf.fv
K <- Kest(Redwood)
g <- pcf(K, method="a")
g <- pcf(K, method="c")
g <- pcf(K, method="d")
#' Kinhom code blocks
X <- rpoispp(function(x,y) { 100 * x }, 100, square(1))
lambda <- 100 * X$x
Kin <- Kinhom(X, lambda, correction=c("none", "border"))
lambda2 <- outer(lambda, lambda, "*")
Ki2 <- Kinhom(X, lambda2=lambda2, diagonal=FALSE,
correction=c("translate", "isotropic"))
}
if(ALWAYS) {
#' edge corrections
rr <- rep(0.1, npoints(Cells))
eC <- edge.Ripley(Cells, rr)
eI <- edge.Ripley(Cells, rr, method="interpreted")
if(max(abs(eC-eI)) > 0.1)
stop("Ripley edge correction results do not match")
}
if(FULLTEST) {
a <- rmax.Ripley(square(1))
a <- rmax.Rigid(square(1))
a <- rmax.Ripley(as.polygonal(square(1)))
a <- rmax.Rigid(as.polygonal(square(1)))
a <- rmax.Ripley(letterR)
a <- rmax.Rigid(letterR)
}
if(ALWAYS) {
#' run slow code for edge correction and compare results
op <- spatstat.options(npixel=128)
X <- Redwood[c(TRUE, FALSE, FALSE, FALSE)]
Window(X) <- as.polygonal(Window(X))
Eapprox <- edge.Trans(X)
Eexact <- edge.Trans(X, exact=TRUE)
maxrelerr <- max(abs(1 - range(Eapprox/Eexact)))
if(maxrelerr > 0.1)
stop(paste("Exact and approximate algorithms for edge.Trans disagree by",
paste0(round(100*maxrelerr), "%")),
call.=FALSE)
spatstat.options(op)
}
})
local({
if(FULLTEST) {
#' ---- multitype ------
K <- Kcross(Amacrine, correction=c("none", "bord.modif"))
K <- Kcross(Amacrine, correction=c("none", "bord", "bord.modif"),
ratio=TRUE)
#' inhomogeneous multitype
K2 <- Kcross.inhom(Amacrine, lambdaX=densityfun(Amacrine))
K3 <- Kcross.inhom(Amacrine, lambdaX=density(Amacrine, at="points"))
K5 <- Kcross.inhom(Amacrine, correction="bord.modif")
#' markconnect, markcorr
M <- markconnect(Amacrine, "on", "off", normalise=TRUE)
M <- markcorr(longleaf, normalise=TRUE,
correction=c("isotropic", "translate", "border", "none"))
M <- markcorr(longleaf, normalise=TRUE, fargs=list())
#' Kmark (=markcorrint)
X <- runifpoint(100) %mark% runif(100)
km <- Kmark(X, f=atan2)
km <- Kmark(X, f1=sin)
km <- Kmark(X, f="myfun")
aa <- Kmark(X, normalise=FALSE, returnL=FALSE)
aa <- Kmark(X, normalise=FALSE, returnL=TRUE)
aa <- Kmark(X, normalise=TRUE, returnL=FALSE)
aa <- Kmark(X, normalise=TRUE, returnL=TRUE)
}
})
local({
if(FULLTEST) {
#' various modified K functions
#'
#' directional K functions
#'
a <- Ksector(swedishpines,
-pi/2, pi/2, units="radians",
correction=c("none", "border", "bord.modif",
"Ripley", "translate"),
ratio=TRUE)
plot(a)
#'
#' local K functions
#'
Z <- as.im(intensity(swedishpines), W=Window(swedishpines))
ZX <- Z[swedishpines]
a <- localLinhom(swedishpines, lambda=Z)
a <- localLinhom(swedishpines, lambda=ZX)
a <- localLinhom(swedishpines, lambda=Z, correction="none")
a <- localLinhom(swedishpines, lambda=Z, correction="translate")
a <- localLcross(Amacrine)
a <- localLcross(Amacrine, from="off", to="off")
a <- localKdot(Amacrine)
a <- localLdot(Amacrine)
a <- localKcross.inhom(Amacrine)
a <- localLcross.inhom(Amacrine)
Zed <- solapply(intensity(amacrine), as.im, W=Window(amacrine))
Lum <- evaluateCovariateAtPoints(Zed, Amacrine)
moff <- (marks(Amacrine) == "off")
a <- localLcross.inhom(Amacrine, from="off", to="on", lambdaX=Zed)
a <- localLcross.inhom(Amacrine, from="off", to="on", lambdaX=Lum)
a <- localLcross.inhom(Amacrine, from="off", to="on",
lambdaFrom=Lum[moff], lambdaTo=Lum[!moff])
a <- localLcross.inhom(Amacrine, from="off", to="on", lambdaX=Zed,
correction="none")
a <- localLcross.inhom(Amacrine, from="off", to="on", lambdaX=Zed,
correction="translate")
#'
#' cases of resolve.lambdacross
#'
h <- resolve.lambdacross(Amacrine, moff, !moff)
h <- resolve.lambdacross(Amacrine, moff, !moff, lambdaX=Zed)
h <- resolve.lambdacross(Amacrine, moff, !moff, lambdaX=Lum)
h <- resolve.lambdacross(Amacrine, moff, !moff,
lambdaI=Zed[["off"]], lambdaJ=Zed[["on"]])
h <- resolve.lambdacross(Amacrine, moff, !moff,
lambdaI=Lum[moff], lambdaJ=Lum[!moff])
d <- densityfun(unmark(Amacrine), sigma=0.1)
dm <- lapply(split(Amacrine), densityfun, sigma=0.1)
h <- resolve.lambdacross(Amacrine, moff, !moff, lambdaX=d)
h <- resolve.lambdacross(Amacrine, moff, !moff,
lambdaI=dm[["off"]], lambdaJ=dm[["on"]])
h <- resolve.lambdacross(Amacrine, moff, !moff,
lambdaX=function(x,y,m){ d(x,y) })
#'
#' multitype inhomogeneous pcf
#'
g <- pcfcross.inhom(Amacrine,
lambdaI=dm[["off"]], lambdaJ=dm[["on"]])
#'
#' lohboot code blocks
#'
Ared <- lohboot(Redwood, fun="Kest", block=TRUE,
Vcorrection=TRUE, global=FALSE, correction="none")
Bred <- lohboot(Redwood, block=TRUE, basicboot=TRUE, global=FALSE)
Cred <- lohboot(Redwood, fun=Kest, block=TRUE, global=TRUE,
correction="translate")
Dred <- lohboot(Redwood, Lest)
Kred <- lohboot(Redwood, Kinhom)
Lred <- lohboot(Redwood, Linhom)
gred <- lohboot(Redwood, pcfinhom, sigma=0.1)
#'
X <- runifpoint(100, letterR)
AX <- lohboot(X, block=TRUE, nx=7, ny=10)
#' multitype
b <- lohboot(Amacrine, Kcross)
b <- lohboot(Amacrine, Lcross)
b <- lohboot(Amacrine, Kdot)
b <- lohboot(Amacrine, Ldot)
b <- lohboot(Amacrine, Kcross.inhom)
b <- lohboot(Amacrine, Lcross.inhom)
## Kscaled
A <- Lscaled(japanesepines, renormalise=TRUE, correction="all")
}
})
local({
if(ALWAYS) {
#' From Ege, in response to a stackoverflow question.
#' The following example has two points separated by r = 1 with 1/4 of the
#' circumference outside the 10x10 window (i.e. area 100).
#' Thus the value of K^(r) should jump from 0 to
#' 100/(2\cdot 1)\cdot ((3/4)^{-1} + (3/4)^{-1}) = 100 \cdot 4/3 = 133.333.
x <- c(4.5,5.5)
y <- c(10,10)-sqrt(2)/2
W <- square(10)
X <- ppp(x, y, W)
compere <- function(a, b, where, tol=1e-6) {
descrip <- paste("discrepancy in isotropic edge correction", where)
err <- as.numeric(a) - as.numeric(b)
maxerr <- max(abs(err))
blurb <- paste(descrip, "is", paste0(signif(maxerr, 4), ","),
if(maxerr > tol) "exceeding" else "within",
"tolerance of", tol)
message(blurb)
if(maxerr > tol) {
message(paste("Discrepancies:", paste(err, collapse=", ")))
stop(paste("excessive", descrip), call.=FALSE)
}
invisible(TRUE)
}
## Testing:
eX <- edge.Ripley(X, c(1,1))
compere(eX, c(4/3,4/3), "at interior point of rectangle")
## Corner case:
Y <- X
Y$x <- X$x-4.5+sqrt(2)/2
eY <- edge.Ripley(Y, c(1,1))
compere(eY, c(2,4/3), "near corner of rectangle")
## Invoke polygonal code
Z <- rotate(Y, pi/4)
eZdebug <- edge.Ripley(Z, c(1,1), internal=list(debug=TRUE))
compere(eZdebug, c(2,4/3), "at interior point of polygon (debug on)")
## test validity without debugger,in case of quirks of compiler optimisation
eZ <- edge.Ripley(Z, c(1,1))
compere(eZ, c(2,4/3), "at interior point of polygon (debug off)")
}
})
reset.spatstat.options()