complete first draft of package
[epclust.git] / old_C_code / stage2_UNFINISHED / src / unused / 03_compute-sums-of-classesRANDOM_2010.r
CommitLineData
ad642dc6
BA
1## File : 03_compute-sum-of-classes_2010.r
2## Description : Calculer les synchrones pour chaque groupe obtenu par le
3## clustering.
4
5rm(list = ls())
6
7setwd("~/Documents/projects/2014_EDF-Orsay-Lyon2/codes/")
8
9## 1. Read auxiliar data files ####
10
11identifiants <- read.table("identifs.txt")[ ,1]
12dates0 <- read.table("datesall.txt")[, 1]
13dates <- dates0[grep("2009", dates0)]
14rm(dates0)
15
16n <- length(identifiants)
17p <- length(dates)
18
19blocks <- c(rep(6500, 3), 5511)
20
21# Fit of the clustering : clfit
22#load('~/Documents/projects/2014_EDF-Orsay-Lyon2/res/clfit500.Rdata')
23load('~/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
27dfclust <- data.frame(cluster = clfit$clustering) # real clustering
28K <- nrow(clfit$clusinfo)
29
30#dfclust2 <- dfclust
31dfclust$cluster <- sample(unlist(Map(rep, 1:200, table(dfclust)))) # random clust
32
33
34
35#dfclust <- head(dfclust, 50) # just for testing purpouses
36synchros <- matrix(0, ncol = p, nrow = K)
37rm(clfit)
38
39## 2. Process the large file ####
40
41close(con)
42con <- file("~/tmp/2010_full.txt") # Establish a connection to the file
43open(con, "r") # Open the connection
44
45for(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
75close(con) # close connection to the file
76
77synchros <- data.frame(t(synchros), total = colSums(synchros))
78
79write.table(synchros, file = "~/tmp/2009_synchros200RANDOM.txt")
80write.table(dfclust, file = "../res/dfclust200RANDOM.txt")
81#write.table(synchros, file = "~/tmp/2009_synchros200RC.txt")
82