forked from erickmartins/training
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIntermediateFiji_macros.html
969 lines (762 loc) · 36.1 KB
/
IntermediateFiji_macros.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
<!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>
<!-- ---------------------- START OF SLIDES -------------------------- -->
<div class="slides">
<section data-menu-title="Title">
<h2>Intermediate Fiji: Macros and Scripting</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><a href="https//github.com/phivk">Philo van Kemenade</a> for the <a href="https://slides.com/phivk/intro2python">set of slides</a> Erick used for the structure of this slide deck and some images</li>
</section>
<section data-state="intro0">
<h2>
<b>This Workshop</b>
</h2>
<ul>
<li>Programming building blocks</li>
<li>Putting the pieces together</li>
<li>Writing complete structures</li></ul>
</section>
<section data-state="intro0">
<h2>These Slides</h2>
<br/>
<a href="https://thejacksonlaboratory.github.io/fiji_workshops/IntermediateFiji_macros.html" target="_blank">https://thejacksonlaboratory.github.io/
fiji_workshops/IntermediateFiji_macros.html</a>
<br/>
</section>
<section data-state="intro0">
<h2>Getting help</h2>
<ul>
<li>Macro Function Documentation at <a href="https://imagej.net/ij/developer/macro/functions.html" target="_blank">https://imagej.nih.gov/ij/developer/macro/functions.html</a>—open it in a new tab!</li>
<li><code>[Plugins > Macro > Record...]</code> is your best friend</li>
<li><a target="_blank" href="https://bridges.monash.edu/articles/educational_resource/Fiji_Training_Manual_v6_5_/21901989">Fiji Training Notes by Cameron Newell, Monash University</a></li>
<li><a href="https://forum.image.sc">image.sc forum</a></li>
</ul>
</section>
<section data-state="intro0">
<h2>Why macros?</h2>
<ul>
<li>Automation: do the same thing many times over</li>
<li>Reproducibility: do exactly the same thing every time, show the world what you've done</li>
<li>Sharing: let someone else do exactly the same analysis as you</li>
<li>Extension: do things Fiji cannot do by default</li>
</ul>
</section>
<!-- ---------------------- APPLICATIONS BATCH PROCESSING -------------------------- -->
<section id="Batch" data-menu-title="Batch Processing" data-state="apps20z"><style>.apps20z header:after { content: ""; }</style>
<h1>Batch Processing</h1>
<h4>Why batch process? File conversion, batch processing, scripting</h4>
</section>
<section data-state="apps20"><style>.apps20 header:after { content: "Batch Processing: Theory"; }</style>
<p>Manual analysis (while sometimes necessary) can be laborious, error prone and not provide the provenance required. Batch processing allows the same processing to be run on multiple images.</p>
<p>The built-in <code>[Process > Batch]</code> menu has lots of useful functions:</p>
<img class="half" src="img-ia/batch_01.png"/>
</section>
<section data-menu-title="Macro Recorder" data-state="apps22"><style>.apps22 header:after { content: "Batch Processing: Macro Recorder"; }</style>
<ul>
<li>Start using: <code>[Plugins > Macros > Record...]</code></li>
<li>Almost every action you take in Fiji will be recorded as a macro command--this includes many Plugin functions!</li>
<li>The recorded Macros can be saved ("Create" button and Save) and then re-run <code>[Plugins > Macros > Run...]</code></li>
</ul>
<img src="img-ia/macro_recorder.png"/>
</section>
<!--BEGINNING of TASK 8 -->
<section data-state="apps22"><style>.apps22 header:after { content: "Batch Processing: Macro Recorder"; }</style>
<h2>Exercise: Macro Recorder</h2>
<ul><li>Download this zip file <a href="material/BBBC008_partial.zip"><code>BBBC008_partial.zip</code></a> and extract to a known location.
<li>Create a second, empty folder called "Output" next to it</li>
<li>Open <code><a href="material/Task1_MacroRec.pdf">Task1_MacroRec.pdf</a></code> in a new tab and follow the instructions there.</li></ul>
</section>
<!--END of TASK 8 -->
<section data-state="apps23z">
<h2>The Script Editor</h2>
<p></p><code>[File > New > Script...]</code> (or keyboard shortcut <code>[</code>)</p>
<img src="img-ia/script_editor.png"/>
</section>
<section data-state="apps23"><style>.apps23 header:after { content: "Fiji Script Editor"; }</style>
<p>The Script Editor can can be used to edit, write, and run macros & scripts. It has a number of nice features:
<ul>
<li>Support for multiple scripting languages, including ImageJ macro</li>
<li>Code hints and completions</li>
<li><code>Run</code> button to easily test macros</li>
<li>Templates to help get you started</li>
<li>Help menu with resources</li>
</ul>
</section>
<section data-state="apps23"><style>.apps23 header:after { content: "Fiji Script Editor"; }</style>
<p>Handy shortcuts:</p>
<ul>
<li>Code suggestions/completions:
<ul>
<li>Control-Space to trigger completions</li>
<li>Arrows to select completions, Tab to accept them</li>
</ul>
<li>Command/Control-R: Run the whole macro</li>
<li>Shift-Command/Control-R: Run selected lines</li>
</ul>
</section>
<section data-state="apps24">
<h2>ImageJ Macro Language</h2>
<p>We will be using the ImageJ Macro language. It's the native programming language to ImageJ, is used by the Macro Recorder, and it's fairly simple—an ideal starting point.</p>
<ul>
<li><a href"https://imagej.net/scripting/macro">Why macros?</a>—A handy overview</li>
<li><a href="https://imagej.net/ij/developer/macro/macros.html">ImageJ Macro Language documentation</a></li>
</ul>
<img src="img-ia/macro_recorder_create.png"/>
<p>Note: If you want to explore other languages on your own, the <a href="https://imagej.net/scripting/">ImageJ Scripting Guide</a> can be a good place to start.</p>
</section>
<section data-menu-title="Building Blocks" data-state="bb">
<h1>
Building Blocks
</h1>
</section>
<section data-state="bb1z">
<h1>Variables</h1>
<img src="img-py/var-name-red.png"/>
<br/>
<font color="#e06666">
<b><i>Variables</i></b></font> store <i>values</i> under a specified name
<br/>
</section>
<section data-state="bb1"><style>.bb1 header:after { content: "Variables"; }</style>
<img src="img-py/vars-nametags.png"/>
</section>
<section data-state="bb1" id="Variable_Types">
<h2>Types of variables</h2>
<p>Variables can store values of different <b><i>types</i></b>:</p>
<p style="text-align: left;">
<font color="#e06666"><b>string</b></font> - a sequence of characters, comprising text
</p>
<pre data-cc="false"><code>"a", "London", 'X', 'General Assembly'</code></pre>
<p style="text-align: left;">
<font color="#e06666"><b>int</b></font> - an integer or whole number
</p>
<pre data-cc="false"><code>1, 5, 9999, -7</code></pre>
<p style="text-align: left;">
<font color="#e06666"><b>float</b></font> - a floating point number (using a decimal point)
</p>
<pre data-cc="false"><code>3.14, 1.68, 1.0</code></pre>
<p style="text-align: left;">
<font color="#e06666"><b>bool</b></font> - boolean; binary true or false values
</p>
<pre data-cc="false"><code>true, false</code></pre>
<p class="fragment">Note: ImageJ Macro Language variables are dynamically typed—you don't need to declare the type.</p>
</section>
<section data-state="bb1">
<h2>Assigning variables in ImageJ Macro</h2>
<pre><code>name = "Erick";
height = 1.73;
state = "CT";</code></pre>
<p class="fragment">Why does running this macro do nothing? 🤔</p>
</section>
<section data-state="bb1">
<h2>Changing types</h2>
<p>'Casting' a variable to another type requires some of the <a href="https://imagej.net/ij/developer/macro/functions.html" target="_blank">built-in functions</a> we have mentioned.</p>
<p>
<br/>
</p>
<pre><code data-line-numbers="1|2-3|4-5|6-7">// cast strings into numbers (int and float)
a = parseInt("42");
b = parseFloat("1.69");
// cast a number into a string with specified decimal places
c = d2s(1.5333, 2);
// print the value stored in the variable `c`
print(c);
</code></pre>
<p class="fragment">What would <code>print(c+1);</code> print? 🤔</p>
</section>
<section data-state="bb1">
<h2>Exercise: Variables and Types
</h2>
<br/>
<p>Choose 4 things around you and create variables of 4 different types based on them.
<br/>If you need to, refer to the <a href="#/Variable_Types">variable types slide</a>.</p>
</section>
<section data-state="bb12z">
<h2>Operators</h2>
<p>
<span class="s1">You can process the values in your variables by
<b>
<i>
<font color="#e06666">operators</font>
</i>
</b>:
</span>
</p>
<table width="100%" cellspacing="10" cellpadding="5" border="0">
<tbody>
<tr>
<td>
<font style="font-size: 30px;" color="#e06666">
<b>=</b>
</font>
</td>
<td>
<font style="font-size: 30px;">Assignment: assign a value to a variable </font>
</td>
</tr>
<tr>
<td>
<font style="font-size: 30px;" color="#e06666">
<b>==</b>
</font>
</td>
<td>
<font style="font-size: 30px;">Comparison: are two variables equal?</font>
</td>
</tr>
<tr>
<td>
<font style="font-size: 30px;" color="#e06666">
<b>!=</b>
</font>
</td>
<td>
<font style="font-size: 30px;">Comparison: are two variables not equal?</font>
</td>
</tr>
<tr>
<td>
<p class="p1">
<span class="s1">
<b>
<font style="font-size: 30px;" color="#e06666"><, >, <=, >=</font>
</b>
</span>
</p>
</td>
<td>
<font style="font-size: 30px;">Less-than, greater-than, less or equal, greater or equal</font>
</td>
</tr>
<tr>
<td>
<font style="font-size: 30px;" color="#e06666">
<b>+, -, *, /</b>
</font>
</td>
<td>
<font style="font-size: 30px;">Mathematical operators</font>
</td>
</tr>
<tr></tr>
<tr>
<td>
<font style="font-size: 30px;" color="#e06666">
<b>&&, ||, !</b>
</font>
</td>
<td>
<font style="font-size: 30px;">Logical operators: and, or, not</font>
</td>
</tr>
</tbody>
</table>
<p><b>Important</b>: the type of the first operand will determine the result!</p>
</section>
<section data-state="bb12"><style>.bb12 header:after { content: "Operators"; }</style>
<p>A few more handy operators:</p>
<table width="100%" cellspacing="10" cellpadding="5" border="0">
<tbody>
<tr>
<td>
<font style="font-size: 30px;" color="#e06666">
<b>++</b>
</font>
</td>
<td>
<font style="font-size: 30px;">Increment; add 1, e.g. <code>i++</code></font>
</td>
</tr>
<tr>
<td>
<font style="font-size: 30px;" color="#e06666">
<b>--</b>
</font>
</td>
<td>
<font style="font-size: 30px;">Decrement; subtract 1, e.g. <code>i--</code></font>
</td>
</tr>
<tr>
<td>
<font style="font-size: 25px;" color="#e06666">
<b>+=, -=, *=, /=</b>
</font>
</td>
<td>
<font style="font-size: 30px;">Perform the operation on the variable and re-assign</font>
</td>
</tr>
</tbody>
</table>
</section>
<section data-state="bb12"><style>.bb12 header:after { content: "Operators"; }</style>
<h2>String manipulation</h2>
<table width="100%" cellspacing="10" cellpadding="5" border="0">
<tbody>
<tr></tr>
<td>
<font style="font-size: 30px;" color="#e06666">
<b>+</b>
</font>
</td>
<td>
<font style="font-size: 30px;"><code>+</code> operator on strings concatenates them</font>
</td>
</tr>
<tr>
<td>
<font style="font-size: 30px;" color="#e06666">
<code>replace(string, old, new)</code>
</font>
</td>
<td>
<font style="font-size: 30px;">Take <code>string</code>, replace <code>old</code> occurrences with <code>new</code></font>
</td>
</tr>
<tr>
<td>
<font style="font-size: 30px;" color="#e06666">
<code>split(string, delimiter)</code>
</font>
</td>
<td>
<font style="font-size: 30px;">Split the <code>string</code> at every <code>delimiter</code> into an array of strings</font>
</td>
</tr>
<tr>
<td>
<font style="font-size: 30px;" color="#e06666">
<code>lengthOf(string)</code>
</font>
</td>
<td>
<font style="font-size: 30px;">Return the length of <code>string</code></font>
</td>
</tr>
<tr></tr>
<td>
<font style="font-size: 30px;" color="#e06666">
<code>endsWith(string, suffix)</code>
</font>
</td>
<td>
<font style="font-size: 30px;">Return true (1) if <code>string</code> ends with <code>suffix</code></font>
</td>
</tr>
</tbody>
</table>
</section>
<section data-state="bb12"><style>.bb12 header:after { content: "Operators"; }</style>
<h2>Exercise: Operators (strings)</h2>
<p>
Create two
<font color="#e06666">
<i>string variables</i>
</font>:
</p>
<p>
<i>first</i> for your first name and <i>last</i> for your last name.
</p>
<p>
Can you make your full name by combining <i>first</i> and <i>last</i>?
</p>
<p>
<font color="#e06666">
(Cheating encouraged)
</font>
</p>
</section>
<section data-state="bb2z">
<h2>
Collections
</h2>
</section>
<section data-state="bb2"><style>.bb2 header:after { content: "Collections"; }</style>
<h2>Collections of Values</h2>
Values can also be stored in a
<font color="#e06666">
<b>
<i>collection</i>
</b>
</font>:
<p><b>we'll only talk about the Array</b></p>
</section>
<section data-state="bb2"><style>.bb2 header:after { content: "Collections"; }</style>
<h2>Arrays</h2>
<p>
We can store multiple values in an <b><i><font color="#e06666">array</font>:</i></b>
<br/>An array is a ordered sequence of items (between [...]) each with their own index.
</p>
<pre><code data-line-numbers class="javascript">b = newArray(1, 3, 9, 4, 884328881);
n = newArray("first", "second", "third", "fourth");
m = Array.concat(b, n);
print(m[6]);
</code></pre>
<p class="fragment">What is the index of the <i>first</i> element of the array? 🤔</p>
</section>
<section data-state="bb2"><style>.bb2 header:after { content: "Collections"; }</style>
<h2>Some array examples
<br/>
</h2>
<pre><code data-line-numbers="1-5|6-8|9-11|12-19" class="javascript">// This macro demonstrates how to use the Array.* functions.
a1 = newArray(10, 5, 15, 2, 1.23456);
a2 = Array.copy(a1);
print("Array 2")
Array.print(a2);
a3 = Array.trim(a1, 2);
print("Array 3");
Array.print(a3);
Array.sort(a1);
print("Array 1");
Array.print(a1);
Array.getStatistics(a1, min, max, mean, std);
print("stats");
print(" min: "+min);
print(" max: "+max);
print(" mean: "+mean);
print(" std dev: "+std);
Array.fill(a1, -1);
Array.print(a1);
</code></pre>
</section>
<section data-state="bb2"><style>.bb2 header:after { content: "Collections"; }</style>
<h2>Some more array examples
<br/>
</h2>
<pre><code data-line-numbers="1-5|6-7|8-9" class="javascript">// Arrays need not contain numbers!
a1 = newArray("one", "two", "three", "four");
print("");
a2 = Array.copy(a1);
Array.print(a1);
a2 = Array.trim(a1, 2);
Array.print(a2);
Array.sort(a1);
Array.print(a1);
</code></pre>
</section>
<section data-menu-title="Putting the pieces together" data-state="pp">
<h1>
Putting the pieces together
</h1>
</section>
<section data-state="pp">
<h1>
Loops
</h1>
<p>"Loops" are a form of "flow of control" (<i>control flow statement</i>) that specifies iteration of a section of code.
<br/>That same code block should be repeated a defined number of times or until some condition has been met, etc.</p>
</section>
<section data-menu-title="For Loops" data-state="pp1"><style>.pp1 header:after { content: "Loops: For Loops"; }</style>
<h2>For Loops</h2>
<p>
You use <i><font color="#e06666">loops</font></i> to repeat a statement.
</p>
<p>
A <b><i><font color="#e06666">for-loop</font></i></b> is useful when you know how many times you want to repeat an action (e.g. for every item in an array)
</p>
<pre data-cc="false"><code class="javascript" >for (start; condition; increment){
do something
}</code></pre>
</section>
<section data-state="pp1"><style>.pp1 header:after { content: "Loops: For Loops"; }</style>
<p>An example of using a <code>for</code> loop:
</p>
<pre><code data-line-numbers="0|1|2|3-4" class="javascript">ages = newArray(18, 21, 16, 12);
for(i=0; i< lengthOf(ages); i++){
print(ages[i]);
}
</code></pre>
<p>Output:</p>
<pre class="fragment" data-cc="false"><code>18
21
16
12
</code></pre>
</section>
<section data-menu-title="While Loops" data-state="pp2"><style>.pp2 header:after { content: "Loops: While Loops"; }</style>
<h2>While Loops</h2>
<p>A <b><i><font color="#e06666">while-loop</font></i></b> is useful when you don’t know how many times you want to run, but you know when you need to stop.
</p>
<p>A while-loop statement checks a <i>condition</i> and loops until the condition is no longer satisfied.
</p>
<pre data-cc="false"><code class="javascript">while (condition){
do something;
}
</code></pre>
</section>
<section data-state="pp2"><style>.pp2 header:after { content: "Loops: While Loops"; }</style>
<p>An example of a <code>while</code> loop:
</p>
<pre><code data-line-numbers="0|1|2|3|4-5" class="javascript">x = 2;
while(x > 0){
print("hooray!");
x = x - random; // `random` is a built-in, returns a random number between 0 and 1
}
</code></pre>
<p>How many times will this code print "hooray!"?</p>
</section>
<section data-state="pp3">
<h1>
Conditional Statements
</h1>
<p>Conditionals enable your code to perform different actions/computations based on the boolean value of a specific expression, the condition.</p>
</section>
<section data-state="pp3"><style>.pp3 header:after { content: "Conditional Statements"; }</style>
<p><b><i><font color="#e06666">Conditional statements</font></i></b> enable you to deal with multiple options.
</p>
<p>
A part of your code is executed based on the truth value of a condition. You perform conditional checks with: <b>if, else</b>
</p>
<pre data-cc="false"><code class="javascript">if(condition){
action
}
else{
final action
}</code></pre>
<pre><code data-line-numbers class="javascript">age = 17;
if(age < 21){
print("no drinks for you");
}
</code></pre>
<p>Notice, you don't need an <code>else</code>, if you don't need one.</p>
</section>
<section data-state="pp4">
<h1>
Functions
</h1>
<p>Functions are <i>callable</i> units of code with defined interfaces, meant to be invoked multiple times.
<br/>They allow you to re-use code, rather than re-writing it!
<br/><b>DRY</b>: Don't Repeat Yourself!</p>
</section>
<section data-state="pp4"><style>.pp4 header:after { content: "Functions"; }</style>
<p><font color="#e06666"><b><i>Functions</i></b></font> perform a collections of tasks, bundled under a specific name
</p>
<p>Take input <i>argument(s)</i>, execute statement(s), return output
</p>
<p>
Input and output can be of all different types
</p>
<p>We've already been using various built-in funtions!</p>
<pre><code data-line-numbers class="javascript">name = "Erick Ratamero";
length = lengthOf(name); // `lengthOf` is a built-in function
print(length); // `print` is a built-in function
</code></pre>
<br/>
</section>
<section data-state="pp4"><style>.pp4 header:after { content: "Functions"; }</style>
<p>As a reminder, here is the super handy reference of built-in functions:
<br/>
<a href="https://imagej.net/ij/developer/macro/functions.html" target="_blank">https://imagej.net/ij/developer/macro/functions.html</a>
</p>
</section>
<section data-state="pp4"><style>.pp4 header:after { content: "Functions"; }</style>
<p>You can also <i><b>define</b> your own functions</i> like this:
</p>
<pre data-cc="false"><code class="javascript">function function_name(argument(s)){
actions with argument(s)
return statement
}
</code></pre>
<p>For example, a complex calculation you want to repeat:</p>
<pre><code data-line-numbers="1|2|3|4-5|6-7|8" class="javascript">// define a function
function calculate(x, y, sigma){
factor = exp(-1.0*(pow(x,2)+pow(y,2))/(2*pow(sigma,2)));
return factor;
}
// call the function
result = calculate(1,2,5);
print(result);
</code></pre>
</section>
<section data-state="pp4"><style>.pp4 header:after { content: "Functions"; }</style>
<h2>Exercise: Functions</h2>
<p>
Create a
<font color="#e06666">
<i>function</i>
</font> that takes two numbers and returns <font color="#e06666">
<i>"BIG!"</i>
</font> if they add up to at least 50, and
<font color="#e06666">
<i>"SMALL!"</i>
</font> otherwise.
</p>
<p>
Make sure to test that it is working as intended!
</p>
<p>
<font color="#e06666">
(Cheating encouraged)
</font>
</p>
</section>
<section id="Image File Handling" data-menu-title="Image File Handling" data-state="ims1"><style>.ims1 header:after { content: ""; }</style>
<h1>Image File Handling</h1>
<h4>Some handy built-ins for handling image files</h4>
</section>
<section data-state="ims1"><style>.ims1 header:after { content: "Image File Handling"; }</style>
<p>The macro language provides some built-ins for handling image files.</p>
<table width="100%" cellspacing="10" cellpadding="5" border="0">
<tbody>
<tr>
<td>
<font style="font-size: 25px;" color="#e06666">
<code>open(path)</code>
</font>
</td>
<td>
<font style="font-size: 25px;">Open and display the image located at <code>path</code>. Note the <code>path</code> should be provided as a string (in quotes).</font>
</td>
</tr>
<tr></tr>
<td>
<font style="font-size: 25px;" color="#e06666">
<code>getTitle()</code>
</font>
</td>
<td>
<font style="font-size: 25px;">Returns the title of the current image.</font>
</td>
</tr>
<tr>
<td>
<font style="font-size: 25px;" color="#e06666">
<code>close(title)</code>
</font>
</td>
<td>
<font style="font-size: 25px;">Close window with a given <code>title</code>. Note that the string <code>title</code> can contain wild cards.</font>
</td>
</tr>
<tr></tr>
<td>
<font style="font-size: 25px;" color="#e06666">
<code>getFileList(path)</code>
</font>
</td>
<td>
<font style="font-size: 25px;">List the contents of the directory located at <code>path</code></font>
</td>
</tr>
<tr></tr>
<td>
<font style="font-size: 25px;" color="#e06666">
<code>File.separator</code>
</font>
</td>
<td>
<font style="font-size: 25px;">Returns the platform specific path file separator (e.g. / or \) as a string</font>
</td>
</tr>
<tr>
<td>
<font style="font-size: 25px;" color="#e06666">
<code>getImageID()</code>
</font>
</td>
<td>
<font style="font-size: 25px;">Returns the unique ID of an image. Use with <code>selectImage(id)</code>, <code>isOpen(id)</code>, and <code>isActive(id)</code>.</font>
</td>
</tr>
</tbody>
</table>
</section>
<section data-state="ims2" data-menu-title="Troubleshooting"><style>.ims2 header:after { content: "Troubleshooting"; }</style>
<h2>You will have bugs and will get errors--everyone does!</h2>
<br>What to do? <b>Read the error message and look for clues!</b>
<ul>
<li>Which line number triggered the error?</li>
<li>What is wrong/missing?</li>
</ul>
<p>Read your code <i>carefully</i> around the problem line. Look for the clues, but they may be one or two lines earlier!</p>
</section>
<section data-state="ims2"><style>.ims2 header:after { content: "Troubleshooting"; }</style>
<h2>Run and debug this code</h2>
<pre><code data-line-numbers class="javascript">Filename = "experiment1_ch1_t1.tif";
if(startsWith(Filename, "experiment1"){
print("First experiment file:", filename);
}
</code>
</section>
<section data-state="ims1"><style>.ims1 header:after { content: "Starting to put it together"; }</style>
<h2>Exercise: Count Objects</h2>
<p>Let's run a simple threshold and object counting workflow on a folder of files and get a summary table. How would we do this?</p>
<ul>
<li>Figure out the workflow on a single image:
<ul>
<li>Threshold -- pick one, e.g. Otsu</li>
<li>Analyze Particles -- what do we want as the "result" per image?</li>
</ul>
<li>Use the Macro Recorder to get the workflow steps</li>
<li>Loop over all the files in a folder and run the workflow commands</li>
</ul>
<ul><li>If you havn't, download this zip file <a href="material/BBBC008_partial.zip"><code>BBBC008_partial.zip</code></a> and extract to a known location.
<li>Open <code><a href="material/Task2_CountMacro.pdf">Task1_CountMacro.pdf</a></code> in a new tab and follow the instructions there.</li></ul>
</section>
<section id="A proper macro" data-menu-title="Hands-on: a proper macro" data-state="pijm1"><style>.pijm1 header:after { content: ""; }</style>
<h1>Hands-on: Assembling a proper ImageJ macro</h1>
<h4><a href="https://thejacksonlaboratory.github.io/fiji_workshops/macroWorkshop.html">https://thejacksonlaboratory.github.io/fiji_workshops/macroWorkshop.html</a></h4>
</section>
</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>