Login  Register

Re: Trouble Calling Variables in Macro for the Image Calculator

Posted by Michael Schmid on Jul 19, 2010; 9:24am
URL: http://imagej.273.s1.nabble.com/Trouble-Calling-Variables-in-Macro-for-the-Image-Calculator-tp3687586p3687593.html

Hi Ari,

ok, two more possibilites:

(1) 'Analyze Particles' does not create a mask if there are no  
particles in the thresholded range.


(2) Otherwise it seems to be a problem of a race condition.

What is the exact error message?

"Mask3.tif not found" ? Then it would come from  
ij.macro.Functions.selectWindowManagerImage(String title).

I had a look at a few of the relevant commands in the current version  
of ImageJ, it all seems to be rather clean, not prone to such problems.

If the error message is as above, could you insert the following in  
your macro:

print(nImages());  //new
selectWindow("Mask3.tif");

In case of the error, the last value of nImages should be lower than  
usual, is this correct?

Do you see the image "Mask3.tif" after the error appears?

In case the race condition is related to the GUI of your operating  
system (though I do not see where it should come from): Which OS are  
you using?


Michael
________________________________________________________________

On 17 Jul 2010, at 00:59, Zwick, Ari A. wrote:


> Hi Michael,
>
> You are indeed correct that there was an error in the code that I  
> showed
> you.  Fortunately or unfortunately this was merely a copy-paste  
> error in
> my email.  I was making the change in the macro, and the errors I was
> getting occurred in the Image Calculator before it had a chance to  
> close
> the windows.  But thanks for taking a close look at it.
>
> Ari
>
> -----Original Message-----
> From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
> Michael Schmid
> Sent: Friday, July 16, 2010 5:12 AM
> To: [hidden email]
> Subject: Re: Trouble Calling Variables in Macro for the Image  
> Calculator
>
> Hi Ari,
>
> from your code:
>
>
>> saveAs("Tiff", "/ecor/azwick/Macro/Mask3.tif");
>>
>> rename(i);
>>
> ...
>
>> selectWindow("Mask3.tif");
>>
>> close();
>>
>
> It seems that you image "Mask3.tif" should be renamed to the number
> contained in variable 'i', so why should it ever work? Did I miss
> something?
>
> Michael
> ________________________________________________________________
>
> On 14 Jul 2010, at 17:49, Zwick, Ari A. wrote:
>
>
>> Hi,
>>
>> Okay so here's my second amendment.  I spoke too soon about the
>> redundancy.  By some happenstance my macro made it through 50
>> iterations
>> and then crashed again after about 5 after that.  So I looked into
>> macro
>> timing and inserted wait(10) between each command and wait(100)
>> between
>> the problematic areas.  I'm now employing multiple levels of
>> redundancy
>> in conjunction with wait and my macro has now made it through 400
>> iterations seamlessly.  I am certain that the macro was getting
>> ahead of
>> itself with assigning titles, but the Image ID's are actually getting
>> miss-assigned too, however they are being convoluted by calling by
>> hybrid title/imageID macro.  The obvious solution would be to use all
>> Image ID's, except that the macro still gets ahead of itself and  
>> can't
>> find image ID's.  So even if I run selectWindow or getImageID 5  
>> times,
>> it will still result in errors.  I don't even have to run the print
>> log
>> because I can see my windows getting mislabeled and ImageID's "not
>> existing", so that tells me that my commands are getting assigned
>> to the
>> wrong windows even when I build in the redundancy.  So the ultimate
>> fix
>> is actually the wait command, and perhaps it would be getImageID if I
>> used it exclusively, but I think I'd still have the problem.  Right
>> now
>> my paranoia level is set to about 9/10, so I'm going to leave my
>> sloppy
>> macro with all the redundancies in place and keep the American ideals
>> up: "If it aint broke, don't fix it."  Ironically (to me at least)
>> with
>> the wait command, the macro has never run faster.  There's almost no
>> visible lag.
>>
>> I am running in "batch mode": z = m*random() *451; // 451 = Number of
>> slices.
>>
>> Although my bugs are not occurring in batch mode.  I extract a random
>> sample (1 window) from my stack and then analyze that sample, and
>> that's
>> where my errors are coming in.  The batch sampling process has worked
>> seamlessly from the beginning, so I think it's just a macro calling
>> problem.  Maybe a future bug fix for ImageJ would be to have a  
>> default
>> wait(10) command built into ImageJ to avoid these call errors,
>> although
>> something tells me that ImageJ programmers would have some beef with
>> that.  That's my two cents anyway.  Thanks again for your help and
>> advice.  It's people like you that have fueled my transition to
>> dedicated Linux for my next computer purchase in 2 weeks.
>>
>> Ari Zwick
>>
>> -----Original Message-----
>> From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of
>> Ben Tupper
>> Sent: Tuesday, July 13, 2010 6:35 PM
>> To: [hidden email]
>> Subject: Re: Trouble Calling Variables in Macro for the Image
>> Calculator
>>
>> Hi,
>>
>> On Jul 13, 2010, at 5:52 PM, Zwick, Ari A. wrote:
>>
>>
>>> Hi Ben,
>>>
>>> Stunning.
>>>
>>
>> I haven't been called that before!  This one is getting taped to the
>> ol' refrigerator.
>>
>>
>>> Unfortunately that did not exclusively solve my problem, but
>>> it did certainly help.  The ultimate cure was actually redundancy.
>>> Now
>>> before I close that problematic window I run:
>>>
>>> selectImage(id);
>>> selectwindow(Mask3.tif);
>>> close();
>>>
>>> It made it through 50 iterations without crashing.
>>>
>>
>> That stumps me.  I know that others have had luck by inserting a  
>> small
>> wait(100), but I thought use of image IDs obviated the need for that.
>> I am curious if you can demonstrate that the title of the image
>> selected by id really is "mask3.tif". I might be that you are  
>> creating
>> an image (somewhere) that is not being properly disposed of.  Perhaps
>> you could try the following for debugging purposes.
>>
>> selectImage(id);
>> print("id has a title of " + getTitle());
>> selectwindow("Mask3.tif");
>> print("Mask3.tif has id of " + getImageID());
>> close();
>>
>> Also, are you running in batch mode?
>>
>>
>>
>>>
>>> Ari Zwick
>>>
>>> -----Original Message-----
>>> From: ImageJ Interest Group [mailto:[hidden email]] On  
>>> Behalf Of
>>> Ben Tupper
>>> Sent: Tuesday, July 13, 2010 1:52 PM
>>> To: [hidden email]
>>> Subject: Re: Trouble Calling Variables in Macro for the Image
>>> Calculator
>>>
>>> Hi,
>>>
>>> I think your best bet is to keep track of images by ID rather  
>>> than by
>>> name.  Right after you instantiate/open/create/whatever an image use
>>> the getImageID() function to get that image's unique identifier.  
>>> For
>>> most subsequent commands that operate on that image be sure to use
>>> the
>>> command selectImage(id).  Note that the Image Calculator command  
>>> (and
>>> perhaps others) will accept an image ID instead of image name, which
>>> is very handy.
>>>
>>> I will be surprised if that doesn't resolve the issue for you.
>>>
>>> Cheers,
>>> Ben
>>>
>>>
>>> On Jul 13, 2010, at 2:33 PM, Zwick, Ari A. wrote:
>>>
>>>
>>>> Dear ImageJ Community,
>>>>
>>>>
>>>>
>>>> I'm writing a macro that will loop ~800 times, and I'm getting a  
>>>> few
>>>> glitches with my macro calling functions out of the order that I've
>>>> written them in.  My solution to this problem is to give individual
>>>> titles to 1 set of problematic images (the rest use the same title
>>>> for
>>>> each iteration).  So I was thinking of something simple such as
>>>> naming
>>>> my problematic image equal to the value of i (number of
>>>> iterations).  So
>>>> I can rename the image rename(i) and the title works, however I
>>>> can't
>>>> get the Image Calculator to call the value of i, which will change
>>>> for
>>>> each iteration.
>>>>
>>>>
>>>>
>>>> Can I define a variable such as: p = getTitle(CurrentImage) to call
>>>> in
>>>> the Image Calculator?  I was also thinking about the print command,
>>>> but
>>>> it doesn't seem clear to me that the value is easily accessible in
>>>> the
>>>> macro.  I've been having problems with syntax, so I'd really
>>>> appreciate
>>>> any suggestions about how to call the variable properly, or any
>>>> other
>>>> work around.  Below is a bit of the script that I'm trying to work
>>>> with:
>>>>
>>>>
>>>>
>>>> width = getWidth();
>>>>
>>>> height = getHeight();
>>>>
>>>> work = 10;
>>>>
>>>> for (i=1; i<=work ; i++) {
>>>>
>>>>
>>>>
>>>> m = i/i;
>>>>
>>>> x = m*random() *(width-356);
>>>>
>>>> y = m*random() *(height-356);
>>>>
>>>> z = m*random() *451;
>>>>
>>>> (let p [ getTitle(getCurrentImage)])
>>>>
>>>>
>>>>
>>>> //I know that syntax for p is wrong, found it off a Fiji Wiki
>>>>
>>>> //Later...
>>>>
>>>>
>>>>
>>>> selectWindow("Sample");
>>>>
>>>> makePolygon(356,0,236,0,236,236,0,236,0,356,356,356);
>>>>
>>>> run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00
>>>> show=Masks exclude");
>>>>
>>>> saveAs("Tiff", "/ecor/azwick/Macro/Mask3.tif");
>>>>
>>>> rename(i);
>>>>
>>>>
>>>>
>>>> imageCalculator("Add", "Mask2",p);
>>>>
>>>> rename("Edge Count");
>>>>
>>>>
>>>>
>>>> //Error
>>>>
>>>> //Later
>>>>
>>>>
>>>>
>>>> selectWindow("Mask3.tif");
>>>>
>>>> close();
>>>>
>>>>
>>>>
>>>> //Error Mask3.tif does not exit.  (This is before I tried the i/p
>>>> titles; the error that I'm trying to work around currently)
>>>>
>>>>
>>>>
>>>> I know this is kind of sloppy macro writing, but this is my first
>>>> time
>>>> actually modifying macro script rather than just recording.  The
>>>> saveAs
>>>> Tiff is another attempt to prevent Mask3 from escaping my commands.
>>>> My
>>>> logic is that this problem probably won't go away, but if I can
>>>> rename
>>>> each file per iteration, then there won't be any overlap of files
>>>> that
>>>> don't get closed and mistakenly called in later iterations.  I'm
>>>> also
>>>> aware that the CurrentImage command will cause some serious
>>>> problems,
>>>> because I have about 4 other windows open.  I'd appreciate any help
>>>> with
>>>> this and thanks in advance.
>>>>
>>>>
>>>>
>>>> Ari Zwick
>>>>
>>>>
>>>>
>>>>
>>>>