forked from erickmartins/training
-
Notifications
You must be signed in to change notification settings - Fork 1
/
macroWorkshop.html
411 lines (343 loc) · 16 KB
/
macroWorkshop.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Intermediate Fiji: Macros and Scripting</title>
<link rel="stylesheet" href="dist/reveal.css">
<link rel="stylesheet" href="dist/theme/cci.css">
<link href="https://fonts.googleapis.com/css?family=Poppins" rel="stylesheet">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="plugin/highlight/vs.css">
</head>
<body>
<div class="reveal">
<!-- Header logo -->
<!--img style="position:fixed;top:1em;right:1em;" src="img/logo_cci.png" width="15%"-->
<!-- Slide Title -->
<header style="position: absolute;top: 1em; left: 1em; z-index:10;"></header>
<div class="slides">
<section data-menu-title="Title">
<h2>Assembling a proper ImageJ macro</h2>
<h4>Peter Sobolewski (he/him)<br>Systems Analyst, Imaging Applications - Research IT</h4>
<p class="subtle">Use <i class="fa fa-arrow-right"></i> to advance the slide</p>
</section>
<section data-state="intro0">
<h2>
<b>Acknowledgements</b>
</h2>
<ul>
<li>Erick Ratamero (JAX), who prepared the original of <i>these</i> materials</li>
<li>Material from <a href="https://imagej.net/Introduction_into_Macro_Programming">https://imagej.net/Introduction_into_Macro_Programming</a></li>
</ul>
</section>
<section data-menu-title="Objective" class="future" style="top: 149.5px; display: none;" hidden="" aria-hidden="true">
<h2>"My first proper macro"</h2>
<h3>Objective: apply a simple analysis pipeline over a set of images</h3>
<p>Let's consider re-usability and generalization</p>
<ul>
<li>Download our <a href="./material/data_session6.zip">test data</a> to your desktop.</li>
<li>Unzip (right click and "Extract All")</li>
<li>Make another folder on the desktop called <code>Output</code></li>
<li>Open Script Editor (<code>[File > New > Script...]</code>) and start with an ImageJ macro template:
<br><code>[Templates > ImageJ 1.x > Batch > Process Folder (ImageJ macro)]</code></li>
</ul>
</section>
<section data-state="template" class="future" style="top: 60px; display: none;" hidden="" aria-hidden="true">
<h2>Process_Folder Template</h2>
<p><code>[Templates > ImageJ 1.x > Batch > Process Folder (IJ1 macro)]</code> </p>
<pre><code data-line-numbers="1-3|5|8-11|12-13|14-15|16-17|19-24" class="javascript">#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix
processFolder(input);
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
function processFile(input, output, file) {
// Do the processing here by adding your own code.
// Leave the print statements until things work, then remove them.
print("Processing: " + input + File.separator + file);
print("Saving to: " + output);
}
</code></pre>
</section>
<section data-state="template"><style>.ims2 header:after { content: ""; }</style>
<h2>Script Parameters</h2>
<p><code>#@</code> at the top of a macro define <a href="https://imagej.net/scripting/parameters">script parameters</a> for declaring inputs/output dialogs (Fiji, ImageJ2 only)</p>
<ul>
<li>Input parameters are defined by their type and variable name, 1 per line</li>
<ul><li>
<code>#@ Type variable_name</code></li>
<li>Listing of types: <a href="https://imagej.net/scripting/parameters#parameter-types">https://imagej.net/scripting/parameters#parameter-types</a></li>
<li>Dialog elements are determined by the type, but can be customized by passing settings in <code>()</code> after the type, e.g. <code>(label="File to open")</code></li>
<li><code>#@ output Type variable_name</code>can be used to generate output</li>
</ul>
</ul>
</section>
<section data-state="template"><style>.ims2 header:after { content: "Process_Folder Template"; }</style>
<h2>Warmup</h2>
<p>Run the template macro on the data folder. Does it do what you expect?</p>
</section>
<section data-state="python" class="future" style="top: 86px; display: none;" hidden="" aria-hidden="true">
<h2>Exercise Hints</h2>
<ul>
<li>Use each macro as the basis for the next one.</li>
<li>Remember the macro recorder!</li>
<li>The list of <a href="https://imagej.net/ij/developer/macro/functions.html">all built-in functions</a> is EXTREMELY useful!</li>
<li>You can refer to the slide deck: <a href="https://thejacksonlaboratory.github.io/fiji_workshops/IntermediateFiji_macros.html" target="_blank">https://thejacksonlaboratory.github.io/
fiji_workshops/IntermediateFiji_macros.html</a></li>
</ul>
</section>
<section data-menu-title="Task 1" class="future" style="top: 234px; display: none;" hidden="" aria-hidden="true">
<h2>Task 1- Create a macro that:</h2>
<ol>
<li>Opens every image, prints how many slices there are in the image and then closes the image.</li>
</ol>
<p>i.e. you shouldn't have all of the image open at the end</p>
</section>
<section data-menu-title="Task 2" class="future" style="top: 239px; display: none;" hidden="" aria-hidden="true">
<h2>Task 2- Create a macro that:</h2>
<ol>
<li>Opens every image, prints how many slices there are in the image...</li>
<li>duplicates the first slice</li>
<li>then saves this as a separate image (with a suitable filename).</li>
<li>Closes all the images.</li>
</ol>
</section>
<section data-menu-title="Task 3" class="future" style="top: 223.5px; display: none;" hidden="" aria-hidden="true">
<h2>Task 3- Create a macro that:</h2>
<ol>
<li>Opens every image, prints how many slices there are in the image, then...</li>
<li>Sets the slice to 5, duplicates this frame, </li>
<li>performs background subtraction then saves this as a separate image (with a suitable filename).</li>
<li>Closes all the images.</li>
</ol>
</section>
<section data-menu-title="Task 4" class="future" style="top: 239px; display: none;" hidden="" aria-hidden="true">
<h2>Task 4- Create a macro that:</h2>
<ol>
<li>Opens every image, prints how many slices there are in the image, then...</li>
<li>Loops through every individual slice of the image, subtracts the background and saves each as new image.</li>
<li>Closes all the images.</li>
</ol>
</section>
<section data-menu-title="Extension task 5" class="future" style="top: 208.5px; display: none;" hidden="" aria-hidden="true">
<h2>Extension task 5- Create a macro that:</h2>
<ol>
<li>Opens every image, prints how many slices there are in the image, then...</li>
<li>Loops through every individual slice of the image and saves each as new image.</li>
<li>Does a Z projection between slices 3 and 7 and measures the "mean gray value" of this projection.</li>
<li>Closes all the images and then saves the results table as a .csv file.</li>
</ol>
</section>
<section data-menu-title="Solution 1" class="future" style="top: 97px; display: none;" hidden="" aria-hidden="true">
<h2>Solution 1:</h2>
<pre><code data-line-numbers="20-22" class="javascript">#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix
processFolder(input);
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
function processFile(input, output, file) {
open(input+File.separator+file);
print(nSlices);
close();
}
</code></pre>
</section>
<section data-menu-title="Solution 2" class="future" style="top: 97px; display: none;" hidden="" aria-hidden="true">
<h2>Solution 2:</h2>
<pre><code data-line-numbers="20-26" class="javascript">#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix
processFolder(input);
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
function processFile(input, output, file) {
open(input+ File.separator +file);
print(nSlices);
run("Duplicate...", " ");
saveAs("Tiff", output+File.separator+File.nameWithoutExtension+"_slice1" + suffix);
close();
close();
//or close("*")
}
</code></pre>
</section>
<section data-menu-title="Solution 3" class="future" style="top: 97px; display: none;" hidden="" aria-hidden="true">
<h2>Solution 3:</h2>
<pre><code data-line-numbers="20-26" class="javascript">#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix
processFolder(input);
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
function processFile(input, output, file) {
open(input+ File.separator +file);
print(nSlices);
setSlice(5);
run("Duplicate...", " ");
run("Subtract Background...", "rolling=20");
saveAs("tiff", output+File.separator+File.nameWithoutExtension+"_slice5"+ suffix);
close("*");
}
</code></pre>
</section>
<section data-menu-title="Solution 4" class="future" style="top: 97px; display: none;" hidden="" aria-hidden="true">
<h2>Solution 4:</h2>
<pre><code data-line-numbers="20-31" class="javascript">#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix
processFolder(input);
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
function processFile(input, output, file) {
open(input+ File.separator +file);
print(nSlices);
for (i=1; i<=nSlices; i++){
setSlice(i);
run("Duplicate...", " ");
run("Subtract Background...", "rolling=20");
// Note: if you change the file type, e.g. to PNG, saveAs will yield the new name as File.nameWithoutExtension
// So then you will want to store the initial File.nameWithoutExtension in a variable before this loop
saveAs("tiff", output+File.separator+File.nameWithoutExtension+"_slice"+i+suffix);
close();
}
close();
}
</code></pre>
</section>
<section data-menu-title="Solution 5" class="future" style="top: 97px; display: none;" hidden="" aria-hidden="true">
<h2>Solution 5:</h2>
<pre><code data-line-numbers="21-35|38-39" class="javascript">#@ File (label = "Input directory", style = "directory") input
#@ File (label = "Output directory", style = "directory") output
#@ String (label = "File suffix", value = ".tif") suffix
processFolder(input);
// function to scan folders/subfolders/files to find files with correct suffix
function processFolder(input) {
list = getFileList(input);
list = Array.sort(list);
for (i = 0; i < list.length; i++) {
if(File.isDirectory(input + File.separator + list[i]))
processFolder(input + File.separator + list[i]);
if(endsWith(list[i], suffix))
processFile(input, output, list[i]);
}
}
function processFile(input, output, file) {
open(input+ File.separator +file);
print(nSlices);
for (i=1; i<=nSlices; i++){
setSlice(i);
run("Duplicate...", " ");
run("Subtract Background...", "rolling=20");
// Note: if you change the file type, e.g. to PNG, saveAs will yield the new name as File.nameWithoutExtension
// So then you will want to store the initial File.nameWithoutExtension in a variable before this loop
saveAs("tiff", output+File.separator+File.nameWithoutExtension+"_slice"+i+suffix);
close();
}
run("Z Project...", "start=3 stop=7 projection=[Average Intensity]");
run("Set Measurements...", "mean redirect=None decimal=7");
run("Measure");
saveAs("tiff", output+File.separator+File.nameWithoutExtension+"_projection_BackSubtract" + suffix);
close("*");
}
// Important: this is outside the processFile function!
saveAs("Results", output+File.separator+"MeanIntensity.csv");
</code></pre>
</section>
<section data-state="ims2" data-menu-title="Running/Installing macros"><style>.ims2 header:after { content: ""; }</style>
<h3>Running & Installing macros</h3>
<ul>
<li>Drag-n-drop the <code>.ijm</code> macro file onto the Fiji bar <b>OR</b> use <code>[Plugins > Macros > Edit...]</code>
<br/>Both will launch the Script Editor so you can use <code>run</code>
<br/>Note: <code>[Plugins > Macros > Run...]</code> won't work for macros with Script Parameters</li>
<li>Use <code>[Plugins > Install...]</code> to install a macro into the Plugins menu (bottom section)
<br/>This will <i>copy the macro</i> to the <code>plugins</code> folder inside your Fiji installation
</li>
<li>Make a folder in <code>Fiji/plugins</code>, e.g. <code>"ZZ My Macros"</code> to collect your macros and have easy access</li>
<li>Copy your macro file to <code>Fiji/plugins/Macros</code> (or make a folder there) to make it appear at bottom of [Plugins > Macros]</li>
</ul>
<p>Note: For the menus, the file name <b>must</b> have an <code>_</code> e.g. <code>Process_Folder.ijm</code> or <code>_MyMacro.ijm</code>
</section>
</div>
<!-- Footer -->
<img style="position:fixed;bottom:1em;right:1em;" src="img/jax.jpg" width="20%">
<!--img style="position:fixed;bottom:1em;left:1em;" src="img/logo_tw.png" width="20%"-->
</div>
<script src="dist/reveal.js"></script>
<script src="plugin/menu/menu.js"></script>
<script src="plugin/highlight/highlight.js"></script>
<script src="plugin/zoom/zoom.js"></script>
<script src="plugin/markdown/markdown.js"></script>
<script src="plugin/copycode/copycode.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
controls: true,
controlsLayout: 'edges',
slideNumber: true, //-- Added for development
hashOneBasedIndex: true,
hash: true,
respondToHashChanges: true,
plugins: [ RevealZoom, RevealHighlight, RevealMarkdown, RevealMenu, CopyCode ],
menu: {
hideMissingTitles: true,
openSlideNumber: true,
},
copycode: {
display: "icons",
style: {
copybg: "white",
}
}
});
</script>
</body>
</html>