[Zooimage-commits] r235 - in pkg/zooimage: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Nov 30 16:24:49 CET 2012


Author: phgrosjean
Date: 2012-11-30 16:24:49 +0100 (Fri, 30 Nov 2012)
New Revision: 235

Modified:
   pkg/zooimage/NAMESPACE
   pkg/zooimage/R/ZIClass.R
   pkg/zooimage/R/utilities.R
   pkg/zooimage/man/ZIClass.Rd
   pkg/zooimage/man/utilities.Rd
Log:
Added drop.vars and drop.vars.def arguments to calcVars() and ZIClass()

Modified: pkg/zooimage/NAMESPACE
===================================================================
--- pkg/zooimage/NAMESPACE	2012-08-12 23:46:30 UTC (rev 234)
+++ pkg/zooimage/NAMESPACE	2012-11-30 15:24:49 UTC (rev 235)
@@ -95,6 +95,7 @@
 export(addClass)
 export(calcVars)
 export(calibrate)
+export(dropVars)
 export(ecd)
 export(getDec)
 export(listSamples)

Modified: pkg/zooimage/R/ZIClass.R
===================================================================
--- pkg/zooimage/R/ZIClass.R	2012-08-12 23:46:30 UTC (rev 234)
+++ pkg/zooimage/R/ZIClass.R	2012-11-30 15:24:49 UTC (rev 235)
@@ -18,14 +18,40 @@
 ## Create basically a mlearning object, but with predicted and cvpredicted added
 ## to it, and the '+other+' level added at the end of all levels
 ZIClass <- function (formula, data, method = getOption("ZI.mlearning",
-"mlRforest"), calc.vars = getOption("ZI.calcVars", calcVars), cv.k = 10,
-cv.strat = TRUE, ..., subset, na.action = na.omit)
+"mlRforest"), calc.vars = getOption("ZI.calcVars", calcVars), drop.vars = NULL,
+drop.vars.def = dropVars(), cv.k = 10, cv.strat = TRUE, ...,
+subset, na.action = na.omit)
 {	
+	## Rework calc.vars to freeze the list of variables to drop
+	## Default (minimal) calc.vars function if none is provided
+	if (!length(calc.vars)) {
+		## Create a simple calc.vars function that just drop variables
+		calc.vars <- function(x, drop.vars = NULL, drop.vars.def = dropVars()) {
+			## Eliminate variables that are not predictors... and use Id as rownames
+			Id <- x$Id
+			if (length(Id)) rownames(x) <- Id
+	
+			## Variables to drop
+			dropAll <- unique(as.character(c(drop.vars, drop.vars.def)))
+			for (dropVar in dropAll) x[[dropVar]] <- NULL
+	
+			## Return the recalculated data frame
+			x
+		}
+	}
+	
+	## Freeze data for drop.vars and drop.vars.def arguments of calc.vars
+	if (!length(drop.vars)) drop.vars <- character(0) else
+		drop.vars <- as.character(drop.vars)
+	formals(calc.vars)$drop.vars <- drop.vars
+	if (!length(drop.vars.def)) drop.vars.def <- character(0) else
+		drop.vars.def <- as.character(drop.vars.def)
+	formals(calc.vars)$drop.vars.def <- drop.vars.def
+	
 	## Check calc.vars and use it on data
-	if (length(calc.vars))
-		if (!is.function(calc.vars)) {
-			stop("'calc.vars' must be a function or NULL")
-		} else data <- calc.vars(data)
+	if (!is.function(calc.vars)) {
+		stop("'calc.vars' must be a function or NULL")
+	} else data <- calc.vars(data)
 	
 	## Train the machine learning algorithm
 	ZI.class <- mlearning(formula, data = data, method = method,
@@ -35,9 +61,11 @@
 		
 	## Add ZIClass as class of the object
 	class(ZI.class) <- c("ZIClass", class(ZI.class))
+	
+	## Save our customized calc.vars function in the object
 	attr(ZI.class, "calc.vars") <- calc.vars
 
-	## Get useful attrobutes from ZITrain
+	## Get useful attributes from ZITrain
 	attr(ZI.class, "traindir") <- attr(data, "traindir")
 	attr(ZI.class, "path") <- attr(data, "path")
 
@@ -105,24 +133,30 @@
 	## Make sure we have correct objects
 	if (!inherits(object, "ZIClass"))
 		stop("'object' must be a 'ZIClass' object")
-	if (!inherits(newdata, c("ZIDat", "data.frame")))
-		stop("'newdata' must be a 'ZIDat' or 'data.frame' object")
 	
-    class(object) <- class(object)[-1]
-	data <- as.data.frame(newdata)
+	if (!missing(newdata)) {
+		if (!inherits(newdata, c("ZIDat", "data.frame")))
+			stop("'newdata' must be a 'ZIDat' or 'data.frame' object")
+		data <- as.data.frame(newdata)
+		if (isTRUE(as.logical(calc)))
+			data <- attr(object, "calc.vars")(data)
+	}
+    
+	class(object) <- class(object)[-1]
+		
+	class.only <- isTRUE(as.logical(class.only))
 	
-	if (isTRUE(as.logical(calc)))
-		data <- attr(object, "calc.vars")(data)
+	type <- as.character(type)[1]
 	
-	class.only <- isTRUE(as.logical(class.only))
-	type <- as.character(type)[1]
 	if (class.only && type != "class") {
-		warning("with class.only == TRUE, tyep can only be 'class' and is force to it")
+		warning("with class.only == TRUE, type can only be 'class' and is force to it")
 		type <- "class"
 	}
 	
 	## Perform the prediction
-	res <- predict(object, newdata = data, ...)
+	if (missing(newdata)) {
+		res <- predict(object, ...)
+	} else res <- predict(object, newdata = data, ...)
 	
 	## Return either the prediction, or the ZIDat object with Predicted
 	## column append/replaced

Modified: pkg/zooimage/R/utilities.R
===================================================================
--- pkg/zooimage/R/utilities.R	2012-08-12 23:46:30 UTC (rev 234)
+++ pkg/zooimage/R/utilities.R	2012-11-30 15:24:49 UTC (rev 235)
@@ -94,16 +94,21 @@
 	ZIDat
 }
 
-## Default list of variables to keep
-#keepVars <- function ()
-#	c("(log)ECD", "(log)Area", "(log)Perim.", "(log)Major", "(log)Minor", "(log)Feret",
-#	  "Mean", "Median", "Mode", "Min", "Max", "StdDev", "Range", "MeanPos",
-#	  "SDNorm", "CV", "IntDen", "Transp1", "Transp2", "Skew", "Kurt",
-#	  "Circ.", "AspectRatio",  "Elongation", "Compactness", "Roundness",
-#	  "CentBoxD", "GrayCentBoxD", "CentroidsD", "(log)MeanDia", "(log)MeanFDia")
+## Default list of variables to drop
+dropVars <- function ()
+{
+	res <- try(get("ZI.dropVarsDef"), silent = TRUE)
+	if (inherits(res, "try-error"))
+		res <- getOption("ZI.dropVarsDef",
+			c("Id", "Label", "Item", "X", "Y", "XM", "YM", "BX", "BY", "Width",
+			"Height", "Angle", "XStart", "YStart", "Dil", "Predicted",
+			"Predicted2"))
+	as.character(res)
+}
 
+
 ## Calculate derived variables... default function
-calcVars <- function (x)
+calcVars <- function (x, drop.vars = NULL, drop.vars.def = dropVars())
 {	
 	## This is the calculation of derived variables
 	## Note that you can make your own version of this function for more
@@ -153,24 +158,11 @@
 	## Eliminate variables that are not predictors... and use Id as rownames
 	Id <- x$Id
 	if (length(Id)) rownames(x) <- Id
-	x$Id <- NULL
-	x$Label <- NULL
-	x$Item <- NULL
-	x$X <- NULL
-	x$Y <- NULL
-	x$XM <- NULL
-	x$YM <- NULL
-	x$BX <- NULL
-	x$BY <- NULL
-	x$Width <- NULL
-	x$Height <- NULL
-	x$Angle <- NULL
-	x$XStart <- NULL
-	x$YStart <- NULL
-	x$Dil <- NULL
-	x$Predicted <- NULL
-	x$Predicted2 <- NULL
-
+	
+	## Variables to drop
+	dropAll <- unique(as.character(c(drop.vars, drop.vars.def)))
+	for (dropVar in dropAll) x[[dropVar]] <- NULL
+	
 	## Return the recalculated data frame
 	x
 }

Modified: pkg/zooimage/man/ZIClass.Rd
===================================================================
--- pkg/zooimage/man/ZIClass.Rd	2012-08-12 23:46:30 UTC (rev 234)
+++ pkg/zooimage/man/ZIClass.Rd	2012-11-30 15:24:49 UTC (rev 235)
@@ -18,7 +18,8 @@
 
 \usage{
 ZIClass(formula, data, method = getOption("ZI.mlearning", "mlRforest"),
-    calc.vars = getOption("ZI.calcVars", calcVars), cv.k = 10, cv.strat = TRUE,
+    calc.vars = getOption("ZI.calcVars", calcVars), drop.vars = NULL,
+    drop.vars.def = dropVars(), cv.k = 10, cv.strat = TRUE,
     \dots, subset, na.action = na.omit)
 
 \method{print}{ZIClass}(x, \dots)
@@ -46,6 +47,12 @@
     with plankton). }
   \item{calc.vars}{ a function to use to calculate variables from the original
     data frame. }
+  \item{drop.vars}{ a character vector with names of variables to drop for the
+    classification, or \code{NULL} (by default) to keep them all. }
+  \item{drop.vars.def}{ a second list of variables to drop contained in a
+    character vector. That list is supposed to match the name of variables that
+    are obviously non informative and are dropped by default. It can be gathered
+    automatically using \code{dropVars()}. See \code{?calcVars} for more details. }
   \item{cv.k}{ the k times for cross-validation. }
   \item{cv.strat}{ do we use a stratified sampling for cross-validation?
     (recommended). }

Modified: pkg/zooimage/man/utilities.Rd
===================================================================
--- pkg/zooimage/man/utilities.Rd	2012-08-12 23:46:30 UTC (rev 234)
+++ pkg/zooimage/man/utilities.Rd	2012-11-30 15:24:49 UTC (rev 235)
@@ -6,6 +6,7 @@
 \alias{makeId}
 \alias{addClass}
 \alias{calcVars}
+\alias{dropVars}
 \alias{ecd}
 \alias{parseIni}
 \alias{calibrate}
@@ -29,7 +30,8 @@
 listSamples(ZIobj)
 makeId(ZIDat)
 addClass(ZIDat, ZIobj)
-calcVars(x)
+calcVars(x, drop.vars = NULL, drop.vars.def = dropVars())
+dropVars()
 
 ecd(area)
 
@@ -60,6 +62,13 @@
     'ZITest' object; most probably one of the last two for \code{addClass()}). }
   \item{ZIDat}{ a 'ZIDat' object, or a data frame with correct column labels. }
   \item{x}{ a data frame, but most probably, a 'ZIDat' object. }
+  \item{drop.vars}{ a character vector with names of variables to drop, or
+    \code{NULL} (by default) to keep them all. }
+  \item{drop.vars.def}{ a second list of variables to drop contained in a
+    character vector. That list is supposed to match the name of variables that
+    are obviously non informative and that are dropped by default. It can be
+    gathered automatically using \code{dropVars()}. That list includes Id,
+    Label, Dil, ... (see details, variables with an asterisk). }
   \item{area}{ a numerical vector with areas from which ECDs are calculated
     (Equivalent Circular Diameter, a more suitable term for 2D images than ESD,
 	Equivalent Spherical Diameter). }
@@ -80,7 +89,11 @@
   element found.
 
   The data.frame with additional columns for calculated variables with
-  \code{calcVars()}.
+  \code{calcVars()}. Variables to drop are gathered using \code{dropVars()},
+  altogether with a list provided explicitly in the \code{drop.vars =} argument.
+  The list of variable names to drop automatically and silently can be stored in
+  a variable named \code{ZI.dropVarsDef} or in
+  \code{options(ZI.dropVarsDef = ....)}.
 
   A vector of numerical values for \code{ecd()}.
 
@@ -111,6 +124,69 @@
   As these functions are not made to be directly used by end-users, We don't
   give more details here. Developers interested to use these functions are
   encouraged to look at their code in the zooimage package source!
+  
+  Here is the list of all variables you got after running the standard version
+  of \code{calcVars()} on ZIDat objects made by one of the ZooImage ImageJ
+  plugins (you can provide your own version for, e.g., calculating
+  more features):
+  
+  \tabular{lll}{
+    Variable    \tab Description                                                       \tab Origin \cr
+        Area \tab Area of the region of interest (ROI)                                 \tab ImageJ \cr
+        Mean \tab Average gray value of the ROI                                        \tab ImageJ \cr
+      StdDev \tab Standard deviation of the gray values                                \tab ImageJ \cr
+        Mode \tab Most frequent gray value within the ROI                              \tab ImageJ \cr
+         Min \tab Minimum gray value within the ROI                                    \tab ImageJ \cr
+         Max \tab Maximum gray value within the ROI                                    \tab ImageJ \cr
+          X* \tab X coordinate of the centroid of the ROI in the image                 \tab ImageJ \cr
+          Y* \tab Y coordinate of the centroid of the ROI in the image                 \tab ImageJ \cr
+         XM* \tab X coordinate of the center of mass of the ROI in the image           \tab ImageJ \cr
+         YM* \tab Y coordinate of the center of mass of the ROI in the image           \tab ImageJ \cr
+      Perim. \tab Perimeter of the ROI                                                 \tab ImageJ \cr
+         BX* \tab X coordinate of the upper left corner of the bounding rectangle (BR) \tab ImageJ \cr
+         BY* \tab Y coordinate of the upper left corner of the BR                      \tab ImageJ \cr
+      Width* \tab Width of the rectangle enclosing the ROI                             \tab ImageJ \cr
+     Height* \tab Height of the rectangle enclosing the ROI                            \tab ImageJ \cr
+       Major \tab Length of the longest axis of the ellipse fitted to the ROI          \tab ImageJ \cr
+       Minor \tab Length of the smallest axis of ellipse fitted to the ROI             \tab ImageJ \cr
+      Angle* \tab Angle between longest axis and an horizontal line                    \tab ImageJ \cr
+       Circ. \tab Circularity of the ROI                                               \tab ImageJ \cr
+       Feret \tab Longest Feret diameter                                               \tab ImageJ \cr
+      IntDen \tab Sum of the gray values within the ROI                                \tab ImageJ \cr
+      Median \tab Median value of the gray values within the ROI                       \tab ImageJ \cr
+        Skew \tab Third order moment for the gray value                                \tab ImageJ \cr
+        Kurt \tab Fourth order moment for the gray value                               \tab ImageJ \cr
+     XStart* \tab X coordinate of initial point for the outline of the ROI             \tab ImageJ \cr
+     YStart* \tab Y coordinate of initial point for the outline of the ROI             \tab ImageJ \cr
+         Id* \tab Unique identifier of the ROI (Label_Item)                            \tab zooimage \cr
+      Label* \tab Unique name of the image                                             \tab zooimage \cr
+       Item* \tab Name of the ROI                                                      \tab zooimage \cr
+         ECD \tab Equivalent circular diameter of the ROI                              \tab zooimage \cr
+        Dil* \tab Dilution coefficient to use for that ROI                             \tab zooimage \cr
+ AspectRatio \tab Aspect ratio of the ROI                                              \tab zooimage \cr
+    CentBoxD \tab Distance between the centroid and the center of the BR               \tab zooimage \cr
+GrayCentBoxD \tab Distance between the center of mass and the center of the BR         \tab zooimage \cr
+  CentroidsD \tab Distance between the centroid and the center mass                    \tab zooimage \cr
+       Range \tab Range of the gray values in the ROI                                  \tab zooimage \cr
+     MeanPos \tab Position of mean gray value in the range of gray values              \tab zooimage \cr
+      SDNorm \tab Normalized standard deviation of the gray values                     \tab zooimage \cr
+          CV \tab Coefficient of variation of gray values                              \tab zooimage \cr
+     MeanDia \tab Mean diameter calculated on Major and Minor                          \tab zooimage \cr
+    MeanFDia \tab Mean diameter calculated on Feret and Minor                          \tab zooimage \cr
+     Transp1 \tab Transparency calculated using ECD and MeanDia                        \tab zooimage \cr
+     Transp2 \tab Transparency calculated using ECD and MeanFDia                       \tab zooimage \cr
+  Elongation \tab Elongation of the ROI                                                \tab zooimage \cr
+ Compactness \tab Compactness of the ROI                                               \tab zooimage \cr
+   Roundness \tab Roundness of the ROI                                                 \tab zooimage \cr
+      Class* \tab Manual identification of the vignette for that ROI                   \tab zooimage \cr
+  Predicted* \tab Automatic identification of the vignette for that ROI                \tab zooimage \cr
+ Predicted2* \tab Second automatic identification of the vignette for that ROI         \tab zooimage
+  }
+  
+  For the origin, ImageJ = measured during image ananlysis plugin in ImageJ,
+  zooimage = calculated either during importation of data, or by
+  \code{calcVars()}. Variables whose name ends with an asterisk are dropped by
+  default.
 }
 
 \author{Philippe Grosjean <Philippe.Grosjean at umons.ac.be>}



More information about the Zooimage-commits mailing list