-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpresentation.html
More file actions
1138 lines (1046 loc) · 46.9 KB
/
Copy pathpresentation.html
File metadata and controls
1138 lines (1046 loc) · 46.9 KB
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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="dns-prefetch" href="https://cdnjs.cloudflare.com/">
<link rel="dns-prefetch" href="https://agenttix.kapsi.fi/">
<link rel="dns-prefetch" href="https://www.ptplot.org/">
<title>Unit testing & CI/CD pipelines for research</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/5.2.1/reveal.min.css" integrity="sha512-ZIo9ivf88U0L1S+0D/npMNTTkjez5B9cK5xIecN4IA2OmtbTNBbPAZo/0cBC195sq7FU7cEOVPwtit37f/+1rQ==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/5.2.1/plugin/highlight/monokai.min.css" integrity="sha512-z8wQkuDRFwCBfoj7KOiu1MECaRVoXx6rZQWL21x0BsVVH7JkqCp1Otf39qve6CrCycOOL5o9vgfII5Smds23rg==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/5.2.1/theme/sky.min.css" integrity="sha512-gLymIttr2gq642OBM4g7Oab3Bx3J1XB1TSbP5DZqcXaDWULMmSHHHkXFdRsx4lMFmt9705mNRozs1qeSvOAJpw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<style>
:root {
--bg-deep: #07081a;
--bg-mid: #121638;
--bg-glow: #2a2458;
--fg-primary: #f0ece4;
--fg-muted: #a9a4bf;
--accent-warm: #f5b461; /* hot plasma */
--accent-cool: #6ee3d6; /* bubble wall */
--accent-pink: #ff6b9d; /* broken-phase bubble */
--accent-deep: #7a5cff; /* deep violet */
--stroke-dim: rgba(240, 236, 228, 0.12);
--stroke-mid: rgba(240, 236, 228, 0.28);
--font-display: "Fraunces", Georgia, serif;
--font-body: "IBM Plex Sans", system-ui, sans-serif;
--font-mono: "JetBrains Mono", Menlo, monospace;
}
/* --- Global background: deep cosmic gradient with subtle bubbles --- */
html, body {
background: var(--bg-deep);
}
.reveal-viewport {
background:
radial-gradient(ellipse 80% 50% at 20% 10%, rgba(122,92,255,0.18), transparent 60%),
radial-gradient(ellipse 60% 40% at 85% 85%, rgba(110,227,214,0.10), transparent 60%),
radial-gradient(ellipse 50% 40% at 50% 50%, rgba(245,180,97,0.05), transparent 70%),
linear-gradient(180deg, #07081a 0%, #121638 100%);
background-attachment: fixed;
}
/* --- reveal.js typography overrides --- */
.reveal {
font-family: var(--font-body);
color: var(--fg-primary);
font-weight: 400;
}
.reveal .slides {
text-align: left;
}
.reveal h1, .reveal h2, .reveal h3, .reveal h4 {
font-family: var(--font-display);
color: var(--fg-primary);
text-transform: none;
letter-spacing: -0.01em;
font-weight: 500;
line-height: 1.1;
}
.reveal h1 { font-size: 2.8em; font-weight: 400; font-variation-settings: "opsz" 144; }
.reveal h2 { font-size: 1.7em; margin-bottom: 0.6em; }
.reveal h3 { font-size: 1.2em; color: var(--accent-warm); margin-bottom: 0.4em; }
.reveal h4 { font-size: 0.95em; color: var(--accent-cool); text-transform: uppercase; letter-spacing: 0.12em; font-weight: 500; font-family: var(--font-body); }
.reveal p, .reveal li {
font-size: 0.8em;
line-height: 1.45;
color: var(--fg-primary);
}
.reveal ul, .reveal ol {
margin-left: 1em;
}
.reveal ul li ul li {
font-size: 0.9em;
}
.reveal li { margin-bottom: 0.35em; }
.reveal li::marker { color: var(--accent-cool); }
.reveal strong { color: var(--accent-warm); font-weight: 600; }
.reveal em { color: var(--accent-cool); font-style: italic; }
/* Inline code */
.reveal code {
font-family: var(--font-mono);
background: rgba(110,227,214,0.08);
color: var(--accent-cool);
padding: 0.1em 0.35em;
border-radius: 3px;
font-size: 0.9em;
}
/* Block code */
.reveal pre {
/*font-size: 1em;*/
width: 100%;
box-shadow: 0 10px 40px rgba(0,0,0,0.5);
border-radius: 6px;
border: 1px solid var(--stroke-dim);
margin: 0.4em 0;
}
.reveal pre code {
font-size: 1em;
font-family: var(--font-mono);
background: rgba(7,8,26,0.85);
color: #f0ece4;
padding: 1em 1.2em;
border-radius: 6px;
max-height: 1000px;
/*font-size: 0.56em;*/
line-height: 1.45;
}
/* Links */
.reveal a {
color: var(--accent-cool);
text-decoration: none;
border-bottom: 1px dotted var(--accent-cool);
}
.reveal a:hover { color: var(--accent-pink); border-bottom-color: var(--accent-pink); }
/* Slide number + controls */
.reveal .slide-number {
font-family: var(--font-mono);
background: transparent;
color: var(--fg-muted);
font-size: 14px;
}
.reveal .progress {
color: var(--accent-cool);
height: 2px;
}
/* --- Custom slide archetypes --- */
/* Title slide */
section.title-slide {
text-align: center !important;
}
section.title-slide h1 {
font-size: 3.4em;
font-weight: 300;
font-style: italic;
color: var(--fg-primary);
margin-bottom: 0.2em;
}
section.title-slide .subtitle {
font-family: var(--font-display);
font-size: 1.3em;
color: var(--accent-warm);
font-weight: 400;
font-style: italic;
margin-bottom: 1.5em;
}
section.title-slide .byline {
font-family: var(--font-mono);
font-size: 0.7em;
color: var(--fg-muted);
letter-spacing: 0.05em;
}
section.title-slide .byline span {
color: var(--accent-cool);
}
/* Section divider slide */
section.divider {
text-align: center !important;
}
section.divider h4 {
font-size: 0.8em;
margin-bottom: 1em;
}
section.divider h1 {
font-size: 3em;
font-style: italic;
font-weight: 300;
letter-spacing: -0.02em;
}
section.divider .part-num {
font-family: var(--font-mono);
color: var(--accent-warm);
font-size: 0.8em;
letter-spacing: 0.3em;
display: block;
margin-bottom: 0.5em;
}
/* Hands-on callout */
section.hands-on {
background: linear-gradient(135deg, rgba(255,107,157,0.08), rgba(245,180,97,0.05));
}
section.hands-on::before {
content: "";
position: absolute;
inset: 40px;
border: 1px dashed rgba(255,107,157,0.3);
border-radius: 4px;
pointer-events: none;
}
.hands-on-badge {
display: inline-block;
font-family: var(--font-mono);
font-size: 0.55em;
background: var(--accent-pink);
color: var(--bg-deep);
padding: 0.3em 0.9em;
letter-spacing: 0.2em;
font-weight: 600;
border-radius: 2px;
margin-bottom: 0.8em;
}
/* Two-column layout */
.two-col {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 1.5em;
align-items: start;
}
.two-col.wide-left { grid-template-columns: 1.4fr 1fr; }
.two-col.wide-right { grid-template-columns: 1fr 1.4fr; }
/* Callouts / notes */
.callout {
border-left: 2px solid var(--accent-warm);
padding: 0.3em 0 0.3em 0.8em;
font-size: 0.7em;
color: var(--fg-muted);
font-style: italic;
}
.pill {
display: inline-block;
font-family: var(--font-mono);
font-size: 0.6em;
background: rgba(110,227,214,0.1);
color: var(--accent-cool);
padding: 0.15em 0.6em;
border-radius: 999px;
border: 1px solid var(--accent-cool);
margin-right: 0.3em;
}
/* Small tag at corner of each content slide */
.topic-tag {
position: absolute;
top: 30px;
right: 60px;
font-family: var(--font-mono);
font-size: 12px;
color: var(--fg-muted);
letter-spacing: 0.15em;
text-transform: uppercase;
}
/* Title slide SVG bubble artwork */
.bubble-artwork {
width: 380px;
height: 260px;
margin: 0 auto 1.5em;
display: block;
}
/* Overflow-friendly section */
.reveal section {
padding-top: 0.3em;
}
/* Speaker notes hint on slides */
.reveal aside.notes { display: none; }
/* Table */
.reveal table {
font-size: 0.7em;
border-collapse: collapse;
margin: 0.5em 0;
}
.reveal table th, .reveal table td {
border: 1px solid var(--stroke-dim);
padding: 0.4em 0.8em;
text-align: left;
}
.reveal table th {
background: rgba(110,227,214,0.08);
color: var(--accent-cool);
font-weight: 500;
}
/* Fragment colors */
.reveal .fragment.highlight-current-blue.visible.current-fragment { color: var(--accent-cool); }
/* Kbd */
kbd {
font-family: var(--font-mono);
background: rgba(240,236,228,0.08);
border: 1px solid var(--stroke-mid);
padding: 0.1em 0.4em;
border-radius: 3px;
font-size: 0.85em;
color: var(--fg-primary);
}
/* Bigquote */
blockquote.big {
font-family: var(--font-display);
font-style: italic;
font-weight: 300;
font-size: 1.3em !important;
line-height: 1.3;
color: var(--fg-primary);
border: none !important;
box-shadow: none !important;
background: none !important;
padding: 0.5em 0 !important;
margin: 0.3em 0;
}
blockquote.big::before {
content: "\201C";
color: var(--accent-warm);
font-size: 2em;
line-height: 0;
vertical-align: -0.4em;
margin-right: 0.1em;
}
.attrib {
font-family: var(--font-body);
font-size: 0.65em;
color: var(--fg-muted);
font-style: normal;
display: block;
margin-top: 0.6em;
}
/* Checklist */
ul.checklist { list-style: none; margin-left: 0; }
ul.checklist li::before {
content: "◯ ";
color: var(--accent-cool);
font-family: var(--font-mono);
}
/* Steps list */
ol.steps {
list-style: none;
counter-reset: step;
margin-left: 0;
}
ol.steps > li {
counter-increment: step;
position: relative;
padding-left: 2em;
margin-bottom: 0.6em;
}
ol.steps > li::before {
content: counter(step, decimal-leading-zero);
position: absolute;
left: 0; top: 0;
font-family: var(--font-mono);
font-size: 0.75em;
color: var(--accent-warm);
font-weight: 600;
}
</style>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/5.2.1/plugin/highlight/highlight.min.js" integrity="sha512-IkPzJ3njtxNL2Frm7zr18CEj9HH+CEFAqh+jSKjXTHVFGtWG1+USbIXINU8zdGmyLpf/OBIVfpNLGNgwhlymkA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/5.2.1/plugin/math/math.min.js" integrity="sha512-tdpe8NdPiaypY/eUgKqr/aL5y0tLiFEde/joBsD4bfjyB8yNuGsReuN/D+XDcGbHQ8/xbDbJ0mqkfGdh2JxCAw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/5.2.1/plugin/notes/notes.min.js" integrity="sha512-6LAcdj7pQal9JkdALNexzVKd4zWuYUxOXuzdF21iOx7LJSlF5ibFi2TAR7GQ00XhfPAw7/R8f1dZ69gFGhzXtw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script defer id="revealjs" src="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/5.2.1/reveal.min.js" integrity="sha512-lRYVCjdH7dCPCzqLL6eBn78p6WGK5pZxgYBgzTQfWlfX7yiZFLt/qHOMSETHUIz2aZIU/KbjwYHiMsmrcVgJnA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<body>
<div class="reveal">
<div class="slides">
<section data-transition="fade">
<h1>Unit testing & CI/CD pipelines for research</h1>
<div>Mika Mäki</div>
<div>Computational Field Theory Group retreat 2026</div>
<div>University of Helsinki</div>
</section>
<section>
<h2>Outline</h2>
<div class="two-col">
<div>
<h4>Presentation</h4>
<ul>
<li>Why test research code at all?</li>
<li>What is unit testing?</li>
<li>Unit testing in Python</li>
<li>What is CI/CD?</li>
<li>How to run CI/CD pipelines in the cloud?</li>
</ul>
</div>
<div>
<h4>Hands-on exercises</h4>
<ul>
<li><strong>Ex. 1</strong>: Write simple unit tests for a Python program</li>
<li><strong>Ex. 2</strong>: Add a GitHub Actions workflow that runs them</li>
</ul>
<p class="callout">Please feel free to interrupt at any moment. Questions are welcome mid-slide.</p>
</div>
</div>
</section>
<section class="divider" data-background-gradient="radial-gradient(circle at 50% 50%, rgba(122,92,255,0.25), transparent 70%)">
<span class="part-num">PART ONE</span>
<h1>Why should I test my code?</h1>
<p style="font-family: var(--font-display); font-style: italic; color: var(--fg-muted); font-size: 1.1em; margin-top: 0.5em;">
We need to be able to trust our results
</p>
</section>
<section>
<h2>Research code has real stakes</h2>
<div class="two-col wide-right">
<div>
<h4>The risk</h4>
<p>A plot. A figure. A paper. A <em>conclusion about the universe</em>.</p>
<p>All depending of a function you wrote in a hurry before a deadline (at 2 AM?) and never quite revisited.</p>
</div>
<div>
<h4>Some famous examples</h4>
<ul>
<li><a href="https://www.science.org/doi/10.1126/science.314.5807.1856"><strong>Miller & Chang (2006)</strong></a> — five <em>Science</em>/<em>PNAS</em> protein structure papers retracted after a sign-flip bug in a home-made analysis script.</li>
<li><a href="https://en.wikipedia.org/wiki/Growth_in_a_Time_of_Debt"><strong>Herndon, Ash & Pollin (2013)</strong></a> — A spreadsheet silently dropped five countries from statistics, leading to widely cited flawed conclusions that affected economic policies.</li>
<li><a href="https://github.com/mrc-ide/covid-sim/issues/358"><strong>Imperial College London (2020)</strong></a> — COVID simulation results depended on the choice of the C++ compiler, and whether the simulation was run multiple times.</li>
<li><strong>Your overnight simulation at 02:37 last Wednesday</strong> — did it? We'll never know.</li>
</ul>
</div>
</div>
<p class="callout">Testing is not about perfection. It's about being able to <em>trust your own results when you open them six months from now</em>.</p>
<!--
<ul>
<li>
In computational research, the code provides the results for your
<ul>
<li>Plots and figures</li>
<li>Conclusions</li>
</ul>
</li>
<li>
Research code is updated all the time
<ul>
<li>Adding new theoretical developments</li>
</ul>
</li>
<li>How can you know that the updates don't change the results?</li>
<li>
Research code is updated all the time
<ul>
<li>Adding new theoretical developments</li>
<li>Extracting additional data from existing simulations</li>
<li>Testing different numerical methods</li>
</ul>
</li>
</ul>
-->
</section>
<section>
<h2>Stories from PTtools</h2>
<div class="two-col wide-left">
<div>
<ul>
<li>
Several additions to the theory over time
<ul>
<li>Temperature-dependent equation of state</li>
<li>$z \rightarrow f$ conversion and $\Omega_{\text{gw},0}$</li>
<li>Low-k power laws for GW spectra</li>
<li>Source lifetime factor</li>
<li>Thermal suppression of bubble nucleation</li>
<li>Droplets</li>
<li>It's more common for a new feature to break something old than not</li>
</ul>
</li>
<li>
Code changes
<ul>
<li>Optimization, compilation with Numba</li>
<li>Parallelisation</li>
<li>Object-oriented interface</li>
</ul>
</li>
<li>Library and Python versions can drop functionality or change its behavior</li>
<li>Incompatibility issues affecting only Linux, Windows or macOS</li>
<li>Users reporting that things don't work as expected</li>
</ul>
</div>
<div>
<! https://stackoverflow.com/questions/4476526/do-i-use-img-object-or-embed-for-svg-files >
<!-- <img src="phase_transition_velocity_field.svg" alt="bubble" style="height: 100%"> -->
<svg viewBox="0 0 680 680" xmlns="http://www.w3.org/2000/svg" style="height: 20em">
<style>
@keyframes sa{0%,87%,100%{opacity:.8}93%{opacity:.15}}
@keyframes sb{0%,72%,100%{opacity:.65}76%{opacity:.1}}
@keyframes gfa{0%,43%,57%,100%{opacity:1}44%,56%{opacity:0}}
@keyframes gfb{0%,18%,26%,100%{opacity:0}19%,25%{opacity:1}}
@keyframes px{0%,37%,43%,100%{opacity:1}39%,41%{opacity:0}}
@keyframes px2{0%,22%,30%,100%{opacity:0}23%,29%{opacity:1}}
.sa{animation:sa 6.9s ease-in-out infinite}
.sb{animation:sb 5.1s ease-in-out infinite}
.gsa{animation:gfa 3.9s step-end infinite}
.gsb{animation:gfb 2.5s step-end infinite}
.px1{animation:px 1.8s step-end infinite}
.px2{animation:px2 2.6s step-end .9s infinite}
</style>
<defs>
<clipPath id="cp1"><rect x="0" y="168" width="680" height="10"/></clipPath>
<clipPath id="cp2"><rect x="0" y="215" width="680" height="7"/></clipPath>
<clipPath id="cp3"><rect x="0" y="262" width="680" height="15"/></clipPath>
<clipPath id="cp4"><rect x="0" y="328" width="680" height="9"/></clipPath>
<clipPath id="cp5"><rect x="0" y="385" width="680" height="20"/></clipPath>
<clipPath id="cp6"><rect x="0" y="455" width="680" height="6"/></clipPath>
<clipPath id="cp7"><rect x="0" y="495" width="680" height="12"/></clipPath>
<clipPath id="cp8"><rect x="0" y="128" width="680" height="7"/></clipPath>
<pattern id="scl" x="0" y="0" width="1" height="3" patternUnits="userSpaceOnUse">
<rect x="0" y="0" width="1" height="1" fill="#000" opacity=".11"/>
</pattern>
<radialGradient id="vf" cx="340" cy="340" r="500" gradientUnits="userSpaceOnUse">
<stop offset="0%" stop-color="#030812"/>
<stop offset="28%" stop-color="#050e1d"/>
<stop offset="34%" stop-color="#080d1e"/>
<stop offset="36.0%" stop-color="#1c0630"/>
<stop offset="36.3%" stop-color="#8e1a5c"/>
<stop offset="36.5%" stop-color="#fff4c0"/>
<stop offset="36.7%" stop-color="#fdc030"/>
<stop offset="37.1%" stop-color="#ee5e1e"/>
<stop offset="39%" stop-color="#d44c16"/>
<stop offset="41.5%" stop-color="#aa2e0e"/>
<stop offset="44%" stop-color="#771a08"/>
<stop offset="45.5%" stop-color="#b03c12"/>
<stop offset="46.5%" stop-color="#300806"/>
<stop offset="55%" stop-color="#1a0504"/>
<stop offset="70%" stop-color="#0e0403"/>
<stop offset="86%" stop-color="#0b0403"/>
<stop offset="100%" stop-color="#07090f"/>
</radialGradient>
<g id="bub">
<rect x="-60" y="-60" width="800" height="800" fill="url(#vf)"/>
</g>
</defs>
<rect width="680" height="680" fill="#07090f"/>
<circle cx="44" cy="30" r=".9" fill="#c8d8e8" class="sa"/>
<circle cx="113" cy="48" r=".7" fill="#d0dde8"/>
<circle cx="188" cy="22" r="1" fill="#c0d2e3" class="sb"/>
<circle cx="257" cy="41" r=".8" fill="#b9cde2"/>
<circle cx="354" cy="13" r="1.3" fill="#d5e5f5" class="sa"/>
<circle cx="423" cy="34" r=".7" fill="#d4e1ec"/>
<circle cx="507" cy="50" r=".9" fill="#c1d2e5" class="sb"/>
<circle cx="589" cy="19" r=".8" fill="#c8d9ed"/>
<circle cx="638" cy="43" r="1" fill="#cddee9" class="sa"/>
<circle cx="70" cy="82" r=".6" fill="#b9cde2"/>
<circle cx="150" cy="66" r=".8" fill="#c1d2e5" class="sb"/>
<circle cx="232" cy="79" r=".7" fill="#b5c9de"/>
<circle cx="302" cy="56" r="1.1" fill="#ccd9e6" class="sa"/>
<circle cx="465" cy="70" r=".8" fill="#c8d6e2"/>
<circle cx="541" cy="63" r=".9" fill="#bccee2" class="sb"/>
<circle cx="618" cy="77" r=".7" fill="#c5d5e9"/>
<circle cx="26" cy="178" r=".7" fill="#c1d2e5"/>
<circle cx="70" cy="242" r=".9" fill="#b9cde2" class="sa"/>
<circle cx="42" cy="318" r=".6" fill="#c4d1e2"/>
<circle cx="81" cy="392" r=".8" fill="#c0cde2" class="sb"/>
<circle cx="28" cy="458" r=".7" fill="#b9c9de"/>
<circle cx="65" cy="522" r=".9" fill="#c5d2e5" class="sa"/>
<circle cx="108" cy="208" r=".7" fill="#bccde2"/>
<circle cx="94" cy="355" r=".6" fill="#c1cde5"/>
<circle cx="48" cy="414" r=".8" fill="#b9cde9" class="sb"/>
<circle cx="566" cy="176" r=".8" fill="#c9d6e5" class="sa"/>
<circle cx="612" cy="244" r=".7" fill="#c1d2e9"/>
<circle cx="638" cy="322" r=".9" fill="#c9d5e5" class="sb"/>
<circle cx="590" cy="394" r=".6" fill="#bdcde2"/>
<circle cx="618" cy="460" r=".8" fill="#c5cde2" class="sa"/>
<circle cx="574" cy="524" r=".7" fill="#b9cde5"/>
<circle cx="545" cy="210" r=".9" fill="#c1d2e5" class="sb"/>
<circle cx="625" cy="362" r=".7" fill="#c9d5e2"/>
<circle cx="650" cy="410" r=".8" fill="#bdcde9" class="sa"/>
<circle cx="38" cy="570" r=".7" fill="#c1d2e5"/>
<circle cx="118" cy="612" r=".9" fill="#c5d2e5" class="sb"/>
<circle cx="188" cy="638" r=".6" fill="#b9cde2"/>
<circle cx="278" cy="652" r=".8" fill="#c1cde5" class="sa"/>
<circle cx="382" cy="664" r=".7" fill="#c9d5e2"/>
<circle cx="458" cy="648" r=".9" fill="#bdcde5" class="sb"/>
<circle cx="538" cy="628" r=".6" fill="#c5d2e5"/>
<circle cx="618" cy="602" r=".8" fill="#c1cde2" class="sa"/>
<circle cx="655" cy="570" r=".7" fill="#b9cde2"/>
<circle cx="86" cy="670" r=".8" fill="#c5d2e2" class="sb"/>
<circle cx="248" cy="668" r=".9" fill="#c1cde5" class="sa"/>
<circle cx="430" cy="666" r=".7" fill="#c5d2e5"/>
<circle cx="510" cy="674" r=".8" fill="#bdcde2" class="sb"/>
<circle cx="598" cy="658" r=".6" fill="#c9d5e5"/>
<circle cx="642" cy="643" r=".9" fill="#c1d2e2" class="sa"/>
<use href="#bub"/>
<g clip-path="url(#cp1)">
<rect x="0" y="168" width="680" height="10" fill="#07090f"/>
<use href="#bub" transform="translate(25,0)"/>
</g>
<g clip-path="url(#cp2)" class="gsb">
<rect x="0" y="215" width="680" height="7" fill="#07090f"/>
<use href="#bub" transform="translate(-20,0)"/>
</g>
<g clip-path="url(#cp3)">
<rect x="0" y="262" width="680" height="15" fill="#07090f"/>
<use href="#bub" transform="translate(39,0)"/>
</g>
<g clip-path="url(#cp4)" class="gsa">
<rect x="0" y="328" width="680" height="9" fill="#07090f"/>
<use href="#bub" transform="translate(-25,0)"/>
</g>
<g clip-path="url(#cp5)">
<rect x="0" y="385" width="680" height="20" fill="#07090f"/>
<use href="#bub" transform="translate(32,0)"/>
</g>
<g clip-path="url(#cp6)" class="gsb">
<rect x="0" y="455" width="680" height="6" fill="#07090f"/>
<use href="#bub" transform="translate(-47,0)"/>
</g>
<g clip-path="url(#cp7)">
<rect x="0" y="495" width="680" height="12" fill="#07090f"/>
<use href="#bub" transform="translate(17,0)"/>
</g>
<g clip-path="url(#cp8)" class="gsb">
<rect x="0" y="128" width="680" height="7" fill="#07090f"/>
<use href="#bub" transform="translate(33,0)"/>
</g>
<circle cx="344" cy="340" r="182" fill="none" stroke="#ff2c1a" stroke-width="4" opacity=".14"/>
<circle cx="336" cy="340" r="182" fill="none" stroke="#1accd8" stroke-width="4" opacity=".14"/>
<rect x="518" y="337" width="9" height="5" fill="#ff2040" class="px1"/>
<rect x="494" cy="246" width="7" height="4" fill="#20ff88" class="px2"/>
<rect x="427" y="179" width="6" height="7" fill="#00d8ff"/>
<rect x="336" y="155" width="11" height="3" fill="#ffff22" class="px1"/>
<rect x="245" y="179" width="7" height="5" fill="#ff40c2" class="px2"/>
<rect x="178" y="246" width="5" height="8" fill="#40ffff" class="px1"/>
<rect x="154" y="337" width="5" height="6" fill="#ff8800" class="px2"/>
<rect x="178" y="428" width="6" height="4" fill="#22ff44" class="px1"/>
<rect x="245" y="495" width="8" height="5" fill="#ff2244" class="px2"/>
<rect x="336" y="519" width="7" height="4" fill="#00ffff" class="px1"/>
<rect x="427" y="495" width="5" height="6" fill="#ff44c2" class="px2"/>
<rect x="494" y="428" width="7" height="5" fill="#a2ff22" class="px1"/>
<rect x="447" y="208" width="4" height="4" fill="#ff8844" class="px2"/>
<rect x="183" y="394" width="5" height="3" fill="#00d8ff" class="px1"/>
<rect x="565" y="318" width="6" height="3" fill="#ff2282" class="px2"/>
<rect x="110" y="358" width="6" height="4" fill="#44ffff" class="px1"/>
<rect width="680" height="680" fill="url(#scl)"/>
</svg>
</div>
</div>
</section>
<section class="divider" data-background-gradient="radial-gradient(circle at 50% 50%, rgba(122,92,255,0.25), transparent 70%)">
<span class="part-num">PART TWO</span>
<h1>Unit testing in Python</h1>
<p style="font-family: var(--font-display); font-style: italic; color: var(--fg-muted); font-size: 1.1em; margin-top: 0.5em;">
How to implement testing for research code
</p>
</section>
<section>
<h2>Setting things up 🚀</h2>
<ul>
<li>
Necessary packages (just in case)<br>
<pre><code class="language-bash" data-trim>
sudo apt update
sudo apt install git python3-dev python3-setuptools python3-venv python3-wheel
</code></pre>
</li>
<li>
GitHub account
<ul>
<li>For running pipelines on the GitHub Actions cloud runners</li>
</ul>
</li>
<li>
Do you have a Python project that does not yet have unit tests?
<ul>
<li>
If yes, I recommend using it for the exercises.
If the project is not yet on Git, please create a repository for it.
<ul>
<li>Preferably on GitHub so that you can also practice running the tests in the cloud</li>
</ul>
</li>
<!--
<li>
If not, please go to
<a href="https://github.com/CFT-HY/unit-testing-ci-cd">https://github.com/CFT-HY/unit-testing-ci-cd</a>
and click <i>Fork</i>.
<ul>
<li>Then go to the web page of the forked repository, and clone it to your computer.</li>
</ul>
</li>
-->
</ul>
</li>
</ul>
</section>
<!--
<section>
<h2>Unit testing</h2>
<ul>
<li>= Testing the code, one unit at a time</li>
<li>= Test each module or even function of the code separately</li>
<li>If something breaks, you know where the calculations go wrong</li>
<li>Takes some time to set up, but saves time (and one's sanity) in the long run</li>
<li>Ideal: when you write a function, also write a test for it</li>
</ul>
</section>
-->
<section>
<h2>What is a unit test?</h2>
<blockquote class="big">
A small, fast, automated check that <em>one</em> piece of code does what you expect it to do.
</blockquote>
<div class="two-col" style="margin-top: 0.8em;">
<div>
<h4>Three properties worth protecting</h4>
<ul class="checklist">
<li><strong>Small</strong> — tests one logical behaviour</li>
<li><strong>Fast</strong> — runs in milliseconds, ideally</li>
<li><strong>Deterministic</strong> — passes or fails for a reason, not for external conditions</li>
</ul>
</div>
<div>
<h4>The AAA pattern</h4>
<ol>
<li><strong>Arrange</strong> — set up inputs / state</li>
<li><strong>Act</strong> — call the function under test</li>
<li><strong>Assert</strong> — check the result against expectation</li>
</ol>
<p class="callout">
<! If you can't separate these three cleanly, the test is probably doing too much and is worth splitting.>
A good unit test is like a AAA battery: small and contained, but powerful enough to do its job.
</p>
</div>
</div>
</section>
<section>
<h2>Unit testing in Python: <code>unittest</code></h2>
<pre><code class="language-python" data-trim>
import math
import unittest
class TestNumbers(unittest.TestCase):
def test_add(self):
self.assertEqual(1 + 2, 3)
def test_pi(self):
self.assertGreater(math.pi, 3.14)
def test_negative(self):
with self.assertRaises(ValueError):
math.sqrt(-1)
</code></pre>
<div class="two-col" style="margin-top: 0.4em;">
<div>
<p><code>TestCase</code> subclass → test methods prefixed <code>test_</code>.</p>
<p>Descriptive names help in understanding: <code>test_negative_v_wall</code> beats <code>test_case_3</code></p>
</div>
<div>
<p>You can it the old way:</p>
<pre><code class="language-bash" data-trim>
$ python -m unittest tests.my_test_file
</code></pre>
<p>…but we'll upgrade the runner shortly.</p>
</div>
</div>
</section>
<section>
<h2>The assertion toolkit</h2>
<table>
<tr><th>Method</th><th>Checks</th><th>Typical use</th></tr>
<tr><td><code>assertEqual(a, b)</code></td><td><code>a == b</code></td><td>Integers, strings, enums</td></tr>
<tr><td><code>assertTrue(x)</code></td><td>truthiness</td><td>Boolean flags, predicates</td></tr>
<tr><td><code>assertAlmostEqual(a, b, places=7)</code></td><td>scalar floats</td><td>One-off numerical results</td></tr>
<tr><td><code>assertRaises(ExcType)</code></td><td>exception is raised</td><td>Input validation</td></tr>
<tr><td><code>assertIn(x, seq)</code></td><td>membership</td><td>Classification outputs</td></tr>
<tr><td><code>assertIsInstance(x, T)</code></td><td>type</td><td>Return-type contracts</td></tr>
</table>
<p>For arrays, prefer NumPy's testing helpers:</p>
<pre><code class="language-python" data-trim>
import numpy as np
np.testing.assert_allclose(actual, desired, rtol=1e-7, atol=0)
np.testing.assert_array_equal(integer_mask, expected_mask)
</code></pre>
<p class="callout">
<strong>Rule of thumb:</strong> for numerical values, use <code>assert_allclose</code> with a relative tolerance.
Machine epsilon for FP64 is ~1e-16; a reasonable <code>rtol</code> for physics is often 1e-10 … 1e-6 depending on the algorithm.
Changing the code can change the numerical results slightly, even when there is nothing wrong.
</p>
</section>
<section>
<h2>Fixtures: <code>setUpClass</code>, <code>tearDownClass</code></h2>
<div class="two-col">
<div>
<pre><code class="language-python" data-trim style="font-size: 0.8em">
import unittest
import numpy as np
from pttools.bubble import Bubble
from pttools.models import ConstCSModel
class TestSoundShell(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""Runs once for the whole class — use for expensive objects"""
cls.model = ConstCSModel(css=1/np.sqrt(3), csb=1/np.sqrt(3)-0.01)
cls.bubble = Bubble(cls.model, v_wall=0.7, alpha_n=0.1)
cls.spectrum = Spectrum(cls.bubble, r_star=0.1)
def setUp(self):
"""Runs before every test method: fresh state per test"""
...
def test_kappa(self):
kappa = self.bubble.kappa
self.assertGreater(kappa, 0)
self.assertLess(kappa, 1)
def test_total(self):
val = self.spectrum.omgw0_total()
ref = np.trapezoid(y=self.spectrum.omgw0(), x=self.spectrum.f())
self.assertAlmostEqual(val, ref)
</code></pre>
</div>
<div>
<p class="callout">
<code>setUpClass</code> / <code>tearDownClass</code> run <em>once per class</em> — perfect for expensive objects like pre-solved fluid shells.
</p>
<p class="callout">
<code>setUp</code> / <code>tearDown</code> run <em>once per test function</em> — useful for setting a clean state for each test
</p>
</div>
</div>
</section>
<section>
<h2>Why reach for <code>pytest</code></h2>
<div class="two-col">
<div>
<h4>What you get</h4>
<ul>
<li><strong>Test discovery</strong> — anything named <code>test_*.py</code> containing <code>test_*</code> functions just runs.</li>
<li><strong>Full unittest compatibility</strong> — your <code>TestCase</code> classes work unchanged.</li>
<li><strong>Assertion rewriting</strong> — <code>assert x == y</code> gives you diff-style failure output, not a plain AssertionError.</li>
<li><strong>Plugin ecosystem</strong> — <code>pytest-cov</code>, <code>pytest-xdist</code>, <code>pytest-benchmark</code>, …</li>
<li><strong>Conditional tests</strong> — skipping tests on certain platforms, conditionally expected failures etc.</li>
</ul>
</div>
<div>
<h4>In our group</h4>
<p>Both <a href="https://github.com/CFT-HY/pttools"><code>pttools</code></a> and <a href="https://github.com/CFT-HY/PTPlot"><code>PTPlot</code></a> use <code>pytest</code> as the runner — even though many tests are written as <code>unittest.TestCase</code>. You get the best of both worlds.</p>
<p class="callout"><code>pyproject.toml</code> configures pytest with <code>pytest-cov</code>, <code>pytest-xdist</code>, and <code>pytest-html</code>. Steal the config.</p>
</div>
</div>
<aside class="notes">
You don't have to pick one or the other. Write tests with unittest.TestCase if you like classes,
write plain test_ functions if you prefer, run both with pytest.
</aside>
</section>
<section>
<h2>Configuration lives in <code>pyproject.toml</code>; shared helpers in <code>conftest.py</code></h2>
<div class="two-col">
<div>
<h4>pyproject.toml</h4>
<pre><code class="language-toml" data-trim>
[tool.pytest.ini_options]
addopts = [
"--cov=.",
"--cov-report=html",
"--cov-report=term",
"--cov-report=term-missing",
"--cov-report=xml",
"--dist=loadscope",
"--html=test-results/html/test-report.html",
"--numprocesses=auto"
]
norecursedirs = "test_data"
testpaths = [
"tests"
]
</code></pre>
</div>
<div>
<h4>conftest.py (shared fixtures)</h4>
<pre><code class="language-python" data-trim>
import logging
import pytest
from pttools.logging import setup_logging
logger = logging.getLogger(__name__)
def pytest_configure(config: pytest.Config):
setup_logging()
@pytest.fixture(scope="function", autouse=True)
def log_test_name_at_start(request):
"""
Before starting a test, log its name.
This makes it easier to retrieve the logs for a specific test.
"""
logger.info("=" * 20 + request.node.nodeid + "=" * 20)
</code></pre>
</div>
</div>
<p class="callout">This mirrors what <code>pttools/conftest.py</code> and <code>pttools/pyproject.toml</code> do — worth reading as a real-world example.</p>
<aside class="notes">
conftest.py is a magic filename — pytest picks up fixtures from it automatically for any test
in the same directory tree. No import needed.
</aside>
</section>
<section class="divider" data-background-gradient="radial-gradient(circle at 50% 50%, rgba(122,92,255,0.25), transparent 70%)">
<span class="part-num">PART THREE</span>
<h1>CI/CD?</h1>
<p style="font-family: var(--font-display); font-style: italic; color: var(--fg-muted); font-size: 1.1em; margin-top: 0.5em;">
Tests need to be run to be useful
</p>
</section>
<section>
<h2>What is CI/CD?</h2>
<div class="two-col">
<div>
CI = Continuous Integration
<ul>
<li>= Test your code automatically every time you upload changes</li>
<li><code>git push</code> $\to$ get an email if something is not working</li>
<li>Run unit tests, run larger-scale integration tests, measure test coverage, measure performance, build binaries</li>
<li>
Test compatibility on multiple platforms
<ul>
<li>Linux, Windows, macOS</li>
<li>x86, ARM</li>
</ul>
</li>
<li>
Build documentation
<ul>
<li>Sphinx, Doxygen etc., with examples</li>
<li>Create automatic graphs of program structure</li>
</ul>
</li>
</ul>
</div>
<div>
CD = Continuous Deployment
<ul>
<li>Make your changes automatically available for others, if they pass the CI tests first</li>
<li>Create and upload pip packages and Docker containers</li>
<li>
Upload releases to Zeonodo & Software Heritage Foundation
<li>Create a release on GitHub $\to$ automatic DOI code \& addition to your ORCiD profile</li>
</li>
</ul>
</div>
</div>
</section>
<section>
<h2>Why CI matters specifically for <em>research</em> code</h2>
<ul>
<li><strong>Your collaborator's laptop is not your laptop.</strong> CI runs on a clean Ubuntu box with pinned Python — if it works there, it's not "just my machine".</li>
<li><strong>Environment drift is silent.</strong> A NumPy upgrade six months from now can change a corner-case result. Nightly scheduled runs surface it before a paper goes out.</li>
<li><strong>Reproducibility.</strong> The YAML workflow <em>is</em> the reproducible install recipe — a self-documenting <code>pip install</code> sequence that really works.</li>
<li><strong>Docs build too.</strong> If Sphinx fails, the PR fails. No more broken docs on <code>main</code>.</li>
</ul>
<p class="callout">
For <code>pttools</code>, CI runs on Linux, macOS, <em>and</em> Windows on every push.
That's how we know that a <code>numba</code> update didn't silently break compatibility on Mark's ARM macOS laptop.
</p>
<aside class="notes">