Have you experienced strange error in imagej macro which is meaningless?

I am running an imagej macro code and it works. I only changed something trivial in the code and all the rest is the same. Now I get the error “there is no image open”. I checked everything and now I am getting to the point that it is a real bug of the software. also, sometimes I get random errors of the code which was working perfectly.

@Zeynab_Mousavi

Would you be able to share with us a minimal working example enough of your code to test? So a code snippet at least that can run independently for us to also try, including example data as well. We just need to see about reproducing this ‘error’ that you are encountering.

1 Like

Hi @Zeynab_Mousavi,

If in ImageJ, you debug, and after the Debug Macro command you step through the code by pressing cmd-E. When the command executed happens to be ‘Close’, the focus is removed from the macro window, the command executed so the image is closed. At that moment there is no image open.
Pressing cmd-E, which is the shorthand for ‘Scale…’, then gives this error.

Could this be the case with you?

1 Like

This is how to run the debug option -

Open the macro in the Fiji Script Editor, type cmd-A (Select All), cmd-C
(Copy), close the Script Editor and type cmd-V (Paste). The macro will
open in an ImageJ macro editor window (named “Clipboard”) that has a
Debug menu. On Linux and Windows, use the control key instead for
keyboard shortcuts.

Source - http://imagej.1557.x6.nabble.com/Debugging-macros-in-Fiji-td5010227.html

2 Likes

If you have obtained the imageID beforehand and stored it in a variable, you can -prior to the crashing macro statement- check if the image is still open using isopen.
JFWIW.

1 Like

It is hard to explain. I get different errors every time I run the code. I decided to cut the code to small pieces and troubleshoot separately.
Hope it works! Thanks

I think there is no such thing as ‘random errors’ unless you use the command random :slight_smile:

It is hard to gauge how involved your code and your method of debugging is. Do you step through your code line by line (called ‘tracing’) and inspect the results of each command? This can really reveal unexpected behavior. The boring bits of code can quickly be transversed by clicking the line of the interesting command and choose ‘Run to Insertion Point’ from the Debug menu.

And if all fails, post a bit of code here.

1 Like

Where is ‘Run to Insertion Point’?

Where is the Run to Insertion Point?
As you may have found in this forum, ImageJ1 shows the Debug menu as long as you are in a Macro window.
Fiji, which is what you use, isnt EXACTLY Just ImageJ in this respect and doesn’t have this Debug menu natively, so you need a trick to reveal the menu: you have to have your macro window open in Fiji, Edit>Select All of your macro text, Edit>Copy the text, Close the macro window and Paste the text (which seems a bit strange to do as there is no text window open to paste into, but this is exactly the trick as @somsubhro explained)

A new window called Clipboard opens, with your macro text in it. Also, the menus should have changed, notably the Debug menu should be visible now.
In order to debug your macro line by line, you first choose Debug>Debug Macro.
Subsequently a new window opens somewhere in the background in which Debug shows the values of your variables.
By repeatedly choosing Debug>Step (or pressing cmd-E on a mac or ctrl-E on windos,) you execute one command at a time, the one highlighted in the text window.
Apart from Step-ping through the code, you can also Debug>Run the macro and it will run until an error occurs or the end of the macro is reached.

To quickly advance to the line where you expect the error to happen you can either put the statement ‘debug;’ in the line preceding the expected error so execution is interrupted after you issue Debug>Run, or click the cursor in that line and press Debug>Run to Insertion Point.

By either method your macro is executed to the critical point, allowing you to inspect the values of the variables before executing the offending command.

1 Like

@eljonco @etadobson @somsubhro Now I can say for sure that sometimes there is sth wrong with fiji macro language. I press run and get error and did not change sth and pressed run and this time I did NOT get any error.
Any idea?
A brief explanation of the code: I use for loop to do some analysis on multiple images with lpx plugin. The error is: no image is open.
Not easy to track the source of error since it happens sometimes and other times it runs with no problem.

You have gotten quite a few directions on how to debug, which somehow you haven’t seem to follow up, or at least, haven’t given any feedback on.

Give us some code, what steps from our hints you have tried and some feedback where things run awry and we might be able to help you further.

2 Likes

I agree with @eljonco. It could be a simple issue in the code that is resulting in this problem. But without seeing the code, it is almost impossible to identify your issue. I do not experience any such issue that you have mentioned, @Zeynab_Mousavi.

2 Likes

@eljonco Thank you so much. Yes I tried the debug mode and it was useful based on your instructions. Good to know this skill. I am now trying to figure out the error.