Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2886_SymRegGrammarEnumeration/ExpressionClustering_R/ClusteringScript.R @ 16030

Last change on this file since 16030 was 15936, checked in by gkronber, 7 years ago

#2886 mapping GP solutions in R

File size: 5.1 KB
Line 
1library(largeVis)
2library(ggplot2)
3library(dplyr)
4
5#setwd("D:/heal/documents/trunk/Publications/2018/GPTP/data");
6setwd("C:/reps/HEAL/Publications-2018-GPTP/data");
7sentenceFileName <- "evaluations_allSentences_2018-04-13_16-40_TreeSize-7_1d.csv.gz";
8
9# read from CSV and store as R binary (must be done once to produce the .rds file)
10#evalData <- read.csv(sentenceFileName,header = TRUE, sep = ";", dec=",");
11#saveRDS(evalData, "evaluations_allSentences_2018-04-13_16-40_TreeSize-7_1d.csv.rds");
12
13# read from R binary (faster)
14evalData <- readRDS("evaluations_allSentences_2018-04-13_16-40_TreeSize-7_1d.csv.rds");
15
16max(evalData$R2.keijzer4);
17max(evalData$R2.keijzer9);
18max(evalData$R2.pagie);
19max(evalData$R2.nguyen5);
20max(evalData$R2.nguyen6);
21max(evalData$R2.nguyen7);
22
23outputs <- evalData[,10:109];
24
25#check zero mean, unit variance
26#mean(t(outputs[2,]))
27#sd(t(outputs[2,]))
28
29# check
30# plot(t(outputs[4,]))
31
32#apprNN <- randomProjectionTreeSearch(t(outputs), K=100, n_trees=50, distance_method="Euclidean", verbose=TRUE)
33# check ANN
34#cluster_1 <- tidyr::gather(dplyr::tbl_df(t(outputs)[,apprNN[,5]]), "rowNum", "value");
35#xs <- rep(seq(1:100),100)
36#ggplot(cluster_1, aes(x=xs, y=value, c=rowNum)) + geom_line();
37
38#edgeMatrix <- buildEdgeMatrix(t(outputs),apprNN, verbose=TRUE);
39#clusters <- hdbscan(edgeMatrix, apprNN, minPts = 10, K = 5, verbose=TRUE);
40
41# check cluster
42#cluster_1 <- tidyr::gather(dplyr::tbl_df(t(outputs)[,!is.na(clusters$clusters) & clusters$clusters==3]), "rowNum", "value");
43#xs <- rep(seq(1:100),nrow(cluster_1)/100) # reps must be the number of functions in the cluster
44#ggplot(cluster_1, aes(x=xs, y=value, c=rowNum)) + geom_line();
45
46
47lv <- largeVis(t(outputs), dim=2, distance_method="Cosine",
48               perplexity=100, K = 100, n_trees = 150, threads=4,
49               save_neighbors = TRUE, save_edges = TRUE, verbose=TRUE) ;
50clusters <- hdbscan(lv, verbose=TRUE, threads=4, minPts = 10, K = 20);
51
52
53# calculate quality distribution for each cluster
54qualities <- evalData$R2.keijzer4;
55clusterQualities <- data.frame(Qualities = qualities, Clusters = clusters$clusters, x=t(lv$coords)[,1], y=t(lv$coords)[,2] );
56
57clusterQualityAvg <- clusterQualities %>% group_by(Clusters) %>% summarize(AvgQuality = mean(Qualities)) ;
58clusterQualityStdDev <- clusterQualities %>% group_by(Clusters) %>% summarize(StdDevQuality = sd(Qualities));
59clusterQualityCount <- clusterQualities %>% group_by(Clusters) %>% summarize(Count = n());
60clusterXCenter <- clusterQualities %>% group_by(Clusters) %>% summarize(meanX = mean(x));
61clusterYCenter <- clusterQualities %>% group_by(Clusters) %>% summarize(meanY = mean(y));
62clusterStats <- clusterQualityAvg %>% full_join(clusterQualityStdDev, by="Clusters") %>% full_join(clusterQualityCount, by="Clusters") %>% full_join(clusterXCenter, by ="Clusters") %>% full_join(clusterYCenter, by="Clusters");
63clusterStats <- dplyr::arrange(clusterStats, desc(AvgQuality));
64clusterStats$Rank <- seq(1:nrow(clusterStats));
65ggplot(clusterStats, aes(x = Rank, y=AvgQuality)) + geom_point();
66
67write.csv2(clusters$clusters, "cluster_assignment_new.csv", sep = " ", dec = ".");
68
69#check clusters
70for(i in seq(1:nrow(clusterStats))) {
71  clusterNumber <- clusterStats$Clusters[i] # number of cluster with smallest quality (error!)
72  cluster_i <- tidyr::gather(dplyr::tbl_df(t(outputs)[,!is.na(clusters$clusters) & clusters$clusters==clusterNumber]), "rowNum", "value");
73  xs <- rep(seq(1:100),nrow(cluster_i)/100) # reps must be the number of functions in the cluster
74  ggplot(cluster_i, aes(x=xs, y=value, c=rowNum)) +
75    theme_void() +
76    geom_line(alpha=0.1);
77 
78  ggsave(paste(as.character(i), as.character(round(clusterStats$meanX[i], 3)), as.character(round(clusterStats$meanY[i], 3)), ".png"));
79}
80
81funs_in_cluster <- t(outputs)[,!is.na(clusters$clusters) & clusters$clusters==1748]
82cor(method="pearson", target_keijzer4, t(outputs[20281, ]))
83plot(funs_in_cluster[,4], target_keijzer4)
84
85xi <- seq(0,9.99,0.1);
86#  x³  * exp(-x) * cos(x) * sin(x) * (sin(x)² * cos(x) - 1)
87target_keijzer4 <- xi^3 * exp(-xi) * cos(xi) * sin(xi) * (sin(xi)*sin(xi) * cos(xi) - 1);
88plot(xi, target_keijzer4);
89
90m <- data.frame(x=t(lv$coords)[,1], y=t(lv$coords)[,2], c=clusters$clusters, q=qualities, outputs)
91m_sub <- m[m$q<1.0,];
92
93# plot mapped points (clusters)
94ggplot(data=m, aes(x=x, y=y)) +
95  geom_point(aes(color=c))  +
96  theme(legend.position = "none")
97#  scale_color_gradient(low = "red",high = "black")
98;
99ggsave("phenotypic_clusters.png")
100
101
102# plot mapped points (qualities)
103ggplot(data=m, aes(x=x, y=y)) +
104  geom_point(aes(color=q))  +
105  scale_color_gradientn(colors=heat.colors(30))
106;
107
108
109#write.csv2(m, "mapping_evaluations_allSentences_2018-04-13_16-40_TreeSize-7_1d.csv");
110m <- read.csv2("mapping_evaluations_allSentences_2018-04-13_16-40_TreeSize-7_1d.csv");
111
112cluster_n <- dplyr::filter(m, c==9);
113cluster_evals <- data.frame(x=seq(1,100,1), t(cluster_n[,5:104]))
114evals_cluster_n <- tidyr::gather(cluster_evals,"f", "fx", 2:ncol(cluster_evals))
115
116p <- ggplot(evals_cluster_n, aes(x=x, y=fx,color=f)) + geom_line();
117p
Note: See TracBrowser for help on using the repository browser.