Re: Macro problem/bug?

Posted by Wayne Rasband on
URL: http://imagej.273.s1.nabble.com/Macro-problem-bug-tp3696476p3696477.html

I suspect you are using the LSM_Toolbox to open the files. It opens  
an LSM file in a separate thread, returning before it has finished.  
Switching to the LSM_Reader at

     http://rsb.info.nih.gov/ij/plugins/lsm-reader.html

should fix the problem. You will need to remove LSM_Toolbox.jar from  
the plugins folder since the open() macro function will use it if it  
is present. Or upgrade to the latest Handle_Extra_File_Types plugin at

     http://rsb.info.nih.gov/ij/plugins/file-handler.html

which gives priority to the LSM_Reader.

-wayne

On Apr 21, 2008, at 4:36 PM, Mike Myerburg wrote:

> I made a macro to scale/calibrate, threshold, apply gaussian blur,
> change the LUT, and rename images(to blind myself).  This macro
> (“ASLCalibration”) is within a macro (“BatchProcessFolders”) that
> processes all of the images in a folder.  It also provides the  
> option to
> batch process/measure all the files, otherwise I measure the height of
> an object on each image, and then the macro continues to the next  
> image.
>
> Unless I add multiple wait() commands throughout the above steps, the
> image becomes locked, the command is called prior to the image  
> opening,
> or I get unexpected errors.  The necessary wait times vary from  
> computer to computer, which makes it difficult to use and has  
> required a lot of
> debugging to get it to work.  These wait times add up and slow the
> routine down, so it is not a good solution.
>
> I have added setOption("QueueMacros",true) as I thought that this  
> might
> help.  This has made no difference.  Is this a known bug or am I  
> missing
> something?  Any help or suggestions would be appreciated.
>
> Below are the portions of the macro that are giving me difficulty (if
> you need the whole thing I can post it, but it gets a bit long).  I  
> have
> this macro as a toolset if that makes any difference.
>
> Thanks, Mike
>
> ________________________________________________
> //Global Variables
>   var measurecount=0;       //Number of ASL measurements
>   var linecount=0;          //Number of ASL line measurements
>   var ASLLine=newArray(3);  //Array containing line measurements
>   var ShowThreshold=0;      //Toggle for showing threshold  
> automaticaly
> enabled by ASLCalibration
>   var ImageName;            //Image Name prior to renaming the window
>   var ImagesProcessed=0;    //Count of imgaes proccessed helopful  
> for resume
>   var dir;
>
> //Options
>   var AutoMode=0;           //Toggle for Full Automatic mode - User is
> asked when starting BatchMode
>   var Blinded=1;            //Toggle to enable blinded mode  - User is
> asked when starting BatchMode
>   var expertmode=1;         //Change to set the expertmode
>   var PicX=30; //Set Snapshot Dimentions X in um
>   var PicY=30; //Set Snapshot Dimentions Y in um
>   var scaleFactor=2; //Scale Factor
>
> macro "ASLCalibration" {      //Calibrates and filters image, sets
> threshold and LUT
>     measurecount=0;           //sets all the counts to zero as well
>     linecount=0;
>     ASLLine=newArray(3);
>     getVoxelSize(pWidth, pHeight, pDepth, unit);
> pHeight=pDepth; //Corrects for LSM file format
>     setVoxelSize(pWidth, pHeight, pDepth, unit);
> run("Gaussian Blur...", "sigma=2");
>
>     if (AutoMode==0) {  //Scales Image and sets LUT
> width=getWidth();
> height=getHeight();
> scaleCorrection=((1/pWidth)*scaleFactor);
> xCorrection=(scaleCorrection*pWidth);
> yCorrection=(scaleCorrection*pHeight);
> delFile=getImageID();
> run("Scale...", "x="+xCorrection+" y="+yCorrection+"
> width="+(width*xCorrection)+" height="+(height*yCorrection)+"
> interpolate create title=calibrated_"+ImageName);
> selectImage(delFile);
> close();
> selectWindow("calibrated_"+ImageName);
> run("Flip Vertically");
> run("Fire");
>     }
>
>     setAutoThreshold();
>     ShowThreshold=1;
> }
>
> macro "BatchProcessFolders" {                          //This is the
> Batch process routine
>    requires("1.33s");
>    dir = getDirectory("Choose a Directory ");
>    count = 0;
>    countFiles(dir);
>    n = 0;
>    first=0;
>    run("Set Measurements...", "area bounding area_fraction");
>
>    if (expertmode==1) {                                 //Expermode  
> options
>     Dialog.create("Batch Mode Options");
>     Dialog.addCheckbox("Automatic Batch Mode",AutoMode);  //Set  
> AutoMode
>     Dialog.addCheckbox("Blinded Mode",Blinded);           //Set  
> BlindMode
>     Dialog.addNumber("Start at file#",0);
>     Dialog.show();
>     AutoMode = Dialog.getCheckbox;
>     Blinded = Dialog.getCheckbox;
>     StartFile = Dialog.getNumber();
>     ImagesProcessed=StartFile;
>    }
>
>    setOption("QueueMacros",true);   //option to put in QueueMacro  
> mode
>
>    if (AutoMode==1) { //Put in batch mode if desired
>   setBatchMode(true);
>     }else setBatchMode(false);
>
>    processFiles(dir);
>    //print(count+" files processed");
>
>    function countFiles(dir) {
>       list = getFileList(dir);
>       for (i=0; i<list.length; i++) {
>           if (endsWith(list[i], "/"))
>               countFiles(""+dir+list[i]);
>           else
>               count++;
>       }
>   }
>
>    function processFiles(dir) {            //Also sets ImageName
>       list = getFileList(dir);
>       for (i=0; i<list.length; i++) {
>          if (i>=StartFile) {
>             if (endsWith(list[i], "/")) {
>                 ImageName=list[i];      //Set ImageName for the image
>                 processFiles(""+dir+list[i]);
>             }
>             else {
>                showProgress(n++, count);
>                path = dir+list[i];
>                ImageName=list[i];      //Set ImageName for the image
>                processFile(path);
>             }
>          }
>       }
>   }
>
>   function processFile(path) {         //This is the batch function
>       if (endsWith(path, ".lsm")) {
>
>         if (AutoMode==0) setBatchMode(true);  //Batchmode is used to
> manipulate the image and change the name
>         open(path);                           //  prior to showing  
> it on
> the screen
>
>         if (first==0) {                     //Make it wait for 2  
> sec to
> load first image
>           first++;
>           wait(2000);
>         }
>
> wait(300);
>         run("ASLCalibration");
> wait(300);
>
>         if ((Blinded==1) && (AutoMode==0)) {   //Has a long wait that
> should be removed - it is necessary to keep blinded when ImageJ  
> auto zooms
> wait(500);
> rename("blank");
> }
>
>         if (AutoMode==0) setBatchMode(false); //BatchMode is  
> disabled if
> not in AutoMode
>
>         if (AutoMode==1) {
>           ASLheight = ASLAutoMeasure();
>           ASLHeightLogger(ASLheight,"AutoMode");
>         }
>         else{
>           while (measurecount<3) wait(10);  //Wait for the user to  
> take
> three measurements
>         }
>
>         close();
>         ImagesProcessed++;
>       }
>   }
>
>   AutoMode=0;
> }
>