Menu

Groovy scripting example

See demo and advanced demo, of the sample program below.

This script programmingly presses the Save MIP menu entry. It is an example of using the Groovy scripting language.

Return to Help.

import ij.IJ
import ij.WindowManager

def main() {
//  autoRunDirectory = IJ.getDirectory("imagej") + "/plugins/Scripts/Plugins/AutoRun/"
//  println(autoRunDirectory)
    readStudy = findProgram('Read Studies')
//  readStudy = findProgram('BI database')
    waitTime = 2    // Read Studies - 1->2 min, BI database - 3->4 min
    if(readStudy != null) {
        readStudy.setForceDicomDir(true)    // comment out for BI database
        sleep(2000) // wait for update in force dicomdir, comment out as above line
        numStudy=readStudy.extReadButton(-1,0)
        println("Number of studies = " + numStudy)
        for(currSdy=0; currSdy<numStudy; currSdy++) {
            if(currSdy > 0) println('Next study......')
            multSeries = readStudy.extReadButton(currSdy,0)
            procStudy()
        }
    }
}

def findProgram(match) {
    fndProg = null
    progs = WindowManager.getNonImageTitles()
    for(i=0; i<progs.length; i++) {
        pname = progs[i]
        println("    "+pname)
        if(pname.startsWith(match)) break
    }
    if(i < progs.length) fndProg = WindowManager.getWindow(pname)
    return fndProg;
}

def eraseImages() {
    imgs = WindowManager.getImageTitles()
    println('Number of images ' + imgs.length)
    for(i=0; i<imgs.length; i++) {
        win = WindowManager.getWindow(imgs[i])
        println(win)
        if(win == null || win.isClosed()) continue
        WindowManager.setCurrentWindow(win)
        win.setVisible(false)
        IJ.doCommand('Close')
        sleep(1000) // Close needs a bit of time to complete
    }
}

def procStudy() {
    sleep(45000)    // wait 45 sec for study to load
    pickOnce = false
    imgIn = WindowManager.getImageTitles()
    numSeries = imgIn.length
    if(numSeries < 2) {
        if(multSeries > 1) {
            // give a second chance - another "waitTime" min
            println("Give another " + waitTime + " min to load study")
            sleep(waitTime * 60000)
            imgIn = WindowManager.getImageTitles()
            numSeries = imgIn.length
        }
        if(numSeries < 2) {
            if(numSeries > 0) eraseImages()
            println("Input data isn't compatible with Pet-Ct")
        return;
        }
    }
    timeOut = 0
    petCt = null
    while(petCt == null) {
        petCt = findProgram('Pet-Ct')
        if(petCt == null) {
            if(++timeOut > 10) {
                eraseImages()
                println('Study timed out')
                return;
            }
            pickStudies()
            sleep(10000) // continue to wait another 10 sec
        }
    }
    // do processing here
    petCt.scriptMip()
    // done, close any open image windows
    petCt.setVisible(false)
    petCt.dispose()
    eraseImages()
}

def isChooseVisible() {
    chooseSdy = readStudy.getDlgWindow('ChoosePetCt')
    if( chooseSdy == null) return false
    if( chooseSdy.isVisible()) return true
    return false;
}

def pickStudies() {
    if( pickOnce) return    // do only once
    if( !isChooseVisible()) return null
    println('Need to choose PET and CT series')
    pickOnce = true
    int i=0
    while((tmp=chooseSdy.getSeriesName(i)) != null) {
        if( tmp.startsWith('CTAC')) {
            chooseSdy.setCheckBox(i)
            println('found CT')
        }
        if( tmp.startsWith('PET AC')) {
            chooseSdy.setCheckBox(i)
            println('found PET')
        }
        i++;
    }
    sleep(100)  // let it update the check boxes and the OK button
    chooseSdy.pressOkCancel()
    sleep(1000) // let it dispose itself
    if(isChooseVisible()) println('not dead')
}

main()

Related

Wiki: Pet Ct Viewer Help
Wiki: Save MIP

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.