Skip to content

Commit

Permalink
Commit directly from the sky, #567
Browse files Browse the repository at this point in the history
Working SVM and CEC standard structure-like autocompletion
Workaround for RStudio autocompl bug
  • Loading branch information
kudkudak committed Sep 17, 2015
1 parent 27e78d0 commit 30fd1fb
Show file tree
Hide file tree
Showing 20 changed files with 142 additions and 224 deletions.
6 changes: 3 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Generated by roxygen2 (4.1.1): do not edit by hand

S3method(SVM,default)
S3method(SVM,formula)
S3method(clustering,Rcpp_CecModel)
S3method(clustering,Rcpp_GNGServer)
S3method(node,Rcpp_GNGServer)
Expand All @@ -28,6 +26,8 @@ export(CEC)
export(GNG)
export(OptimizedGNG)
export(SVM)
export(SVM.default)
export(SVM.formula)
export(calculateCentroids)
export(caret.gmumSvmLinear)
export(caret.gmumSvmPoly)
Expand All @@ -50,9 +50,9 @@ export(gngLoad)
export(gngSave)
export(insertExamples)
export(isRunning)
export(iterations)
export(logClusters)
export(logEnergy)
export(logIterations)
export(meanError)
export(node)
export(numberNodes)
Expand Down
36 changes: 18 additions & 18 deletions R/R_scripts/test_cec_energy_func.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ test_that("EllipseGauss: energy is correct", {
for(i in 1:t)
{
c <- CEC(k=nclusters, x=dataset_points, method.init='random', method.type='standard')
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering(), entropy_func = standard_entropy)
expect_equal(expected=c$energy(), object=energy_func_value, tolerance=.000001)
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering, entropy_func = standard_entropy)
expect_equal(expected=c$energy, object=energy_func_value, tolerance=.000001)

c <- CEC(k=nclusters, x=dataset_points, method.init='random', method.type='spherical')
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering(), entropy_func = sphere_entropy)
expect_equal(expected=c$energy(), object=energy_func_value, tolerance=.000001)
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering, entropy_func = sphere_entropy)
expect_equal(expected=c$energy, object=energy_func_value, tolerance=.000001)

c <- CEC(k=nclusters, x=dataset_points, method.init='random', method.type='diagonal')
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering(), entropy_func = diagonal_entropy)
expect_equal(expected=c$energy(), object=energy_func_value, tolerance=.000001)
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering, entropy_func = diagonal_entropy)
expect_equal(expected=c$energy, object=energy_func_value, tolerance=.000001)
}
print("EllipseGauss: energy is correct")
})
Expand All @@ -37,16 +37,16 @@ test_that("mouse_1: energy is correct", {
for(i in 1:t)
{
c <- CEC(k=nclusters, x=dataset_points, method.init='random', method.type='standard')
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering(), entropy_func = standard_entropy)
expect_equal(expected=c$energy(), object=energy_func_value, tolerance=.000001)
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering, entropy_func = standard_entropy)
expect_equal(expected=c$energy, object=energy_func_value, tolerance=.000001)

c <- CEC(k=nclusters, x=dataset_points, method.init='random', method.type='spherical')
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering(), entropy_func = sphere_entropy)
expect_equal(expected=c$energy(), object=energy_func_value, tolerance=.000001)
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering, entropy_func = sphere_entropy)
expect_equal(expected=c$energy, object=energy_func_value, tolerance=.000001)

c <- CEC(k=nclusters, x=dataset_points, method.init='random', method.type='diagonal')
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering(), entropy_func = diagonal_entropy)
expect_equal(expected=c$energy(), object=energy_func_value, tolerance=.000001)
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering, entropy_func = diagonal_entropy)
expect_equal(expected=c$energy, object=energy_func_value, tolerance=.000001)
}
print("mouse_1: energy is correct")
})
Expand All @@ -62,16 +62,16 @@ test_that("mouse_1_spherical: energy is correct", {
for(i in 1:t)
{
c <- CEC(k=nclusters, x=dataset_points, method.init='random', method.type='standard')
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering(), entropy_func = standard_entropy)
expect_equal(expected=c$energy(), object=energy_func_value, tolerance=.000001)
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering, entropy_func = standard_entropy)
expect_equal(expected=c$energy, object=energy_func_value, tolerance=.000001)

c <- CEC(k=nclusters, x=dataset_points, method.init='random', method.type='spherical')
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering(), entropy_func = sphere_entropy)
expect_equal(expected=c$energy(), object=energy_func_value, tolerance=.000001)
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering, entropy_func = sphere_entropy)
expect_equal(expected=c$energy, object=energy_func_value, tolerance=.000001)

c <- CEC(k=nclusters, x=dataset_points, method.init='random', method.type='diagonal')
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering(), entropy_func = diagonal_entropy)
expect_equal(expected=c$energy(), object=energy_func_value, tolerance=.000001)
energy_func_value <- cec_energy(dataset = dataset_points, clustering = c$clustering, entropy_func = diagonal_entropy)
expect_equal(expected=c$energy, object=energy_func_value, tolerance=.000001)
}
print("mouse_1_spherical: energy is correct")
})
51 changes: 33 additions & 18 deletions R/cec.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,22 +240,22 @@ logClusters <- NULL
#' @export
logEnergy <- NULL

#' @name logIterations
#' @title logIterations
#' @name iterations
#' @title iterations
#'
#' @aliases logIterations,Rcpp_CecModel-method
#' @aliases iterations,Rcpp_CecModel-method
#'
#' @title logIterations
#' @title iterations
#'
#' @description Print how many iterations it took to learn CEC model
#'
#' @param c object Trained CEC model object.
#' @examples
#' \dontrun{
#' logIterations(c)
#' iterations(c)
#' }
#' @export
logIterations <- NULL
iterations <- NULL

loadModule('cec', TRUE)

Expand Down Expand Up @@ -332,7 +332,21 @@ CEC <- function(x = NULL,

model <- new(CecModel, config)



assign("call", call, model)
assign("energy", model$.getEnergy(), model)
assign("clustering", model$.getClustering(), model)
assign("centers", model$.getCenters(), model)
assign("covMatrix", model$.getCovMatrix(), model)
assign("logEnergy", model$.getLogEnergy(), model)
assign("logNumberOfClusters", model$.getLogNumberOfClusters(), model)

assign("iterations", model$.getIterations(), model)

assign(".staticFields", c("call", "energy", "clustering", "centers", "covMatrix",
"logNumberOfClusters", "logEnergy", "iterations"), model)

model
}

Expand All @@ -345,35 +359,36 @@ runOneIteration <- function(c) {
}

energy <- function(c) {
c$energy()
c$.getEnergy()
}

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

getDataset <- function(c) {
c$getDataset()
c$.getDataset()
}

centers <- function(c) {
c$centers()
c$.getCenters()
}

covMatrix <- function(c) {
c$covMatrix()
c$.getCovMatrix()
}

logClusters <- function(c) {
c$log.ncluster()
logEnergy <- function(c) {
c$.getLogEnergy()
}

logEnergy <- function(c) {
c$log.energy()
logNumberOfClusters <- function(c) {
c$.getLogNumberOfClusters()
}

logIterations <- function(c) {
c$log.iters()

iterations <- function(c) {
c$.getIterations()
}

predict.Rcpp_CecModel <- function(object, x, ...) {
Expand All @@ -388,7 +403,7 @@ predict.Rcpp_CecModel <- function(object, x, ...) {
x = data.matrix(x)
}

if(dim(object$getDataset())[2] != dim(x)[2]){
if(dim(object$.getDataset())[2] != dim(x)[2]){
stop("Incompatible dimension!")
}

Expand Down
12 changes: 6 additions & 6 deletions R/cec.plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#' }
plot.Rcpp_CecModel <- function(x, slice = c(), pca=FALSE, ellipses = FALSE, centers = FALSE, ...) {

d <- x$getDataset()
d <- x$.getDataset()
if(pca){
if(ncol(d) <= 2){
stop("CEC dataset should have dimension > 2 to use PCA")
Expand All @@ -44,17 +44,17 @@ plot.Rcpp_CecModel <- function(x, slice = c(), pca=FALSE, ellipses = FALSE, cent
} else {
slice <- c(1:(dim(d)[2]))
}
plot(d[,slice], col = (x$clustering() + 1), pch=20)
plot(d[,slice], col = (x$clustering + 1), pch=20)
}
else if (length(slice) == 1 || length(slice) == 2) {
plot(d[,slice], col = (x$clustering() + 1), pch=20)
plot(d[,slice], col = (x$clustering + 1), pch=20)
}
else{
pairs(d[,slice], col = (x$clustering() + 1))
pairs(d[,slice], col = (x$clustering + 1))
}

if (ellipses || centers) {
cen <- x$centers()
cen <- x$centers
n <- length(cen)
if(pca){
for (i in 1:n) {
Expand All @@ -65,7 +65,7 @@ plot.Rcpp_CecModel <- function(x, slice = c(), pca=FALSE, ellipses = FALSE, cent
}
if (ellipses && length(slice) <= 2){
#library("car")
cov <- x$covMatrix()
cov <- x$covMatrix
for (i in 1:n) {
data <- unlist(cov[i])
covMat <- matrix(data,ncol=sqrt(length(data)))
Expand Down
6 changes: 3 additions & 3 deletions R/cec.print.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
#'
print.Rcpp_CecModel <- function(x, ...) {
print(sprintf("CEC clustering; %d clusters with energy = %f",
length(x$centers()), x$energy()))
length(x$centers), x$energy))
print("Centers: ")
print(x$centers())
print(x$centers)
print("Covariances: ")
print(x$covMatrix())
print(x$covMatrix)
}

show.Rcpp_CecModel <- function(object){
Expand Down
12 changes: 6 additions & 6 deletions R/cec.summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
summary.Rcpp_CecModel <- function(object, ...) {
print(object)

if(isParameterOn(object$log.iters())){
if(isParameterOn(object$iterations)){
print("Iterations: ")
print(object$log.iters())
print(object$iterations)
}
if(isParameterOn(object$log.energy())){
if(isParameterOn(object$logEnergy)){
print("Energy for every iteration: ")
print(object$log.energy())
print(object$logEnergy)
}
if(isParameterOn(object$log.ncluster())){
if(isParameterOn(object$logNumberOfClusters)){
print("Number of clusters for every iteration: ")
print(object$log.ncluster())
print(object$logNumberOfClusters)
}
}

Expand Down
15 changes: 13 additions & 2 deletions R/gmum.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# Lazy loading to allow for discovery of all files
evalqOnLoad( {
# Autocompletion fix
.GlobalEnv$`.DollarNames.C++Object` <- function( x, pattern ){
# Autocompletion override
autocompl <- function(x, pattern="") {
targets <- c(asNamespace("Rcpp")$complete(x), x[['.staticFields']])
grep(pattern, targets, value = TRUE)[! (substr(grep(pattern, targets, value = TRUE),1,1)==".")]
}

`.DollarNames.Rcpp_C++Object` <<- autocompl
.DollarNames.Rcpp_SVMClient <<- autocompl
.DollarNames.Rcpp_GNGServer <<- autocompl
.DollarNames.Rcpp_CecModel <<- autocompl

# Workaround RStudio bug
if(exists(".rs.getAnywhere")) {
.rs.getAnywhere.original <<- .rs.getAnywhere
.rs.getAnywhere <<- function(a, envir=.GlobalEnv){ .rs.getAnywhere.original(a, .GlobalEnv) }
}
})
14 changes: 5 additions & 9 deletions R/svm.R
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ caret.gmumSvmLinear <- NULL
caret.gmumSvmPoly <- NULL

#' @rdname svm
#' @export
#' @export SVM.formula
SVM.formula <- NULL

#' @rdname svm
#' @export
#' @export SVM.default
SVM.default <- NULL

loadModule('svm_wrapper', TRUE)
Expand Down Expand Up @@ -317,7 +317,7 @@ SVM.formula <- function(formula, data, ...) {
x <- data.matrix(x)

ret <- SVM.default(x, y, ...)
assign("call", as.name("SVM"), ret) # SVM.formula call is different so overwriting
assign("call", call, ret)
return(ret)
}

Expand Down Expand Up @@ -365,8 +365,7 @@ SVM.formula <- function(formula, data, ...) {
}

models <- list()
call[[1]] <- as.name("SVM")


# Fit one model after another
for (j in J) {
x.model <- NULL
Expand Down Expand Up @@ -482,8 +481,7 @@ SVM.default <-
core <- "svmlight"

call <- match.call(expand.dots = TRUE)
call[[1]] <- as.name("SVM")


# check for errors
if (core != "libsvm" && core != "svmlight") {
stop(sprintf("bad core: %s, available are: libsvm, svmlight", core))
Expand Down Expand Up @@ -697,8 +695,6 @@ SVM.default <-
client <- new(SVMClient, config)
client$.train()

call[[1]] <- as.name("SVM")

# R object often have fields that don't change accessible through $ notation
assign("call", call, client)
assign(".levels", levels, client)
Expand Down
4 changes: 3 additions & 1 deletion R/svm.utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ svm.dataset.circles <- function() {
#'
#' @description Calculates accuracy of a prediction, returns precent of correctly predicted examples
#' over all test examples.
#' @export
#' @export svm.accuracy
#' @rdname svm.accuracy
#'
#' @usage svm.accuracy(prediction, target)
#'
#' @param prediction factor or 1 dim vector with predicted classes
#' @param target factor or 1 dim vector with true classes
#'
Expand Down
Loading

0 comments on commit 30fd1fb

Please sign in to comment.