Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
Conflicts:
	include/GNGGraph.h
	include/SHGraphDefs.h
	scripts/GNGConvertToIGraph.r
	scripts/RcppInterface.r
	src/ExtGraphNodeManager.hpp
	src/RcppInterface.cpp
	tests/test_many_servers.r
  • Loading branch information
kudkudak committed May 31, 2014
2 parents e303ffe + 75dc688 commit 4b5f5bb
Show file tree
Hide file tree
Showing 130 changed files with 6,468 additions and 66,574 deletions.
3 changes: 2 additions & 1 deletion .gitignore
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.depend/
build/
*.so
*.o
*.d
*~
data/
data/
15 changes: 15 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Package: GrowingNeuralGas
Version: 0.1
Date: 2014-01-08
Title: Online, very fast big-data clustering algorithm Growing Neural Gas implementation
for R written in C++.
Author: Stanislaw Jastrzebski <[email protected]>
Maintainer: Stanislaw Jastrzebski <[email protected]>
Depends: Rcpp (>= 0.10.4), BH(>= 1.54.0)
LinkingTo: BH, Rcpp
Suggests: RUnit
Description: Cluster big amounts of data using online lightspeed implementation of Growing Neural Gas.
OS_type: unix
License: LGPL-3
Packaged: 2014-01-08 22:39:25 UTC; staszek
NeedsCompilation: yes
50 changes: 0 additions & 50 deletions Makefile

This file was deleted.

3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import(igraph, methods)
useDynLib(GrowingNeuralGas)
exportPattern("^[[:alpha:]]+")
110 changes: 110 additions & 0 deletions R/gng-presets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
.gng.box_point<-function(r, center, prob=-1){
point <- c()

if(prob == -1)
point<-center
else
point<-c(center, prob)

point[1:3] = point[1:3] + runif(3, min=-r/2.0, max=r/2.0)

point
}


.gng.plane_point<-function(r,center){
if(!hasArg(r)) r<-1.0
if(!hasArg(center)) center<-c(0,0,0)

point<-center
point[1]<-point[1]+r*runif(1.0)
point[2]<-point[2]+r*runif(1.0)
point[3]<-point[3]

return(point)
}

.gng.sphere_point<-function(r,center){
if(!hasArg(r)) r<-1.0
if(!hasArg(center)) center<-c(0,0,0)

alpha<-runif(1)*2*pi
beta<-runif(1)*pi

point<-center
point[1]<-point[1]+r*cos(alpha)*sin(beta)
point[2]<-point[2]+r*sin(alpha)*sin(beta)
point[3]<-point[3]+r*cos(beta)

return(point)
}

gng.preset.box<-function(N, r=0.5, center=c(0.5,0.5,0.5), prob=-1){
mat<-NULL


if(prob == -1)
mat<-matrix(0,N,3)
else
mat<-matrix(0,N,4)

for(i in 1:N){
mat[i,] = .gng.box_point(r=r, center=center, prob=prob)
}

mat
}



gng.preset.plane<-function(N, side=0.5, center=c(0.5,0.5,0.5), prob=-1){


mat<-matrix(0,N,3)

for(i in 1:N){
mat[i,1:3] = .gng.plane_point(side, center)
mat[i,3] = mat[i,1]
}

mat
}


gng.preset.sphere<-function(N, r=0.5, center=c(0.5,0.5,0.5),prob=-1){
mat<-matrix(0,N,3)

for(i in 1:N){
mat[i,1:3] = .gng.sphere_point(r, center)
}

mat
}


.sigmoid <- function(x){
1./(1.+exp(-x))
}

gng.preset_potential<-function(N, r=0.5, center=c(0.5,0.5,0.5), prob=-1){
mat <- matrix(rnorm(20,mean=1), N,3)

for(j in 1:N){
t<-rnorm(1,mean=0,sd=1)
u<-rnorm(1,mean=0,sd=1)
val<-.sigmoid(t^2+u^2);
mat[j,1] = t
mat[j,2] = u
mat[j,3] = val

}

mat
}







27 changes: 27 additions & 0 deletions R/gng-utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

.plane.point<-function(r,center){
if(!hasArg(r)) r<-1.0
if(!hasArg(center)) center<-c(0,0,0)

point<-center
point[1]<-point[1]+r*runif(1.0)
point[2]<-point[2]+r*runif(1.0)
point[3]<-point[3]

return(point)
}

.sphere.point<-function(r,center){
if(!hasArg(r)) r<-1.0
if(!hasArg(center)) center<-c(0,0,0)

alpha<-runif(1)*2*pi
beta<-runif(1)*pi

point<-center
point[1]<-point[1]+r*cos(alpha)*sin(beta)
point[2]<-point[2]+r*sin(alpha)*sin(beta)
point[3]<-point[3]+r*cos(beta)

return(point)
}
129 changes: 129 additions & 0 deletions R/gng-visualize.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
library(igraph)



if("rgl" %in% rownames(installed.packages()) == TRUE){
.gng.plot3d<-function(gngServer){
tmp_name <- paste("tmp",sample(1:1000, 1),".graphml", sep="")
gngServer$export_to_graphml(tmp_name)
print("Reading GraphML dumped")
.visualizeIGraphRGL(.readFromGraphML(tmp_name))
file.remove(tmp_name)
}
#' Draw igraph using rgl - assumes >=3 dimensions and draws 3 first
.visualizeIGraphRGL<-function(g, radius=NULL){
library(multicore)
library(rgl)
library(igraph)

if(length(V(g))==0) return

iteration<-0
nodes <- length(V(g))

# Init 3d data
x_lines <- c(1:2*length(E(g)))
y_lines <- c(1:2*length(E(g)))
z_lines <- c(1:2*length(E(g)))
k<-1
m<-1
x<-c(1:nodes)
y<-c(1:nodes)
z<-c(1:nodes)

# Write 3d positions
for(i in 1:nodes){

x[i]=V(g)[i]$v0
y[i]=V(g)[i]$v1
z[i]=V(g)[i]$v2
}

# TODO: edges might be huge..
for(edg_idx in 1:length(E(g)))
{
edg <- get.edges(g, E(g)[edg_idx])
x_lines[k] = V(g)[edg[1]]$v0
y_lines[k] = V(g)[edg[1]]$v1
z_lines[k] = V(g)[edg[1]]$v2

x_lines[k+1] = V(g)[edg[2]]$v0
y_lines[k+1] = V(g)[edg[2]]$v1
z_lines[k+1] = V(g)[edg[2]]$v2
k = k + 2
}

if(is.null(radius)){
radius <- 8.0*(0.3333* (abs(max(x) - min(x))+abs(max(y) - min(y))+abs(max(z) - min(z)))/(nodes+0.0))
}

cx <- V(g)$error
cx <- abs(cx)/max(abs(cx))
cy <- c(1:(nodes))
cz <- c(1:(nodes))

cy <- 0.1
cz <- 0.1
print(cx)


### Draw graph ###
rgl.clear()
rgl.light()
rgl.bg(color="white")
axes3d(edges="bbox")


rgl.spheres(x,y,z,
radius = rep(radius, length(cx)),
col=rgb(cx,cy, cz))

rgl.lines(x_lines[1:k-1],y_lines[1:k-1],z_lines[1:k-1],color="bisque")
}
}
.gng.plot2d.errors<-function(gngServer, cluster, layout_2d, start_s=2){
tmp_name <- paste("tmp",sample(1:1000, 1),".graphml", sep="")
gngServer$export_to_graphml(tmp_name)
.visualizeIGraph2dWithErrors(.readFromGraphML(tmp_name ), cluster, layout_2d, start_s)
file.remove(tmp_name)
}

.gng.plot2d<-function(gngServer, cluster, layout_2d){
tmp_name <- paste("tmp",sample(1:1000, 1),".graphml", sep="")
gngServer$export_to_graphml(tmp_name)
.visualizeIGraph2d(.readFromGraphML(tmp_name ), cluster, layout_2d)
file.remove(tmp_name)
}
#' Visualize igraph using igraph plot
#' It will layout graph using v0 and v1 coordinates
#' @note It is quite slow, works for graphs < 2000 nodes, and for graphs <400 when using layout
.visualizeIGraph2d<-function(g, cluster, layout_2d){
#g<-as.undirected(g)
L<-NULL

if(layout_2d){
L<-cbind(V(g)$v0, V(g)$v1)
}else{
L <- layout.auto(g)
# L<-layout.fruchterman.reingold(g, niter=10000, area=4*vcount(g)^2)
}

if(cluster){
l = fastgreedy.community(g)#as.undirected(g))
col<-rainbow(length(l))
plot.igraph(g,vertex.size=3.0,vertex.label=NA,vertex.color=col[membership(l)],layout=L)
}else{
plot.igraph(g,vertex.size=3.0,vertex.label=NA,layout=L)
}
}

.visualizeIGraph2dWithErrors<-function(ig, cluster, layout_2d, start_s=2){
plot.new()
par(mfrow=c(1,2))
.visualizeIGraph2d(ig, cluster, layout_2d)
title("Graph visualization")
errors_raw = gng$get_error_statistics()
errors = log((errors_raw+1)/min(errors_raw+1))[start_s:length(errors_raw)]
plot(errors, type="l", lty=2, lwd=2, xlab="Time [s]", ylab="Mean error (log)", frame.plot=F)
title("Mean error (log)")
}
Loading

0 comments on commit 4b5f5bb

Please sign in to comment.