Subfolders processing

fiji

#1

Hi there I am using the script attached (some parts have been taken from the internet ), it is working but I want to save a summary file for every folder processed, so this just creates a summary file at the end. It does not matter if I have 3 or 25 folders/subfolders just saves one at the end of the processing. So how can I save an individual file (summary) for every folder processed? Any suggestion is welcome.
Cheers

requires(“1.33s”);
dir = getDirectory(“Choose a Directory “);
setBatchMode(true);
count = 0;
countFiles(dir);
n = 0;
processFiles(dir);
//print(count+” files processed”);
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) {
list = getFileList(dir);
for (i=0; i<list.length; i++) {
if (endsWith(list[i], “/”))
processFiles(""+dir+list[i]);
else {
showProgress(n++, count);
path = dir+list[i];
processFile(path);
}
}
}

function processFile(path) {
if (endsWith(path, “.tif”)) {
open(path);

myDir=dir+"/Processed/";
File.makeDirectory(myDir);

sourceID = getImageID();
img_title = getTitle();

run(“Set Scale…”, “distance=2.99 known=1 unit=[um] global”);
run(“16-bit”);
run(“Enhance Contrast…”, “saturated=0.3 normalize”);
run(“Enhance Contrast”, “saturated=0.35”);
run(“Subtract Background…”, “rolling=50 light sliding”);
run(“Make Binary”);
run(“EDM Binary Operations”, “iterations=1 operation=close”);
run(“Watershed Irregular Features”, “erosion=3 convexity_threshold=1 separator_size=0-Infinity”);
run(“Fill Holes”);
run(“EDM Binary Operations”, “iterations=1 operation=erode”);

run(“Set Measurements…”, “area mean min centroid center perimeter shape median skewness kurtosis area_fraction stack redirect=None decimal=3”);
run(“Analyze Particles…”, “size=6-Infinity display clear summarize add in_situ”);
d_o = img_title + “”;
fullpath = myDir +d_o ;
saveAs(“TIFF”, fullpath);
close();

print("Processing Image: " + path + " ");

  }

}

selectWindow(“Summary”);
saveAs(“Results”, dir +“Summary.xls”);
run(“Collect Garbage”);
close("*");


#2

How about moving this bit of code as a last step inside the processFiles(dir) function?
Maybe also add a clear step after having saved the results.
BTW if you use indetation in your code it is easier to read.


#3

Hi @eljonco thanks for your response, I already tried to move that part in different positions but the only result I got is saving it for every image processed or one for all the folders. I have no idea what I can add to indicate that a folder was processed and another one will start. Regards.


#4

@Ed.DeLaO, when using nested loops combined with getFileList, you don’t have control on the order of execution, so you could process one file in a directory, then all files in its subdirectory, then other files in the original directory, and so on. Bottom line, the Summary window will not be filled consistently per folder. Therefore you need first to get a list of all the directories available, then you can process all the files in each directory separately. Finally, after processing all the files in a folder you need to save and close the Summary window. Below is a minimally modified version of your script. Note that directories named Processed are excluded from analysis, otherwise you risk to process the processed files, if you run the script more than once.

requires("1.33s");
dir = getDirectory("Choose a Directory");
setBatchMode(true);

count = 0;
countFiles(dir);
print("Found "+count+" files to process");

// generate a list of directories to process
listDirs = newArray();
listDirs = listDirectories(dir,listDirs);

n = 0;
for (i=0; i<listDirs.length; i++) {
    processFiles(listDirs[i]);
}
print(n+" files processed");

function countFiles(dir) {
    list = getFileList(dir);
    for (i=0; i<list.length; i++) {
        if (endsWith(list[i], "/") && !endsWith(list[i], "Processed/"))
            countFiles(""+dir+list[i]);
        else if (endsWith(list[i],".tif")) {
            count++;
        }
    }
}


function listDirectories(dir,dirArray) {

    dirArray = Array.concat(dirArray,dir);

    list = getFileList(dir);
    for (i=0; i<list.length; i++) {
        if (endsWith(list[i], "/") && !endsWith(list[i], "Processed/")) {
        	dirArray = listDirectories(dir+list[i],dirArray);
        }
    }    	
    return dirArray;
}


function processFiles(dir) {
    list = getFileList(dir);
    for (i=0; i<list.length; i++) {
        path = dir+list[i];
        processFile(path);
    }
    selectWindow("Summary");
    saveAs("Results", dir +"Summary.xls");
    run("Close");
}


function processFile(path) {
    if (endsWith(path, ".tif")) {

        print("Processing Image: " + path + " ");
        showProgress(n++, count);
        open(path);
        
        myDir=dir+"/Processed/"; 
        File.makeDirectory(myDir);

        sourceID = getImageID();
        img_title = getTitle();

        run("Set Scale...", "distance=2.99 known=1 unit=[um] global");
        run("16-bit");
        run("Enhance Contrast...", "saturated=0.3 normalize");
        run("Enhance Contrast...", "saturated=0.35");
        run("Subtract Background...", "rolling=50 light sliding");
        run("Make Binary");
        run("EDM Binary Operations", "iterations=1 operation=close");
        run("Watershed Irregular Features", "erosion=3 convexity_threshold=1 separator_size=0-Infinity");
        run("Fill Holes");
        run("EDM Binary Operations", "iterations=1 operation=erode");

        run("Set Measurements...", "area mean min centroid center perimeter shape median skewness kurtosis area_fraction stack redirect=None decimal=3");
        run("Analyze Particles...", "size=6-Infinity display clear summarize add in_situ");
        d_o = img_title + "";
        fullpath = myDir +d_o ;
        saveAs("TIFF", fullpath);
        close();
    }
}

#5

Hi @gcardone, thanks for your response, I had tested the script with the modification you made but I guess the location selectWindow(“Summary”) is not good because I got an error saying there is no summary window, then I changed it that part to where I used to have it and it works the same as without your modification.
I am not expertise using macros, is there a way I can say when a folder is done?
Cheers


#6

I don’t know how your data are organized, so I need to guess. Do you have empty directories? That could explain the error. In that case, the function processFiles should check if any result is generated for a given directory

function processFiles(dir) {
    list = getFileList(dir);
    for (i=0; i<list.length; i++) {
        path = dir+list[i];
        processFile(path);
    }
    if (isOpen("Summary")) {
    	selectWindow("Summary");
        saveAs("Results", dir +"Summary.xls");
        run("Close");
    }
}

#7

Hi @gcardone, this time it works!
I have a folder that contents subfolders, and those have more subfolders.
Thanks a lot!