Login  Register

Macro slowing down

Posted by TimFeinstein on Jan 12, 2017; 2:19pm
URL: http://imagej.273.s1.nabble.com/Macro-slowing-down-tp5017866.html

I hate to ping the list twice in two days, but this one is a puzzle.  I wrote a macro that measures the difference between each frame of a time series with frame 1, with the ultimate goal of measuring the periodicity of the average difference value (with Fourier analysis in Excel) to measure ciliary beat frequency.  The macro uses nested for() loops to open each file in a folder, process it framewise and build a results table.  

The macro runs pretty fast, but if I walk away from the computer for a while at some point it will slow down by almost ten fold, making it impractical to run through large data sets overnight.  Each stack is about 200-500 kb and Fiji has 10 GB of RAM allocated, so I doubt that RAM is my problem.  I quit all other programs, prevented sleep and Fiji remains foregrounded the whole time.  I am using the latest Fiji update on OSX 10.10.5.  

It would be great to know what might cause the slowdown.  At the same time, odds are my approach could be streamlined quite a bit.  Right now the macro duplicates out frame 1 and frame n from the movie, makes an image of the difference and records the average pixel value of the difference image to a results table.  Making the new images and closing them again seems to take time, even with batch mode on.  Does anyone know how to measure the average difference between frame 1 and frame n of a stack without duplicating them out?  I pasted the macro below.  

Thanks again,


T

Timothy Feinstein, Ph.D.
Research Scientist
University of Pittsburgh Department of Developmental Biology

------------------------------------------------------------


//Analyze ciliary beat frequency using cropped movies

//Requires stable movies.  Use the stackreg plugin on
//uncropped originals if you can see sample or camera motion.

setBatchMode(true);

if(isOpen("diff_Quant")){
        selectWindow("diff_Quant");
        run("Close");
}

if(isOpen("Results")) {
        selectWindow("Results");
        run("Close");
}

run("Clear Results");

run("Set Measurements...", "mean limit display redirect=None decimal=5");

//Find the movies to analyze

files = getDirectory("Movies");
count = 0;
list = getFileList(files);
n = lengthOf(list);

//Set up the results table

for (i=0; i < n; i++) {
       
        result = 0;
        fileName = list[i];
        setResult(fileName, result, 1);
        updateResults();
}

IJ.renameResults("diff_Quant");

//Duplicate out the 1st and nth frame of each movie
//and measure the average difference

for (i=0; i <= n; i++) {
       
open(files+list[count]);
stack = getTitle();
time = 0;
fileName = list[i];
result = 0;
run("Gaussian Blur 3D...", "x=1 y=1 z=1");

n = nSlices();

for (slice=1; slice<=(300); slice++){

        selectWindow(stack);
        setSlice(1);
        run("Duplicate...", "use");
        first = getTitle();
       
        selectWindow(stack);
        setSlice(slice + 1);
        run("Duplicate...", "use");
        test = getTitle();
       
        imageCalculator("Difference create", first, test);
        diff = getTitle();
        run("Measure");
        mean = getResult("Mean", 0);
       
        selectWindow("Results");
    run("Clear Results");
    run("Close");

        selectWindow("diff_Quant");
            IJ.renameResults("Results");
            result = slice;
            setResult(fileName, result, mean);
            updateResults();
            IJ.renameResults("diff_Quant");
                       

        selectWindow(first);
        run("Close");
        selectWindow(test);
        run("Close");
        selectWindow(diff);
        run("Close");
        time = time + 0.005;

}

selectWindow(stack);
        run("Close");
        count++;
}


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html