Skip to content

Commit

Permalink
Closes #567, refactor and hide GNG function names
Browse files Browse the repository at this point in the history
  • Loading branch information
kudkudak committed Sep 19, 2015
1 parent b3230f7 commit 2636256
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 67 deletions.
40 changes: 20 additions & 20 deletions R/gng.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ findClosests <- NULL
#' print(isRunning(gng))
#'
isRunning <- function(object) {
return(object$isRunning())
return(object$.isRunning())
}

#' Find closest component
Expand Down Expand Up @@ -204,7 +204,7 @@ node <- function(x, gng_id) UseMethod("node")
#' predict(gng, c(1,2,2))
predict.Rcpp_GNGServer <- function(object, x, ...){
if( is.vector(x)){
object$predict(x)
object$.predict(x)
}else{
if ( !is(x, "data.frame") && !is(x, "matrix") && !is(x,"numeric") ) {
stop(gmum.error(GMUM_WRONG_PARAMS, "Wrong target class, please provide data.frame, matrix or numeric vector"))
Expand All @@ -217,7 +217,7 @@ predict.Rcpp_GNGServer <- function(object, x, ...){
y <- rep(NA, nrow(x))

for(i in 1:nrow(x)){
y[i] <- object$predict(x[i,])
y[i] <- object$.predict(x[i,])
}

y
Expand Down Expand Up @@ -254,7 +254,7 @@ run.Rcpp_GNGServer <- NULL
#' @examples
#' gng <- GNG(gng.preset.sphere(100))
#' pause(gng)
#' print(gng$isRunning())
#' print(gng$.isRunning())
pause <- function(object) UseMethod("pause")

#' @export
Expand Down Expand Up @@ -478,7 +478,7 @@ numberNodes <- function(object){
#' X = gng.preset.sphere(100)
#' insertExamples(gng, X)
#'
#' @note It copies your examples twice in RAM. You might want to use object$insertExamples.
#' @note It copies your examples twice in RAM. You might want to use object$.insertExamples.
insertExamples <- NULL

.GNG <- function(x=NULL, labels=c(),
Expand Down Expand Up @@ -594,10 +594,10 @@ insertExamples <- NULL

tryCatch({

while(server$getCurrentIteration() == 0 || server$isRunning()){}
while(server$getCurrentIteration() == 0 || server$.isRunning()){}

# max_iter is checked in GNG
while(iter == 0 || server$isRunning()){
while(iter == 0 || server$.isRunning()){
Sys.sleep(0.1)
iter = server$getCurrentIteration()

Expand Down Expand Up @@ -640,15 +640,15 @@ insertExamples <- NULL



if(server$isRunning()){
if(server$.isRunning()){
terminate(server)
}

server$.updateClustering()

}, interrupt=
function(interrupt){
if(server$isRunning()){
if(server$.isRunning()){
terminate(server)
}

Expand Down Expand Up @@ -769,25 +769,25 @@ node.Rcpp_GNGServer <- function(x, gng_id){
run.Rcpp_GNGServer <- function(object){
# Invalidate components
assign("components.membership", NULL, object)
object$run()
object$.run()
}

pause.Rcpp_GNGServer <- function(object){
object$pause()
object$.pause()
n = 0.0
sleep = 0.1
while(object$isRunning()){
while(object$.isRunning()){
Sys.sleep(sleep)
n = n + 1
if(n > 2/sleep){
print("Warning: GNG has not paused! Check status with gng$isRunning(). Something is wrong.")
print("Warning: GNG has not paused! Check status with gng$.isRunning(). Something is wrong.")
return()
}
}
}

terminate.Rcpp_GNGServer <- function(object){
object$terminate()
object$.terminate()
}

meanError <- function(object){
Expand All @@ -799,7 +799,7 @@ errorStatistics <- function(object){
}

clustering.Rcpp_GNGServer <- function(c){
c$clustering()
c$.getClustering()
}

gngSave <- function(object, filename){
Expand Down Expand Up @@ -835,7 +835,7 @@ calculateCentroids <- function(object, community.detection.algorithm=spinglass.


convertToIGraph <- function(object, calculate.dist=TRUE){
was_running = object$isRunning()
was_running = object$.isRunning()
if(was_running){
pause(object)
}
Expand Down Expand Up @@ -902,7 +902,7 @@ convertToIGraph <- function(object, calculate.dist=TRUE){
if(calculate.dist){
# Add distance information
dists <- apply(get.edges(g, E(g)), 1, function(x){
object$nodeDistance(indexesIGraphToGNG[x[1]], indexesIGraphToGNG[x[2]])
object$.nodeDistance(indexesIGraphToGNG[x[1]], indexesIGraphToGNG[x[2]])
})
E(g)$dists = dists
}
Expand Down Expand Up @@ -946,21 +946,21 @@ findClosests <- function(object, node.ids, x){

insertExamples <- function(object, examples, labels=c()){
if(length(labels) == 0){
object$insertExamples(examples)
object$.insertExamples(examples)
}else if(typeof(labels) == "character"){
if(typeof(labels) == "list"){
if(is.null(examples$labels)){
stop(gmum.error(GMUM_WRONG_PARAMS, "Empty labels column"))
}else{
label.column <- examples$labels
examples$labels <- NULL
object$insertLabeledExamples(examples, label.column)
object$.insertLabeledExamples(examples, label.column)
}
}else{
stop(gmum.error(GMUM_WRONG_PARAMS, "Please pass data frame"))
}
}else{
object$insertLabeledExamples(examples, labels)
object$.insertLabeledExamples(examples, labels)
}
}

Expand Down
59 changes: 25 additions & 34 deletions src/gng/gng_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ GNGServer * loadFromFile(std::string filename){
return out;
}

static void finalizer_GNGServer( GNGServer* ptr ){
delete ptr;
}

RCPP_MODULE(gng_module){
//TODO: Rcpp doesn't accept dot starting name so no way to hide it easily
Rcpp::function("fromFileGNG", &loadFromFile);
Expand Down Expand Up @@ -64,38 +60,33 @@ RCPP_MODULE(gng_module){
.field("max_iter", &GNGConfiguration::max_iter)
.method(".check_correctness", &GNGConfiguration::check_correctness)
.method(".set_bounding_box", &GNGConfiguration::setBoundingBox)
.method(".show", &GNGConfiguration::show);



.method(".show", &GNGConfiguration::show);

class_<GNGServer>("GNGServer")
.constructor<GNGConfiguration*>()
.method("setVerbosity", &GNGServer::setVerbosity)
.method(".save", &GNGServer::save)
.method("isRunning", &GNGServer::isRunning)
.method("run", &GNGServer::run)
.method("getCurrentIteration", &GNGServer::getCurrentIteration)
.method("pause", &GNGServer::pause)
.method("terminate", &GNGServer::terminate)
.method("getMeanError", &GNGServer::getMeanError)
.method("hasStarted", &GNGServer::hasStarted)
.method("nodeDistance", &GNGServer::nodeDistance)
.method("clustering", &GNGServer::RgetClustering)
.method(".getConfiguration", &GNGServer::getConfiguration)
.method("getDatasetSize", &GNGServer::getDatasetSize)
.method("getNumberNodes", &GNGServer::getNumberNodes)
.method(".exportToGraphML", &GNGServer::exportToGraphML)
.method(".getGNGErrorIndex", &GNGServer::getGNGErrorIndex)
.method("getNode", &GNGServer::getNode)
.method("insertExamples", &GNGServer::RinsertExamples)
.method("insertLabeledExamples", &GNGServer::RinsertLabeledExamples)
.method("getErrorStatistics", &GNGServer::RgetErrorStatistics)
.method("predict", &GNGServer::Rpredict)

.method(".getLastNodeIndex", &GNGServer::_getLastNodeIndex)
.method(".updateClustering", &GNGServer::_updateClustering);
// .finalizer(&finalizer_GNGServer);
.constructor<GNGConfiguration*>()
.method(".setVerbosity", &GNGServer::setVerbosity)
.method(".save", &GNGServer::save)
.method(".isRunning", &GNGServer::isRunning)
.method(".run", &GNGServer::run)
.method(".pause", &GNGServer::pause)
.method(".terminate", &GNGServer::terminate)
.method(".exportToGraphML", &GNGServer::exportToGraphML)
.method(".getGNGErrorIndex", &GNGServer::getGNGErrorIndex)
.method(".hasStarted", &GNGServer::hasStarted)
.method(".nodeDistance", &GNGServer::nodeDistance)
.method(".insertExamples", &GNGServer::RinsertExamples)
.method(".insertLabeledExamples", &GNGServer::RinsertLabeledExamples)
.method(".predict", &GNGServer::Rpredict)
.method(".getConfiguration", &GNGServer::getConfiguration)
.method(".getLastNodeIndex", &GNGServer::_getLastNodeIndex)
.method(".updateClustering", &GNGServer::_updateClustering)
.method("getClustering", &GNGServer::RgetClustering)
.method("getErrorStatistics", &GNGServer::RgetErrorStatistics)
.method("getCurrentIteration", &GNGServer::getCurrentIteration)
.method("getDatasetSize", &GNGServer::getDatasetSize)
.method("getNumberNodes", &GNGServer::getNumberNodes)
.method("getNode", &GNGServer::getNode)
.method("getMeanError", &GNGServer::getMeanError);
}

#include <RcppArmadillo.h>
Expand Down
2 changes: 1 addition & 1 deletion tests/cpp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ include ../../src/Makevars
CXX := $(CXX)
PREPROCESS = $(DEBUG) -DNRCPP_INTERFACE -DARMA_64BIT_WORD -DBOOST_DISABLE_ASSERTS
PKG_CPPFLAGS = $(GCC_STD) $(PREPROCESS) $(R_CPPFLAGS) $(INCLUDES) -pthread
CPPFLAGS := $(PKG_CPPFLAGS) -O2 -g -s --std=c++0x
CPPFLAGS := $(PKG_CPPFLAGS) -O2 -g -s
LDLIBS := $(PKG_LIBS) -lpthread -lgtest -lgtest_main

###
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/test_cec_control_eps_boundary_values.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test_that("mouse_1_spherical kmeans test control eps boundary values", {
nclusters <- 3
npoints = dim(dataset_points)[1]
c <- CEC(k=nclusters, x=dataset_points, control.nstart=1, method.init='kmeans++', control.eps=((npoints - 1) / npoints), seed=13371337)
final_nclusters = tail(c$logNumberOfClusters( ), n=1)
final_nclusters = tail(c$logNumberOfClusters, n=1)
expect_that(final_nclusters == 1, is_true())
print("mouse_1_spherical kmeans test control eps boundary values is correct")
})
Expand All @@ -18,7 +18,7 @@ test_that("mouse_1_spherical random test control eps boundary values", {
nclusters <- 3
npoints = dim(dataset_points)[1]
c <- CEC(k=nclusters, x=dataset_points, control.nstart=1, method.init='random', control.eps=((npoints - 1) / npoints), seed=13371337)
final_nclusters = tail(c$logNumberOfClusters( ), n=1)
final_nclusters = tail(c$logNumberOfClusters, n=1)
expect_that(final_nclusters == 1, is_true())
print("mouse_1_spherical random test control eps boundary values is correct")
})
Expand All @@ -29,7 +29,7 @@ test_that("EllipseGauss kmeans test control eps boundary values", {
nclusters <- 4
npoints = dim(dataset_points)[1]
c <- CEC(k=nclusters, x=dataset_points, control.nstart=1, method.init='kmeans++', control.eps=((npoints - 1) / npoints), seed=13371337)
final_nclusters = tail(c$logNumberOfClusters( ), n=1)
final_nclusters = tail(c$logNumberOfClusters, n=1)
expect_that(final_nclusters == 1, is_true())
print("EllipseGauss kmeans test control eps boundary values is correct")
})
Expand All @@ -40,7 +40,7 @@ test_that("EllipseGauss random test control eps boundary values", {
nclusters <- 4
npoints = dim(dataset_points)[1]
c <- CEC(k=nclusters, x=dataset_points, control.nstart=1, method.init='random', control.eps=((npoints - 1) / npoints), seed=13371337)
final_nclusters = tail(c$logNumberOfClusters( ), n=1)
final_nclusters = tail(c$logNumberOfClusters, n=1)
expect_that(final_nclusters == 1, is_true())
print("EllipseGauss random test control eps boundary values is correct")
})
Expand All @@ -51,7 +51,7 @@ test_that("mouse_1 kmeans test control eps boundary values", {
nclusters <- 3
npoints = dim(dataset_points)[1]
c <- CEC(k=nclusters, x=dataset_points, control.nstart=1, method.init='kmeans++', control.eps=((npoints - 1) / npoints), seed=13371337)
final_nclusters = tail(c$logNumberOfClusters( ), n=1)
final_nclusters = tail(c$logNumberOfClusters, n=1)
expect_that(final_nclusters == 1, is_true())
print("mouse_1 kmeans test control eps boundary values is correct")
})
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test_gng_basic.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test_that("Basic saving/loading works", {
# Check basic equivalency (TODO: check something deeper)
for(i in 1:100){
point <- runif(3)
expect_that(g$predict(point) == g2$predict(point), is_true())
expect_that(g$.predict(point) == g2$.predict(point), is_true())
}

file.remove("mygraph.bin")
Expand Down Expand Up @@ -131,7 +131,7 @@ test_that("GNG is working on mouse dataset", {
dataset = cec.mouse1.spherical
gng <- GNG(dataset, seed=778)
expect_that(gng$getMeanError() < 0.1, is_true())
expect_that(all(gng$clustering() == predict(gng,dataset)), is_true())
expect_that(all(gng$getClustering() == predict(gng,dataset)), is_true())
gng.refit <- GNG(dataset, seed=778)
# Seeding works => error statistics are the same
expect_that(all(abs(errorStatistics(gng.refit) - errorStatistics(gng)) < 1e-2), is_true() )
Expand All @@ -141,7 +141,7 @@ test_that("GNG clustering and predict are returning the same", {
print("GNG clustering and predict are returning the same")
X <- replicate(10, rnorm(20))
gng <- GNG(X)
expect_that(all(gng$clustering() == predict(gng,X)), is_true())
expect_that(all(gng$getClustering() == predict(gng,X)), is_true())
})

test_that("GNG errorStatistics and node retrieval work", {
Expand All @@ -161,15 +161,15 @@ test_that("GNG synchronization looks ok", {
synchronization_test <- function(){
gng <- GNG(dataset, verbosity=3, max.nodes=20)
gng$.updateClustering()
sum_1 = (sum( gng$clustering() != predict(gng, dataset)))
sum_1 = (sum( gng$getClustering() != predict(gng, dataset)))

gng <- GNG(train.online=TRUE, dim=2, verbosity=3, max.nodes=20)
gng$insertExamples(dataset)
gng$.insertExamples(dataset)
Sys.sleep(1)
gng$pause()
gng$.pause()
gng$.updateClustering()

sum_2 = (sum( gng$clustering() != predict(gng, dataset)))
sum_2 = (sum( gng$getClustering() != predict(gng, dataset)))

expect_that(sum_1 == 0 && sum_2 == 0, is_true())
expect_that(isRunning(gng), is_false())
Expand Down

0 comments on commit 2636256

Please sign in to comment.