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)
}
// 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;
}