Macro runs with no output

Hello all,

I have been battling with this macro for awhile now. When I hit run it says “running” for about 2 minutes and then stops, but the expected new images are not there. Heres the code:

// Stitching 3x3 grid for a single channel

input = "/Users/almou/Desktop/Fiji_Images/20190522/FV10__20190522_143413_Correct/Channel_1"
output = "/Users/almou/Desktop/Fiji_Images/20190522"

// Setting up the file names

list = getFileList(input);
for (i = 0; i < list.length; i++) {
	IndexStart = indexOf(list[i], "FV10__20190522_143413_"); // This finds the idenx of the file name where the X, Y coordinated begin
	IndexEnd = indexOf(list[i], "_10"); // This is the index of the file name where the X, Y coordinates end
	Coord = substring(list[i], IndexStart+22, IndexEnd); // The string of X, Y coordinates from the file name
	genericFilename = replace(list[i], Coord, "Y{yyy}_X{xxx}"); // Replace the coordinate numbers with a variable for the stitch plugin
	}

// Running the stitch plugin for each file

function action(input,output,genericFilename,list[i],Coord) {
	open(input+filename)
	run("Grid/Collection stitching", "type=[Filename defined position] order=[Defined by filename         ] grid_size_x=3 grid_size_y=3 tile_overlap=0 first_file_index_x=1 first_file_index_y=1 directory= " + input + " file_names= " + genericFilename + ".tif output_textfile_name= " + Coord + "_Channel_Stitched.txt fusion_method=[Linear Blending] regression_threshold=0.30 max/avg_displacement_threshold=2.50 absolute_displacement_threshold=3.50 computation_parameters=[Save memory (but be slower)] image_output=[Fuse and display]");
saveAs("Tiff", output + filename);
close();
}

The code is aimed to open up a folder and stitch the images in the folder and then save it. No errors are popping up but there’s no output either… Does anyone have any ideas as to why this might be happening?

Thank you!
Alexa

It looks like you have created a function called action, but then you never called it. The stitching will never be done. I suspect you mean to call action on something within your For loop.
https://imagej.nih.gov/ij/developer/macro/macros.html#functions

1 Like

Yes, my mistake. I added the call but I am still having the same issue. The code runs with no bugs but there is no output. Here is the updated code:

// Stitching 3x3 grid for a single channel

input = ('Users/almou/Desktop/Fiji_Images/20190522/FV10__20190522_143413_Renamed/Channel_5')
output = ('Users/almou/Desktop')

// Setting up the file names

list = getFileList(input);
for (i = 0; i < list.length; i++) {
	IndexStart = indexOf(list[i], "FV10__20190522_143413_"); // This finds the index of the file name where the X, Y coordinated begin
	IndexEnd = indexOf(list[i], "_10"); // This is the index of the file name where the X, Y coordinates end
	Coord = substring(list[i], IndexStart+22, IndexEnd); // The string of X, Y coordinates from the file name
	genericFilename = replace(list[i], Coord, "Y{yyy}_X{xxx}"); // Replace the coordinate numbers with a variable for the stitch plugin
	action(input, output, genericFilename, list, Coord);
	 }

// Running the stitch plugin for each file

function action(input, output, genericFilename, list, Coord) {
	open(input+filename)
	run("Grid/Collection stitching", "type=[Filename defined position] order=[Defined by filename         ] grid_size_x=3 grid_size_y=3 tile_overlap=0 first_file_index_x=1 first_file_index_y=1 directory= " + input + " file_names= " + genericFilename + ".tif output_textfile_name= " + Coord + "_Channel_Stitched.txt fusion_method=[Linear Blending] regression_threshold=0.30 max/avg_displacement_threshold=2.50 absolute_displacement_threshold=3.50 computation_parameters=[Save memory (but be slower)] image_output=[Fuse and display]");
saveAs("Tiff", output + filename);
close();
}

Any advice would be appreciated!

I don’t know anything about the stitching function, but have you tried print statements with the file names to verify that the script is getting those correct?

Even printing “i” would let you know if the macro is looping the correct number of times.

Also, if the stitching is working, you might want to look around for a large pile of tiffs. If the name concatenation isn’t working as expected, they might all be ending up somewhere else!

Yes I have tried that! Adding in print statements does not change the output. The code runs and nothing happens, even with the print functions!

Hmm, starting from the basics, have you tested just the first three lines with a print statement showing your list of files? If print statements inside the loop are not working, it sounds like you might not be entering the loop. Not sure why it would take 2 minutes to do that… but that’s the best suggestion I have got. And if printing variables doesn’t work, just print statements everywhere (Right at the top, print(“Start script”) and see where they stop showing up :slight_smile: They aren’t intended to do anything or fix anything, but you should get a list of them if the code is running.

Another possibility I came across looking at another post is the format of the initial input line. It’s an old post, so the format may have changed but…
http://imagej.1557.x6.nabble.com/Saving-results-automatically-without-going-through-save-as-dialog-td5018135.html
shows the use of the line:
dir = getDirectory("path");

Thank you! Your advice was helpful and not the script runs! I am running into an error that I haven’t been able to solve. All the statements are correct, however, the macro cannot find the file. The path is right and the file names are correct. If I do the stitching manually, Fiji finds the images no problem. Here is my code:

// Stitching 3x3 grid for a single channel

print("Start script")
input = getDirectory("Choose a Directory")
output = getDirectory("Choose a Directory")

// Setting up the file names

list = getFileList(input)
for (i = 0; i < list.length; i++) {
	IndexStart = indexOf(list[i], "143413_"); // This finds the index of the file name where the X, Y coordinated begin
	print("IndexStart:",IndexStart);
	IndexEnd = indexOf(list[i], "_10"); // This is the index of the file name where the X, Y coordinates end
	print("IndexEnd:",IndexEnd);
	Coord = substring(list[i], IndexStart+7, IndexEnd); // The string of X, Y coordinates from the file name
	print("Coord:",Coord);
	genericFilename = replace(list[i], Coord, "Y{yyy}_X{xxx}"); // Replace the coordinate numbers with a variable for the stitch plugin
	print("GenericFileName:",genericFilename);
	action(input, output, genericFilename, list[i], Coord);
	print("i:",i);
	 }

// Running the stitch plugin for each file

function action(input, output, genericFilename, filename, Coord) {
	run("Grid/Collection stitching", "type=[Filename defined position] order=[Defined by filename         ] grid_size_x=3 grid_size_y=3 tile_overlap=5 first_file_index_x=1 first_file_index_y=1 directory= " + input + " file_names=" + genericFilename + " output_textfile_name=TileConfiguration.txt fusion_method=[Linear Blending] regression_threshold=0.30 max/avg_displacement_threshold=2.50 absolute_displacement_threshold=3.50 computation_parameters=[Save memory (but be slower)] image_output=[Fuse and display]");
}

And here is the error:

The error repeats 9 times all for the same Y001_X001 image.

Any thoughts?

I think this gets into the stitching, which I have never played with, but based on the 9 repeats, I would guess there is some problem with it being able to read the directory being passed to it. While input works for getting your file list, the stitching command itself might have specific requirements, and you would need to edit the directory path somehow.

I would look for other examples of that line of code and replace the input variable with the exact type of text used (single vs double quotes, etc etc).

Could the problem be the space after the directory option in the call to the stiching?

first_file_index_y=1 directory= " + input + " file_names=" + genericFilename

2 Likes