major folder reorganisation, R pkg is now epclust/ at first level. Experimental usage...
[epclust.git] / old_C_code / stage2 / src / unused / 00_plots-energycon.r
diff --git a/old_C_code/stage2/src/unused/00_plots-energycon.r b/old_C_code/stage2/src/unused/00_plots-energycon.r
new file mode 100644 (file)
index 0000000..9f70085
--- /dev/null
@@ -0,0 +1,190 @@
+## File : 00_plots-energycon.r
+## Description : Using the full data matrix, we extract handy features to
+##               cluster.
+
+rm(list = ls())
+
+library(Rwave)       # CWT
+#library(cluster)     # pam
+#library(flexclust)   # kcca
+source("aux.r")               # auxiliary clustering functions 
+source("sowas-superseded.r")  # auxiliary CWT functions
+source('http://eric.univ-lyon2.fr/~jcugliari/codes/functional-clustering.r')
+setwd("~/recherche/03_projects/2014_EDF-Orsay-Lyon2/codes/")
+
+
+## 1. Read auxiliar data files ####
+
+#identifiants <- read.table("identifs.txt")[ ,1]
+dates0       <- read.table("datesall.txt")[, 1]
+dates        <- dates0[grep("2009", dates0)]
+#rm(dates0)
+
+#n <- length(identifiants)
+p <- length(dates)
+
+#blocks <- c(rep(6500, 3), 5511)  
+
+# table( substr(dates, 11, 15) ) # Sunlight time saving produces an 
+# unbalanced number of time points
+# per time stepa across the year
+
+
+## 2. Process the large file ####
+
+# if(exists("con")) close(con)
+# con <- file("~/tmp/2009_full.txt")  # Establish a connection to the file
+# open(con, "r")                      # Open the connection
+# 
+# nb <- 4
+# actual <- readLines(con = con, n = nb )[-3]
+# auxmat <- matrix(unlist(strsplit(actual, " ")), ncol = p + 1, byrow = TRUE)
+# rm(actual)
+#   
+# datamat <- t(apply(auxmat[, -1], 1, as.numeric))
+# rownames(datamat) <- substr(auxmat[, 1], 2, 7)
+# rm(auxmat)
+# 
+# datamat <- datamat[, 1:(48 * 14)]
+# p <- ncol(datamat)
+# 
+# auxDWT <- t(apply(datamat, 1, toDWT))
+# matcontrib <- t(apply(auxDWT,  1, contrib))
+# rm(auxDWT)
+# 
+# close(con); rm(con)                    # close connection to the file
+
+
+load("~/data/Irlande/Data_CER_clean/SME.RData")
+
+smp <- c(4, 79, 126)
+
+datamat <- t(SME[1:(48 * 7), smp])
+p <- ncol(datamat)
+
+auxDWT <- t(apply(datamat, 1, toDWT))
+matcontrib <- t(apply(auxDWT,  1, contrib))
+rm(auxDWT)
+
+matplot(t(datamat), type = "l", lty = 1, 
+        col = 1:8, lwd = 2)
+
+pdf("~/courbes.pdf")
+#par(mai = c(1, 1, 0.8, 0.6), mfcol = c(4, 2), cex  = 2)
+for(courbe in 1:ncol(SME)) {
+  plot(SME[1:(48 * 7), courbe], main = paste(courbe),
+       xlab = "", ylab = "", type = "l")
+}
+dev.off()
+
+
+
+
+## Plots for ENERGYCON full article ####
+op <- par()
+
+col <- c("grey", "black", "black")
+## Curves
+pdf('~/curves.pdf', width = 12)
+par(mai = c(1.3, 1.3, 0.1, 0.1), cex  = 1.2)
+matplot(t(datamat), type = "l", lty = c(1, 1, 2), 
+        lwd = 2, col = col, # c(1, 2, 4),
+        ylab = "Load", xlab = "Time (1/2 hours)")
+legend("top", c("Cust. A", "Cust. B", "Cust. C"),
+       col = col, lty = c(1, 1, 2), ncol = 3, lwd = 2)
+# matplot(scale(t(datamat), scale = F), 
+#         type = "l", lty = 1, col = c(1, 2, 4), lwd = 2, 
+#         ylab = "Load", xlab = "Time (1/2 hours)")
+dev.off()
+
+normi <- function(x) x / max(x)
+normi2 <- function(x) (x - min(x)) / (max(x) - min(x))
+
+matplot(apply(datamat, 1, normi2),
+        type = "l", lty = 1, col = c(1, 2, 4), lwd = 2, 
+        ylab = "Load", xlab = "Time (1/2 hours)")
+
+
+cont_dist <- dist(scale(matcontrib)) 
+cont_dist <- cont_dist / max(cont_dist)
+
+
+delta <- p
+n     <- nrow(datamat)
+
+## _.a CWT -- Filtering the lowest freqs (>6m) ####
+nvoice   <- 4
+# noctave4 = 2^13 = 8192 half hours ~ 180 days
+noctave4 <- adjust.noctave(N = delta, dt = 1, s0 = 2,
+                           tw = 0, noctave = 10)
+# 10 here represent 2^10 = 1024 half-hours ~ 2 weeks day
+scalevector4  <- 2^(4:(noctave4 * nvoice) / nvoice) * 2
+lscvect4      <- length(scalevector4)
+lscvect <- lscvect4  # i should clean my code: werFam demands a lscvect
+Xcwt4   <- toCWT(datamat, noctave = noctave4, dt = 1,
+                 scalevector = scalevector4,
+                 lt = delta, smooth = FALSE, 
+                 nvoice = nvoice)      # observations node with CWT
+
+Xcwt2 <- matrix(NA_complex_, nrow= n, ncol= 2 + length(c(Xcwt4[, ,1])))
+
+
+for(i in 1:n) 
+  Xcwt2[i,] <- c(delta, lscvect, Xcwt4[,,i] / max(Mod(Xcwt4[,,i])) ) 
+
+rm(conso, Xcwt4); gc()
+
+lscvect <- 41 ## very very nasty: toCWT changes scalevector to 41 (instead of
+              ## the original length--37--)
+## _.b WER^2 distances  ########
+Xwer_dist    <- matrix(0.0, n, n)
+for(i in 1:(n - 1)){
+ mat1   <- vect2mat(Xcwt2[i,])
+ for(j in (i + 1):n){
+    mat2 <- vect2mat(Xcwt2[j,])
+    num     <- Mod(mat1 * Conj(mat2))
+    WX      <- Mod(mat1 * Conj(mat1))
+    WY      <- Mod(mat2 * Conj(mat2))
+    smsmnum <- smCWT(num, scalevector = scalevector4)
+    smsmWX  <- smCWT(WX,  scalevector = scalevector4)
+    smsmWY  <- smCWT(WY,  scalevector = scalevector4)
+    wer2    <- sum(colSums(smsmnum)^2)  /
+      sum( sum(colSums(smsmWX) * colSums(smsmWY)) )
+    Xwer_dist[i, j] <- sqrt(delta * lscvect * (1 - wer2))
+    Xwer_dist[j, i] <- Xwer_dist[i, j]
+  }
+}
+diag(Xwer_dist) <- numeric(n)
+
+
+Xwer_dist <- Xwer_dist / max(Xwer_dist)
+
+
+pdf("~/cmdscale.pdf", width = 14)
+#layout(matrix(1:2, 1, 2))
+par(mai = c(1, 1, 0.8, 0.6), mfcol = c(1, 2), cex  = 2)
+plot(cmdscale(cont_dist), pch = c("A", "B", "C"),#c(15, 17, 19), 
+     main = "RC based distance",
+     #col = c(1, 2, 4), 
+     xlim = c(-1, 1), ylim = c(-1, 1), 
+     xlab = "", ylab = "", asp = 1)
+abline(h= 0, lwd = 2); abline(v = 0, lwd = 2); grid(lty = 1)
+
+plot(cmdscale(Xwer_dist), pch = c("A", "B", "C"),#c(15, 17, 19),
+     main = "WER distance",
+     #col = c(1, 2, 4), 
+     xlim = c(-1, 1), ylim = c(-1, 1), 
+     xlab = "", ylab = "", asp = 1)
+abline(h= 0, lwd = 2); abline(v = 0, lwd = 2); grid(lty = 1)
+dev.off()
+
+
+
+## Effectives distribution
+#load('../res/clfit500.Rdata')
+pdf("~/distro500.pdf", width = 12)
+plot(sort(table(clfit$clustering), decreasing = TRUE), 
+     type ="s", ylab = "Effectives", xlab = "Class", lwd = 2)
+abline(v = 200)
+#dev.off()
+