add alternative approach from 2013-01
[synclust.git] / R / main.utils.R
CommitLineData
15d1825d
BA
1#various util functions for the main program
2
3#preliminary: replace NA's by averaging over each serie's values
4#TODO: find a better way to handle missing values
5replaceNAs = function(M)
6{
7 n = nrow(M)
8 m = ncol(M)
9 res = M
10 for (i in 1:n)
11 {
12 avg = mean(M[i,1:(m-2)] [!is.na(M[i,1:(m-2)])])
13 res[i,1:(m-2)] [is.na(M[i,1:(m-2)])] = avg
14 }
15 return (res)
16}
17
18#standardize matrix M (remove mean, divide by standard deviation)
19standardize = function(M)
20{
21 avgM = colMeans(M, na.rm = TRUE)
22 stdevs = sqrt( unlist( apply(M, 2, var, na.rm=TRUE) ) )
23 res = t(M) - avgM
24 res = t(res / stdevs)
25 return (list("M"=res,"avg"=avgM,"stv"=stdevs))
26}
27
28#opposite of the previous function: get back M from standardized form
29destandardize = function(std)
30{
31 M = std$M
32 M = t(M) * std$stv
33 M = t(M + std$avg)
34 return (M)
35}
36
37#remap neighbors into some connex component
38remapNeighbors = function(NI, indices)
39{
40 revIndices = rep(NA, length(NI))
41 nc = length(indices)
42 for (ii in 1:nc)
43 revIndices[ indices[ii] ] = ii
44 locNI = list("ix"=as.list(rep(NA,nc)), "ds"=as.list(rep(NA,nc)))
45 for (ii in 1:nc)
46 {
47 locNI$ix[[ii]] = revIndices[ NI$ix[[ indices[ii] ]] ]
48 locNI$ds[[ii]] = NI$ds[[ indices[ii] ]]
49 }
50 return (locNI)
51}
52
53#check graph connexity
54getConnectedComponents = function(NIix)
55{
56 return (.Call("getConnectedComponents", NIix));
57}
58
59#auxiliary function to display clustering information
60promptForMapDisplay = function(stage, coordsM, NIix=NULL, clusters=NULL)
61{
62 if (is.null(clusters))
63 clusters = rep(1, nrow(coordsM))
64
65 shouldDisplay = ""
66 if (stage == "interm")
67 shouldDisplay = readline(">>> show intermediate map of neighborhoods ? (y/n)\n")
68 else if (stage == "final")
69 {
70 shouldDisplay = readline(
71">>> show final map of clusters ? (y/n) \
72NOTE: can be plotted later, see '? drawMapWithSites'\n")
73 }
74
75 if (shouldDisplay == "y")
76 {
77 drawMapWithSites(coordsM, clusters)
78 if (!is.null(NIix))
79 drawNeighborhoodGraph(coordsM,NIix)
80 print("Please press 'enter' to continue")
81 readline()
82 if (!is.null(dev.list()))
83 dev.off()
84 }
85}