1.1.0 ,CreatePreprFuncData.qsé*/* CreatePreprFuncData.qs Purpose: create and preprocess multiple FMR projects Notes: 1. Please use separate folders for each fmr project; pos files etc are saved in the raw data folder 2. Reading a directory from cd does not work properly; please store the raw data on a hard disk first. 3. Works for BrainVoyager QX version 1.2.6 How to use: 1. Open the BrainVoyager scripting editor via the BrainVoyager menu > "Scripts" > "Edit scripts..." 2. In the BrainVoyager scripting editor, choose "File" > "Import scripts..."; 3. Choose this 'CreatePreprFuncData.qs' file and click 'OK'. The script will be added to the editor and obtain its own tab 4. Activate the script via the BrainVoyager menu > "Scripts"; the script should be listed there; or double click the function name in the list on the right side of the screen editor Tested on Windows XP and Mac OS 10.2 with a Siemens *.ima and the sample Siemens DICOM mosaic dataset for BrainVoyager QX Hester Breman, Brain Innovation, May 2004; Last update: November 2004. Changes: motion correction, use of 'SaveAs()' and more efficient path name retrieval, slice time parameters, time recording. */ function CreatePreprFuncData() { // ****************************** PREPARATION ********************************* var bvqx = Application.BrainVoyagerQX; var docFMR; var projectPath; var today = new Date(); var projLog; // *** ask number of projects var nrOfProjects = Input.getNumber("This script creates and preprocesses multiple FMR projects. Please enter the number of projects"); // *** create arrays to save variables for each project var firstFilesArray = new Array(nrOfProjects); var rawFilePath, firstFile, posLastSlash, responseNr; var projectPathsArray = new Array(nrOfProjects); var mosaicArray = new Array(nrOfProjects); var TRval = new Array(nrOfProjects); var interSliceTimeVal = new Array(nrOfProjects); var timeResVerified = new Array(nrOfProjects); var pixelSizeDimX = new Array(nrOfProjects); var pixelSizeDimY = new Array(nrOfProjects); var gapThick = new Array(nrOfProjects); var sliceThick = new Array(nrOfProjects); var voxelResVerified = new Array(nrOfProjects); // *** preprocessing preferences var rawDataType = new Array(nrOfProjects); var nrOfVolumes = new Array(nrOfProjects); var skipVolumes = new Array(nrOfProjects); var createPseudoAMR = new Array(nrOfProjects); var nrOfSlices = new Array(nrOfProjects); var STCprefix = new Array(nrOfProjects); var swapBytes = new Array(nrOfProjects); var xResImg = new Array(nrOfProjects); var yResImg = new Array(nrOfProjects); var bytePerPixel = new Array(nrOfProjects); var nrOfVolsInImage = new Array(nrOfProjects); var xResBf = new Array(nrOfProjects); var yResBf = new Array(nrOfProjects); var refCorrVolume = 1; // used volume number to align to var volArray = new Array(nrOfProjects); var useISA = false; // indicates whether intrasession alignment method should be used var refISAFMR = new Array(nrOfProjects); // FMR file where all other FMR projects of the same session are aligned to var useRawDataNames = true; var docMosaicFMR; var FMRNamesArray = new Array(nrOfProjects); for (i=0; i<nrOfProjects; i++) { // *** ask first file of project i MessageBox.information("Please choose the first file of project " + (i+1)); firstFilesArray[i] = FileDialog.getOpenFileName( ); // *** get path from first file location firstFile = firstFilesArray[i]; var firstFileReally = new File(firstFile); FMRNamesArray[i] = firstFileReally.baseName; rawFilePath = firstFileReally.path; MessageBox.information("Please choose a unique directory to save this project"); projectPathsArray[i] = FileDialog.getExistingDirectory(rawFilePath); responseNr = MessageBox.information("Are the image files of this project mosaic?", MessageBox.Yes, MessageBox.No); mosaicArray[i] = ((responseNr == 3) ? true : false); if (i==0) { projLog = new File(rawFilePath + "/projLog.txt"); projLog.open(10); projLog.writeLine("*********************************************************************************************"); projLog.writeLine("Logfile created by BrainVoyager QX script 'CreatePreprFuncData.qs', date: " + today); projLog.writeLine("*********************************************************************************************"); projLog.writeLine(" "); projLog.writeLine("Number of projects: " + nrOfProjects); projLog.writeLine(" "); } projLog.writeLine(" "); projLog.writeLine("First file of project: " + firstFile); // *** create user interface var paramDialog = new Dialog; var TRvalFld = new NumberEdit; TRvalFld.label = "Repetition Time (TR) in ms"; var ISTFld = new NumberEdit; ISTFld.label = "Inter slice time in ms"; ISTFld.toolTip = "Calculate: TR in ms / nr of slices (of course there are exceptions...)"; var timeResVerifiedBox = new CheckBox; timeResVerifiedBox.text = "Time resolution verified?"; var pixResXFld = new NumberEdit; pixResXFld.label = "Pixel resolution in mm, X dimension"; var pixResYFld = new NumberEdit; pixResYFld.label = "Pixel resolution in mm, Y dimension"; var gapThickBox = new NumberEdit; gapThickBox.label = "Gap thickness in mm"; gapThickBox.value = 1; gapThickBox.toolTip = "Calculate: Spacing between slices - slice thickness"; var sliceThickBox = new NumberEdit; sliceThickBox.label = "Slice thickness in mm"; sliceThickBox.value = 5; var voxelResVerifiedBox = new CheckBox; voxelResVerifiedBox.text = "Voxel resolution verified?"; // *** add components to user interface paramDialog.add(TRvalFld); paramDialog.add(ISTFld); paramDialog.add(timeResVerifiedBox); paramDialog.add(pixResXFld); paramDialog.add(pixResYFld); paramDialog.add(gapThickBox); paramDialog.add(sliceThickBox); paramDialog.add(voxelResVerifiedBox); paramDialog.title = "Functional project (FMR) parameters"; paramDialog.width = 300; //*** general tab for this dialog (does not apply for first project) paramDialog.newTab("General"); var usePreviousBox = new CheckBox; usePreviousBox.text = "Use parameters of previous project (if yes: no need to enter parameters for this project). Please enter OK"; usePreviousBox.checked = false; if (i!=0) { paramDialog.add(usePreviousBox); } //*** parameter tab for this dialog paramDialog.newTab("Parameters"); var rawDataTypeBox = new ComboBox; rawDataTypeBox.label = "Raw data type"; rawDataTypeBox.itemList= ["DICOM", "ANALYZE","SIEMENS","GE_I","GE_MR","BMP"]; var nrOfVolumesBox = new SpinBox; nrOfVolumesBox.value = 1; nrOfVolumesBox.maximum = 600; nrOfVolumesBox.label = "Nr of volumes"; var skipVolumesBox = new SpinBox; skipVolumesBox.value = 1; skipVolumesBox.label = "Skip volumes"; var createPseudoAMRBox = new CheckBox; createPseudoAMRBox.text = "Create pseudo AMR"; var nrOfSlicesBox = new NumberEdit; nrOfSlicesBox.label = "Nr of slices"; var STCprefixBox = new LineEdit; // will not be put on dialog STCprefixBox.label = "STC prefix"; STCprefixBox.text = "untitled-"; var swapBytesBox = new CheckBox; swapBytesBox.text = "Swap bytes (in case data are Big Endian)"; swapBytesBox.checked = false; var xResImgBox = new NumberEdit; xResImgBox.label = "Image width (x resolution)"; var yResImgBox = new NumberEdit; yResImgBox.label = "Image height (y resolution)"; var bytePerPixelBox = new SpinBox; bytePerPixelBox.label = "Bytes per pixel"; bytePerPixelBox.value = 2; var nrOfVolsInImageBox = new SpinBox; nrOfVolsInImageBox.label ="Nr of volumes in image"; nrOfVolsInImageBox.value = 1; var xResBfBox = new NumberEdit; xResBfBox.label = "Total width of mosaic image (x resolution)"; var yResBfBox = new NumberEdit; yResBfBox.label = "Total height of mosaic image (y resolution)"; paramDialog.add(rawDataTypeBox); paramDialog.add(nrOfVolumesBox); paramDialog.add(skipVolumesBox); paramDialog.add(createPseudoAMRBox); paramDialog.add(nrOfSlicesBox); paramDialog.add(swapBytesBox); paramDialog.add(xResImgBox); paramDialog.add(yResImgBox); paramDialog.add(bytePerPixelBox); paramDialog.okButtonText = "OK"; if (mosaicArray[i]) { paramDialog.add(nrOfVolsInImageBox); paramDialog.add(xResBfBox); paramDialog.add(yResBfBox); } // ****************************** COLLECT DATA OF PROJECTS ********************** // *** present dialog to user for collection of project variables if (paramDialog.exec()) { if ((i!=0) && usePreviousBox.checked) { // let dialog disappear when parameters of a previous project are used TRval[i] = TRval[i-1]; interSliceTimeVal[i] = interSliceTimeVal[i-1]; timeResVerified[i] = timeResVerified[i-1]; pixelSizeDimX[i] = pixelSizeDimX[i-1]; pixelSizeDimY[i] = pixelSizeDimY[i-1]; gapThick[i] = gapThick[i-1]; sliceThick[i] = sliceThick[i-1]; voxelResVerified[i] = voxelResVerified[i-1]; rawDataType[i]= rawDataType[i-1]; nrOfVolumes[i]= nrOfVolumes[i-1]; skipVolumes[i]= skipVolumes[i-1]; createPseudoAMR[i]= createPseudoAMR[i-1]; nrOfSlices[i] = nrOfSlices[i-1]; swapBytes[i]= swapBytes[i-1]; xResImg[i] = xResImg[i-1]; yResImg[i] = yResImg[i-1]; bytePerPixel[i] = bytePerPixel[i-1]; if (mosaicArray[i]) { nrOfVolsInImage[i]=nrOfVolsInImage[i-1]; xResBf[i]=xResBf[i-1]; yResBf[i]=yResBf[i-1]; } } else { // collect project and preprocessing parameters TRval[i] = TRvalFld.value; interSliceTimeVal[i] = ISTFld.value; timeResVerified[i] = timeResVerifiedBox.checked; pixelSizeDimX[i] = pixResXFld.value; pixelSizeDimY[i] = pixResYFld.value; gapThick[i] = gapThickBox.value; sliceThick[i] = sliceThickBox.value; voxelResVerified[i] = voxelResVerifiedBox.checked; rawDataType[i]=rawDataTypeBox.currentItem; nrOfVolumes[i]=nrOfVolumesBox.value; skipVolumes[i]=skipVolumesBox.value; createPseudoAMR[i]=createPseudoAMRBox.checked; nrOfSlices[i]=nrOfSlicesBox.value; swapBytes[i]= swapBytesBox.checked; xResImg[i]=xResImgBox.value; yResImg[i]=yResImgBox.value; bytePerPixel[i]=bytePerPixelBox.value; if (mosaicArray[i]) { nrOfVolsInImage[i]=nrOfVolsInImageBox.value; xResBf[i]=xResBfBox.value; yResBf[i]=yResBfBox.value; } } } else { MessageBox.information("You chose to end the script"); bvqx.Exit(); } projLog.writeLine(" "); projLog.writeLine("PROJECT PARAMETERS"); projLog.writeLine("TRval: " + TRval[i]); projLog.writeLine("interSliceTimeVal: " + interSliceTimeVal[i]); projLog.writeLine("timeResVerified: "+ timeResVerified[i]); projLog.writeLine("pixelSizeDimX: " + pixelSizeDimX[i]); projLog.writeLine("pixelSizeDimY: " + pixelSizeDimY[i]); projLog.writeLine("gapThick: " + gapThick[i]); projLog.writeLine("sliceThick: " + sliceThick[i]); projLog.writeLine("voxelResVerified: " + voxelResVerified[i]); projLog.writeLine("rawDataType: " + rawDataType[i]); projLog.writeLine("nrOfVolumes: " + nrOfVolumes[i]); projLog.writeLine("skipVolumes: " + skipVolumes[i]); projLog.writeLine("createPseudoAMR: " + createPseudoAMR[i]); projLog.writeLine("nrOfSlices: " + nrOfSlices[i]); projLog.writeLine("swapBytes: " + swapBytes[i]); projLog.writeLine("xResImg: " + xResImg[i]); projLog.writeLine("yResImg: " + yResImg[i]); projLog.writeLine("bytePerPixel: " + bytePerPixel[i]); if (mosaicArray[i]) { projLog.writeLine("nrOfVolsInImage: " + nrOfVolsInImage[i]); projLog.writeLine("xResBf: " + xResBf[i]); projLog.writeLine("yResBf: " + yResBf[i]); } } // ****************************** ASK PREPROCESSING PREFERENCES ********************** // Declare variables and provide initial values var motionCorr = false; var sliceTimeCorr = false; var spatialGaussSmooth = false; var spatialGaussKernel = 4; var spatialGaussUnit = "mm"; var linearTrendRem = false; var tempHighPassFilter = false; var tempHighPassValue = 3; var tempHighPassUnit = "cycles"; var tempGaussSmooth = false; var tempGaussKernel = 2.8; var tempGaussUnit = "s"; var sliceCorrType = "Ascending-Interleaved"; var preprocDialog = new Dialog; preprocDialog.title = "Values needed for the preprocessing of FMR files"; preprocDialog.width = 300; preprocDialog.newTab("Parameters"); // Create the user interface var explPreprLbl = new Label; explPreprLbl.text = "Preprocessing is used to reduce artifacts in the data. <BR> Please choose the preprocessing options for the current projects"; var motionCorrBox = new CheckBox; motionCorrBox.text = "3D Motion correction"; var sliceTimeCorrBox = new CheckBox; sliceTimeCorrBox.text = "Slice time correction"; var refCorrVolumeBox = new SpinBox; refCorrVolumeBox.label = "Reference volume for motion correction"; function numerically( a, b ) { return a < b ? -1 : a > b ? 1 : 0; } for (i=0; i<nrOfVolumes.length; i++) { volArray[i] = nrOfVolumes[i]; } volArray.sort( numerically ); refCorrVolumeBox.value = refCorrVolume; refCorrVolumeBox.maximum = volArray[volArray.length-1]; var ISABox = new CheckBox; ISABox.text = "Use motion correction combined with intrasession alignment"; var fmrNameBox = new CheckBox; fmrNameBox.text = "Use original name for FMR project"; fmrNameBox.checked = true; var sliceTimeCorrTypeBox = new ComboBox; sliceTimeCorrTypeBox.label = "Slice acquisition order"; sliceTimeCorrTypeBox.itemList= ["Ascending", "Ascending-Interleaved", "Ascending-Interleaved2", "Descending", "Descending-Interleaved", "Descending-Interleaved2"]; var spatialGaussSmoothBox = new CheckBox; spatialGaussSmoothBox.text = "Spatial Gaussian smoothing"; var spatialGaussKernelBox = new SpinBox; spatialGaussKernelBox.value = 4; spatialGaussKernelBox.label = "FWHM"; var spatialGaussUnitBox = new ComboBox; spatialGaussUnitBox.label = "Unit"; spatialGaussUnitBox.itemList = ["mm", "pixel"]; var linearTrendRemBox = new CheckBox; linearTrendRemBox.text = "Linear trend removal (not necessary with high pass filtering)"; var tempHighPassFilterBox = new CheckBox; tempHighPassFilterBox.text = "Temporal high pass filter"; var tempHighPassUnitBox = new ComboBox; tempHighPassUnitBox.itemList = ["cycles"]; var tempHighPassValueBox = new SpinBox; tempHighPassValueBox.label = "High pass value"; tempHighPassValueBox.value = 3; var tempGaussSmoothBox = new CheckBox; tempGaussSmoothBox.text = "Temporal Gaussian smoothing"; var tempGaussKernelBox = new NumberEdit; tempGaussKernelBox.value = 2.8; tempGaussKernelBox.label = "Frequency"; var tempGaussUnitBox = new ComboBox; tempGaussUnitBox.label = "Unit"; tempGaussUnitBox.itemList = ["s"]; // *** add components to user interface preprocDialog.add(explPreprLbl); preprocDialog.add(motionCorrBox); preprocDialog.add(refCorrVolumeBox); preprocDialog.add(ISABox); preprocDialog.add(fmrNameBox); preprocDialog.add(sliceTimeCorrBox); preprocDialog.add(sliceTimeCorrTypeBox); preprocDialog.add(spatialGaussSmoothBox); preprocDialog.add(spatialGaussKernelBox); preprocDialog.add(spatialGaussUnitBox); preprocDialog.add(linearTrendRemBox); preprocDialog.add(tempGaussSmoothBox); preprocDialog.add(tempGaussKernelBox); preprocDialog.add(tempGaussUnitBox); preprocDialog.add(tempHighPassFilterBox); preprocDialog.add(tempHighPassUnitBox); preprocDialog.add(tempHighPassValueBox); preprocDialog.okButtonText = "OK"; if (preprocDialog.exec()) { // *** Retrieve values that are entered by user motionCorr = motionCorrBox.checked; sliceTimeCorr = sliceTimeCorrBox.checked; sliceCorrType = sliceTimeCorrTypeBox.currentItem; spatialGaussSmooth = spatialGaussSmoothBox.checked; spatialGaussKernel = spatialGaussKernelBox.value; spatialGaussUnit = spatialGaussUnitBox.currentItem; linearTrendRem = linearTrendRemBox.checked; tempHighPassFilter = tempHighPassFilterBox.checked; tempHighPassValue = tempHighPassValueBox.value; tempHighPassUnit = tempHighPassUnitBox.currentItem; tempGaussSmooth = tempGaussSmoothBox.checked; tempGaussKernel = tempGaussKernelBox.value; tempGaussUnit = tempGaussUnitBox.currentItem; refCorrVolume = refCorrVolumeBox.value; useRawDataNames = fmrNameBox.checked; useISA = ISABox.checked; } projLog.writeLine(" "); projLog.writeLine("PREPROCESSING PARAMETERS"); projLog.writeLine("motionCorr: " + motionCorr); projLog.writeLine("sliceTimeCorr : " + sliceTimeCorr); projLog.writeLine("sliceCorrType: "+ sliceCorrType); projLog.writeLine("spatialGaussSmooth: " + spatialGaussSmooth); projLog.writeLine("spatialGaussKernel: " + spatialGaussKernel); projLog.writeLine("spatialGaussUnit: " + spatialGaussUnit); projLog.writeLine("linearTrendRem: " + linearTrendRem); projLog.writeLine("tempHighPassFilter: " + tempHighPassFilter); projLog.writeLine("tempHighPassValue: " + tempHighPassValue); projLog.writeLine("tempHighPassUnit: " + tempHighPassUnit); projLog.writeLine("tempGaussSmooth: " + tempGaussSmooth); projLog.writeLine("tempGaussKernel: " + tempGaussKernel); projLog.writeLine("tempGaussUnit: " + tempGaussUnit); projLog.writeLine("Reference volume for motion correction: " + refCorrVolume); projLog.writeLine("Use original file names: " + useRawDataNames); projLog.writeLine("Use intrasession alignment combined with motion correction: " + useISA); // ****************************** ASK ADDITIONAL PREPROCESSING PARAMETERS ********************** // *** process reference volumes, runs, names and run types var altFMRNamesArray = new Array(nrOfProjects); var teArray = new Array(nrOfProjects); var grbArray = new Array(nrOfProjects); var isaRefBoxArray = new Array(nrOfProjects); var isaRefArray = new Array(nrOfProjects); var autSTCprefix; // *** create dialog to choose runs var ISAdialog = new Dialog; ISAdialog.title = "Intrasession alignment reference projects and names"; ISAdialog.width = 1000; var explLbl = new Label; explLbl.text = "Please take care to choose only runs from the same session"; if (useISA) { ISAdialog.add(explLbl); } // ISAdialog.add(refISAProjectBox); for (i = 0; i < FMRNamesArray.length; i++) { var grB = "grb" + i; grB = new GroupBox; grB.title = "Project " + firstFilesArray[i]; if (!useRawDataNames) { var tmpTxtEdit = "te" + i; tmpTxtEdit = new TextEdit; teArray[i] = tmpTxtEdit; tmpTxtEdit.text = FMRNamesArray[i]; grB.add(tmpTxtEdit); } var refISAProjectBox = "isaref" + i; refISAProjectBox = new ComboBox; refISAProjectBox.label = "Reference run for ISA:"; refISAProjectBox.itemList = FMRNamesArray; refISAProjectBox.currentItem = FMRNamesArray[0]; isaRefBoxArray[i] = refISAProjectBox; if (useISA) { grB.add(refISAProjectBox); } ISAdialog.add(grB); } // *** launch dialog if (useISA || !useRawDataNames) { if (ISAdialog.exec()) { for (i=0; i<nrOfProjects; i++) { projLog.writeLine("~~~ Project:" + (i+1) + "~~~"); if (!useRawDataNames) { tmpTE = teArray[i]; altFMRNamesArray[i] = tmpTE.text; projLog.writeLine("Alternative filename for project " + i + ": " + altFMRNamesArray[i]); } if (useISA){ tmpCB = isaRefBoxArray[i]; isaRefArray[i] = tmpCB.currentItem; projLog.writeLine("Intrasubject alignment reference run:" + isaRefArray[i]); } } } } // ****************************** CREATE THE PROJECTS ********************** var saveAsSucceeded; var SCSsucceeded, MCsucceeded, SD3Dsucceeded, STCsucceeded, SGSsucceeded, LTRsucceeded, THPsucceeded, TGSsucceeded; var docFMR, mcFMR, stcFMR, sgsFMR, ltrFMR, thpFMR, tgsFMR; var projectError; var startCreateProjectTime, startProcessing, endProcessing; // will be initialized later because otherwise time stamp will be the same for both variables var startFirstProjectTime = new Date(); MessageBox.information("All necessary data are entered. The script doesn't require user input any more"); for (j=0;j<nrOfProjects;j++) { // *** set initial values per project saveAsSucceeded = false; SCSsucceeded = false; MCsucceeded = false; SD3Dsucceeded = false; STCsucceeded = false; SGSsucceeded = false; LTRsucceeded = false; THPsucceeded = false; TGSsucceeded = false; projectError = false; try { projLog.writeLine(" "); projLog.writeLine("Projectnumber: " + (j+1) + ", first file: " + firstFilesArray[j]); projLog.writeLine("Mosaic: " + ((mosaicArray[j])? "Yes" : "No")); if (useRawDataNames) { autSTCprefix = FMRNamesArray[j]; // name without extension } else { autSTCprefix = altFMRNamesArray[j]; } projLog.writeLine("STC Prefix: " + autSTCprefix); startCreateProjectTime = new Date(); projLog.writeLine("Time at start creating project: " + startCreateProjectTime.toString()); if (mosaicArray[j]) { projLog.writeLine("Starting to create mosaic FMR project..."); docFMR = bvqx.CreateProjectMosaicFMR(rawDataType[j], firstFilesArray[j], nrOfVolumes[j], skipVolumes[j],createPseudoAMR[j], nrOfSlices[j], autSTCprefix, swapBytes[j], xResBf[j], yResBf[j], bytePerPixel[j], projectPathsArray[j],nrOfVolsInImage[j], xResImg[j], yResImg[j]); } else { projLog.writeLine("Starting to create FMR project..."); docFMR = bvqx.CreateProjectFMR(rawDataType[j], firstFilesArray[j], nrOfVolumes[j], skipVolumes[j],createPseudoAMR[j], nrOfSlices[j], autSTCprefix, swapBytes[j], xResImg[j], yResImg[j], bytePerPixel[j], projectPathsArray[j]); } docFMR.TR = TRval[j]; docFMR.InterSliceTime = interSliceTimeVal[j]; docFMR.TimeResolutionVerified = true; docFMR.PixelSizeOfSliceDimX = pixelSizeDimX[j]; docFMR.PixelSizeOfSliceDimY = pixelSizeDimY[j]; docFMR.GapThickness = gapThick[j]; docFMR.SliceThickness = sliceThick[j]; docFMR.VoxelResolutionVerified = true; try { if (useRawDataNames) { saveAsSucceeded = docFMR.SaveAs(FMRNamesArray[j]); projLog.writeLine("Save project as: " + FMRNamesArray[j] + "; Result succeeded: " + saveAsSucceeded); } else { saveAsSucceeded = docFMR.SaveAs(altFMRNamesArray[j]); projLog.writeLine("Save project as: " + altFMRNamesArray[j] + "; Result succeeded: " + saveAsSucceeded); } } catch (f) { saveAsSucceeded = docFMR.Save(); projectError = saveAsSucceeded ? true : false; projLog.writeLine("SaveAs() failed. Error: " + f + ". Save() succeeded: " + saveAsSucceeded); } } catch (e) { projLog.writeLine("Error while saving FMR project: " + e); projectError = true; } // *** start preprocessing projLog.writeLine(" "); startProcessing = new Date(); projLog.writeLine("Time at start preprocessing of FMR project: " + startProcessing.toString()); if (!projectError) { try { projLog.writeLine("Starting motion correction"); if (!useISA) { MCsucceeded = docFMR.CorrectMotion(refCorrVolume); projLog.writeLine("Correct motion with reference volume '" + refCorrVolume + "' succeeded: " + MCsucceeded); } else { MCsucceeded = docFMR.CorrectMotionTargetVolumeInOtherRun(isaRefArray[j],refCorrVolume); projLog.writeLine("Correct motion with Reference volume from run: " + isaRefArray[j] + " succeeded: " + MCsucceeded); } } catch (e) { projLog.writeLine("Error while applying motion correction FMR project: " + e ); projectError = true; } } if (!projectError) { try { if (sliceTimeCorr) { projLog.writeLine("Starting slice time correction..."); if (motionCorr) { stcFMR = bvqx.OpenDocument(docFMR.FileNameOfPreprocessdFMR); docFMR.Close(); } else { stcFMR = docFMR; } var sliceCorrNr; switch (sliceCorrType) { case "Ascending": sliceCorrNr = 0; break; case "Ascending-Interleaved": sliceCorrNr = 1; break; case "Ascending-Interleaved2": sliceCorrNr = 2; break; case "Descending": sliceCorrNr = 10; break; case "Descending-Interleaved": sliceCorrNr = 11; break; case "Descending-Interleaved2": sliceCorrNr = 12; break; default: sliceCorrNr = 1; break; } SCSsucceeded = stcFMR.SliceTimeCorrection(sliceCorrNr); projLog.writeLine("Slice time correction succeeded: " + SCSsucceeded); } } catch (s) { projLog.writeLine("Error while applying slice time correction: " + s); projectError = true; } } if (!projectError) { try { if (spatialGaussSmooth) { projLog.writeLine("Starting spatial Gaussian smoothing..."); if (sliceTimeCorr) { sgsFMR = bvqx.OpenDocument(stcFMR.FileNameOfPreprocessdFMR); stcFMR.Close(); } else if (motionCorr) { sgsFMR = bvqx.OpenDocument(docFMR.FileNameOfPreprocessdFMR); } else { sgsFMR = docFMR; } SD3Dsucceeded = sgsFMR.SpatialGaussianSmoothing(spatialGaussKernel, spatialGaussUnit); projLog.writeLine("Spatial Gaussian smoothing succeeded: " + SD3Dsucceeded); } } catch (g) { projLog.writeLine("Error while applying spatial Gaussian smoothing: " + g); projectError = true; } } if (!projectError) { try { if (linearTrendRem) { projLog.writeLine("Starting linear trend removal..."); if (spatialGaussSmooth) { ltrFMR = bvqx.OpenDocument(sgsFMR.FileNameOfPreprocessdFMR); sgsFMR.Close(); } else if (sliceTimeCorr) { ltrFMR = bvqx.OpenDocument(stcFMR.FileNameOfPreprocessdFMR); stcFMR.Close(); } else if (motionCorr) { ltrFMR = bvqx.OpenDocument(docFMR.FileNameOfPreprocessdFMR); } else { ltrFMR = docFMR; } LTRsucceeded = ltrFMR.LinearTrendRemoval(); projLog.writeLine("Linear trend removal succeeded: " + LTRsucceeded); } } catch (l) { projLog.writeLine("Error while applying linear trend removal: " + l); projectError = true; } } if (!projectError) { try { if (tempHighPassFilter) { projLog.writeLine("Starting temporal high pass filtering..."); if (linearTrendRem) { thpFMR = bvqx.OpenDocument(ltrFMR.FileNameOfPreprocessdFMR); ltrFMR.Close(); } else if (spatialGaussSmooth) { thpFMR = bvqx.OpenDocument(sgsFMR.FileNameOfPreprocessdFMR); sgsFMR.Close(); } else if (sliceTimeCorr) { thpFMR = bvqx.OpenDocument(stcFMR.FileNameOfPreprocessdFMR); stcFMR.Close(); } else if (motionCorr) { thpFMR = bvqx.OpenDocument(docFMR.FileNameOfPreprocessdFMR); } else { thpFMR = docFMR; } THPsucceeded = thpFMR.TemporalHighPassFilter(tempHighPassValue, tempHighPassUnit); projLog.writeLine("Temporal high pass filtering succeeded: " + THPsucceeded); } } catch (hp) { projLog.writeLine("Error while applying high pass filtering: " + hp); projectError = true; } } if (!projectError) { try { if (tempGaussSmooth) { projLog.writeLine("Starting temporal Gaussian smoothing..."); if (tempHighPassFilter) { tgsFMR = bvqx.OpenDocument(thpFMR.FileNameOfPreprocessdFMR); } else if (linearTrendRem) { tgsFMR = bvqx.OpenDocument(ltrFMR.FileNameOfPreprocessdFMR); ltrFMR.Close(); } else if (spatialGaussSmooth) { tgsFMR = bvqx.OpenDocument(sgsFMR.FileNameOfPreprocessdFMR); sgsFMR.Close(); } else if (sliceTimeCorr) { tgsFMR = bvqx.OpenDocument(stcFMR.FileNameOfPreprocessdFMR); stcFMR.Close(); } else if (motionCorr) { tgsFMR = bvqx.OpenDocument(docFMR.FileNameOfPreprocessdFMR); } else { tgsFMR = docFMR; } TGSsucceeded = tgsFMR.TemporalGaussianSmoothing(tempGaussKernel, tempGaussUnit); projLog.writeLine("Temporal Gaussian smoothing succeeded: " + TGSsucceeded); } } catch (tgsError) { projLog.writeLine("Error while applying temporal Gaussian smoothing: " + tgsError); } } //*** log process duration for benchmark purposes // note: the minutes can be rounded off with seconds when (implicit Number).toPrecision(2) will be available (see QX Scripting Guide > Chapter 2C > Number) projLog.writeLine(" "); endProcessing = new Date(); projLog.writeLine("Finished at " + endProcessing.toString()); var projDur = (endProcessing.getTime() - startCreateProjectTime.getTime())/1000; projLog.writeLine("Duration of processing this FMR project: " + ((projDur > 60) ? projDur/60 + " minutes" : projDur.toString() + " seconds")); var createDur = (startProcessing.getTime() - startCreateProjectTime.getTime())/1000; projLog.writeLine("Duration of creating FMR project (& adjust parameters): " + ((createDur > 60) ? createDur/60 + " minutes" : createDur.toString() + " seconds")); var preprocDur = ( endProcessing.getTime() - startProcessing.getTime())/1000; projLog.writeLine("Duration of preprocessing FMR project: " + ((preprocDur > 60) ? preprocDur/60 + " minutes" : preprocDur.toString() + " seconds")); } var totDur = (endProcessing.getTime() - startFirstProjectTime.getTime())/1000; projLog.writeLine(" "); projLog.writeLine("Total processing time: " + ((totDur > 60) ? totDur/60 + " minutes" : totDur.toString() + " seconds")); projLog.writeLine(" "); projLog.writeLine("*** Finished. *** "); MessageBox.information("Script is ready; log file is available at: " + projLog.fullName); projLog.close(); } *CreateVMRviaDialog.qsF/* CreateVMRviaDialog.qs This script is meant to create a VMR project via a user interface.. The script can be run directly via menu > Scripts > CreateVMRviaDialog or via doubleclicking on the function name 'CreateVMRviaDialog()' in the 'Defined Functions' window of the Script Editor. The script does not require manual modifications, because a user interface for the script is provided. Pop-up dialogs will ask for necessary files and values. Hester Breman, Brain Innovation; last update: March 2005 (specification of possible file types: Philips and UFF). */ function CreateVMRviaDialog(){ // *************** CREATE VARIABLES FOR VMR PROJECT *********************** var bvqx = Application.BrainVoyagerQX; var strFileType; var strFirstFile; var longNrOfSlices; var longSwapBytes; var longx_res; var longy_res; var longbyte_per_pixel; // *************** CREATE VMR DIALOG *********************** var paramDialog = new Dialog; paramDialog.title = "Values needed for the creation of a VMR project"; paramDialog.width=400; var rawDataTypeBox = new ComboBox; rawDataTypeBox.label = "Raw data type"; // own filetypes, defined in a *.uff file, can be added to the itemlist and should bear the name of the uff file, f.e. "BRIK" in case the file is called "BRIK.uff" rawDataTypeBox.itemList= ["DICOM", "ANALYZE","SIEMENS","GE_I","GE_MR", "PHILIPS_REC"]; var nrOfSlicesBox = new NumberEdit; nrOfSlicesBox.label = "Nr of slices"; var swapBytesBox = new CheckBox; swapBytesBox.text = "Swap bytes"; swapBytesBox.checked = true; var xResImgBox = new NumberEdit; xResImgBox.label = "x resolution image"; var yResImgBox = new NumberEdit; yResImgBox.label = "y resolution image"; var bytePerPixelBox = new SpinBox; bytePerPixelBox.label = "Bytes per pixel"; bytePerPixelBox.value = 2; paramDialog.add(rawDataTypeBox); paramDialog.add(nrOfSlicesBox); paramDialog.add(swapBytesBox); paramDialog.add(xResImgBox); paramDialog.add(yResImgBox); paramDialog.add(bytePerPixelBox); paramDialog.okButtonText = "OK"; // *************** COLLECT DATA ABOUT VMR PROJECT *********************** MessageBox.information("Please choose the first file of the anatomical series"); strFirstFile = FileDialog.getOpenFileName(); if (paramDialog.exec()) { strFileType = rawDataTypeBox.currentItem; longNrOfSlices = nrOfSlicesBox.value; longSwapBytes = swapBytesBox.checked; longx_res = xResImgBox.value; longy_res = yResImgBox.value; longbyte_per_pixel = bytePerPixelBox.value; } // *************** CREATE THE VMR PROJECT *********************** var docVMR = bvqx.CreateProjectVMR(strFileType, strFirstFile, longNrOfSlices, longSwapBytes, longx_res, longy_res, longbyte_per_pixel); docVMR.Save(); MessageBox.information("End of VMR Project script"); } Preprocessing.qs#‚ // // Example script for preprocessing a FMR project (single run) // Feel free to adapt this script to your needs // // Created by Rainer Goebel, last modified May 07 2003 // function Preprocess_FMR() { var ret = BrainVoyagerQX.TimeOutMessageBox( "This script function will run standard FMR preprocessing steps.\n\nYou can cancel this script by pressing the 'ESCAPE' key.", 8); if( !ret ) return; // // Create a new FMR or open a previously created one. Here we open the "CG_OBJECTS.fmr" demo file // var docFMR = BrainVoyagerQX.OpenDocument(ObjectsRawDataPath + "CG_OBJECTS.fmr"); // // Set spatial and temporal parameters relevant for preprocessing // You can skip this, if you have checked that these values are set when reading the data // To check whether these values have been set already (i.e. from header), use the "VoxelResolutionVerified" and "TimeResolutionVerified" properties // if( !docFMR.TimeResolutionVerified ) { docFMR.TR = 4500; docFMR.InterSliceTime = 155; docFMR.TimeResolutionVerified = true; } if( !docFMR.VoxelResolutionVerified ) { docFMR.PixelSizeOfSliceDimX = 1.5625; docFMR.PixelSizeOfSliceDimY = 1.5625; docFMR.SliceThickness = 5; docFMR.GapThickness = 1; docFMR.VoxelResolutionVerified = true; } // We also link the PRT file, if available (if no path is specified, the program looks in folder of dcoument) docFMR.LinkStimulationProtocol( "CG_OBJECTS.prt" ); // We save the new settings into the FMR file docFMR.Save(); // // Preprocessing step 1: 3D motion correction // ret = BrainVoyagerQX.TimeOutMessageBox("Preprocessing step 1: 3D motion correction.\n\nTo skip this step, press the 'ESCAPE' key.", 5); if(ret) { //docFMR.MotionCorrection3D(); docFMR.CorrectMotion(1); // uses first volume for motion correction ResultFileName = docFMR.FileNameOfPreprocessdFMR; // the current doc (input FMR) knows the name of the automatically saved output FMR docFMR.Close(); // close input FMR docFMR = BrainVoyagerQX.OpenDocument( ResultFileName ); // Open motion corrected file (output FMR) and assign to our doc var } // // Preprocessing step 2: Slice time correction // ret = BrainVoyagerQX.TimeOutMessageBox("Preprocessing step 2: Slice time correction.\n\nTo skip this step, press the 'ESCAPE' key.", 5); if(ret) { docFMR.SliceTimeCorrection( 1 ); // 0 -> Ascending, 1 -> Asc-Interleaved, 2 -> Asc-Int2, 10 -> Descending, 11 -> Desc-Int, 12 -> Desc-Int2 ResultFileName = docFMR.FileNameOfPreprocessdFMR; docFMR.Close(); // docFMR.Remove(); // close or remove input FMR docFMR = BrainVoyagerQX.OpenDocument( ResultFileName ); } // // Preprocessing step 3: Spatial Gaussian Smoothing (not recommended for individual analysis with a 64x64 matrix) // ret = BrainVoyagerQX.TimeOutMessageBox("Preprocessing step 3: Spatial gaussian smoothing.\n\nTo skip this step, press the 'ESCAPE' key.", 5); if(ret) { docFMR.SpatialGaussianSmoothing( 4, "mm" ); // FWHM value and unit ResultFileName = docFMR.FileNameOfPreprocessdFMR; docFMR.Close(); // docFMR.Remove(); // close or remove input FMR docFMR = BrainVoyagerQX.OpenDocument( ResultFileName ); } // // Preprocessing step 4: Temporal High Pass Filter, includes Linear Trend Removal // ret = BrainVoyagerQX.TimeOutMessageBox("Preprocessing step 4: Temporal high-pass filter.\n\nTo skip this step, press the 'ESCAPE' key.", 5); if(ret) { docFMR.TemporalHighPassFilter( 3, "cycles" ); ResultFileName = docFMR.FileNameOfPreprocessdFMR; docFMR.Close(); // docFMR.Remove(); // close or remove input FMR docFMR = BrainVoyagerQX.OpenDocument( ResultFileName ); } // // Preprocessing step 5: Temporal Gaussian Smoothing (not recommended for event-related data) // ret = BrainVoyagerQX.TimeOutMessageBox("Preprocessing step 5: Temporal gaussian smoothing.\n\nTo skip this step, press the 'ESCAPE' key.", 5); if(ret) { docFMR.TemporalGaussianSmoothing( 20, "s" ); ResultFileName = docFMR.FileNameOfPreprocessdFMR; docFMR.Close(); // docFMR.Remove(); // close or remove input FMR docFMR = BrainVoyagerQX.OpenDocument( ResultFileName ); } // docFMR.Close() // you may want to close the final document, i..e to preprocess another run } function MotionCorrection() { var docFMR = BrainVoyagerQX.ActiveDocument(); docFMR.MotionCorrection3D(); ResultFileName = docFMR.FileNameOfPreprocessdFMR; docFMR.Close(); docFMR = BrainVoyagerQX.OpenDocument( ResultFileName ); } createVTCs.qs\Þ/* createVTCs.qs This script is meant to create one or several volume time courses (VTC). The script can be run directly via menu > Scripts > CreateVTCFiles or via doubleclicking on the function name 'CreateVTCFiles()' in the 'Defined Functions' window of the Script Editor. The script does not require manual modifications, because a user interface for the script is provided. Pop-up dialogs will ask for necessary files and values. Hester Breman, Brain Innovation, March 2005. */ var BVQX = Application.BrainVoyagerQX; var baseName; // or add directory name where files are located var baseDir; // directory object; will be set to current; VTCs will be saved there var vmrSpaceIATrfFileName; // or add filename: " = <your FMR file to your VMR file>_CLEAN_IA.trf"; var vmrSpaceFATrfFileName; // or add filename: " = <your FMR file to your VMR file>_CLEAN_FA.trf"; var acpcSpaceIATrfFileName; // or add filename: " = <your FMR file to your VMR file>_CLEAN_ACPC_IA.trf"; var acpcSpaceFATrfFileName; // or add filename: " = <your FMR file to your VMR file>_CLEAN_ACPC_FA.trf"; var talSpaceIATrfFileName; // or add filename: " = <your FMR file to your VMR file>_CLEAN_TAL_IA.trf"; var talSpaceFATrfFileName; // or add filename: " = <your FMR file to your VMR file>_CLEAN_TAL_FA.trf"; var ACPCTRFFileName; // or add filename: " = <your FMR file to your VMR file>_ACPC.trf"; var talCoordsFileName; // or add filename: " = <your FMR file to your VMR file>_CLEAN_ACPC.tal"; var resolution = 1; // resolution in mm var interpolationMethod = 1; // trilinear interpolation var boundingBoxIntensityTresh = 100; // voxel intensity value threshold default at 100 var fmrName; // or add filename: " = <name>_firstvol.fmr"; var vmrNameInOrigSpace; // or add filename: " = <name>_CLEAN.vmr"; var vmrNameInACPC; // or add filename: " = <name>_ACPC.vmr"; var vmrNameInTAL; // or add filename: " = <name>_CLEAN_TAL.vmr"; 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 CreateVTCFiles(){ var classicalVTC = false; var vtcInVMRspace = false; var vtcInACPCspace = false; var vtcInTALspace = false; /* uncomment this when the base directory needs to be set automatically (in case the user interface will not be used) var curDir = new Dir; var baseName = curDir.canonicalPath + "/VTCs"; baseDir = new Dir(baseName); BVQX.SetCurrentDirectory(baseDir); */ // *** user interface *** // create main dialog var choiceDialog = new Dialog; choiceDialog.title = "Please choose the VTCs to be created"; // create components on main dialog var classVTCchb = new CheckBox; classVTCchb.text = "Create classical VTC in Talairach space"; var origVTCchb = new CheckBox; origVTCchb.text = "Create VTC in original space"; var acpcVTCchb = new CheckBox; acpcVTCchb.text = "Create VTC in ACPC space"; var talVTCchb = new CheckBox; talVTCchb.text = "Create VTC in Talairach space with extended parameters"; // add components to main dialog choiceDialog.add(classVTCchb); choiceDialog.add(origVTCchb); choiceDialog.add(acpcVTCchb); choiceDialog.add(talVTCchb); if (choiceDialog.exec()) { classicalVTC = classVTCchb.checked; vtcInVMRspace = origVTCchb.checked; vtcInACPCspace = acpcVTCchb.checked; vtcInTALspace = talVTCchb.checked; } // *** get filenames (comment this part out when entering the filenames on beforehand without using the interface) baseName = FileDialog.getExistingDirectory("*.*", "Please select a directory to save the VTCs"); vmrSpaceIATrfFileName = (vtcInVMRspace ? FileDialog.getOpenFileName("*_IA.trf", "Please select the initial alignment file for the VTC in VMR space") : ""); vmrSpaceFATrfFileName = (vtcInVMRspace ? FileDialog.getOpenFileName("*_FA.trf", "Please select the fine alignment file for the VTC in VMR space") : ""); acpcSpaceIATrfFileName = (vtcInACPCspace ? FileDialog.getOpenFileName("*_ACPC_IA.trf", "Please select the initial alignment file for the VTC in ACPC space") : ""); acpcSpaceFATrfFileName = (vtcInACPCspace ? FileDialog.getOpenFileName("*_ACPC_FA.trf", "Please select the fine alignment file for the VTC in ACPC space") : ""); talSpaceIATrfFileName = ((classicalVTC || vtcInTALspace) ? FileDialog.getOpenFileName("*_TAL_IA.trf", "Please select the initial alignment file for the VTC in Talairach space") : ""); talSpaceFATrfFileName = ((classicalVTC || vtcInTALspace) ? FileDialog.getOpenFileName("*_TAL_FA.trf", "Please select the fine alignment file for the VTC in Talairach space") : ""); ACPCTRFFileName = ((classicalVTC ||vtcInACPCspace || vtcInTALspace) ? FileDialog.getOpenFileName("*_ACPC.trf", "Please select the VMR to ACPC space transformation file") : ""); talCoordsFileName = ((classicalVTC || vtcInTALspace) ? FileDialog.getOpenFileName("*.tal", "Please select the Talairach coordinates file") : ""); fmrName = FileDialog.getOpenFileName("*_firstvol.fmr", "Please select the FMR project to be transformed"); vmrNameInOrigSpace = (vtcInVMRspace ? FileDialog.getOpenFileName("*.vmr", "Please select the untransformed VMR") : ""); vmrNameInACPC = (vtcInACPCspace ? FileDialog.getOpenFileName("*_ACPC.vmr", "Please select the VMR in ACPC space") : ""); vmrNameInTAL = ((classicalVTC || vtcInTALspace) ? FileDialog.getOpenFileName("*_TAL.vmr", "Please select the VMR in Talairach space") : ""); // *** var projLogName = baseName + "/createVTCLog.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 CreateVTCFiles(), date: " + today.toString()); projLog.writeLine("******************************************************************************************"); projLog.writeLine(" "); projLog.writeLine("FMR name: " + fmrName); projLog.writeLine(" "); // *** values for VTC creation (comment this part out when entering the parameter values on beforehand without using the interface) if (vtcInVMRspace || vtcInACPCspace || vtcInTALspace) { var paramsDialog = new Dialog; paramsDialog.title = "Please enter the parameter values for the VTC creation"; 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; 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; paramsDialog.add(resBox); paramsDialog.add(interpolBox); paramsDialog.add(intensityBox); if (paramsDialog.exec()) { 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); } } // *** var fmrFile = new File(fmrName); var fmrBaseName = fmrFile.baseName; newVTCname = baseName + "/" + fmrBaseName + "classicalVTCinTAL.vtc"; if (classicalVTC) { docVMR = BVQX.OpenDocument(vmrNameInTAL); projLog.writeLine(" "); projLog.writeLine("Files for creation of classical VTC in Talairach space:"); projLog.writeLine(vmrNameInTAL); projLog.writeLine(fmrName); projLog.writeLine(talSpaceIATrfFileName); projLog.writeLine(talSpaceFATrfFileName); projLog.writeLine(ACPCTRFFileName); projLog.writeLine(talCoordsFileName); try { createVTCsucceeded = docVMR.CreateVTC(fmrName, talSpaceIATrfFileName, talSpaceFATrfFileName, ACPCTRFFileName, talCoordsFileName, newVTCname); projLog.writeLine("CreateVTC() succeeded: " + (createVTCsucceeded? "Yes" : "No") + "; VTC name: " + newVTCname); } catch (error) { projLog.writeLine("Error creating VTC with CreateVTC(): " + error); } docVMR.Close(); } // create VTC in VMR space newVTCname = baseName + "/" + fmrBaseName + "VTCinVMRspace.vtc"; if (vtcInVMRspace) { docVMR = BVQX.OpenDocument(vmrNameInOrigSpace); projLog.writeLine(" "); projLog.writeLine("Files for creation of VTC in untransformed space:"); projLog.writeLine(fmrName); projLog.writeLine(vmrSpaceIATrfFileName); projLog.writeLine(vmrSpaceFATrfFileName); try { createVTCsucceeded = docVMR.CreateVTCInVMRSpace(fmrName, vmrSpaceIATrfFileName, vmrSpaceFATrfFileName, newVTCname, resolution, interpolationMethod, boundingBoxIntensityTresh); projLog.writeLine("CreateVTCInVMRSpace() succeeded: " + (createVTCsucceeded? "Yes" : "No") + "; VTC name: " + newVTCname); } catch (error) { projLog.writeLine("Error creating VTC with CreateVTCInVMRSpace(): " + error); } docVMR.Close(); } // create VTC in ACPC space newVTCname = baseName + "/" + fmrBaseName + "VTCinACPCspace.vtc"; if (vtcInACPCspace) { docVMR = BVQX.OpenDocument(vmrNameInACPC); projLog.writeLine(" "); projLog.writeLine("Files for creation of VTC in ACPC space:"); projLog.writeLine(fmrName); projLog.writeLine(vmrSpaceIATrfFileName); projLog.writeLine(vmrSpaceFATrfFileName); projLog.writeLine(ACPCTRFFileName); try { createVTCsucceeded = docVMR.CreateVTCInACPCSpace(fmrName, vmrSpaceIATrfFileName, vmrSpaceFATrfFileName, ACPCTRFFileName, newVTCname, resolution, interpolationMethod, boundingBoxIntensityTresh); projLog.writeLine("CreateVTCInACPCSpace() succeeded: " + (createVTCsucceeded? "Yes" : "No") + "; VTC name: " + newVTCname); } catch (error) { projLog.writeLine("Error creating VTC with CreateVTCInACPCSpace(): " + error); } docVMR.Close(); } // create VTC in TAL space newVTCname = baseName + "/" + fmrBaseName + "VTCinTALspace.vtc"; if (vtcInTALspace) { docVMR = BVQX.OpenDocument(vmrNameInTAL); projLog.writeLine(" "); projLog.writeLine("Files for creation of VTC in Talairach space:"); projLog.writeLine(vmrNameInTAL); projLog.writeLine(fmrName); projLog.writeLine(talSpaceIATrfFileName); projLog.writeLine(talSpaceFATrfFileName); projLog.writeLine(ACPCTRFFileName); projLog.writeLine(talCoordsFileName); try { createVTCsucceeded = docVMR.CreateVTCInTALSpace(fmrName, talSpaceIATrfFileName, talSpaceFATrfFileName, ACPCTRFFileName, talCoordsFileName, newVTCname, resolution, interpolationMethod, boundingBoxIntensityTresh); projLog.writeLine("CreateVTCInTALSpace() succeeded: " + (createVTCsucceeded? "Yes" : "No") + "; VTC name: " + newVTCname); } catch (error) { projLog.writeLine("Error creating VTC with CreateVTCInTALSpace(): " + error); } docVMR.Close(); } projLog.writeLine("CreateVTCFiles() script finished."); projLog.close(); MessageBox.information("CreateVTCFiles() script finished. A log file has been created: " + projLogName); } activeDocInfo.qs À/* Function to illustrate the use of the QSA language in BrainVoyager QX Hester Breman, Brain Innovation, 2003 */ function activeDocInfo() { var bvqx = Application.BrainVoyagerQX; var BVdoc; var scriptPath; var anyDialog; anyDialog = new Dialog; BVdoc = bvqx.ActiveDocument(); if (BVdoc == null) { var lblMessage = new Label; lblMessage.text = "No document active."; anyDialog.add(lblMessage); anyDialog.okButtonText = "Open *.fmr document"; if (anyDialog.exec()) { var BVfilename = FileDialog.getOpenFileName("*.fmr"); BVdoc = bvqx.OpenDocument(BVfilename); } } else { bvDialog = new Dialog; bvDialog.caption = "BrainVoyager document info"; bvDialog.addSpace(20); bvDialog.width = 350; bvDialog.newTab("Document properties"); var lblFMRname = new Label; lblFMRname.text = "<B><FONT FACE='Tahoma'>Name of active document: </B>" + BVdoc.name; bvDialog.add(lblFMRname); var lblTR = new Label; lblTR.text = "<B><FONT FACE='Tahoma'>TR: </B>" + BVdoc.TR; bvDialog.add(lblTR); var lblSliceThick = new Label; lblSliceThick.text = "<B><FONT FACE='Tahoma'>Slice Thickness: </B>" + BVdoc.SliceThickness; bvDialog.add(lblSliceThick); var lblGapThick = new Label; lblGapThick.text = "<B><FONT FACE='Tahoma'>Gap Thickness: </B>" + BVdoc.GapThickness; bvDialog.add(lblGapThick); bvDialog.newTab("Associated files"); var lblStimProtName = new Label; lblStimProtName.text = "<B><FONT FACE='Tahoma'>Protocol name: </B>" + BVdoc.StimulationProtocolFile; bvDialog.add(lblStimProtName); bvDialog.addSpace(5); bool = bvDialog.exec(); } } &WorkingWithFiles.qs/4/* WorkingWithFiles.qs This script shows how to write and read text files, f.e. a log file Functions in this script: writeLogfile(): uses open(), writeLine() and close() commands readBVTextFile(): uses different properties of file objects findFileType(): creates a list with all files in a directory of a certain extension HB, BI, 2004. */ function writeLogfile(){ var bvqx = Application.BrainVoyagerQX; var VMRproject; var ObjectsRawDataPath; var projectLog; // this will be the file wherein the logging information is written var today = new Date(); // create a date object that will contain today's date because the date is not set MessageBox.information("This script will create a VMR project. Please choose the directory where the anatomical image files are located"); ObjectsRawDataPath = FileDialog.getExistingDirectory() + "/"; projectLog = new File(ObjectsRawDataPath + "scriptLog.txt"); // creation of file by indicating its path, name and extension projectLog.open(projectLog.WriteOnly); // after creation, the file should be opened; in this case in writing mode: "File.WriteOnly" projectLog.writeLine("******************************************************************************************"); projectLog.writeLine("Logfile for creating a VMR project, date: " + today.toString()); projectLog.writeLine("******************************************************************************************"); projectLog.writeLine(" "); projectLog.writeLine("Before creation of VMR project"); var VMRproject = bvqx.CreateProjectVMR( "SIEMENS", ObjectsRawDataPath + "5807-130-1924.ima", 170, true, 256, 256, 2 ); projectLog.writeLine("After creation of VMR project; location of VMR file: " + ObjectsRawDataPath); VMRproject.Save(); VMRproject.Close(); projectLog.writeLine("End of script"); projectLog.close(); MessageBox.information("End of script! Logfile available in folder: " + ObjectsRawDataPath); } function readBVTextFile() { MessageBox.information("This script will read a BrainVoyager text file (*.pos, *.prt, *.log, *.avg, *.txt, *.fmr, *.rtc)." + "Please indicate which file should be read..."); var txtFileName = FileDialog.getOpenFileName(); // dialog for user to select a file var textForLabel; // the value for this variable will be added later in the function, while reading the text var nrOfLinesCounter = 0; // preparation for action var displayDialog = new Dialog; // this is a base for a dialog displayDialog.title = "Contents of logfile"; // as it reveals, the title... displayDialog.width = 500; // ... and width in pixels... var contentsLabel = new Label; // create a widget (component) for the dialog (otherwise the dialog is empty) to contain the text txtFile = new File(txtFileName); // creation of file object by indicating its path, name and extension; the file itself already exists, it is selected by the user earlier if (txtFile.extension == "txt" || txtFile.extension == "fmr" || txtFile.extension == "log" || txtFile.extension == "pos" || txtFile.extension == "prt" || txtFile.extension == "avg" || txtFile.extension == "rtc") { txtFile.open(txtFile.ReadOnly); // after creation, the file should be opened; in this case in reading mode: "File.ReadOnly" while (!txtFile.eof) { // this means: while the end of the file (end-of-file: eof) is not (='!') reached, do... try { // the function will try to execute the command(s). when the program fails, it will do what is written in the 'catch' part textForLabel += txtFile.readLine(); // the '+=' assignment is used to add information to already existing information, instead of replacement by '=' nrOfLinesCounter += 1; // number of lines incremented by 1 } catch (e) { // any error should be caught by this command, in variable 'e' MessageBox.warning("Something went wrong: " + e); // error message plus error type is shown to user } } contentsLabel.text = textForLabel; // the text, read from the file, is communicated to the label widget displayDialog.add(contentsLabel); // the label widget is connected to the dialog displayDialog.exec(); // the dialog with the text in the label widget will appear to the user } else { var warningText = "This type of file cannot be read by this function. Would you like to try to read another text file?"; var responseNr = MessageBox.warning(warningText, MessageBox.Yes, MessageBox.No); // A messagebox pops up, with the warning text and a 'Yes' and 'No' button if (responseNr == 3) { // MessageBox returns number for button that was pressed. OK: 1; Cancel: 2; Yes: 3; No: 4; Abort: 5; Retry: 6; Ignore: 7 MessageBox.information("Please try again..."); // If no button is indicated, for an information message box the default button will be 'OK' readBVTextFile(); // The function invokes itself } } } function findFileType() { // create an (empty) directory object var BVprojectDir = new Dir; MessageBox.information("Please enter the directory where you'd like to search..."); // obtain a directory from the user var path = FileDialog.getExistingDirectory(); // set the directory of the directory object BVprojectDir.path = path; var fileType = Input.getText("Please enter the file extension that you are looking for, for example \"*.pos\""); // ask the directory object to which files there are in the directory, with the filter specified in the 'fileType' variable; when filter "*.*" is used, all files are shown var entryList = BVprojectDir.entryList(fileType); // the list with files, in the variable 'entryList', is converted to a string for showing to the user (of course it is nicer to use a proper dialog when there are many files to be shown...) MessageBox.information("The following files were found in the directory " + path + ": " + entryList.toString()); } HelpFuncs.qs¢ function Close_Active_Document() { //var docs = BrainVoyagerQX.Documents(); //MessageBox.information( typeof(docs) ); BrainVoyagerQX.ActiveDocument().Close(); } function CheckDoc( Doc ) { if(typeof(Doc) == "undefined") { MessageBox.information( "No document available."); return false; } return true; } function Move_window() { var x = BrainVoyagerQX.x; var y = BrainVoyagerQX.y; for(i=1; i<=10; i++) { BrainVoyagerQX.MoveWindow(x+5*i, y+5*i); } } function Welcome_Dialog() { // build a dialog var textInput = new LineEdit; textInput.text = "Welcome to BrainVoyager QX scripting !"; textInput.label = "Greeting: "; var dialog = new Dialog; dialog.caption = "BrainVoyager QX Script"; dialog.add( textInput ); dialog.width = 300; var ok = dialog.exec(); } CreateProject.qs ò/* These functions are created to show how to create projects in BrainVoyager QX via scripting Rainer Goebel, Brain Innovation, 2003 */ function Create_FMR_Project() { var bvqx = Application.BrainVoyagerQX; // var docVMR = bvqx.CreateProjectFMR( "SIEMENS", ObjectsRawDataPath + "5807-2-4.ima", 128, 2, true, 15, "untitled-", true, 128, 128, 2, ObjectsRawDataPath ); BrainVoyagerQX.CreateProjectFMR( "SIEMENS", "E:/BrainVoyagerQX_CD/Samples/ObjectsRawData/5807-2-4.ima", 10, 2, true, 15, "untitled-", true, 64, 64, 2, "E:/BrainVoyagerQX_CD/Samples/ObjectsRawData" ); } function Create_Mosaic_FMR_Project() { var bvqx = Application.BrainVoyagerQX; var path = "E:/Notebook2Notebook/Sonata_SuperMosaic/" var docFMR = bvqx.CreateProjectMosaicFMR( "DICOM", path + "JenSch_Feddes^Bas -0003-0016-0021.dcm", 10, 2, true, 22, "untitled-", false, 1280, 1280, 2, path, 4, 64, 64 ); } function Create_VMR_Project() { var bvqx = Application.BrainVoyagerQX; var docVMR = bvqx.CreateProjectVMR( "SIEMENS", ObjectsRawDataPath + "5807-130-1924.ima", 170, true, 256, 256, 2 ); } function Create_AMR_Project() { var bvqx = Application.BrainVoyagerQX; //var docVMR = bvqx.CreateProjectAMR( "SIEMENS", ObjectsRawDataPath + "5807-1-1.ima", 3, true, 256, 256, 2 ); BrainVoyagerQX.CreateProjectAMR( "SIEMENS", "E:/BrainVoyagerQX_CD/Samples/ObjectsRawData/5807-1-1.ima", 3, true, 256, 256, 2 ); } *getEnvironmentVars.qs!/* time.qs This script shows some QSA functions on environment variables (hardware specifications and configuration) HB, BI, 2004. */ function getEnvVarWindows() { // environment variables for Windows var varList = new Array(9); varList[0] = "Platform: " + System.getenv("OS"); varList[1] = "<BR>" + "Computername: " + System.getenv("COMPUTERNAME"); varList[2] = "<BR>" + "Current dir: " + System.getenv("CD"); varList[3] = "<BR>" + "Home path: " + System.getenv("HOMEPATH"); varList[4] = "<BR>" + "Nr of processors: " + System.getenv("NUMBER_OF_PROCESSORS"); varList[5] = "<BR>" + "Processor architecture: " + System.getenv("PROCESSOR_ARCHITECTURE"); varList[6] = "<BR>" + "Prompt (command-prompt settings from cmd.exe): " + System.getenv("PROMPT"); varList[7] = "<BR>" + "Username: " + System.getenv("USERNAME"); varList[8] = "<BR>" + "Qt directory: " + System.getenv("QTDIR"); MessageBox.information("Environment variables for Windows: " + "<BR>" + varList.toString()); } function getEnvVarMac() { // environment variables for Mac var varList = new Array(6); // varList[0] = "Platform: " + System.getenv("OS"); for tcsh: OSTYPE varList[1] = "<BR>" + "Current dir: " + System.getenv("PWD"); varList[2] = "<BR>" + "Home path: " + System.getenv("HOME"); varList[3] = "<BR>" + "Prompt (command-prompt settings from cmd.exe): " + System.getenv("PROMPT"); varList[4] = "<BR>" + "Username: " + System.getenv("USER"); varList[5] = "<BR>" + "Qt directory: " + System.getenv("QTDIR"); MessageBox.information("Environment variables for Mac: " + "<BR>" + varList.toString()); } function getEnvVarLinux() { // SAMPLE FUNCTION: NOT YET USED; remove '/*' and '*/' to use the function /* var varList = new Array(8); varList[0] = "Shell: " + System.getenv("SHELL"); // Common: EDITOR, PAGER (fuer man pages), PATH, PRINTER, TERM (terminal type for editor, pager), TZ (timezone) varList[1] = "<BR>" + "Editor: " + System.getenv("EDITOR"); varList[2] = "<BR>" + "Pager (for man pages): " + System.getenv("PAGER"); varList[3] = "<BR>" + "Path: " + System.getenv("PATH"); varList[4] = "<BR>" + "Printer: " + System.getenv("PRINTER"); varList[5] = "<BR>" + "Terminal type: " + System.getenv("TERM"); varList[6] = "<BR>" + "Time zone: " + System.getenv("TZ"); varList[7] = "<BR>" + "Home: " + System.getenv("HOME"); var shell = new String(varList[0]); MessageBox.information("Common environment variables: " + "<BR>" + varList.toString()); if (shell.indexOf("C") > 0) { // case C and TC: // HOME, USER, UCBPATH, ATTPATH, UCBSHELL, ATTSHELL, LOGNAME, MAIL var varList = new Array(7); varList[0] = "<BR>" + "Mail:" + System.getenv("MAIL"); varList[1] = "<BR>" + "User: " + System.getenv("USER"); varList[2] = "<BR>" + "UCB path: " + System.getenv("UCBPATH"); varList[3] = "<BR>" + "ATT path: " + System.getenv("ATTPATH"); varList[4] = "<BR>" + "UCB shell: " + System.getenv("UCBSHELL"); varList[5] = "<BR>" + "ATT shell: " + System.getenv("ATTSHELL"); varList[6] = "<BR>" + "Logname: " + System.getenv("LOGNAME"); MessageBox.information("C/TC shell environment variables: " + "<BR>" + varList.toString()); } else if (shell.indexOf("B") > 0 || shell.indexOf("K") > 0) { // case Bourne, BASH and Korn: // BASH, BASH_VERSION, DELIVERY, DISPLAY, EDITOR, FOLDRS, HISTFILE, HISTFILESIZE, HISTSIZE, HOME var varList = new Array(7); varList[0] = "Bash: " + System.getenv("BASH"); varList[1] = "<BR>" + "User: " + System.getenv("BASH_VERSION"); varList[2] = "<BR>" + "UCB path: " + System.getenv("DELIVERY"); varList[3] = "<BR>" + "ATT path: " + System.getenv("DISPLAY"); varList[4] = "<BR>" + "UCB shell: " + System.getenv("EDITOR"); varList[5] = "<BR>" + "ATT shell: " + System.getenv("FOLDRS"); varList[6] = "<BR>" + "Logname: " + System.getenv("HISTFILE"); varList[7] = "<BR>" + "Mail:" + System.getenv("HISTFILESIZE"); MessageBox.information("Bourne/Bourne again/Korn shell environment variables: " + "<BR>" + varList.toString()); } else { MessageBox.information("No further environment variables can be retrieved: unknown shell type"); }*/ } time.qs†/* time.qs This script shows some QSA functions on time HB, BI, 2004. */ function timer(){ var d = new Date(); var msec = d.getMilliseconds(); var time1 = d.getTime(); MessageBox.information("Time 1: "+ d.toString() + ", msec: " + msec); var d2 = new Date(); var msec2 = d2.getMilliseconds(); MessageBox.information("Time 2: " + d2.toString() + ", msec2: " + msec2); var time2 = d2.getTime(); MessageBox.information("Subtract msecs: " + (msec2 - msec)); var timeDiff = time2 - time1; with (Math) { MessageBox.information("Time difference in seconds: " + timeDiff/1000 + " <P> or in minutes: " + Math.round(timeDiff/60000)); } MessageBox.information("First timepoint: " + d.toString()+ ", in msec: "+ time1 + "<P> second timepoint: " + d2.toString() + ", in msec: " + time2); }