-
Notifications
You must be signed in to change notification settings - Fork 1
/
cluster.r
240 lines (197 loc) · 6.88 KB
/
cluster.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
# This script forms regions using an extended kmeans algorithm with optimized total number of clusters based on SPD growth rates.
# It processes time segments, reads data, and saves clustering results in binary and Matlab files, and generates plots.
# SPDX-FileCopyrightText: 2023-2024 Helmholtz-Zentrum hereon GmbH
# SPDX-FileContributor: Kai W. Wirtz <kai.wirtz@hereon.de>
# SPDX-License-Identifier: GPL-3.0-or-later
# Input files:
# - out/bin/eurodat_all.bin
# Output files:
# - out/bin/clusti_<region><time_segment>_<cscrit>.bin
# - out/mat/clusti_<time_segment>_<cscrit>.mat
# - out/plots/cluster_withinss<time_segment>_<cscrit>.png
# - out/plots/clusters_<region><time_segment>.png
# - out/plots/clusters_<region><time_segment>.pdf
# Variables:
# - scdir: Directory for output files
# - myargs: Command line arguments for time segments and clustering parameter
# - ti0, ti1: Start and end of time segments
# - cscrit: Critical cluster size
# - cc: Region name
# - XOUT: Output control flag
# - file: Input data file path
# - nclustsum: Sum of clusters
# - bcs: Cluster parameter
# - itern: Number of iterations for clustering
# - lons, lats: Longitude and latitude data
# - kmax: Maximum number of clusters
# - clusti: Cluster indices
# - wi, mcs, mincsv: Clustering statistics
# - cluster, wss0, wss: Clustering results and within-cluster sum of squares
# - cmap: Color map for plots
# checks for input arguments that control partial computation
# in a parallel mode
# Examples: "Rscript cluster.r $SLURM_ARRAY_TASK_ID"
# within a slurm.sh
# "Rscript cluster.r 1 3"
# to process only time segments 1 to 3
scdir <- 'out/'
# retrieve time segments from arguments or sets default range
myargs <- commandArgs(trailingOnly = TRUE)
if (length(myargs) > 0) {
ti0 <- as.numeric(myargs[1])
if (length(myargs) > 1) {
ti1 <- as.numeric(myargs[2])
} else {
ti1 <- ti0
}
if (ti1 < ti0) {
ti1 <- ti0
}
} else {
ti0 <- 1
ti1 <- 18
}
# set clustering parameter cscrit
if (length(myargs) > 2) {
cscrit <- as.numeric(myargs[3])
} else {
cscrit <- 120 # critical cluster size
}
print(paste('time slice:', ti0, ti1, 'cscrit=', cscrit))
cc <- 'Europe'
XOUT <- 0 # no output to window
# run standalone (requires few library calls)
if (1) {
library(sp)
library(rworldmap)
library(stats)
library('R.matlab')
base <- getMap(resolution = "low") # extract basemap
library("RColorBrewer")
}
# read data
file <- paste0(scdir, 'bin/eurodat_all', '.bin')
print(paste('read data from ', file))
load(file)
nclustsum <- 0
bcs <- 10 # cluster parameter
itern <- 1000 # statistical rigor, should be >1000 and <10000, depends on computing resource
lons <- datc$lons
lats <- datc$lats
# maximal number of clusters
nn <- length(lons)
kmax <- 1 + floor(nn / cscrit)
kmax <- min(max(kmax, 10), 50)
clusti <- rep(0, 92000) # max number of dates
# loop over time segments
for (ti in seq(ti0, ti1)) {
tis <- breaks[ti + 1]
print(tis)
# withiness vector; cluster skill
wi <- rep(9, kmax + 1)
mcs <- rep(0, kmax + 1)
mincsv <- rep(0, kmax + 1)
# single cluster as reference
cluster <- kmeans(datc[c(1:2, 2 + ti)], 1)
wss0 <- cluster$tot.withinss
# screen sequence of clusters
for (k in 26 + seq(kmax - 23)) {
wss <- 9E9
k0 <- which.min(wi)
if (wi[k - 1] < wi[k0] * 3) {
# loop over random iterations
for (it0 in seq(itern)) {
# data are clustered by the k-means method
cluster <- kmeans(datc[c(1:2, 3 + ti - ti0)], k)
mclu <- sort(cluster$size)
mincs <- mean(mclu[1:2]) # size of two smallest clusters
# skill function depending on minimal cluster size
mf <- 1 + exp(-(mincs / (cscrit) - 1) * bcs)
# compare with best partitioning among random samples
ww <- cluster$tot.withinss / wss0
# interim output
if (it0 %% 150 == 0) {
X <- sprintf('%d %4d mc=%1.1f %1.3f %1.3f', k, it0, mincs, ww * mf, wss)
print(X)
}
# store best cluster layout
if (ww * mf < wss) {
wi[k] <- ww * mf
mcs[k] <- mf
mincsv[k] <- mincs
wss <- wi[k]
}
}
}
}
# compute optimal cluster number
k <- which.min(wi)
k <- max(k, 2)
Xa <- paste(ti, 'k=', k, 'mc=', mincsv[k])
print(Xa)
wi[1] <- wi[2]
# plot clustering statistics
if (XOUT == 1) {
x11(width = 9, height = 8)
} else {
pfile <- paste0(scdir, 'plots/cluster_withinss', ti, '_', cscrit, '.png')
print(paste("plot to ", pfile))
png(filename = pfile, width = 9, height = 8, units = "cm", res = 300)
}
plot(wi, main = paste(cc[1], tis), log = "y", xlim = c(10, kmax))
abline(v = k, col = "red", lwd = 3)
lines(mcs * 0.5, col = "blue", lwd = 2)
wss <- 9E9
# repeat kmeans for optimal cluster number
for (it in seq(itern)) {
cluster <- kmeans(datc[c(1:2, 3 + ti - ti0)], k)
mclu <- sort(cluster$size)
mincs <- mean(mclu[1:2])
mf <- 1 + exp(-(mincs / (cscrit) - 1) * bcs)
if (cluster$tot.withinss / wss0 * mf < wss) {
wss <- cluster$tot.withinss / wss0 * mf
cluc <- cluster$cluster
mincs0 <- mincs
cluster0 <- cluster
}
}
# spatial plot of clusters
xrange <- c(min(lons) + 3, max(lons) - 3)
yrange <- c(min(lats) + 1, max(lats) - 2)
cmap <- palette(rainbow(k + 4))
cmap <- palette(rainbow(k + 4)) # needs to be invoked 2 times : WEIRD!
if (XOUT == 1) {
x11(width = 11, height = 13)
} else {
pfile <- paste0(scdir, 'plots/clusters_', cc[1], tis, '.png')
print(paste("plot to ", pfile))
png(filename = pfile, width = 11, height = 13, units = "cm", res = 600)
}
par(oma = c(1, 1, 2, 1), mar = c(0.08, 0.08, 0.9, 0.3), mfrow = c(1, 1), cex.lab = 0.5, cex.sub = 0.5, cex.main = 1., cex.axis = 0.7)
plot(base, col = rgb(0.95, 0.95, 0.95), border = "antiquewhite3", xlim = xrange, ylim = yrange, main = paste(tis, "kBP"))
# plot all sites of a clustered region in a specific color
for (i in seq(k)) {
ii <- which(cluc == i)
rgrm <- mean(rmm[ii, 1 + ti - ti0], na.rm = TRUE)
X <- sprintf('%d %d %1.3f', i, length(ii), rgrm)
X <- sprintf('%d %1.1f', i, rgrm * 10)
points(lons[ii], lats[ii], pch = 19, col = cmap[i], cex = .3)
text(mean(lons[ii]) - 1, mean(lats[ii]), X, cex = 0.5 + XOUT * 1.)
clusti[pcl[ii]] <- i
nclustsum <- nclustsum + length(ii)
}
text(-6, 68, Xa, cex = 1 + XOUT * 1.)
# store results for each data segment in R binary file
file <- paste0(scdir, 'bin/clusti_', cc[1], tis, '_', cscrit, '.bin')
save(file = file, clusti, k, wi, cluc)
# store cluster index of dates for each data segment in Matlab binary file
writeMat(paste0(scdir, 'mat/clusti_', tis, '_', cscrit, '.mat'), clusti = clusti)
# export plot as PDF
if (XOUT == 1) {
file <- paste0(scdir, 'plots/clusters_', cc[1], tis, '.pdf')
print(paste("plot to", file))
dev.copy2pdf(file = file, out.type = "pdf")
} else {
dev.off()
}
}