Do motion correction for several projects

Script text

 

function Correct_motion_in_several_FMRs() {

 

var cancel = false;

var doISA = false;

var isaFMR;

var correctedtargetfmr;

 

var targetvolume = 1;

var interpolationmethod = 1; // trilinear

var doSINC = false;

var doSINCforAll = false;

var nrofiterations = 100; // default in BVQX

var usefulldataset = 0; // no

var createmovies = 0; // no

var extendedlogfile = 1; // yes

 

BrainVoyagerQX.ShowLogTab();

BrainVoyagerQX.PrintToLog("Start motion correction batch...");

var nrfmrs = Input.getNumber("Please enter the number of FMRs in which to correct motion");

if (nrfmrs != "undefined") {

var fmrnamearray = new Array();

 

// first ask for all the filenames

var counter;

for (counter = 0; counter < nrfmrs; counter++) {

fmrnamearray.unshift(FileDialog.getOpenFileName("*.fmr", "Please select FMR project " + (counter+1).toString() + " of " + nrfmrs.toString()));

}

targetvolume = Input.getNumber("Please select a target volume for the motion correction", 1);

// interpolationmethod = Input.getNumber("Please enter select the target volume for the motion correction", 1);

var doSINCanswer = MessageBox.information("Would you like to use SINC interpolation for the (target) FMR?", MessageBox.Yes, MessageBox.No);

doSINC = (doSINCanswer == MessageBox.Yes);

if (nrfmrs > 1) {

var isaAnswer = MessageBox.information("Would you like to perform intrasession alignment?", MessageBox.Yes, MessageBox.No);

doISA = (isaAnswer == MessageBox.Yes);

if (doISA) {

isaFMR = Input.getItem("Please select the target FMR for intrasession alignment", fmrnamearray);

if (isaFMR == "undefined") cancel = true;

} else {

var doSINCforAllanswer = MessageBox.information("Would you like to use SINC interpolation for all FMRs (is slow)?", MessageBox.Yes, MessageBox.No);

doSINCforAll = (doSINCforAllanswer == MessageBox.Yes);

}

}

 

nrofiterations = Input.getNumber("Please select the maximum number of iterations", 100);

var doReduceddataset = MessageBox.information("Would you like to use a reduced dataset for the determination of the motion parameters (recommended)?", MessageBox.Yes, MessageBox.No);

usefulldataset = ((doReduceddataset == MessageBox.Yes) ? 0 : 1);

var doCreateMovies = MessageBox.information("Would you like to create movies?", MessageBox.Yes, MessageBox.No);

createmovies = ((doCreateMovies == MessageBox.Yes) ? 1 : 0);

var doExtendedLog = MessageBox.information("Would you like to have extended logfiles?", MessageBox.Yes, MessageBox.No);

extendedlogfile = ((doExtendedLog == MessageBox.Yes) ? 1 : 0);

 

var info = "FMR names: "+ fmrnamearray.toString()

   + "\nNumber of iterations: " + nrofiterations.toString()

   + "\nUse reduced data set: " + ((doReduceddataset == MessageBox.Yes) ? "Yes" : "No")

   + "\nTarget FMR: " + isaFMR

   + "\nSINC interpolation for first FMR: " + ((doSINCanswer == MessageBox.Yes) ?  "Yes" : "No")

   + "\nCreate movies: " + ((doCreateMovies == MessageBox.Yes) ?  "Yes" : "No")

   + "\nExtended log files: " + ((doExtendedLog == MessageBox.Yes) ?  "Yes" : "No");    

BrainVoyagerQX.PrintToLog(info);

 

// start motion correction

if (!cancel) {

var doStart = MessageBox.information("Would you like to start the motion correction now?<BR>(No = cancel)", MessageBox.Yes, MessageBox.No);

if (doStart == MessageBox.Yes) {

// do first FMR

var success;

if (nrfmrs == 1) isaFMR = fmrnamearray[0];

var targetfmr = BrainVoyagerQX.OpenDocument(isaFMR);

if (doSINC) {

success = targetfmr.CorrectMotion(targetvolume);

} else {

success = targetfmr.CorrectMotionEx(targetvolume, interpolationmethod, usefulldataset, nrofiterations, createmovies, extendedlogfile);

}

correctedtargetfmr = targetfmr.FileNameOfPreprocessdFMR;

targetfmr.Close();

 

if (nrfmrs > 1) {

// get thing out of array

var fmrnr;

for (fmrnr = 0; fmrnr < nrfmrs; fmrnr++) {

if (fmrnamearray[fmrnr] == isaFMR) {

fmrnamearray.splice(fmrnr, 1);

break;

}

}

for (fmrnr = 0; fmrnr < (nrfmrs-1); fmrnr++) {

var fmr = BrainVoyagerQX.OpenDocument(fmrnamearray[fmrnr]);

if (doISA) {

success = fmr.CorrectMotionTargetVolumeInOtherRunEx(correctedtargetfmr, targetvolume, interpolationmethod, usefulldataset, nrofiterations, createmovies, extendedlogfile);

} else {

if (doSINCforAll) {

success = fmr.CorrectMotion(targetvolume);

} else {

success = fmr.CorrectMotionEx(targetvolume, interpolationmethod, usefulldataset, nrofiterations, createmovies, extendedlogfile);

}

}

fmr.Close();

}

}

} else {

cancel = true;

}

}

} else {

cancel = true;

}

if (cancel) {

BrainVoyagerQX.PrintToLog("Further actions cancelled.");

} else {

BrainVoyagerQX.PrintToLog("Finished.");

}

}

 

Script during execution