// load a volume for computation void emotionRoiProcessing::loadVolume(studyParams &vdb, volume<float> &v, int index) { char processedFile[200], prefix[BUFF_SIZE], tempVolume[BUFF_SIZE]; if (0) { // gets the motion corrected and gaussian file vdb.getMCGVolumeName(processedFile, index); read_volume(v, string(processedFile)); } else { // making the activation volume for viewing vdb.getFinalVolumeFormat(prefix); vdb.setActivationFile(index); estimateActivation(index, index, vdb.slidingWindowSize, prefix, vdb.maskFile, vdb.activationFile); // gets the motion corrected and gaussian file and calculates the mean with the last n volumes (n = sliding window size) sprintf(tempVolume, "%s%s", vdb.outputDir, "temp.nii"); vdb.getMCGVolumeFormat(prefix); estimateActivation(index, index, vdb.slidingWindowSize, prefix, tempVolume); read_volume(v, string(tempVolume)); if (fileExists(tempVolume)) remove(tempVolume); } }
// plug in test function DLLExport testSVM(studyParams &vdb, int index, float &classnum, float &projection, void * &userData) { char prefix[BUFF_SIZE]; SVMProcessing *svmProcessingVar = (SVMProcessing *) userData; vdb.getFinalVolumeFormat(prefix); fprintf(stderr, "Generating activation file.\n"); vdb.setActivationFile(index); estimateActivation(index, index, vdb.slidingWindowSize, prefix, vdb.maskFile, vdb.activationFile); fprintf(stderr, "Classifying.\n"); svmProcessingVar->test(index, vdb.activationFile, classnum, projection); return 1; }
// handles the training void SVMProcessing::train() { char prefix[BUFF_SIZE], lastCummulativeTrainingFile[BUFF_SIZE], actualCummulativeTrainingFile[BUFF_SIZE], cummulativeModelFile[BUFF_SIZE], cummulativeTestingFile[BUFF_SIZE]; vector <int> classes, indices; std::stringstream CmdLn; vdbPtr->getFinalVolumeFormat(prefix); // calculating the sliding window and saving a 4D volume estimateActivation(1, vdbPtr->interval.maxIndex(), vdbPtr->slidingWindowSize, prefix, vdbPtr->train4DFile); // getting the volume indexes, excluding the baseline and the first ones discarted by haemodynamic stabilization // vdbPtr->interval.getVolumeIndices(vdbPtr->offset, 1, vdbPtr->interval.maxIndex(), indices); // added on 02AUG2016 fillVolumeIndexes(vdbPtr,indices); // getting a vector containing the classes for each volume vdbPtr->interval.getClassArray(classes); char svmMask[BUFF_SIZE], svmTestingFile[BUFF_SIZE]; sprintf(svmMask, "%s%s", vdbPtr->featuresSuffix, vdbPtr->trainFeatureSuffix); if (cummulativeTraining) { if (fileExists(featuresTestMask)) { fprintf(stderr, "################## Updating %s to %s due to cummulative train option ########################## \n", svmMask, featuresTestMask); CmdLn.str(""); CmdLn << "fslmaths " << featuresTestMask << " " << svmMask; fslmaths((char *)CmdLn.str().c_str()); } } // transforms the 4D volume in a svm like input file saveSVMFile(vdbPtr->train4DFile, svmMask, svmTrainingFile, 0, indices, classes); CmdLn.str(""); SVMObj svmObject; // training CmdLn << "svmtrain -t 0 " << svmTrainingFile << " " << svmModelFile; svmObject.train(CmdLn.str().c_str()); CmdLn.str(""); sprintf(svmTestingFile, "%s%s%s%s", svmDir, "training", vdbPtr->trainFeatureSuffix, ".tst"); // testing the training data CmdLn << "svmpredict " << svmTrainingFile << " " << svmModelFile << " " << svmTestingFile; svmObject.predict(CmdLn.str().c_str()); generateProjetionsGraph(svmTestingFile); // testing the training data with the prediction model if (fileExists(svmModelPredictFile)) { CmdLn.str(""); sprintf(svmTestingFile, "%s%s%s%s", svmDir, "testing", vdbPtr->trainFeatureSuffix, ".tst"); CmdLn << "svmpredict " << svmTrainingFile << " " << svmModelPredictFile << " " << svmTestingFile; svmObject.predict(CmdLn.str().c_str()); generateProjetionsGraph(svmTestingFile); } if (cummulativeTraining) { sprintf(lastCummulativeTrainingFile, "%s%s%s%s", svmDir, "cummulative_training", vdbPtr->testFeatureSuffix, ".txt"); sprintf(actualCummulativeTrainingFile, "%s%s%s%s", svmDir, "cummulative_training", vdbPtr->trainFeatureSuffix, ".txt"); if (fileExists(lastCummulativeTrainingFile)) { mergeFiles(svmTrainingFile, lastCummulativeTrainingFile, actualCummulativeTrainingFile); } else copyFile(svmTrainingFile, actualCummulativeTrainingFile); sprintf(cummulativeModelFile, "%s%s%s%s%s", svmDir, "cummulative_", vdbPtr->subject, vdbPtr->trainFeatureSuffix, ".model"); // training CmdLn.str(""); CmdLn << "svmtrain -t 0 " << actualCummulativeTrainingFile << " " << cummulativeModelFile; svmObject.train(CmdLn.str().c_str()); CmdLn.str(""); sprintf(cummulativeTestingFile, "%s%s%s%s", svmDir, "cummulative_training", vdbPtr->trainFeatureSuffix, ".tst"); // testing the cummulative training data CmdLn << "svmpredict " << actualCummulativeTrainingFile << " " << cummulativeModelFile << " " << cummulativeTestingFile; svmObject.predict(CmdLn.str().c_str()); generateProjetionsGraph(cummulativeTestingFile); /* if (fileExists(svmModelPredictFile)) { CmdLn.str(""); sprintf(cummulativeTestingFile, "%s%s%s%s", svmDir, "cummulative_testing", vdbPtr->trainFeatureSuffix, ".tst"); CmdLn << "svmpredict " << actualCummulativeTrainingFile << " " << svmModelPredictFile << " " << cummulativeTestingFile; svmObject.predict(CmdLn.str().c_str()); generateProjetionsGraph(cummulativeTestingFile); } */ } // Generating weight map volume fprintf(stderr, "Generating weight map volumes\n"); sprintf(svmMask, "%s.nii", vdbPtr->featuresTrainSuffix); fileExists(svmMask); fprintf(stderr, "using %s \n", svmMask); model=svm_load_model(svmModelFile); if (model != NULL) { generateWeightVolume(model, svmMask, 1, svmWeightNormFile); generateWeightVolume(model, svmMask, 0, svmWeightFile); unloadModel(model); } if (cummulativeTraining) { char cummulativeSvmWeightNormFile[BUFF_SIZE], cummulativeSvmWeightFile[BUFF_SIZE]; sprintf(cummulativeSvmWeightNormFile, "%s%s%s%s", svmDir, "cummulative_weights_norm", vdbPtr->trainFeatureSuffix, ".nii"); sprintf(cummulativeSvmWeightFile, "%s%s%s%s", svmDir, "cummulative_weights", vdbPtr->trainFeatureSuffix, ".nii"); model = svm_load_model(cummulativeModelFile); if (model != NULL) { generateWeightVolume(model, svmMask, 1, cummulativeSvmWeightNormFile); generateWeightVolume(model, svmMask, 0, cummulativeSvmWeightFile); unloadModel(model); } } }