Macro Fiji error loop

Hello everybody,
I made some progress with the macro writing but I always have an error in my loop. For some reason the loop didn’t stop after the last image and I have an error who say cannot run(“Bio-Formats Importer”, “open=” + inputPath + " autoscale color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");

I know I create 2 new folder in the folders where I have my pictures but how should I manage this problem.

Best,
//////

inputFolder = getDirectory(“choose the input directory”); //ask for the input directory
outputFolder1= inputFolder + “/Measures/”; // define outputFolder as a subfolder with the name process
File.makeDirectory(outputFolder1); // outputFolder creation
outputFolder2= inputFolder + “/Images/”; // define outputFolder as a subfolder with the name process
File.makeDirectory(outputFolder2); // outputFolder creation

processFolder(inputFolder);

// function to process all files within the folder
function processFolder(inputFolder) {
listdir = getFileList(inputFolder);

for (i=0; i<listdir.length; i++) {
setBatchMode(true); //batch mode
inputPath= inputFolder + listdir[i];
if ( File.isDirectory( inputPath ) );
run(“Bio-Formats Importer”, “open=” + inputPath + " autoscale color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
imagesName=getTitle();

//then my setting …

close();

    }

Dear Alex.h,

I think the problem is most probably this line:
if ( File.isDirectory( inputPath ) );

It should be written:

if ( File.isDirectory( inputPath ) )
run("Bio-Formats…

or

if ( File.isDirectory( inputPath ) ) {

run("Bio-Formats…

}

Otherwise it will still feed a directory into Bio-Formats

Check the templates provided for working examples:
Macro Editor > Templates > …

Hello schmiedc,
I tried both of your methods but the macro didn’t start.

With
if ( File.isDirectory( inputPath ) )
run("Bio-Formats…

the macro complain because it didn’t find an image so it cannot : imagesName=getTitle();

with the second
error cannot import with bio-format

True my solution did not really make sense to input into Bioformats. Sorry.

I would recommend to use the following template:

/*

  • Macro template to process multiple images in a folder
    */

#@ File (label = “Input directory”, style = “directory”) input
#@ File (label = “Output directory”, style = “directory”) output
#@ String (label = “File suffix”, value = “.tif”) suffix

// See also Process_Folder.py for a version of this code
// in the Python scripting language.

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}

function processFile(input, output, file) {
// Do the processing here by adding your own code.
// Leave the print statements until things work, then remove them.
print("Processing: " + input + File.separator + file);
print("Saving to: " + output);
}

1 Like

Thank you for your help,
but in my case i have to create 2 differents folders output 1 measurement and the second with binary file in .tif

inputFolder = getDirectory(“choose the input directory”); //ask for the input directory
outputFolder1= inputFolder + “/Measures/”;
File.makeDirectory(outputFolder1);
outputFolder2= inputFolder + “/Images/”;
File.makeDirectory(outputFolder2);

Your solution will process again my new tif ? We use the new tiff as a control of our segmentation

// This macro will calculate the number of nuclei in channel green
// this macro will work in batch mode

inputFolder = getDirectory(“choose the input directory”); //ask for the input directory
outputFolder1= inputFolder + “/Measures/”; // define outputFolder as a subfolder with the name process
File.makeDirectory(outputFolder1); // outputFolder creation
outputFolder2= inputFolder + “/Images/”; // define outputFolder as a subfolder with the name process
File.makeDirectory(outputFolder2); // outputFolder creation

processFolder(inputFolder);

// function to process all files within the folder
function processFolder(inputFolder) {
listdir = getFileList(inputFolder);

for (i=0; i<listdir.length; i++) {
setBatchMode(true); //batch mode
inputPath= inputFolder + listdir[i];
if ( File.isDirectory( inputPath ) );
run(“Bio-Formats Importer”, “open=” + inputPath + " autoscale color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
imagesName=getTitle();

// split channels
run(“Split Channels”);

//take infos from the pricture
list1 = getList(“image.titles”);

// select channel green
selectWindow(“C1-” + imagesName);
run(“Gaussian Blur…”, “sigma=1”);
setAutoThreshold(“Huang dark”);
//setThreshold(12, 255);
setOption(“BlackBackground”, true);
run(“Convert to Mask”);
run(“Fill Holes”);
run(“Set Measurements…”, “area redirect=None decimal=5”);
run(“Analyze Particles…”, “size=50-Infinity pixel clear summarize”);
selectWindow(“Summary”);
saveAs(“Measurements”, outputFolder1 + imagesName +"_Green_area.csv");
selectWindow(imagesName +"_Green_area.csv");
run(“Close”);

selectWindow(“C2-” + imagesName);
run(“Gaussian Blur…”, “sigma=1.50”);
setAutoThreshold(“Huang dark”);
setThreshold(37, 255);
//setThreshold(37, 255);
setOption(“BlackBackground”, true);
run(“Convert to Mask”);
run(“Watershed”);
run(“Set Measurements…”, “area redirect=None decimal=5”);
run(“Analyze Particles…”, “size=50-Infinity display clear”);
selectWindow(“Results”);
saveAs(“Measurements”, outputFolder1 + imagesName +"_number_cells.csv");
selectWindow(“Results”);
run(“Close”);

//image calculator
imageCalculator(“AND create”, “C2-” + imagesName, “C1-”+imagesName);
//erreur ne trouve pas la bonne fenêtre result
selectWindow(“Result of C2-”+ imagesName);
run(“Erode”);
run(“Erode”);
run(“Dilate”);
run(“Dilate”);
run(“Analyze Particles…”, “size=80-Infinity display clear”);
saveAs(“Measurements”, outputFolder1 + imagesName +"_number_cells_in_green.csv");
selectWindow(“Results”);
run(“Close”);

selectWindow(“C1-” + imagesName);
saveAs(“Tiff”, outputFolder2 +imagesName+ “_green”);

selectWindow(“C2-” + imagesName);
saveAs(“Tiff”, outputFolder2 +imagesName+ “_nuclei”);

close();

    }

Hm I usually strongly recommend to separate the input and the output folder. 1.) Makes it easier to separate different runs of the workflow with different settings on the same data. 2.) Allows to separate original data from intermediate files, preventing unnecessary duplication for final storage. Is there any very important reason to keep them the same?

If the workflow accesses any intermediate result files that should be no problem then since you just can reference to the specific output folders you anyways have as variable present.

Otherwise I would recommend to modify the existing template to take care of this. The easiest solution is to add a Prefix to the output image files which you can also filter against with startsWith().

Cheers,
Christopher

Ah ok I see now. Instead of:

if(endsWith(list[i], suffix))

just add:

if(endsWith(list[i], suffix) && !endsWith(list[i], “_green.tif”) && !endsWith(list[i], “_nuclei.tif”) )

This excludes files with the suffix _green.tif or _nuclei.tif
The Operator ! indicates false or does not equal in this case.

If you want to be fancy you can replace the strings in the endsWith() functions with more suffix variables which you specify in the GUI =)

1 Like

Thank you, for your advices and your time.

Have a nice week-end

I’m very sorry but now I have new problem. The console is open
java.lang.IllegalArgumentException: Invalid character at index 9
at org.scijava.parse.Position.die(Position.java:70)
at org.scijava.parse.ExpressionParser$ParseOperation.parsePostfix(ExpressionParser.java:212)
at org.scijava.parse.ExpressionParser.parsePostfix(ExpressionParser.java:113)
at org.scijava.parse.eval.AbstractEvaluator.evaluate(AbstractEvaluator.java:87)
at org.scijava.parse.DefaultParseService$ItemsList.parseItems(DefaultParseService.java:105)
at org.scijava.parse.DefaultParseService$ItemsList.(DefaultParseService.java:73)
at org.scijava.parse.DefaultParseService.parse(DefaultParseService.java:59)
at org.scijava.script.process.ParameterScriptProcessor.parseAttrs(ParameterScriptProcessor.java:232)
at org.scijava.script.process.ParameterScriptProcessor.parseParam(ParameterScriptProcessor.java:182)
at org.scijava.script.process.ParameterScriptProcessor.process(ParameterScriptProcessor.java:169)
at org.scijava.script.process.ParameterScriptProcessor.process(ParameterScriptProcessor.java:138)
at org.scijava.script.process.ScriptProcessorService.process(ScriptProcessorService.java:79)
at org.scijava.script.ScriptInfo.parseParameters(ScriptInfo.java:293)
at org.scijava.module.AbstractModuleInfo.initParameters(AbstractModuleInfo.java:192)
at org.scijava.module.AbstractModuleInfo.inputList(AbstractModuleInfo.java:154)
at org.scijava.module.AbstractModuleInfo.inputs(AbstractModuleInfo.java:96)
at org.scijava.module.process.GatewayPreprocessor.process(GatewayPreprocessor.java:66)
at org.scijava.module.ModuleRunner.preProcess(ModuleRunner.java:105)
at org.scijava.module.ModuleRunner.run(ModuleRunner.java:157)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:127)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:66)
at org.scijava.thread.DefaultThreadService$3.call(DefaultThreadService.java:238)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

/// here the code

#@ File (label = “Input directory”, style = “directory”) inputFolder
#@ File (label = “Output directory”, style = “directory”) outputFolder1
#@ File (label = “Output directory”, style = “directory”) outputFolder2
#@ String (label = “File suffix”, value = “.tif”) suffix

//process entire inputFolder
function processFolder(inputFolder) {
list = getFileList(inputFolder);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(inputFolder + File.separator + list[i]))
processFolder(inputFolder + File.separator + list[i]);
//This excludes files with the suffix _green.tif or _nuclei.tif. The Operator ! indicates false or does not equal in this case.
if(endsWith(list[i], suffix) && !endsWith(list[i], “_green.tif”) && !endsWith(list[i], “_nuclei.tif”) )
processFile(inputFolder, outputFolder1, outputFolder2, list[i]);
}
}

// process the file in inputFolder
function processFile(inputFolder, outputFolder1, outputFolder2, file) {

imagesName=getTitle();
// split channels
run(“Split Channels”);

//take infos from the pricture
list1 = getList(“image.titles”);

// select channel green
selectWindow(“C1-” + imagesName);
run(“Gaussian Blur…”, “sigma=1”);
setAutoThreshold(“Huang dark”);
//setThreshold(12, 255);
setOption(“BlackBackground”, true);
run(“Convert to Mask”);
run(“Fill Holes”);
run(“Set Measurements…”, “area redirect=None decimal=5”);
run(“Analyze Particles…”, “size=50-Infinity pixel clear summarize”);
selectWindow(“Summary”);
saveAs(“Measurements”, outputFolder1 + imagesName +"_Green_area.csv");
selectWindow(imagesName +"_Green_area.csv");
run(“Close”);

selectWindow(“C2-” + imagesName);
run(“Gaussian Blur…”, “sigma=1.50”);
setAutoThreshold(“Huang dark”);
setThreshold(37, 255);
//setThreshold(37, 255);
setOption(“BlackBackground”, true);
run(“Convert to Mask”);
run(“Watershed”);
run(“Set Measurements…”, “area redirect=None decimal=5”);
run(“Analyze Particles…”, “size=50-Infinity display clear”);
selectWindow(“Results”);
saveAs(“Measurements”, outputFolder1 + imagesName +"_number_cells.csv");
selectWindow(“Results”);
run(“Close”);

//image calculator
imageCalculator(“AND create”, “C2-” + imagesName, “C1-”+imagesName);
//erreur ne trouve pas la bonne fenêtre result
selectWindow(“Result of C2-”+ imagesName);
run(“Erode”);
run(“Erode”);
run(“Dilate”);
run(“Dilate”);
run(“Analyze Particles…”, “size=80-Infinity display clear”);
saveAs(“Measurements”, outputFolder1 + imagesName +"_number_cells_in_green.csv");
selectWindow(“Results”);
run(“Close”);

selectWindow(“C1-” + imagesName);
saveAs(“Tiff”, outputFolder2 +imagesName+ “_green”);

selectWindow(“C2-” + imagesName);
saveAs(“Tiff”, outputFolder2 + imagesName+ “_nuclei”);

print("Processing: " + inputFolder + File.separator + file);
print("Saving to: " + outputFolder1);
print("Saving to: " + outputFolder2);
}

Problem is a copy from the forum and paste to the editor. Destroys the proper formatting then.
Sorry did not provide the correct way to post code here. Today I am not very fast, I guess it is vacation time. Please copy the code from here:

/*
 * Macro template to process multiple images in a folder
 */

#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix

// See also Process_Folder.py for a version of this code
// in the Python scripting language.

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	list = Array.sort(list);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + File.separator + list[i]))
			processFolder(input + File.separator + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output, list[i]);
	}
}

function processFile(input, output, file) {
	// Do the processing here by adding your own code.
	// Leave the print statements until things work, then remove them.
	print("Processing: " + input + File.separator + file);
	print("Saving to: " + output);
2 Likes

Thank you and good vacation :slight_smile:

1 Like

Hello again,
I tried with .czi file from zeiss but the macro don’t want to start

1 trial:
run(“Bio-Formats Importer”, “open=” + input + " autoscale color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");

error during impotation

second trial:
open(input);
not .tif or .gif

Dear Alex.h,

please provide the file and the macro that gives the error, in order to reproduce the error.

Also complete error messages are crucial for debugging.

Cheers,
Christopher

Hello this is the code I will share a link for the file


#@ File (label = "dossier avec les images en .tif", style = "directory") input
#@ File (label = "dossier pour les fichiers de mesures", style = "directory") output1
#@ File (label = "dossier pour les fichiers images binaires", style = "directory") output2
#@ String (label = "File suffix", value = ".tif") suffix

processFolder(input);

// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
	list = getFileList(input);
	list = Array.sort(list);
	for (i = 0; i < list.length; i++) {
		if(File.isDirectory(input + File.separator + list[i]))
			processFolder(input + File.separator + list[i]);
		if(endsWith(list[i], suffix))
			processFile(input, output1, output2, list[i]);
	}
}

function processFile(input, output1, output2, file) {
// Do the processing here by adding your own code.
// Leave the print statements until things work, then remove them.
print("Processing: " + file);
run("Bio-Formats Importer", "open=" + list[i] + " autoscale color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");
imagesName=getTitle();


// split channels
run("Split Channels");
//take infos from the pricture
list1 = getList("image.titles");

// select channel green
selectWindow("C1-" + imagesName);
run("Gaussian Blur...", "sigma=1");
setAutoThreshold("Huang dark");
setThreshold(1096, 65535);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Fill Holes");
run("Set Measurements...", "area redirect=None decimal=5");
run("Analyze Particles...", "size=50-Infinity pixel clear summarize");	
selectWindow("Summary");
saveAs("Measurements", output1 + imagesName +"_Green_area.csv");
selectWindow(imagesName +"_Green_area.csv");
run("Close"); 

selectWindow("C2-" + imagesName);
run("Gaussian Blur...", "sigma=1.50");
setAutoThreshold("Huang dark");
setThreshold(1384, 65535);
setOption("BlackBackground", true);
run("Convert to Mask");
run("Watershed");
run("Set Measurements...", "area redirect=None decimal=5");
run("Analyze Particles...", "size=50-Infinity display clear add");
run("Flatten");
saveAs("Tiff", output2 +imagesName+ "_nuclei");	
selectWindow("Results");
saveAs("Measurements", output1 + imagesName +"_number_cells.csv");
selectWindow("Results");
run("Close"); 


//image calculator
imageCalculator("AND create", "C2-" + imagesName, "C1-"+ imagesName);
selectWindow("Result of C2-"+ imagesName);
run("Erode");
run("Erode");
run("Dilate");
run("Dilate");
run("Analyze Particles...", "size=80-Infinity display clear add");
run("Flatten");
saveAs("Tiff", output2 +imagesName+ "_nuclei_AND_green");
selectWindow("Results");
saveAs("Measurements", output1 + imagesName +"_number_cells_in_green.csv");
selectWindow("Results");
run("Close"); 

selectWindow("C1-" + imagesName);
saveAs("Tiff", output2 +imagesName+ "_green");

	
	print("Processing: " + input + File.separator + file);
	print("Saving to: " + output1);
	print("Saving to: " + output2);
	

This is the link to download the file. I rename the file 1 because I had some problem with the symbol _ even with the first version of my macro this is normal? The name should be short?

https://edc.ulg.ac.be/merci/1_2ad5732cedfb5179b4fd_.czi

Hi,

replace your line 24 with

run("Bio-Formats Importer", "open=[" + input  + File.separator + list[i] + "] autoscale color_mode=Default rois_import=[ROI manager] view=Hyperstack stack_order=XYCZT");

You forgot to add the directory of your image.
You could also replace .tif with .czi at line 4.

replace line 44 with

saveAs("Measurements", output1 + File.separator + imagesName +"_Green_area.csv");

and same + File.separator at lines 58, 60, 74, 76 and 81

And if, later, you want to use batch macro, prefer selectImage instead of selectWindow for images.

Nico

3 Likes

Hi virtualSlide,
Thank you for your help.

Have a nice day