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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Dec 10 12:07:45 CET 2013


Author: phgrosjean
Date: 2013-12-10 12:07:44 +0100 (Tue, 10 Dec 2013)
New Revision: 244

Modified:
   pkg/zooimage/DESCRIPTION
   pkg/zooimage/NAMESPACE
   pkg/zooimage/NEWS
   pkg/zooimage/R/gui.R
   pkg/zooimage/R/guiutils.R
   pkg/zooimage/R/import.R
   pkg/zooimage/R/utilities.R
   pkg/zooimage/R/zid.R
   pkg/zooimage/R/zim.R
   pkg/zooimage/man/import.Rd
   pkg/zooimage/man/zooimage.package.Rd
Log:
Improvement in the FlowCAM importation functions

Modified: pkg/zooimage/DESCRIPTION
===================================================================
--- pkg/zooimage/DESCRIPTION	2013-12-01 14:04:02 UTC (rev 243)
+++ pkg/zooimage/DESCRIPTION	2013-12-10 11:07:44 UTC (rev 244)
@@ -1,12 +1,12 @@
 Package: zooimage
 Type: Package
 Title: Analysis of numerical zooplankton images
-Version: 3.0-4
-Date: 2013-02-12
+Version: 3.0-7
+Date: 2013-12-10
 Author: Ph. Grosjean, K. Denis & R. Francois
 Maintainer: Philippe Grosjean <Philippe.Grosjean at umons.ac.be>
 Depends: R (>= 2.14.0), svMisc (>= 0.9-67), svDialogs (>= 0.9-53), mlearning
-Imports: filehash, jpeg, png, ipred, rpart, e1071, tree, utils
+Imports: filehash, jpeg, png, tiff, utils
 Suggests: rJava, mlbench
 Description: ZooImage is a free (open source) solution for analyzing digital
 	images of zooplankton. In combination with ImageJ, a free image analysis

Modified: pkg/zooimage/NAMESPACE
===================================================================
--- pkg/zooimage/NAMESPACE	2013-12-01 14:04:02 UTC (rev 243)
+++ pkg/zooimage/NAMESPACE	2013-12-10 11:07:44 UTC (rev 244)
@@ -7,6 +7,7 @@
 import(filehash)
 import(jpeg)
 import(png)
+import(tiff)
 #import(ipred)
 #import(MASS)
 #import(randomForest)
@@ -94,6 +95,7 @@
 # Utilities
 export(addClass)
 export(calcVars)
+export(calcVarsVIS)
 export(calibrate)
 export(dropVars)
 export(ecd)
@@ -167,6 +169,11 @@
 export(startPgm)
 export(modalAssistant)
 
+# Importation
+export(readFlowCAMctx)
+export(readFlowCAMlst)
+export(importFlowCAM)
+
 # S3 methods
 S3method(template, default)
 S3method(recode, ZITrain)

Modified: pkg/zooimage/NEWS
===================================================================
--- pkg/zooimage/NEWS	2013-12-01 14:04:02 UTC (rev 243)
+++ pkg/zooimage/NEWS	2013-12-10 11:07:44 UTC (rev 244)
@@ -1,5 +1,36 @@
 = zooimage News
 
+== Changes in zooimage 3.0-7
+
+* readFlowCAMlst() can now read Visual Spreadsheet .lst file format 017 where
+  the name of the columns is explicitly provided in the header of the file.
+  
+* zimDatMakeFlowCAM() now uses readFlowCAMlst() instead of the hidden function
+  .lstRead() to avoid duplicated code.
+  
+* zidVerify() now builds automatically the _dat1.zim file from FlowCAM data if
+  it is not there, but corresponding .lst and .zim files are found in the parent
+  directory.
+
+
+== Changes in zooimage 3.0-6
+
+* zidUncompress() unzipped files in current dir., ignoring its path argument.
+  Solved now.
+  
+* zidUncompressAll() now computes full names for zidfiles argument by default.
+
+* calcVarsVIS() added. Specific function to calculate derived variables from
+  FlowCAM's Visualspreadsheet software.
+  
+* Added the extraction of RGB vignettes when importing FlowCAM data.
+
+
+== Changes in zooimage 3.0-5
+
+* Functions to import FlowCAM data directly.
+
+
 == Changes in zooimage 3.0-4
 
 * Default dropVars(): adding a few items, including 'Skew', 'Kurt' and

Modified: pkg/zooimage/R/gui.R
===================================================================
--- pkg/zooimage/R/gui.R	2013-12-01 14:04:02 UTC (rev 243)
+++ pkg/zooimage/R/gui.R	2013-12-10 11:07:44 UTC (rev 244)
@@ -433,7 +433,7 @@
 				system.file("imagej", "ij.jar", package = "zooimage"),
 				system.file("imagej", "plugins", "_zooimage.jar",
 					package = "zooimage"), ij.plugin,
-					tools:::file_path_as_absolute(zimfile))
+					tools::file_path_as_absolute(zimfile))
 			return(invisible(system(cmd, intern = TRUE)))
 		}		
 		## TODO: update a progress bar from ImageJ (using sockets ?)

Modified: pkg/zooimage/R/guiutils.R
===================================================================
--- pkg/zooimage/R/guiutils.R	2013-12-01 14:04:02 UTC (rev 243)
+++ pkg/zooimage/R/guiutils.R	2013-12-10 11:07:44 UTC (rev 244)
@@ -138,7 +138,7 @@
 {
 	if (isWin()) {
 		startPgm("ImageViewer", sprintf('"%s"',
-			tools:::file_path_as_absolute(dir)))
+			tools::file_path_as_absolute(dir)))
 	} else if (isMac()) {
 		cmd <- sprintf('/Applications/Utilities/XnViewMP.app/Contents/MacOS/xnview "%s"',
 			dir)

Modified: pkg/zooimage/R/import.R
===================================================================
--- pkg/zooimage/R/import.R	2013-12-01 14:04:02 UTC (rev 243)
+++ pkg/zooimage/R/import.R	2013-12-10 11:07:44 UTC (rev 244)
@@ -76,12 +76,12 @@
 	V	
 }
 
-ctxFile <- "/Users/phgrosjean/Desktop/Intercalibration/BE.ArMix.2009-04-29.300A4X_01/BE.ArMix.2009-04-29.300A4X_01.ctx"
-readFlowCAMctx(ctxFile)
-
+## Examples
+#ctxFile <- "/Users/phgrosjean/Desktop/Intercalibration/BE.ArMix.2009-04-29.300A4X_01/BE.ArMix.2009-04-29.300A4X_01.ctx"
+#readFlowCAMctx(ctxFile)
 ## A 1.5.14 file
-ctxFile1 <- "/Users/phgrosjean/Documents/Pgm/ZooPhytoImage_1.2-1-examples/FlowCAM-example-FIT-VIS/143-144526.ctx"
-readFlowCAMctx(ctxFile1)
+#ctxFile1 <- "/Users/phgrosjean/Documents/Pgm/ZooPhytoImage_1.2-1-examples/FlowCAM-example-FIT-VIS/143-144526.ctx"
+#readFlowCAMctx(ctxFile1)
 
 ## Read a flowCAM .lst file
 readFlowCAMlst <- function (lst, skip = 2, read.ctx = TRUE)
@@ -98,59 +98,118 @@
 	}
 	read.ctx <- isTRUE(as.logical(read.ctx))
 	
-	## Determine the version of the FlowCAM's table according to number of cols
-    ncol <- length(
-		read.table(lst, header = FALSE, sep = ":", dec = ".", skip = 2, nrows = 1))
+	## For format 017 we have now column names hardcoded
+	header <- scan(lst, what = character(), nlines = 2L, quiet = TRUE)
+	if (as.integer(header[1]) >= 17 && substr(header[2], 1, 10) == "num-fields") {
+		## Format >= 17. Columns names are hardcoded!
+		nfields <- as.integer(strsplit(header[2], "|", fixed = TRUE)[[1]][2])
+		if (!length(nfields) || is.na(nfields) || nfields < 44)
+			stop("Unrecognized .lst file format: number of fields is ", nfields)
+		skip <- nfields + 2
+		## Read column header information
+		hcol <- scan(lst, what = character(), sep = "|", skip = 2L,
+			nlines = nfields, quiet = TRUE)
+		if (length(hcol) != nfields * 2)
+			stop("Unrecognized .lst file format: incorrect header")
+		hcol <- matrix(hcol, ncol = 2, byrow = TRUE)
+		cnames <- hcol[, 1]
+		## Make sure all names start with FIT_ and are Capitalized
+		capital <- function(x) {
+			s <- strsplit(x, "_")
+			sapply(s, function (x) paste(toupper(substring(x, 1, 1)),
+				substring(x, 2), sep = "", collapse = "_"))
+		}
+		cnames <- paste("FIT", capital(cnames), sep = "_")
+		## Special replacements
+		cnames <- sub("Abd", "ABD", cnames)
+		cnames <- sub("Esd", "ESD", cnames)
+		cnames <- sub("FIT_Ch([1-9])_Width", "FIT_Ch\\1_TOF", cnames)
+		## We need to replace names by their zooimage equivalent
+		cnames[cnames == "FIT_Id"] <- "Id" # The only one not starting woth FIT_
+		cnames[cnames ==  "FIT_ABD_Area"] <-  "FIT_Area_ABD"
+		cnames[cnames == "FIT_ABD_Diameter"] <- "FIT_Diameter_ABD"
+		cnames[cnames == "FIT_ESD_Diameter"] <- "FIT_Diameter_ESD"
+		cnames[cnames == "FIT_Raw_Perimeter"] <- "FIT_Raw_Perim"
+		cnames[cnames == "FIT_Raw_Convex_Perimeter"] <- "FIT_Raw_Convex_Perim"
+		cnames[cnames == "FIT_Collage_File"] <- "FIT_Filename"
+		cnames[cnames == "FIT_Timestamp"] <- "FIT_Timestamp1"
+		cnames[cnames == "FIT_Image_X"] <- "FIT_SaveX"
+		cnames[cnames == "FIT_Image_Y"] <- "FIT_SaveY"
+		cnames[cnames == "FIT_Image_W"] <- "FIT_PixelW"
+		cnames[cnames == "FIT_Image_H"] <- "FIT_PixelH"
+		cnames[cnames == "FIT_Src_X"] <- "FIT_CaptureX"
+		cnames[cnames == "FIT_Src_Y"] <- "FIT_CaptureY"
+		cnames[cnames == "FIT_Src_Image"] <- "FIT_Source_Image"
+		cnames[cnames == "FIT_Cal_Image"] <- "FIT_Calibration_Image"
+		## Note: in comparison to old format, we have in addition:
+		#"FIT_Camera", "FIT_Fringe_Size", "FIT_Circle_Fit", "FIT_Ch1_Area",            
+        #"FIT_Ch2_Area", "FIT_Ch3_Area"    
+		
+		## Read the data in
+		tab <- read.table(lst, header = FALSE, sep = "|", dec = ".", 
+            skip = skip, col.names = cnames)
+		## Add missing fields from the previous versions
+		tab$FIT_Ch4_Peak <- NA
+		tab$FIT_Ch4_TOF <- NA
+		tab$FIT_High_U32 <- NA
+        tab$FIT_Low_U32 <- NA
+        tab$FIT_Total <- NA
+		tab$FIT_Timestamp2 <- as.character(NA)
+	} else { # Older format. We have to guess column names!	
+		## Determine version of the FlowCAM's table according to number of cols
+		ncol <- length(read.table(lst, header = FALSE, sep = ":", dec = ".",
+			skip = skip, nrows = 1))
     
-	## Read .lst data
-	## TODO: if export file exists, verify column names here (.csv file)
-	if (ncol == 44) {  # This should be FlowCAM II
-        tab <- read.table(lst, header = FALSE, sep = ":", dec = '.', skip = skip,
-			col.names = c("Id", "FIT_Cal_Const", "FIT_Raw_Area",
-			"FIT_Raw_Feret_Max", "FIT_Raw_Feret_Min", "FIT_Raw_Feret_Mean",
-			"FIT_Raw_Perim", "FIT_Raw_Convex_Perim", "FIT_Area_ABD",
-			"FIT_Diameter_ABD", "FIT_Length", "FIT_Width", "FIT_Diameter_ESD",
-			"FIT_Perimeter", "FIT_Convex_Perimeter", "FIT_Intensity",
-			"FIT_Sigma_Intensity", "FIT_Compactness", "FIT_Elongation",
-			"FIT_Sum_Intensity", "FIT_Roughness", "FIT_Feret_Max_Angle",
-			"FIT_Avg_Red", "FIT_Avg_Green", "FIT_Avg_Blue", "FIT_PPC",
-			"FIT_Ch1_Peak", "FIT_Ch1_TOF", "FIT_Ch2_Peak", "FIT_Ch2_TOF",
-			"FIT_Ch3_Peak", "FIT_Ch3_TOF", "FIT_Ch4_Peak", "FIT_Ch4_TOF",
-			"FIT_Filename", "FIT_SaveX", "FIT_SaveY", "FIT_PixelW",
-			"FIT_PixelH", "FIT_CaptureX", "FIT_CaptureY", "FIT_High_U32",
-			"FIT_Low_U32", "FIT_Total"))
+		## Read .lst data
+		## TODO: if export file exists, verify column names here (.csv file)
+		if (ncol == 44) {  # This should be FlowCAM II
+			tab <- read.table(lst, header = FALSE, sep = ":", dec = '.',
+				skip = skip, col.names = c("Id", "FIT_Cal_Const", "FIT_Raw_Area",
+				"FIT_Raw_Feret_Max", "FIT_Raw_Feret_Min", "FIT_Raw_Feret_Mean",
+				"FIT_Raw_Perim", "FIT_Raw_Convex_Perim", "FIT_Area_ABD",
+				"FIT_Diameter_ABD", "FIT_Length", "FIT_Width", "FIT_Diameter_ESD",
+				"FIT_Perimeter", "FIT_Convex_Perimeter", "FIT_Intensity",
+				"FIT_Sigma_Intensity", "FIT_Compactness", "FIT_Elongation",
+				"FIT_Sum_Intensity", "FIT_Roughness", "FIT_Feret_Max_Angle",
+				"FIT_Avg_Red", "FIT_Avg_Green", "FIT_Avg_Blue", "FIT_PPC",
+				"FIT_Ch1_Peak", "FIT_Ch1_TOF", "FIT_Ch2_Peak", "FIT_Ch2_TOF",
+				"FIT_Ch3_Peak", "FIT_Ch3_TOF", "FIT_Ch4_Peak", "FIT_Ch4_TOF",
+				"FIT_Filename", "FIT_SaveX", "FIT_SaveY", "FIT_PixelW",
+				"FIT_PixelH", "FIT_CaptureX", "FIT_CaptureY", "FIT_High_U32",
+				"FIT_Low_U32", "FIT_Total"))
         
-        ## Add columns present in .lst from FlowCAM III (same table for all)
-        tab$FIT_Feret_Min_Angle <- NA
-        tab$FIT_Edge_Gradient <- NA
-        tab$FIT_Timestamp1 <- NA
-        tab$FIT_Timestamp2 <- NA
-        tab$FIT_Source_Image <- NA
-        tab$FIT_Calibration_Image <- NA
+			## Add columns present in .lst from FlowCAM III (same table for all)
+			tab$FIT_Feret_Min_Angle <- NA
+			tab$FIT_Edge_Gradient <- NA
+			tab$FIT_Timestamp1 <- NA
+			tab$FIT_Timestamp2 <- NA
+			tab$FIT_Source_Image <- NA
+			tab$FIT_Calibration_Image <- NA
     
-	} else if (ncol == 47) { # This should be FlowCAM III
-        tab <- read.table(lst, header = FALSE, sep = ":", dec = '.', skip = skip,
-			col.names = c("Id", "FIT_Cal_Const", "FIT_Raw_Area",
-			"FIT_Raw_Feret_Max", "FIT_Raw_Feret_Min", "FIT_Raw_Feret_Mean",
-			"FIT_Raw_Perim", "FIT_Raw_Convex_Perim", "FIT_Area_ABD",
-			"FIT_Diameter_ABD", "FIT_Length", "FIT_Width", "FIT_Diameter_ESD",
-			"FIT_Perimeter", "FIT_Convex_Perimeter", "FIT_Intensity",
-			"FIT_Sigma_Intensity", "FIT_Compactness", "FIT_Elongation",
-			"FIT_Sum_Intensity", "FIT_Roughness", "FIT_Feret_Max_Angle",
-			"FIT_Feret_Min_Angle", "FIT_Avg_Red", "FIT_Avg_Green",
-			"FIT_Avg_Blue", "FIT_PPC", "FIT_Ch1_Peak", "FIT_Ch1_TOF",
-			"FIT_Ch2_Peak", "FIT_Ch2_TOF", "FIT_Ch3_Peak", "FIT_Ch3_TOF",
-			"FIT_Ch4_Peak", "FIT_Ch4_TOF", "FIT_Filename", "FIT_SaveX",
-			"FIT_SaveY", "FIT_PixelW", "FIT_PixelH", "FIT_CaptureX",
-			"FIT_CaptureY", "FIT_Edge_Gradient", "FIT_Timestamp1",
-			"FIT_Timestamp2", "FIT_Source_Image", "FIT_Calibration_Image"))
+		} else if (ncol == 47) { # This should be FlowCAM III
+			tab <- read.table(lst, header = FALSE, sep = ":", dec = '.',
+				skip = skip, col.names = c("Id", "FIT_Cal_Const", "FIT_Raw_Area",
+				"FIT_Raw_Feret_Max", "FIT_Raw_Feret_Min", "FIT_Raw_Feret_Mean",
+				"FIT_Raw_Perim", "FIT_Raw_Convex_Perim", "FIT_Area_ABD",
+				"FIT_Diameter_ABD", "FIT_Length", "FIT_Width", "FIT_Diameter_ESD",
+				"FIT_Perimeter", "FIT_Convex_Perimeter", "FIT_Intensity",
+				"FIT_Sigma_Intensity", "FIT_Compactness", "FIT_Elongation",
+				"FIT_Sum_Intensity", "FIT_Roughness", "FIT_Feret_Max_Angle",
+				"FIT_Feret_Min_Angle", "FIT_Avg_Red", "FIT_Avg_Green",
+				"FIT_Avg_Blue", "FIT_PPC", "FIT_Ch1_Peak", "FIT_Ch1_TOF",
+				"FIT_Ch2_Peak", "FIT_Ch2_TOF", "FIT_Ch3_Peak", "FIT_Ch3_TOF",
+				"FIT_Ch4_Peak", "FIT_Ch4_TOF", "FIT_Filename", "FIT_SaveX",
+				"FIT_SaveY", "FIT_PixelW", "FIT_PixelH", "FIT_CaptureX",
+				"FIT_CaptureY", "FIT_Edge_Gradient", "FIT_Timestamp1",
+				"FIT_Timestamp2", "FIT_Source_Image", "FIT_Calibration_Image"))
         
-        ## Add columns present in list files from FlowCAM II
-        tab$FIT_High_U32 <- NA
-        tab$FIT_Low_U32 <- NA
-        tab$FIT_Total <- NA
+			## Add columns present in list files from FlowCAM II
+			tab$FIT_High_U32 <- NA
+			tab$FIT_Low_U32 <- NA
+			tab$FIT_Total <- NA
 		
-    } else stop("Unrecognized FlowCAM format") # TODO: adapt for the new soft
+		} else stop("Unrecognized FlowCAM format") # TODO: adapt for the new soft
+	}
 	
 	## New variables calculation (present in export .csv from the FlowCAM)
 	## Code already checked
@@ -174,14 +233,14 @@
     tab
 }
 
-lstFile <- "/Users/phgrosjean/Desktop/Intercalibration/BE.ArMix.2009-04-29.300A4X_01/BE.ArMix.2009-04-29.300A4X_01.lst"
-res <- readFlowCAMlst(lstFile)
+## Example
+#lstFile <- "/Users/phgrosjean/Desktop/Intercalibration/BE.ArMix.2009-04-29.300A4X_01/BE.ArMix.2009-04-29.300A4X_01.lst"
+#res <- readFlowCAMlst(lstFile)
+#lstFile1 <- "/Users/phgrosjean/Documents/Pgm/ZooPhytoImage_1.2-1-examples/FlowCAM-example-FIT-VIS/143-144526.lst"
+#res1 <- readFlowCAMlst(lstFile1)
 
-lstFile1 <- "/Users/phgrosjean/Documents/Pgm/ZooPhytoImage_1.2-1-examples/FlowCAM-example-FIT-VIS/143-144526.lst"
-res1 <- readFlowCAMlst(lstFile1)
-
 ## Temporary name!
-importFlowCAM <- function (lst)
+importFlowCAM <- function (lst, rgb.vigs = TRUE)
 {
 	dat <- readFlowCAMlst(lst, skip = 2, read.ctx = TRUE)
 	## Check results
@@ -194,8 +253,8 @@
 	## TODO...
 	
 	## ImportVignettes
-	require(tiff)
-	require(png)
+	#require(tiff)
+	#require(png)
 	
 	## List all tiff files in the directory (but exclude masks with _bin.tif)
 	sampledir <- dirname(lst)
@@ -203,6 +262,7 @@
 	on.exit(setwd(odir))
 	
 	## Make sure zidbdir exists and is empty
+	## TODO: use a fresh dir, or erase existing one with user's acceptation
 	zidbdir <- file.path(dirname(sampledir), "_import", basename(sampledir))
 	if (file.exists(zidbdir) && dir(zidbdir) != 0)
 		stop("The destination dir already exists and is not empty!")
@@ -221,8 +281,20 @@
 		
 	## Read all background calibration images into a list
 	cals <- list()
-	for (i in 1:length(calFiles))
+	for (i in 1:length(calFiles)) {
 		cals[[i]] <- readTIFF(source = calFiles[i])
+		## If the image is RGB, we got three dimensions to reduce to two
+		cdim <- dim(cals[[i]])
+		if (length(cdim) == 3 && cdim[3] == 3) {
+			## Calculate the CIE 1931 linear luminance Y as grayscale
+			## Y = 0.2126 R + 0.7152 G + 0.0722 B
+			cals[[i]] <- 0.2126 * cals[[i]][, , 1] + 0.7152 * cals[[i]][, , 2] +
+				0.0722 * cals[[i]][, , 3]
+		}
+		if (length(dim(cals[[i]])) != 2)
+			stop("unrecognized calibration image type; ",
+				"cannot convert it to 8-bit grayscale")	
+	}
 	
 	## Read collages one by one and extract vignettes from them,
 	## using information gathered into dat
@@ -241,18 +313,34 @@
 	
 	## Determine best gray level for background after substraction
 	gray <- attr(dat, "FlowCAM.metadata")$CaptureParameters$ThresholdLight
-	if (!length(gray)) gray <- 0
-	gray <- max(gray, 40)/255	# Target something like 40
+	if (!length(gray)) {
+		warning("Unknown threshold gray level; using 40")
+		gray <- 40 # Target something like 40
+	}
+	gray <- gray / 255
+	## Threshold = 2 * gray, since we add it once while subtracting background
+	threshold <- 1 - 2 * gray
 	
 	## Proceed with each vignette
 	for (i in 1:nrow(dat1)) {
 		d <- dat1[i, ]
 		## Do we need to load the next collage?
-		if (d$FIT_Filename != colFile) {
+		if (as.character(d$FIT_Filename) != colFile) {
 			filename <- as.character(d$FIT_Filename)
 			collage <- readTIFF(source = filename)
 			colFile <- d$FIT_Filename
 			colFiles <- colFiles[colFiles != filename]
+			## If the image is RGB, we got three dimensions to reduce to two
+			cdim <- dim(collage)
+			if (length(cdim) == 3 && cdim[3] == 3) {
+				## Calculate the CIE 1931 linear luminance Y as grayscale
+				## Y = 0.2126 R + 0.7152 G + 0.0722 B
+				collage <- 0.2126 * collage[, , 1] + 0.7152 * collage[, , 2] +
+					0.0722 * collage[, , 3]
+			}
+			if (length(dim(collage)) != 2)
+				stop("unrecognized collage image type; ",
+					"cannot convert it to 8-bit grayscale")
 		}
 		
 		## Get coordinates of the vignette in that collage
@@ -264,6 +352,8 @@
 			
 		## Extract the vignette and corresponding background from the collage
 		vig <- crop(collage, colCoords)
+		## If  FIT_Calibration_Image is NA, use first one => TODO: check this!
+		if (is.na(d$FIT_Calibration_Image)) d$FIT_Calibration_Image <- 1
 		back <- crop(cals[[d$FIT_Calibration_Image]], calCoords)
 		
 		## Substract background and save vignette
@@ -271,6 +361,17 @@
 		vig2[vig2 > 1] <- 1
 		vig2[vig2 < 0] <- 0
 		
+		if (isTRUE(rgb.vigs)) {
+			## Calculate mask
+			mask <- matrix(1, nrow = nrow(vig2), ncol = ncol(vig2))
+			mask[vig2 > threshold] <- 0
+			## Do we need to fill holes?
+			## TODO...
+			
+			## Combine grayscales and mask into a RGB image
+			vig2 <- structure(c(vig2, vig2, mask), dim = c(dim(vig2), 3))
+		}
+		
 		## Write this vignette
 		vigFile <- file.path(zidbdir,
 			sub("\\.tif$", paste0("_", i, ".png"), filename))
@@ -279,13 +380,14 @@
 	
 	## Create zidb
 	## TODO...
+	dat
 }
 
-## Test versin 2.2.1
-lstFile <- "/Users/phgrosjean/Desktop/Intercalibration/BE.ArMix.2009-04-29.300A4X_01/BE.ArMix.2009-04-29.300A4X_01.lst"
-res <- importFlowCAM(lstFile)
-
+## Example
+## Test version 2.2.1
+#lstFile <- "/Users/phgrosjean/Desktop/Intercalibration/BE.ArMix.2009-04-29.300A4X_01/BE.ArMix.2009-04-29.300A4X_01.lst"
+#res <- importFlowCAM(lstFile)
 ## Test version 1.5.14
 ## TODO: This does not work (incorrect number of dimensions => imports images as an array?)
-lstFile1 <- "/Users/phgrosjean/Documents/Pgm/ZooPhytoImage_1.2-1-examples/FlowCAM-example-FIT-VIS/143-144526.lst"
-res1 <- importFlowCAM(lstFile1)
+#lstFile1 <- "/Users/phgrosjean/Documents/Pgm/ZooPhytoImage_1.2-1-examples/FlowCAM-example-FIT-VIS/143-144526.lst"
+#res1 <- importFlowCAM(lstFile1)

Modified: pkg/zooimage/R/utilities.R
===================================================================
--- pkg/zooimage/R/utilities.R	2013-12-01 14:04:02 UTC (rev 243)
+++ pkg/zooimage/R/utilities.R	2013-12-10 11:07:44 UTC (rev 244)
@@ -181,6 +181,47 @@
 	x
 }
 
+## Calculate derived variables... FlowCAM's Visual Spreadsheet
+calcVarsVIS <- function (x, drop.vars = NULL, drop.vars.def = dropVars()) 
+{
+    ## A small hack to correct some 0 (which can be problematic in further calcs)
+	noZero <- function(x) {
+        x[x == 0] <- 1e-09
+        x
+    }
+	
+	## Euclidean distance between two points
+	distance <- function (x, y)
+		sqrt(x^2 + y^2)
+	
+    x$FIT_Area_ABD <- noZero(x$FIT_Area_ABD)
+    x$FIT_Perimeter <- noZero(x$FIT_Perimeter)
+    x$FIT_Length <- noZero(x$FIT_Length)
+    x$FIT_Width <- noZero(x$FIT_Width)
+    x$ARFeret <- x$FIT_Width/x$FIT_Length
+    x$EdgeRange <- abs(x$FIT_Intensity - x$FIT_Edge_Gradient)
+    x$CV <- x$FIT_Sigma_Intensity/x$FIT_Intensity * 100
+    x$MeanFDia <- (x$FIT_Length + x$FIT_Width) / 2
+    x$Transp2 <- 1 - (x$FIT_Diameter_ABD/x$MeanFDia)
+    x$Transp2[x$Transp2 < 0] <- 0
+    x$FeretRoundness <- 4 * x$FIT_Area_ABD/(pi * sqrt(x$FIT_Length))
+    x$Circ. <- 4 * pi * x$FIT_Area_ABD / sqrt(x$FIT_Perimeter) # ImageJ calculation
+    x$EdgeCV <- x$FIT_Sigma_Intensity/x$FIT_Edge_Gradient * 100
+    x$EdgeSDNorm <- x$FIT_Intensity/x$EdgeRange
+    x$Perim_Ratio <- x$FIT_Convex_Perimeter / x$FIT_Perimeter 
+    
+	## 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
+}
+
 ## Calculate equivalent circular diameter (similar to equivalent spherical
 ## diameter, but for 2D images)
 ecd <- function (area)

Modified: pkg/zooimage/R/zid.R
===================================================================
--- pkg/zooimage/R/zid.R	2013-12-01 14:04:02 UTC (rev 243)
+++ pkg/zooimage/R/zid.R	2013-12-10 11:07:44 UTC (rev 244)
@@ -30,8 +30,30 @@
 	## Check the list of _dat1.zim
 	dat1files <- zimDatList(zidir)
 	if (!length(dat1files)) {
-		warning("no '_dat1.zim' file!")
-		return(invisible(FALSE))
+		## Special case for the FlowCAM where the _dat1.zim file is not created
+		## automatically, but all data are there to do so
+		lstfile <- paste(zidir, "lst", sep = ".")
+		zimfile <- paste(zidir, "zim", sep = ".")
+		if (file.exists(lstfile)) {
+			if (!file.exists(zimfile)) {
+				warning("FlowCAM data with no '_dat1.zim' file and no '.zim' file to create it")
+				return(invisible(FALSE))
+			}
+			## Try to create the _dat1.zim file now
+			res <- zimDatMakeFlowCAM(zimfile)
+			if (!res) {
+				warning("cannot create the '_dat1.zim' file from FlowCAM data")
+				return(invisible(FALSE))
+			}
+			dat1files <- zimDatList(zidir)
+			if (!length(dat1files)) {
+				warning("impossible to create '_dat1.zim' file!")
+				return(invisible(FALSE))
+			}
+		} else {
+			warning("no '_dat1.zim' file!")
+			return(invisible(FALSE))
+		}
 	}
 	
     ## Check the content of all these "_dat1.zim" files 
@@ -284,7 +306,7 @@
 	message("Unzipping '", zidfile, "' ...")
 	
 	## Uncompress it
-	if (!length(tryCatch(unzip(zidfile, overwrite = FALSE, exdir = "."),
+	if (!length(tryCatch(unzip(zidfile, overwrite = FALSE, exdir = path),
 			error = function (e) warning(e),
 			warning = function (w) return()))) {
 		message("    ... not done!")
@@ -299,8 +321,9 @@
 }
 
 ## Uncompress all .zid files in the 'path.extract' directory
-zidUncompressAll <- function (path = ".", zidfiles = zidList(path),
-path.extract = path, skip.existing.dirs = TRUE, delete.source = FALSE)
+zidUncompressAll <- function (path = ".", zidfiles = zidList(path,
+full.names = TRUE), path.extract = path, skip.existing.dirs = TRUE,
+delete.source = FALSE)
 {	
 	## Initial checks
 	if (!length(zidfiles)) {
@@ -547,7 +570,7 @@
 			tmpd <- tempdir()
 			unzip(zidfile, file, exdir = tmpd, overwrite = TRUE,
 				junkpaths = TRUE)
-			res <- file.path(tmpd, file)
+			res <- file.path(tmpd, basename(file))
 			if (file.exists(res)) res else NULL
 		}		
 		rdata <- zidExtract(rdata, zidfile)

Modified: pkg/zooimage/R/zim.R
===================================================================
--- pkg/zooimage/R/zim.R	2013-12-01 14:04:02 UTC (rev 243)
+++ pkg/zooimage/R/zim.R	2013-12-10 11:07:44 UTC (rev 244)
@@ -1,4 +1,4 @@
-## Copyright (c) 2004-2012, Ph. Grosjean <phgrosjean at sciviews.org>
+## Copyright (c) 2004-2013, Ph. Grosjean <phgrosjean at sciviews.org>
 ##
 ## This file is part of ZooImage
 ##
@@ -450,8 +450,10 @@
 
 	## Read list file
 	## Read visual spreadsheet data (from the FlowCAM)
-	visdata <- .lstRead(file.path(zidir,
-		paste(basename(zidir), "lst", sep = ".")), skip = 2)
+	#visdata <- .lstRead(file.path(zidir,
+	#	paste(basename(zidir), "lst", sep = ".")), skip = 2)
+	lstfile <- sub("\\.zim$", ".lst", zimfile)
+    visdata <- readFlowCAMlst(lstfile, skip = 2, read.ctx = FALSE)
 	
 	## Read ImageJ results (from FITVis)
 	fitdata <- read.table(file.path(zidir, "results.csv"),
@@ -461,15 +463,16 @@
 	alldata <- cbind(visdata, fitdata)
 	
 	## Add Label columns
-	alldata$Label <- rep(basename(zidir), nrow(alldata))
+	sample <- sub("\\.zim$", "", basename(zimfile))
+	alldata$Label <- rep(sample, nrow(alldata))
 	## Transform Id column in !Item column
 	names(alldata)[grep("Id", names(alldata))] <- "!Item"
 	## Select only useful columns
 	alldata$FIT_Filename <- NULL
 	
 	## Create _dat1.zim file
-	zidatfile <- file.path(zidir, basename(zidir),
-		sub("\\.zim$", "_dat1.zim", basename(zimfile)))
+	zidatfile <- file.path(zidir, sample,
+		paste(sample, "dat1.zim", sep = "_"))
 	if (!file.exists(dirname(zidatfile))) {
 		warning("directory ", dirname(zidatfile), " does not exist")
 		return(invisible(FALSE))
@@ -478,8 +481,9 @@
 	
 	## Add table of measurements at the end
 	cat("\n[Data]\n", file = zidatfile, append = TRUE)
-	write.table(alldata, file = zidatfile, append = TRUE, quote = FALSE,
-		sep = "\t", col.names = TRUE, row.names = FALSE, dec = ".")
+	suppressWarnings(write.table(alldata, file = zidatfile, append = TRUE,
+		quote = FALSE, sep = "\t", col.names = TRUE, row.names = FALSE,
+		dec = ".")) # In R 3.0.2: "appending column names to file" warning
 	
 	invisible(TRUE)
 }
@@ -516,81 +520,81 @@
 
 ## FlowCAM special treatment because the plugin doesn't export dat1.zim!
 ## Read list file
-## TODO: avoid duplicated code between versions
-.lstRead <- function (lstfile, skip = 2)
-{
-	## Determine the version of the FlowCAM
-	ncol <- length(read.table(lstfile, header = FALSE, sep = ":", dec = ".",
-		skip = skip, nrows = 1))
-	if (ncol <= 44) {
-		## FlowCAM II with 44 columns
-		## Read the table
-		tab <- read.table(lstfile, header = FALSE, sep = ":", dec = '.',
-			col.names = c("Id", "FIT_Cal_Const", "FIT_Raw_Area",
-				"FIT_Raw_Feret_Max", "FIT_Raw_Feret_Min", "FIT_Raw_Feret_Mean",
-				"FIT_Raw_Perim", "FIT_Raw_Convex_Perim", "FIT_Area_ABD",
-				"FIT_Diameter_ABD", "FIT_Length", "FIT_Width",
-				"FIT_Diameter_ESD", "FIT_Perimeter", "FIT_Convex_Perimeter",
-				"FIT_Intensity", "FIT_Sigma_Intensity", "FIT_Compactness",
-				"FIT_Elongation", "FIT_Sum_Intensity", "FIT_Roughness",
-				"FIT_Feret_Max_Angle", "FIT_Avg_Red", "FIT_Avg_Green",
-				"FIT_Avg_Blue", "FIT_PPC", "FIT_Ch1_Peak", "FIT_Ch1_TOF",
-				"FIT_Ch2_Peak", "FIT_Ch2_TOF", "FIT_Ch3_Peak", "FIT_Ch3_TOF",
-				"FIT_Ch4_Peak", "FIT_Ch4_TOF", "FIT_Filename", "FIT_SaveX",
-				"FIT_SaveY", "FIT_PixelW", "FIT_PixelH", "FIT_CaptureX",
-				"FIT_CaptureY", "FIT_High_U32", "FIT_Low_U32", "FIT_Total"),
-			skip = skip)
-		## Add columns present in list files from FlowCAM III
-		tab$FIT_Feret_Min_Angle <- NA
-		tab$FIT_Edge_Gradient <- NA
-		tab$FIT_Timestamp1 <- NA
-		tab$FIT_Timestamp2 <- NA
-		tab$FIT_Source_Image <- NA
-		tab$FIT_Calibration_Image <- NA
-		tab$FIT_Ch2_Ch1_Ratio <- tab$FIT_Ch2_Peak / tab$FIT_Ch1_Peak
-		## New variables calc (present in dataexport.csv from the FlowCAM)
-		tab$FIT_Volume_ABD <- (4/3) * pi * (tab$FIT_Diameter_ABD/2)^3
-		tab$FIT_Volume_ESD <- (4/3) * pi * (tab$FIT_Diameter_ESD/2)^3
-		tab$FIT_Aspect_Ratio <- tab$FIT_Width / tab$FIT_Length
-		tab$FIT_Transparency <- 1 - (tab$FIT_Diameter_ABD/tab$FIT_Diameter_ESD)
-		tab$FIT_Red_Green_Ratio <- tab$FIT_Avg_Red / tab$FIT_Avg_Green
-		tab$FIT_Blue_Green_Ratio <- tab$FIT_Avg_Blue / tab$FIT_Avg_Green
-		tab$FIT_Red_Blue_Ratio <- tab$FIT_Avg_Red / tab$FIT_Avg_Blue
-	} else { # FlowCAM III with 47 columns
-		## Read the table
-		tab <- read.table(lstfile, header = FALSE, sep = ":", dec = '.',
-			col.names = c("Id", "FIT_Cal_Const", "FIT_Raw_Area",
-				"FIT_Raw_Feret_Max", "FIT_Raw_Feret_Min", "FIT_Raw_Feret_Mean",
-				"FIT_Raw_Perim", "FIT_Raw_Convex_Perim", "FIT_Area_ABD",
-				"FIT_Diameter_ABD", "FIT_Length", "FIT_Width",
-				"FIT_Diameter_ESD", "FIT_Perimeter", "FIT_Convex_Perimeter",
-				"FIT_Intensity", "FIT_Sigma_Intensity", "FIT_Compactness",
-				"FIT_Elongation", "FIT_Sum_Intensity", "FIT_Roughness",
-				"FIT_Feret_Max_Angle", "FIT_Feret_Min_Angle", "FIT_Avg_Red",
-				"FIT_Avg_Green", "FIT_Avg_Blue", "FIT_PPC", "FIT_Ch1_Peak",
-				"FIT_Ch1_TOF", "FIT_Ch2_Peak", "FIT_Ch2_TOF", "FIT_Ch3_Peak",
-				"FIT_Ch3_TOF", "FIT_Ch4_Peak", "FIT_Ch4_TOF", "FIT_Filename",
-				"FIT_SaveX", "FIT_SaveY", "FIT_PixelW", "FIT_PixelH",
-				"FIT_CaptureX", "FIT_CaptureY", "FIT_Edge_Gradient",
-				"FIT_Timestamp1", "FIT_Timestamp2", "FIT_Source_Image",
-				"FIT_Calibration_Image"),
-			skip = skip)
-		## Add columns present in list files from FlowCAM II
-		tab$FIT_High_U32 <- NA
-		tab$FIT_Low_U32 <- NA
-		tab$FIT_Total <- NA
-		## New variables calcul (present in dataexport.csv from the FlowCAM)
-		tab$FIT_Volume_ABD <- (4/3) * pi * (tab$FIT_Diameter_ABD/2)^3
-		tab$FIT_Volume_ESD <- (4/3) * pi * (tab$FIT_Diameter_ESD/2)^3
-		tab$FIT_Aspect_Ratio <- tab$FIT_Width / tab$FIT_Length
-		tab$FIT_Transparency <- 1 - (tab$FIT_Diameter_ABD/tab$FIT_Diameter_ESD)
-		tab$FIT_Red_Green_Ratio <- tab$FIT_Avg_Red / tab$FIT_Avg_Green
-		tab$FIT_Blue_Green_Ratio <- tab$FIT_Avg_Blue / tab$FIT_Avg_Green
-		tab$FIT_Red_Blue_Ratio <- tab$FIT_Avg_Red / tab$FIT_Avg_Blue
-		tab$FIT_Ch2_Ch1_Ratio <- tab$FIT_Ch2_Peak / tab$FIT_Ch1_Peak
-	}
-	tab
-}
+## TODO: eliminate this. Code is now in readFlowCAMlst() function!
+#.lstRead <- function (lstfile, skip = 2)
+#{
+#	## Determine the version of the FlowCAM
+#	ncol <- length(read.table(lstfile, header = FALSE, sep = ":", dec = ".",
+#		skip = skip, nrows = 1))
+#	if (ncol <= 44) {
+#		## FlowCAM II with 44 columns
+#		## Read the table
+#		tab <- read.table(lstfile, header = FALSE, sep = ":", dec = '.',
+#			col.names = c("Id", "FIT_Cal_Const", "FIT_Raw_Area",
+#				"FIT_Raw_Feret_Max", "FIT_Raw_Feret_Min", "FIT_Raw_Feret_Mean",
+#				"FIT_Raw_Perim", "FIT_Raw_Convex_Perim", "FIT_Area_ABD",
+#				"FIT_Diameter_ABD", "FIT_Length", "FIT_Width",
+#				"FIT_Diameter_ESD", "FIT_Perimeter", "FIT_Convex_Perimeter",
+#				"FIT_Intensity", "FIT_Sigma_Intensity", "FIT_Compactness",
+#				"FIT_Elongation", "FIT_Sum_Intensity", "FIT_Roughness",
+#				"FIT_Feret_Max_Angle", "FIT_Avg_Red", "FIT_Avg_Green",
+#				"FIT_Avg_Blue", "FIT_PPC", "FIT_Ch1_Peak", "FIT_Ch1_TOF",
+#				"FIT_Ch2_Peak", "FIT_Ch2_TOF", "FIT_Ch3_Peak", "FIT_Ch3_TOF",
+#				"FIT_Ch4_Peak", "FIT_Ch4_TOF", "FIT_Filename", "FIT_SaveX",
+#				"FIT_SaveY", "FIT_PixelW", "FIT_PixelH", "FIT_CaptureX",
+#				"FIT_CaptureY", "FIT_High_U32", "FIT_Low_U32", "FIT_Total"),
+#			skip = skip)
+#		## Add columns present in list files from FlowCAM III
+#		tab$FIT_Feret_Min_Angle <- NA
+#		tab$FIT_Edge_Gradient <- NA
+#		tab$FIT_Timestamp1 <- NA
+#		tab$FIT_Timestamp2 <- NA
+#		tab$FIT_Source_Image <- NA
+#		tab$FIT_Calibration_Image <- NA
+#		tab$FIT_Ch2_Ch1_Ratio <- tab$FIT_Ch2_Peak / tab$FIT_Ch1_Peak
+#		## New variables calc (present in dataexport.csv from the FlowCAM)
+#		tab$FIT_Volume_ABD <- (4/3) * pi * (tab$FIT_Diameter_ABD/2)^3
+#		tab$FIT_Volume_ESD <- (4/3) * pi * (tab$FIT_Diameter_ESD/2)^3
+#		tab$FIT_Aspect_Ratio <- tab$FIT_Width / tab$FIT_Length
+#		tab$FIT_Transparency <- 1 - (tab$FIT_Diameter_ABD/tab$FIT_Diameter_ESD)
+#		tab$FIT_Red_Green_Ratio <- tab$FIT_Avg_Red / tab$FIT_Avg_Green
+#		tab$FIT_Blue_Green_Ratio <- tab$FIT_Avg_Blue / tab$FIT_Avg_Green
+#		tab$FIT_Red_Blue_Ratio <- tab$FIT_Avg_Red / tab$FIT_Avg_Blue
+#	} else { # FlowCAM III with 47 columns
+#		## Read the table
+#		tab <- read.table(lstfile, header = FALSE, sep = ":", dec = '.',
+#			col.names = c("Id", "FIT_Cal_Const", "FIT_Raw_Area",
+#				"FIT_Raw_Feret_Max", "FIT_Raw_Feret_Min", "FIT_Raw_Feret_Mean",
+#				"FIT_Raw_Perim", "FIT_Raw_Convex_Perim", "FIT_Area_ABD",
+#				"FIT_Diameter_ABD", "FIT_Length", "FIT_Width",
+#				"FIT_Diameter_ESD", "FIT_Perimeter", "FIT_Convex_Perimeter",
+#				"FIT_Intensity", "FIT_Sigma_Intensity", "FIT_Compactness",
+#				"FIT_Elongation", "FIT_Sum_Intensity", "FIT_Roughness",
+#				"FIT_Feret_Max_Angle", "FIT_Feret_Min_Angle", "FIT_Avg_Red",
+#				"FIT_Avg_Green", "FIT_Avg_Blue", "FIT_PPC", "FIT_Ch1_Peak",
+#				"FIT_Ch1_TOF", "FIT_Ch2_Peak", "FIT_Ch2_TOF", "FIT_Ch3_Peak",
+#				"FIT_Ch3_TOF", "FIT_Ch4_Peak", "FIT_Ch4_TOF", "FIT_Filename",
+#				"FIT_SaveX", "FIT_SaveY", "FIT_PixelW", "FIT_PixelH",
+#				"FIT_CaptureX", "FIT_CaptureY", "FIT_Edge_Gradient",
+#				"FIT_Timestamp1", "FIT_Timestamp2", "FIT_Source_Image",
+#				"FIT_Calibration_Image"),
+#			skip = skip)
+#		## Add columns present in list files from FlowCAM II
+#		tab$FIT_High_U32 <- NA
+#		tab$FIT_Low_U32 <- NA
+#		tab$FIT_Total <- NA
+#		## New variables calcul (present in dataexport.csv from the FlowCAM)
+#		tab$FIT_Volume_ABD <- (4/3) * pi * (tab$FIT_Diameter_ABD/2)^3
+#		tab$FIT_Volume_ESD <- (4/3) * pi * (tab$FIT_Diameter_ESD/2)^3
+#		tab$FIT_Aspect_Ratio <- tab$FIT_Width / tab$FIT_Length
+#		tab$FIT_Transparency <- 1 - (tab$FIT_Diameter_ABD/tab$FIT_Diameter_ESD)
+#		tab$FIT_Red_Green_Ratio <- tab$FIT_Avg_Red / tab$FIT_Avg_Green
+#		tab$FIT_Blue_Green_Ratio <- tab$FIT_Avg_Blue / tab$FIT_Avg_Green
+#		tab$FIT_Red_Blue_Ratio <- tab$FIT_Avg_Red / tab$FIT_Avg_Blue
+#		tab$FIT_Ch2_Ch1_Ratio <- tab$FIT_Ch2_Peak / tab$FIT_Ch1_Peak
+#	}
+#	tab
+#}
 
 ## Read context file
 ## TODO: avoid duplicated code between versions

Modified: pkg/zooimage/man/import.Rd
===================================================================
--- pkg/zooimage/man/import.Rd	2013-12-01 14:04:02 UTC (rev 243)
+++ pkg/zooimage/man/import.Rd	2013-12-10 11:07:44 UTC (rev 244)
[TRUNCATED]

To get the complete diff run:
    svnlook diff /svnroot/zooimage -r 244


More information about the Zooimage-commits mailing list