diff --git a/R/gng.R b/R/gng.R index 7e2fb416..4b275d92 100644 --- a/R/gng.R +++ b/R/gng.R @@ -148,7 +148,7 @@ findClosests <- NULL #' print(isRunning(gng)) #' isRunning <- function(object) { - return(object$isRunning()) + return(object$.isRunning()) } #' Find closest component @@ -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")) @@ -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 @@ -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 @@ -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(), @@ -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() @@ -640,7 +640,7 @@ insertExamples <- NULL - if(server$isRunning()){ + if(server$.isRunning()){ terminate(server) } @@ -648,7 +648,7 @@ insertExamples <- NULL }, interrupt= function(interrupt){ - if(server$isRunning()){ + if(server$.isRunning()){ terminate(server) } @@ -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){ @@ -799,7 +799,7 @@ errorStatistics <- function(object){ } clustering.Rcpp_GNGServer <- function(c){ - c$clustering() + c$.getClustering() } gngSave <- function(object, filename){ @@ -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) } @@ -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 } @@ -946,7 +946,7 @@ 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)){ @@ -954,13 +954,13 @@ insertExamples <- function(object, examples, labels=c()){ }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) } } diff --git a/src/gng/gng_module.cpp b/src/gng/gng_module.cpp index b1e50fd8..c79ec3ec 100644 --- a/src/gng/gng_module.cpp +++ b/src/gng/gng_module.cpp @@ -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); @@ -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") - .constructor() - .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() + .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 diff --git a/tests/cpp/Makefile b/tests/cpp/Makefile index 369f3d0b..40cc59d5 100644 --- a/tests/cpp/Makefile +++ b/tests/cpp/Makefile @@ -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 ### diff --git a/tests/testthat/test_cec_control_eps_boundary_values.R b/tests/testthat/test_cec_control_eps_boundary_values.R index 70f557ff..bb846878 100644 --- a/tests/testthat/test_cec_control_eps_boundary_values.R +++ b/tests/testthat/test_cec_control_eps_boundary_values.R @@ -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") }) @@ -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") }) @@ -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") }) @@ -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") }) @@ -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") }) diff --git a/tests/testthat/test_gng_basic.R b/tests/testthat/test_gng_basic.R index 7887423f..29bf4d94 100644 --- a/tests/testthat/test_gng_basic.R +++ b/tests/testthat/test_gng_basic.R @@ -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") @@ -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() ) @@ -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", { @@ -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())