From: Benjamin Auder <benjamin.auder@somewhere>
Date: Fri, 10 Feb 2017 15:00:58 +0000 (+0100)
Subject: fix generateRunSave_EMGLLF, simplify arrays readings
X-Git-Url: https://git.auder.net/variants/img/current/assets/DESCRIPTION?a=commitdiff_plain;h=e8bb47649b9f4e625ef050aae096d23cfc058163;p=valse.git

fix generateRunSave_EMGLLF, simplify arrays readings
---

diff --git a/src/.gitignore b/src/.gitignore
index bdfbcfb..da9ff21 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -1,4 +1,3 @@
 #ignore object files, library and test executables
 *.o
 *.so
-vgcore.*
diff --git a/src/test/.gitignore b/src/test/.gitignore
index 05354ad..1a02474 100644
--- a/src/test/.gitignore
+++ b/src/test/.gitignore
@@ -1,3 +1,4 @@
 test.*
 !test.*.c
 /data/
+vgcore.*
diff --git a/src/test/generate_test_data/generateRunSaveTest_EMGLLF.R b/src/test/generate_test_data/generateRunSaveTest_EMGLLF.R
index 2c2dc2a..f6059e5 100644
--- a/src/test/generate_test_data/generateRunSaveTest_EMGLLF.R
+++ b/src/test/generate_test_data/generateRunSaveTest_EMGLLF.R
@@ -9,26 +9,38 @@ generateRunSaveTest_EMGLLF = function(n=200, p=15, m=10, k=3, mini=5, maxi=10,
 	require(valse)
 	params = valse:::basic_Init_Parameters(n, p, m, k)
 	io = generateIOdefault(n, p, m, k)
-	write.table(paste(testFolder,"phiInit",sep=""), params$phiInit, sep=delimiter);
-	write.table(paste(testFolder,"rhoInit",sep=""), params$rhoInit, sep=delimiter);
-	write.table(paste(testFolder,"piInit",sep=""), params$piInit, sep=delimiter);
-	write.table(paste(testFolder,"gamInit",sep=""), params$gamInit, sep=delimiter);
-	write.table(paste(testFolder,"mini",sep=""), mini, sep=delimiter);
-	write.table(paste(testFolder,"maxi",sep=""), maxi, sep=delimiter);
-	write.table(paste(testFolder,"gamma",sep=""), gamma, sep=delimiter);
-	write.table(paste(testFolder,"lambda",sep=""), lambda, sep=delimiter);
-	write.table(paste(testFolder,"X",sep=""), io$X, sep=delimiter);
-	write.table(paste(testFolder,"Y",sep=""), io$Y, sep=delimiter);
-	write.table(paste(testFolder,"tau",sep=""), tau, sep=delimiter);
-	write.table(paste(testFolder,"dimensions",sep=""), c(n,p,m,k), sep=delimiter);
+	write.table(as.double(params$phiInit), paste(testFolder,"phiInit",sep=""),
+		row.names=F, col.names=F)
+	write.table(as.double(params$rhoInit), paste(testFolder,"rhoInit",sep=""),
+		row.names=F, col.names=F)
+	write.table(as.double(params$piInit), paste(testFolder,"piInit",sep=""),
+		row.names=F, col.names=F)
+	write.table(as.double(params$gamInit), paste(testFolder,"gamInit",sep=""),
+		row.names=F, col.names=F)
+	write.table(as.integer(mini), paste(testFolder,"mini",sep=""),
+		row.names=F, col.names=F)
+	write.table(as.integer(maxi), paste(testFolder,"maxi",sep=""),
+		row.names=F, col.names=F)
+	write.table(as.double(gamma), paste(testFolder,"gamma",sep=""),
+		row.names=F, col.names=F)
+	write.table(as.double(lambda), paste(testFolder,"lambda",sep=""),
+		row.names=F, col.names=F)
+	write.table(as.double(io$X), paste(testFolder,"X",sep=""),
+		row.names=F, col.names=F)
+	write.table(as.double(io$Y), paste(testFolder,"Y",sep=""),
+		row.names=F, col.names=F)
+	write.table(as.double(tau), paste(testFolder,"tau",sep=""),
+		row.names=F, col.names=F)
+	write.table(as.integer(c(n,p,m,k)), paste(testFolder,"dimensions",sep=""),
+		row.names=F, col.names=F)
 
 	res = EMGLLF(params$phiInit,params$rhoInit,params$piInit,params$gamInit,mini,maxi,
-		gamma,lambda,io$X,io$Y,tau);
+		gamma,lambda,io$X,io$Y,tau)
 
 	#save outputs
-	write.table(paste(testFolder,"phi",sep=""), res$phi, sep=delimiter);
-	write.table(paste(testFolder,"rho",sep=""), res$rho, sep=delimiter);
-	write.table(paste(testFolder,"pi",sep=""), res$pi, sep=delimiter);
-	write.table(paste(testFolder,"LLF",sep=""), res$LLF, sep=delimiter);
-	write.table(paste(testFolder,"S",sep=""), res$S, sep=delimiter);
+	write.table(as.double(res$phi), paste(testFolder,"phi",sep=""), row.names=F, col.names=F)
+	write.table(as.double(res$rho), paste(testFolder,"rho",sep=""), row.names=F, col.names=F)
+	write.table(as.double(res$pi), paste(testFolder,"pi",sep=""), row.names=F, col.names=F)
+	write.table(as.double(res$LLF), paste(testFolder,"LLF",sep=""), row.names=F, col.names=F)
+	write.table(as.double(res$S), paste(testFolder,"S",sep=""), row.names=F, col.names=F)
 }
diff --git a/src/test/generate_test_data/helpers/EMGLLF.R b/src/test/generate_test_data/helpers/EMGLLF.R
index b0d95a3..02fbb47 100644
--- a/src/test/generate_test_data/helpers/EMGLLF.R
+++ b/src/test/generate_test_data/helpers/EMGLLF.R
@@ -169,5 +169,5 @@ EMGLLF = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,lambda,X,Y,tau)
   }
     
   Pi = t(Pi)
-  return(list(phi=phi, rho=rho, Pi=Pi, LLF=LLF, S=S))
+  return(list("phi"=phi, "rho"=rho, "pi"=Pi, "LLF"=LLF, "S"=S))
 }
diff --git a/src/test/utils.c b/src/test/utils.c
index a6370b0..2eac0c2 100644
--- a/src/test/utils.c
+++ b/src/test/utils.c
@@ -37,55 +37,38 @@ void compareArray_int(const char* ID, const void* array, const void* refArray, i
 // Read array by columns (as in MATLAB) and return by-rows encoding
 void* readArray(const char* fileName, int isinteger)
 {
-	// need to prepend '../data/' (not really nice code...)
+	// need to prepend 'data/' (not really nice code...)
 	char* fullFileName = (char*)calloc(5+strlen(fileName)+1, sizeof(char));
 	strcat(fullFileName, "data/");
 	strcat(fullFileName, fileName);
 
 	// first pass to know how many elements to allocate
 	char* command = (char*)calloc(12+strlen(fullFileName)+8+1, sizeof(char));
-	strcat(command, "grep -o ' ' ");
+	strcat(command, "wc -l ");
 	strcat(command, fullFileName);
-	strcat(command, " | wc -l");
-	FILE *countSpaces = popen(command, "r");
-	char* buffer = (char*)calloc(32, sizeof(char));
-	fgets(buffer, sizeof(buffer), countSpaces);
-	int n = atoi(buffer) + 1;
-	free(buffer);
-	pclose(countSpaces);
+	FILE *arraySize = popen(command, "r");
+	char* bufferNum = (char*)calloc(64, sizeof(char));
+	fgets(bufferNum, sizeof(bufferNum), arraySize);
+	int n = atoi(bufferNum);
+	pclose(arraySize);
 
 	// open file for reading
-	FILE* file = fopen(fullFileName, "r");
+	FILE* arrayFile = fopen(fullFileName, "r");
 	free(fullFileName);
 
-	int d = 1;
-	size_t elementSize = isinteger
-		? sizeof(int)
-		: sizeof(float);
-
 	// read all values, and convert them to by-rows matrices format
+	size_t elementSize = isinteger ? sizeof(int) : sizeof(float);
 	void* array = malloc(n*elementSize);
-	char curChar = ' ';
-	char bufferNum[64];
-	for (int u=0; u<n; u++)
+	for (int i=0; i<n; i++)
 	{
-		// read number (as a string)
-		int bufferIndex = 0;
-		while (!feof(file) && curChar!=' ')
-		{
-			curChar = fgetc(file);
-			bufferNum[bufferIndex++] = curChar;
-		}
-		bufferNum[bufferIndex] = 0;
-		// transform string into float, and store it at appropriate location
+		// transform buffer content into float or int, and store it at appropriate location
 		if (isinteger)
-			((int*)array)[u] = atoi(bufferNum);
+			((int*)array)[i] = atoi(bufferNum);
 		else
-			((float*)array)[u] = atof(bufferNum);
-		// position to next non-separator character
-		curChar = fgetc(file);
+			((float*)array)[i] = atof(bufferNum);
 	}
-	fclose(file);
+	fclose(arrayFile);
+	free(bufferNum);
 
 	return array;
 }