complete first draft of package
[epclust.git] / old_C_code / stage2_UNFINISHED / src / unused / 03_compute-sums-of-classesRANDOM_2010.r
1 ## File : 03_compute-sum-of-classes_2010.r
2 ## Description : Calculer les synchrones pour chaque groupe obtenu par le
3 ## clustering.
4
5 rm(list = ls())
6
7 setwd("~/Documents/projects/2014_EDF-Orsay-Lyon2/codes/")
8
9 ## 1. Read auxiliar data files ####
10
11 identifiants <- read.table("identifs.txt")[ ,1]
12 dates0 <- read.table("datesall.txt")[, 1]
13 dates <- dates0[grep("2009", dates0)]
14 rm(dates0)
15
16 n <- length(identifiants)
17 p <- length(dates)
18
19 blocks <- c(rep(6500, 3), 5511)
20
21 # Fit of the clustering : clfit
22 #load('~/Documents/projects/2014_EDF-Orsay-Lyon2/res/clfit500.Rdata')
23 load('~/Documents/projects/2014_EDF-Orsay-Lyon2/res/clfit200.Rdata')
24 #load('~/Documents/projects/2014_EDF-Orsay-Lyon2/res/clfit200RC.Rdata')
25 # table(clfit$clustering)
26
27 dfclust <- data.frame(cluster = clfit$clustering) # real clustering
28 K <- nrow(clfit$clusinfo)
29
30 #dfclust2 <- dfclust
31 dfclust$cluster <- sample(unlist(Map(rep, 1:200, table(dfclust)))) # random clust
32
33
34
35 #dfclust <- head(dfclust, 50) # just for testing purpouses
36 synchros <- matrix(0, ncol = p, nrow = K)
37 rm(clfit)
38
39 ## 2. Process the large file ####
40
41 close(con)
42 con <- file("~/tmp/2010_full.txt") # Establish a connection to the file
43 open(con, "r") # Open the connection
44
45 for(b in seq_along(blocks)){ # Reading loop
46 nb <- blocks[b]
47 actual <- readLines(con = con, n = nb )
48 auxmat <- matrix(unlist(strsplit(actual, " ")), ncol = p + 1, byrow = TRUE)
49 rm(actual)
50
51 datamat <- t(apply(auxmat[, -1], 1, as.numeric))
52 rownames(datamat) <- substr(auxmat[, 1], 2, 7)
53 rm(auxmat)
54
55 # obtain for each line of datamat the cluster membership (if any)
56 clustfactor <- dfclust$cluster[match(rownames(datamat), rownames(dfclust))]
57
58 # if(length(which(rownames(datamat) == "218095"))>0) print(b);
59
60 for(k in 1:K){
61 clustk <- which(clustfactor == k)
62 if(length(clustk) > 0) {
63
64 if(length(clustk) > 1) {
65 synchrosk <- colSums(datamat[which(clustfactor == k), ])
66 } else {
67 synchrosk <- datamat[which(clustfactor == k), ]
68 }
69 synchros[k, ] <- synchros[k, ] + synchrosk
70 rm(synchrosk)
71 }
72 }
73 }
74
75 close(con) # close connection to the file
76
77 synchros <- data.frame(t(synchros), total = colSums(synchros))
78
79 write.table(synchros, file = "~/tmp/2009_synchros200RANDOM.txt")
80 write.table(dfclust, file = "../res/dfclust200RANDOM.txt")
81 #write.table(synchros, file = "~/tmp/2009_synchros200RC.txt")
82