-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc02_01_clustering_basin_slurm.R
106 lines (72 loc) · 2.75 KB
/
sc02_01_clustering_basin_slurm.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
# Set working directory
setwd("/my/working/directory/data")
# Functions ---------------------------------------------------------------
# Function to check if the required packages are installed and to load the library
usePackage <- function(p){
if (!is.element(p, installed.packages()[,1])) install.packages(p, dep = TRUE)
library(p, character.only = TRUE)
}
# Process time function
finish_progress <- function(nmax, t0, word) {
cat("\r", paste0(rep(" ", 75), collapse = ""))
interval(t0,now()) %>%
round(.) %>%
as.period(.) %>%
as.character(.) %>%
paste("Completed",nmax, word, "in", .)
}
# Libraries ---------------------------------------------------------------
# Load libraries
usePackage("data.table")
usePackage("tidyverse")
usePackage("lubridate")
usePackage("doMC")
usePackage("foreach")
# Select basin and dataset ------------------------------------------------
# Define computational unit
cunit <- Sys.getenv(c("CUNIT"))
# Define basin ID
basinID <- Sys.getenv(c("BID"))
# Define the set of variables
vset <- Sys.getenv(c("VSET"))
# Define seed
rnum <- as.numeric(Sys.getenv(c("SEED")))
# Define number of starts
nstr <- as.numeric(Sys.getenv(c("NSTART")))
# Directories -------------------------------------------------------------
basin_path <- paste0("/my/working/directory/data/partitional_clustering/",
vset, "/basin_", cunit, "_", basinID)
if(!dir.exists(basin_path)) dir.create(basin_path)
# Output path
k_path <- paste0(basin_path, "/kmeans")
if(!dir.exists(k_path)) dir.create(k_path)
# Input data --------------------------------------------------------------
basin <- fread(paste0(basin_path, "/basin_envVar_sc_", cunit, "_", basinID,
"_", vset, ".csv"))
# Preparation of the dataset ----------------------------------------------
# Convert data table to matrix
df_sc <- as.matrix(basin[,4:ncol(basin)])
# k-means cluster analysis ------------------------------------------------
# Define numbers of k to test
centers <- 1:60
# Register cores
#registerDoMC(max(centers))
registerDoMC(30)
print("Start clusting")
t0 <- now()
# kmeans for different numbers of k
k <- foreach(i = centers) %dopar% {
set.seed(rnum)
kmeans(df_sc, centers = i, nstart=nstr, iter.max = 500)
}
# Name the lists
names(k) <- paste0("n=", centers)
t1 <- finish_progress(max(centers), t0, "cluster centers")
print(t1)
# Save output -------------------------------------------------------------
write(x = t1, file = paste0(k_path, "/time_kmeans_60k.txt"), append = T)
write_rds(k, paste0(k_path, "/kmean_60k_", cunit, "_", basinID, "_", vset,
"_seed", rnum, "_nstart", nstr, ".rds"))
# Exit R ------------------------------------------------------------------
gc()
quit(save = "no")