Create multiple VTCs script

The following code will make it possible to create multiple VTC files automatically (see below). Sample use is demonstrated in the section Use of script for creating multiple VTCs.

 

 

/*

  createMultipleVTCs.qs

  The purpose of this script is to create one or several volume time courses files (VTCs).

  The script should be located in '/Documents/BVQXExtensions/Scripts/'. The script can be run directly via the BrainVoyager QX menu > Scripts > CreateMultipleVTCs() when it is imported in

  the Script Editor or via doubleclicking on the function name ' CreateMultipleVTCs' in the 'Defined Functions'  window of the Script Editor.

 

  To import the script, do the following:

  1. copy the script to /(My)Documents/BVQXExtensions/Scripts/.

  2. choose BrainVoyager QX menu > Scripts > Edit scripts... > File > Import scripts... and select this script.

 

 The script does not require manual modifications, because a user interface for the script is provided. A pop-up dialogs will ask for the VTC parameters and a file dialog

  makes it possible to choose the main directory. The script presumes that each BrainVoyager project has its own subfolder, and that only one fmr project,

 *.vmr, *_IA.trf, *_FA.trf and if necessary *_ACPC.trf and *.tal file is present in each folder.

  

 For remarks and suggestions, please send an email to H.Breman@BrainVoyager.com.

 Hester Breman, Brain Innovation, September 2005.

 */

 

var BVQX = Application.BrainVoyagerQX;

var baseName;

var IA;

var FA;

var ACPC;

var TAL;

var FMR;

var VMR;

var resolution = 1; // resolution in mm

var interpolationMethod = 1; // trilinear interpolation

var boundingBoxIntensityTresh = 100; // voxel intensity value threshold default at 100

var fmrName;

var vmrName;

var docVMR; // value will be assigned in script

var newVTCname; // value is defined in script

var createVTCsucceeded = false; // value will be returned from CreateVTC...() methods

var error; /* since try...catch blocks are used, to prevent BrainVoyager from crashing when an error occurs,

   an eventual error will be caught in this variable */

var today = new Date();

 

function createMultipleVTCs(){

 

var vtcInVMRspace = false;

var vtcInACPCspace = false;

var vtcInTALspace = false;

 

MessageBox.information("This script will create multiple VTC's from files it finds in subfolders containing a project. The script will ask for the main folder and the VTC parameters.");

var baseName = FileDialog.getExistingDirectory("*.*", "Please select the main directory of the BrainVoyager projects");

var bvqxProjectsDir = new Dir(baseName);

bvqxProjectsDir.setCurrent();

var projLogName = baseName + "/createMultipleVTCsLog.txt"; // a text file will be saved containing the logging information

var projLog = new File( projLogName );  

projLog.open( File.WriteOnly );

projLog.writeLine("*****************************************************************************************")

projLog.writeLine("In method createMultipleVTCFiles(), date: " + today.toString());    projLog.writeLine("****************************************************************************************");

projLog.writeLine(" ");

projLog.writeLine("Main directory: '" + baseName + "'");

 

// *** user interface ***

// create main dialog

var choiceDialog = new Dialog;

choiceDialog.title = "Please select the VTC parameters...";

// create components on main dialog

var spaceBox = new ComboBox;

spaceBox.label = "Target space for VTC";

spaceBox.itemList= ["Talairach", "AC-PC","VMR"];

var resBox = new SpinBox;

resBox.label = "Please choose the desired resolution (1 mm^3, 2 mm^3 or 3mm^3)";

resBox.value = resolution;

resBox.maximum = 3;

resBox.minimum = 1;

resBox.value = 3; // default

var interpolBox = new ComboBox;

interpolBox.itemList = ["Trilinear interpolation"];

interpolBox.label = "Please select the interpolation method";

interpolBox.currentItem = "Trilinear interpolation";

var intensityBox = new NumberEdit;

intensityBox.label = "Please enter the intensity threshold";

intensityBox.value = boundingBoxIntensityTresh;

intensityBox.maximum = 255;

 

// add components to main dialog

choiceDialog.add(spaceBox);

choiceDialog.add(resBox);

choiceDialog.add(interpolBox);

choiceDialog.add(intensityBox);

 

var tmp;

if (choiceDialog.exec()) {

tmp = spaceBox.currentItem;

projLog.writeLine("Target space for VTC: " + tmp);

projLog.writeLine(" ");

resolution = resBox.value;

projLog.writeLine("Resolution: " + resolution + "mm^3");

var intMethod = interpolBox.currentItem;

interpolationMethod = ((intMethod == "Trilinear interpolation") ? 1 : 1); // will always choose trilinear interpolation

projLog.writeLine("Interpolation method: " + intMethod);

boundingBoxIntensityTresh = intensityBox.value;

projLog.writeLine("Intensity threshold for bounding box: " + boundingBoxIntensityTresh);

projLog.writeLine(" ");

}

 

if (tmp == "Talairach") {

vtcInTALspace = true;

} else if (tmp == "AC-PC") {

vtcInACPCspace = true;

} else {

vtcInVMRspace = true;

}

 

// set the directory of the directory object

var subfolderList = bvqxProjectsDir.entryList("*", Dir.Dirs, Dir.Time);

var i, j, tmpFile;

projLog.writeLine("Number of subfolders: " + subfolderList.length.toString());

for (i=0;i< subfolderList.length;i++) {

// go in subfolders

var tmpDir = new Dir(subfolderList[i]);

if (!tmpDir.name.startsWith(".")) {

 

var tmpFilelist = tmpDir.entryList("*.*", Dir.Files, Dir.Time);

projLog.writeLine("Subdirectory: '" + tmpDir.name + "'");

projLog.writeLine(" ");

var iapres, talpres, fmrpres, vmrpres, acpcpres = false;

 

for (j=0;j<tmpFilelist.length;j++) {

 

var tmpFile = new File(tmpFilelist[j]);

projLog.writeLine("File: " + tmpFile.name);

var filename = tmpFile.name;

if ((filename.indexOf("_IA.trf") > -1)) {

IA = tmpDir.absPath + "/" + tmpFilelist[j];

iapres = true;

} else if ((filename.indexOf("_FA.trf") > -1)) {

FA = tmpDir.absPath + "/" + tmpFilelist[j];

fapres = true;

} else if (tmpFile.extension == "fmr") {

if ((filename.indexOf("firstvol") < 0)) {

FMR = tmpDir.absPath + "/" + tmpFilelist[j];

fmrpres = true;

}

} else if (tmpFile.extension == "tal") {

TAL = tmpDir.absPath + "/" + tmpFilelist[j];

talpres = true;

} else if (tmpFile.extension == "vmr") {

VMR = tmpDir.absPath + "/" + tmpFilelist[j];

vmrpres = true;

} else if ((filename.indexOf("_ACPC.trf") > -1)) {

ACPC = tmpDir.absPath + "/" + tmpFilelist[j];

acpcpres = true;

}

}

var baseConditions = (iapres && fapres && fmrpres && vmrpres);

var acpcConditions = (baseConditions && acpcpres);

var talConditions = (acpcConditions && talpres);

 

if (baseConditions) {

projLog.writeLine(" ");

projLog.writeLine("Files for creation of VTC:");

projLog.writeLine(" ");

projLog.writeLine(FMR);

projLog.writeLine(VMR);

projLog.writeLine(IA);

projLog.writeLine(FA);

projLog.writeLine((vtcInACPCspace || vtcInTALspace)? ACPC: "No *_ACPC.trf file needed.");

projLog.writeLine((vtcInTALspace)? TAL: "No *.tal file needed.");

 

docVMR = BVQX.OpenDocument(VMR);

var fmrFile = new File(FMR);

var fmrBaseName = fmrFile.baseName;

 

// create VTC in VMR space

newVTCname = bvqxProjectsDir.absPath + "/" + tmpDir.name + "/" + fmrBaseName + "_VMRspace.vtc";

if (vtcInVMRspace) {

try {

createVTCsucceeded = docVMR.CreateVTCInVMRSpace(FMR, IA, FA, newVTCname, resolution, interpolationMethod, boundingBoxIntensityTresh);

projLog.writeLine("CreateVTCInVMRSpace() succeeded: " + (createVTCsucceeded? "Yes" : "No"));

if (createVTCsucceeded) { projLog.writeLine("VTC name: " + newVTCname);}

} catch (error) {

projLog.writeLine("Error creating VTC with CreateVTCInVMRSpace(): " + error);

}

}

 

// create VTC in ACPC space

newVTCname = bvqxProjectsDir.absPath + "/" + tmpDir.name + "/" + fmrBaseName + "_ACPCspace.vtc";

if (vtcInACPCspace && acpcConditions) {

projLog.writeLine(ACPC);

projLog.writeLine(" ");

try {

createVTCsucceeded = docVMR.CreateVTCInACPCSpace(FMR, IA, FA, ACPC, newVTCname, resolution, interpolationMethod, boundingBoxIntensityTresh);

projLog.writeLine("CreateVTCInACPCSpace() succeeded: " + (createVTCsucceeded? "Yes" : "No"));

if (createVTCsucceeded) { projLog.writeLine("VTC name: " + newVTCname);}

} catch (error) {

projLog.writeLine("Error creating VTC with CreateVTCInACPCSpace(): " + error);

}

}

 

// create VTC in TAL space

newVTCname = bvqxProjectsDir.absPath + "/" + tmpDir.name + "/" + fmrBaseName + "_TALspace.vtc";

if (vtcInTALspace && talConditions) {

projLog.writeLine(ACPC);

projLog.writeLine(TAL);

projLog.writeLine(" ");

try {

createVTCsucceeded = docVMR.CreateVTCInTALSpace(FMR, IA, FA, ACPC, TAL, newVTCname, resolution, interpolationMethod, boundingBoxIntensityTresh);

projLog.writeLine("CreateVTCInTALSpace() succeeded: " + (createVTCsucceeded? "Yes" : "No"));

if (createVTCsucceeded) { projLog.writeLine("VTC name: " + newVTCname);}

} catch (error) {

projLog.writeLine("Error creating VTC with CreateVTCInTALSpace(): " + error);

}

}

docVMR.Close();

bvqxProjectsDir.setCurrent();

} else {

projLog.writeLine("Not all files available in folder for creation of VTC.");

}

projLog.writeLine(" ");

}

  }

projLog.writeLine("CreateVTCFiles() script finished.");

projLog.close();

MessageBox.information("CreateVTCFiles() script finished. A log file has been created: " + projLogName);

}