# How to automate "grouped z project" in macro

Dear Forum,

I’m working on a short script in which I want to use the stack function “grouped z project” (Image/Stacks/Tools/grouped z project).
There, one can enter the Group size on how much parts one wants to make a maximum intensity projection of a stack. But only valid factors are allowed for input.
Since I want to process a lot of different stacks with different numbers of slices, I cant set a fixed value.
But I don’t want the user to this.
I know I always want 5 groups.

Is there a way to calculate the correct (valid) factor?

If I have 234 slices and I like to have 5 groupes I would calculate 234 / 5 = 46,8.
But I can only use 39 or 78 as a valid factor (they are about in the right range).
How do I get the calculation right?

I hope this is not to silly to ask

Best Tobias

[Edit:

I think I need to calculate the next integer.
like

if nSlices divided with 5 == integer then do grouped z project
else divide nSlices with 6 if the answer is an integeger do z project…
and so on…

do you think this would be the best approach?
]

Hi again,

I did some digging in photon, and there is a way to check if a variable is an integer.

isinstance(z, (int, long))

To use this in the ImageJ Macro Language i can evaluate this phyton line.

But I get an error message that my variable z is not defined.

Here is the code I use as test

macro "pyhton"{
x = 234;
y = 6;
z = x / y;
print(z);
string = "b = isinstance(z, (int, long))";
eval("python", string)
print(b);
}

Error message:

Traceback (most recent call last):
File "<string>", line 1, in <module>
NameError: name 'z' is not defined

Maybe you have some Ideas on how to solve this

Best Tobias

Good day Tobias,

here is a code line that checks integer versus float:

if ( val % parseInt( d2s( val, 0 ) ) == 0 ) { print( "Is integer" ); } else {print( "Is float" ); }

Regards

Herbie

Thank you Herbie,

this is working.
But I don’t understand what the code is doing.
Could you give me some hints what parseInt is doing there exactly? (converts string into integer)
Why do you use d2s (Converting the number n into a string).
Does the code just converts my number into a string, then you check if it has a decimal in it and then prints “is integer”?
What is the % good for?

Thank you very much

Tobias

Tobias,

as always you should consult and study the docs!

1. Commented list of ImageJ built-in macro functions:
https://imagej.nih.gov/ij/developer/macro/functions.html
2. Description of ImageJ macro coding:
https://imagej.nih.gov/ij/developer/macro/macros.html

From 1:

d2s(n, decimalPlaces)
Converts the number n into a string using the specified number of decimal places. Uses scientific notation if 'decimalPlaces is negative. Note that d2s stands for “double to string”.

From 2:

% means remainder

HTH

Herbie

1 Like