-
Notifications
You must be signed in to change notification settings - Fork 44
/
solist.R
194 lines (170 loc) · 4.91 KB
/
solist.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
##
## solist.R
##
## Methods for class `solist' (spatial object list)
##
## and related classes 'anylist', 'ppplist', 'imlist'
##
## plot.solist is defined in plot.solist.R
##
## $Revision: 1.13 $ $Date: 2016/06/27 06:59:28 $
anylist <- function(...) {
x <- list(...)
class(x) <- c("anylist", "listof", class(x))
return(x)
}
print.anylist <- function (x, ...) {
ll <- length(x)
if(ll == 0) {
splat("(Zero length list)")
return(invisible(NULL))
}
nn <- names(x)
if (length(nn) != ll)
nn <- paste("Component", seq.int(ll))
spaceok <- waxlyrical('space')
for (i in seq_len(ll)) {
splat(paste0(nn[i], ":"))
print(x[[i]], ...)
if(spaceok && i < ll) cat("\n")
}
return(invisible(NULL))
}
as.anylist <- function(x) {
if(inherits(x, "anylist")) return(x)
if(!is.list(x))
x <- list(x)
class(x) <- c("anylist", "listof", class(x))
return(x)
}
"[.anylist" <- function(x, i, ...) {
cl <- oldClass(x)
## invoke list method
y <- NextMethod("[")
if(length(y) == 0) return(list())
class(y) <- cl
return(y)
}
"[<-.anylist" <- function(x, i, value) {
as.anylist(NextMethod("[<-"))
}
summary.anylist <- function(object, ...) {
as.anylist(lapply(object, summary, ...))
}
pool.anylist <- function(x, ...) {
do.call(pool, append(x, list(...)))
}
## .................... solist .............................
is.sob <- local({
## test whether x is a spatial object suitable for solist
sobjectclasses <- c("ppp", "psp", "im", "owin",
"quad", "tess", "msr",
"quadratcount", "quadrattest",
"layered",
"funxy", "distfun", "nnfun",
"lpp", "linnet", "linfun",
"influence.ppm", "leverage.ppm")
# Note 'linim' inherits 'im'
# 'dfbetas.ppm' inherits 'msr'
is.sob <- function(x) { inherits(x, what=sobjectclasses) }
is.sob
})
solist <- function(..., check=TRUE, promote=TRUE, demote=FALSE) {
stuff <- list(...)
if((check || demote) && !all(sapply(stuff, is.sob))) {
if(demote)
return(as.anylist(stuff))
stop("Some arguments of solist() are not 2D spatial objects")
}
class(stuff) <- c("solist", "anylist", "listof", class(stuff))
if(promote) {
if(all(unlist(lapply(stuff, is.ppp)))) {
class(stuff) <- c("ppplist", class(stuff))
} else if(all(unlist(lapply(stuff, is.im)))) {
class(stuff) <- c("imlist", class(stuff))
}
}
return(stuff)
}
as.solist <- function(x, ...) {
if(inherits(x, "solist") && length(list(...)) == 0)
return(x)
if(!is.list(x))
x <- list(x)
return(do.call(solist, append(x, list(...))))
}
print.solist <- function (x, ...) {
what <- if(inherits(x, "ppplist")) "point patterns" else
if(inherits(x, "imlist")) "pixel images" else "spatial objects"
splat(paste("List of", what))
parbreak()
NextMethod("print")
}
"[.solist" <- function(x, i, ...) {
cl <- oldClass(x)
if(!missing(i) && is.owin(i)) {
## spatial subset
y <- lapply(unclass(x), "[", i=i)
} else {
## invoke list method
y <- NextMethod("[")
}
if(length(y) == 0) return(list())
class(y) <- cl
return(y)
}
"[<-.solist" <- function(x, i, value) {
## invoke list method
y <- NextMethod("[<-")
## check again
return(do.call(solist, y))
}
summary.solist <- function(object, ...) {
x <- lapply(object, summary, ...)
attr(x, "otype") <-
if(inherits(object, "ppplist")) "ppp" else
if(inherits(object, "imlist")) "im" else ""
class(x) <- c("summary.solist", "anylist")
x
}
print.summary.solist <- function(x, ...) {
what <- switch(attr(x, "otype"),
ppp="point patterns",
im="pixel images",
"spatial objects")
splat("Summary of", length(x), what)
parbreak()
NextMethod("print")
}
as.layered.solist <- function(X) {
layered(LayerList=X)
}
# --------------- counterparts of 'lapply' --------------------
anylapply <- function(X, FUN, ...) {
v <- lapply(X, FUN, ...)
return(as.anylist(v))
}
solapply <- function(X, FUN, ..., check=TRUE, promote=TRUE, demote=FALSE) {
v <- lapply(X, FUN, ...)
u <- as.solist(v, check=check, promote=promote, demote=demote)
return(u)
}
density.ppplist <- function(x, ..., se=FALSE) {
y <- lapply(x, density, ..., se=se)
if(!se) return(as.solist(y, demote=TRUE))
y.est <- lapply(y, getElement, name="estimate")
y.se <- lapply(y, getElement, name="SE")
z <- list(estimate = as.solist(y.est, demote=TRUE),
SE = as.solist(y.se, demote=TRUE))
return(z)
}
expandSpecialLists <- function(x, special="solist") {
## x is a list which may include entries which are lists, of class 'special'
## unlist these entries only
hit <- sapply(x, inherits, what=special)
if(!any(hit)) return(x)
# wrap each *non*-special entry in list()
x[!hit] <- lapply(x[!hit], list)
# now strip one layer of list() from all entries
return(unlist(x, recursive=FALSE))
}