Skip to content

Commit

Permalink
conditions, intro, still editing to be synced with the book
Browse files Browse the repository at this point in the history
  • Loading branch information
miura committed May 17, 2016
1 parent 6ac515b commit e611419
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 10 deletions.
45 changes: 38 additions & 7 deletions sections/macroConditionsLoops.tex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ \subsection{Loop: for-looping}
(2) Change the second parameter in \ilcom{for(i=0;i<5;i+=1)} so that the macro prints out 10 lines.

(3) Change the third parameter in \ilcom{for(i=0;i<5;i+=1)} so that the macro prints out 10 lines.

\item \textbf{Answer}:
\item (1) \ilcom{for(i=4;i<5;i+=1)}
\item (2) \ilcom{for(i=0;i<10;i+=1)}
\item (3) \ilcom{for(i=0;i<5;i+=0.5)}
\end{indentexercise}


Expand All @@ -54,7 +59,10 @@ \subsubsection{Stack Analysis by for-looping}
Without macro programming, you need to execute the command while you flip the frame manually.
Macro programming enables you to automate this process.
Here is an example of measuring intensity change over time\footnote{What we write as macro here could be done with a single command \ilcom{[Image > Stacks > Plot Z-Profile]} but this only measures intensity. If you want to measure other values such as the minimum intensity, a macro should be written. }.

\label{code:10}
\lstinputlisting[morekeywords={*, run, setSlice, nSlices}]{code/code10.ijm}

\begin{itemize}
\item Line 3: \ilcom{nSlices} is a macro function that returns the number of slices in the active stack.

Expand Down Expand Up @@ -98,13 +106,16 @@ \subsubsection{Stack Analysis by for-looping}
\end{figure}


Measurement parameters can be added as argument by modifying the line 4 in the code 10. "Set Measurement" could be added with more parameters to be measured, and the digits after the decimal point could be increased by increasing the number after ``decimal=''. For example,
Measurement parameters can be added as argument by modifying the line 4 in the code 10.

\begin{indentexercise}{1}
Modify code 10 to include more measurement parameters (whatever you like), and test the macro. Check the results.

\item \textbf{Answer}:``Set Measurement'' could be added with more parameters to be measured, and the digits after the decimal point could be increased by increasing the number after ``decimal=''. For example,
\begin{lstlisting}[numbers=none]
run("Set Measurements...", "area mean standard modal min centroid center perimeter bounding integrated median stack redirect=None decimal=5");
\end{lstlisting}

\begin{indentexercise}{1}
Modify code 10 to include more measurement parameters (whatever you like), and test the macro. Check the results.
\end{indentexercise}

% figure
Expand Down Expand Up @@ -170,15 +181,21 @@ \subsubsection{Basics of while statement}
\begin{indentexercise}{1}
(1) Try changing code 11 so that it uses "+=" sign.\\
(2) Change code 11 so that it uses "++" sign, and prints out integers from 0 to 9.\\

\item \textbf{Answer}: (1) Change line 6 to \ilcom{counter += 1;}. (2) Change line 4 to \ilcom{while (counter<=9)} and line 6 to \ilcom{counter++}.
\end{indentexercise}
Evaluation of \ilcom{while} condition could also be at the end of loop. In this case, \ilcom{do} should be stated at the beginning of the loop. With do-while combination, the loop is always executed at least once, regardless of the condition defined by \ilcom{while} since macro interpreter reads lines from top to bottom. Try with the following exercise.

\begin{indentexercise}{2}
Change line 4 of code 11 to \ilcom{while (counter <0)} and check the effect (see below).
\end{indentexercise}

Evaluation of \ilcom{while} condition could also be at the end of loop. In this case, \ilcom{do} should be stated at the beginning of the loop. With do-while combination, the loop is always executed at least once, regardless of the condition defined by \ilcom{while} since macro interpreter reads lines from top to bottom. Try with the following exercise.


\lstinputlisting[morekeywords={*, while}]{code/code11_5.ijm}

In this example, the exit condition for going out from looping is \ilcom{counter < 0}, and the initial value of \ilcom{counter} is 0, which does not satisfy that looping condition. Since this evaluation occurs only after the looping part is executed for the first time, the macro still prints out a line before it exits from the loop.

Condition for the while-statement could be various. Here is a small list of comparison operators.

\begin{indentCom}
Expand All @@ -194,8 +211,21 @@ \subsubsection{Basics of while statement}

\begin{indentexercise}{3}
Modify code 11 so that the macro prints out numbers from 200 to 100, with an increment of -10.

\item \textbf{Answer}: There could be slightly various ways to do this modification, but here is one way.
\begin{lstlisting}
macro "while looping1" {
counter=200;
while (counter>=100) {
print(counter);
counter -= 10;
}
}
\end{lstlisting}

\end{indentexercise}


\subsubsection{Why is there while-loop?}

An often raised question with the while-loop is why do we have two types of loops,
Expand Down Expand Up @@ -308,10 +338,11 @@ \subsubsection{Complex Conditions}
true. Only when both sides are false, the returned value becomes false (0).

\begin{indentexercise}{1}
Change the values of \ilcom{a} and \ilcom{b} in code 12\_65 to \ilcom{false} and
Adjust the values of \ilcom{a} and \ilcom{b} in code 12\_65 to \ilcom{true} or \ilcom{false} and
compose other three possible combinations (e.g. \ilcom{a = true}, \ilcom{b = false} will print
only one line).
Check the output. Change the values of \ilcom{a} and \ilcom{b} also to 0 and/or
only one line). Check the output.

Next, change the values of \ilcom{a} and \ilcom{b} to 0 and/or
1 and check the results.
\end{indentexercise}

Expand Down
6 changes: 3 additions & 3 deletions sections/macroIntro.tex
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
\subsection{ImageJ macro makes your life easier}

To customize functions in ImageJ, a typical way is to write a Java plugin that directly accesses the application interface of ImageJ.
This is a powerful method for customizing your own tool but in many cases is a bit too much for small tasks we often encounter in biological research projects. Compared to the Java programming, ImageJ macro is much easier for quickly solving problems.
This is a powerful method for customizing your own tool but in many cases is a bit too much for small tasks we often encounter in biological research projects. Compared to the Java programming, ImageJ macro is much easier to access and to quickly solve problems.

A typical usage is to automate repetitive tasks with hundreds of times of mouse clicking. Clicking ranges from menu selections to inspection of single pixel value. By writing a macro, we could save such exhausting job to a single execution of a macro file, which is a text file with a sequence of image processing commands. As ImageJ macro functions are directly mirroring the GUI menu items, one could intuitively learn how to write one's own macro even without much experiences in programming.
A typical usage is to automate repetitive tasks with hundreds of times of mouse clicking. Clicking could range from menu selections to inspection of single pixel value. By writing a macro, we could save such exhausting job to a single execution of a macro file, which is a text file with a sequence of image processing commands. As ImageJ macro functions are directly mirroring the GUI menu items, one could intuitively learn how to write one's own macro even without much experiences in programming.

Another important aspect of writing a macro is its role as a documentation: as the processing becomes complex, we easily forget the steps and details of the procedures and the values of parameters that were used for that task. Even if your job is not a repetitive one, a macro written for a task becomes a valuable record of what was done to the image, and ensures the \textbf{reproducibility} of your image analysis.

Expand Down Expand Up @@ -104,4 +104,4 @@ \subsection{How to learn Macro programming}
\subsection{Summary}
ImageJ Macro radically decreases your work load and is a practical way to keep your image analysis workflow in text file. Less workload provides more time for us to analyze details of image data.
The potential of macro is similar to other scripting languages and Java Plugins, all adding capability to customize your image analysis. For coding interactive procedures PlugIn works better than macro. Macro cannot be used as a library.
Image processing by macro is slower than that by Java written plugins.
Image processing by macro is slower than that by Java written plugins.

0 comments on commit e611419

Please sign in to comment.