Skip to content
Snippets Groups Projects
Commit 695a523f authored by Miroslav Kratochvil's avatar Miroslav Kratochvil :bicyclist:
Browse files

Merge branch 'mk-julia3' into 'develop'

julia 3

See merge request R3/school/courses!163
parents 9c5f7e32 8339dbb6
No related branches found
No related tags found
No related merge requests found
# <i class="twa twa-pencil"></i> Exercise (continued): median
Let's try to shrink the approximate median function code by 90%.
# <i class="twa twa-pencil"></i> Exercise: let's play the maze game (again)
- Make a function that annotates the whole maze based on how many (axis-aligned) steps a person (maze inhabitant) needs to take from some point
- let's use the slow <i class="twa twa-water-wave"></i>wave<i class="twa twa-water-wave"></i> algorithm
- Use `UnicodePlots` to plot interesting stuff
- shortest path length distribution
- shortest-path-length heatmap of the maze
- "most blocking walls" (how much time to reach the father side of the wall could be gained by removing the wall?)
# Exercise: Tricky questions about the maze
What is the slowest part of the "wave" solution?
How can we make it faster?
<div class=leader>
<i class="twa twa-mechanical-arm"></i>
<i class="twa twa-battery"></i>
<i class="twa twa-nut-and-bolt"></i>
<i class="twa twa-mechanical-leg"></i>
<br>
Compiler &amp; language internals (continued)
</div>
# What is this "compiled code"?
You can inspect various stages of code representation (and reveal bugs):
- `code_lowered(+)`
- `code_typed(+)`
- `code_typed(+, (Int, Float64))`
- `code_llvm`
- `code_native`
What happens if we use an abstract type?
<center style="font-size:200%; line-height:120%; margin-bottom: 1em">
Performance rule #1:<br><br>
<strong>Less instructions is typically better,</strong><br>
<strong>less jumps is typically much better.</strong>
</center>
<center style="font-size:200%; line-height:120%; margin-bottom: 1em">
Performance rule #2:<br><br>
<strong>Memory allocations comprise of many instructions<br> and plenty of jumps.</strong>
</center>
# What are all these things starting with `@`?
Macros allow you to generate code before compiler runs it:
- `@info` can know what variable names to display
- `@benchmark` can run the code several times
- `@test` can safely wrap the failing code and print what failed
## Important types for macros:
- symbols: `:abc`, `:+`, ...
- expressions: `:(1+2)`, `quote ... end`
Macros are functions that get unevaluated arguments (as expressions) and return unevaluated results (as expressions).
```julia
macro clear_data(v)
quote
$(Symbol(v, :_orig)) = load_original_data()
$(Symbol(v, :_enriched)) = nothing
$(Symbol(v, :_final)) = nothing
end
end
@clear_data patients # reloads patients_orig, clears patients_enriched, patients_final
```
# Macro use-cases
Use macros to improve your code whenever Julia syntax is too unwieldy.
```julia
model = @reaction_network MyModel begin
c1, s + i --> 2i
c2, i --> r
end
```
(Taken from `Catalyst.jl`)
<div class=leader>
<i class="twa twa-volcano"></i>
<i class="twa twa-mount-fuji"></i>
<i class="twa twa-snow-capped-mountain"></i>
<i class="twa twa-mountain"></i>
<i class="twa twa-sunrise-over-mountains"></i>
<br>
Julia for scientific computing
</div>
# What you mostly do in scientific/numeric computing?
- `LinearAlgebra.jl`, `SparseArrays.jl`
- easily utilizes `CUDA.jl`, `AMDGPU.jl`, `Metal.jl`
- statistics
- simulations (typically custom made)
- neural networks, ODEs, constrained problems (next time)
- plotting!
# Statistics in Julia
- `StatsBase.jl` for all "base" runctions (correlations, medians, quantiles, ranks, ...)
- `Distances.jl` for measuring distances/similarities
- `Distributions.jl` for generating numbers
- `HypothesisTests.jl` for getting p-values
- `Clustering.jl`
See more at [juliastats.org](https://juliastats.org)
# Mini-exercise
- Plot the correlations between the columns of our maze in a heatmap
- Order the heatmap by similarity (from hclust) to visualize clusters
# Simulation exercise: Let's layout some graphs!
<img src="img/graph.jpeg" width="70%">
# Simulation exercise: Let's layout some graphs!
- Read a set of edges (integer pairs)
- Simulate that the vertices are pushed around by 2 forces:
- quadratic attractive force ("gravity")
- inverse quadratic attractive force
- no momentum or any other kind of physics!
- Draw what happens after a few rounds of simulation
# Exercise: Let's align some <i class="twa twa-dna"></i>DNA<i class="twa twa-dna"></i>!
```
- S a t u r d a y
- 0 1 2 3 4 5 6 7 8
S 1 0 1 2 3 4 5 6 7
u 2 1 1 2 2 3 4 5 6
n 3 2 2 2 3 3 4 5 6
d 4 3 3 3 3 4 3 4 5
a 5 4 3 4 4 4 4 3 4
y 6 5 4 4 5 5 5 4 3
* not actual DNA
```
# Exercise: Let's align some <i class="twa twa-dna"></i>DNA<i class="twa twa-dna"></i>!
Let's do bioinformatics; simple short 2-sequence alignment can be done with
Levenshtein algorithm.
- Print out the alignment nicely (is it unique?)
```
++ *
Saturday
S unday
-- *
```
- Make the "costs" easily parametrizable
- some DNA mutations are more probable than others
- what's a good way to parametrize the functionality?
2023/2023-03-14_ProgrammingWithJulia-3/slides/img/favicon.ico

39.9 KiB

2023/2023-03-14_ProgrammingWithJulia-3/slides/img/graph.jpeg

23.1 KiB

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 153.14 98.64"><defs><style>.cls-1{fill:#1a1a1a}.cls-2{fill:#4d64ae}.cls-3{fill:#ca3c32}.cls-4{fill:#9259a3}.cls-5{fill:#399746}</style></defs><title>Asset 2</title><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><g id="layer1"><g id="g3855"><g id="g945"><g id="g984"><g id="g920"><path id="path3804" d="M93.14,80.94h-13V21.13l13-3.58Z" class="cls-1"/><g id="g898"><g id="g893"><path id="path19" d="M22.17,36.33a8.9,8.9,0,1,1,8.9-8.9A8.91,8.91,0,0,1,22.17,36.33Z" class="cls-2"/></g><path id="path3819" d="M29.14,80.83A26.48,26.48,0,0,1,27.83,90a12.12,12.12,0,0,1-3.62,5.4A12.33,12.33,0,0,1,18.57,98a36.64,36.64,0,0,1-7.32.67,22.47,22.47,0,0,1-4.81-.47A13,13,0,0,1,2.9,96.93,6,6,0,0,1,.76,95.07,3.62,3.62,0,0,1,0,92.88,4.26,4.26,0,0,1,1.59,89.5a6.47,6.47,0,0,1,4.33-1.35,5,5,0,0,1,1.87.32,6,6,0,0,1,1.43.79,12,12,0,0,1,1.16,1.07c.31.4.59.77.83,1.12A7.58,7.58,0,0,0,12.72,93a2.3,2.3,0,0,0,1.15.4,1.85,1.85,0,0,0,1-.28,2,2,0,0,0,.71-1,7.18,7.18,0,0,0,.4-1.91,23.12,23.12,0,0,0,.16-3.06V40.48l13-3.58Z" class="cls-1"/></g><path id="path3802" d="M48.14,37.94V68a6.14,6.14,0,0,0,.47,2.39A6.45,6.45,0,0,0,50,72.24a7,7,0,0,0,2,1.27,6.12,6.12,0,0,0,2.4.48,4.2,4.2,0,0,0,1.61-.4,8.42,8.42,0,0,0,1.8-1.12,13.27,13.27,0,0,0,1.81-1.66,12.92,12.92,0,0,0,1.61-2.11V37.94h13v43h-13v-4a22.47,22.47,0,0,1-5.43,3.53,13.62,13.62,0,0,1-5.59,1.28,16.52,16.52,0,0,1-5.9-1,15.59,15.59,0,0,1-4.76-2.89,13.56,13.56,0,0,1-3.17-4.28,12.41,12.41,0,0,1-1.15-5.29V37.94Z" class="cls-1"/><g id="g905"><g id="g890"><path id="path13" d="M105.79,36.33a8.9,8.9,0,1,1,8.91-8.9A8.91,8.91,0,0,1,105.79,36.33Z" class="cls-3"/><path id="path25" d="M127.18,36.33a8.9,8.9,0,1,1,8.91-8.9A8.91,8.91,0,0,1,127.18,36.33Z" class="cls-4"/><path id="path31" d="M116.49,17.8a8.9,8.9,0,1,1,8.9-8.9,8.89,8.89,0,0,1-8.9,8.9Z" class="cls-5"/></g><path id="path3823" d="M100.14,40.6l13-3.58V80.94h-13Z" class="cls-1"/></g><path id="path3808" d="M140.14,58.77a37.64,37.64,0,0,0-3.77,1.87,21.89,21.89,0,0,0-3.46,2.3,12.77,12.77,0,0,0-2.55,2.67,5.12,5.12,0,0,0-1,2.94,8.53,8.53,0,0,0,.32,2.34,7,7,0,0,0,.87,1.91,5.15,5.15,0,0,0,1.23,1.27,2.67,2.67,0,0,0,1.51.48,6.3,6.3,0,0,0,3.18-1,41.31,41.31,0,0,0,3.62-2.47Zm13,22.17h-13V77.52c-.71.61-1.42,1.17-2.11,1.67a14.2,14.2,0,0,1-2.3,1.35,13.56,13.56,0,0,1-2.82.88,19.75,19.75,0,0,1-3.78.31,16,16,0,0,1-5.33-.83,12.23,12.23,0,0,1-4-2.31,10.23,10.23,0,0,1-2.51-3.53,11,11,0,0,1-.87-4.37,10.27,10.27,0,0,1,.91-4.42,13.11,13.11,0,0,1,2.55-3.57,19.36,19.36,0,0,1,3.77-2.86,40.26,40.26,0,0,1,4.65-2.31c1.67-.69,3.4-1.32,5.17-1.91l5.25-1.71,1.43-.31V49.34a11.91,11.91,0,0,0-.44-3.45,5.82,5.82,0,0,0-1.15-2.31,4,4,0,0,0-1.79-1.31,6.6,6.6,0,0,0-2.34-.4,7.38,7.38,0,0,0-2.59.4,4.37,4.37,0,0,0-1.67,1.11,3.94,3.94,0,0,0-.91,1.59,6.52,6.52,0,0,0-.28,2,9.51,9.51,0,0,1-.28,2.35,4.85,4.85,0,0,1-.91,2A4.47,4.47,0,0,1,126,52.6a6.84,6.84,0,0,1-2.9.52,7.51,7.51,0,0,1-2.51-.4,6.16,6.16,0,0,1-1.91-1.15,6,6,0,0,1-1.27-1.75,5.59,5.59,0,0,1-.44-2.18,6.42,6.42,0,0,1,1.51-4.1,13.16,13.16,0,0,1,4.06-3.3,23.45,23.45,0,0,1,5.92-2.14,31.07,31.07,0,0,1,7.12-.8,32.21,32.21,0,0,1,7.87.84,16.37,16.37,0,0,1,5.49,2.34,9.55,9.55,0,0,1,3.18,3.66,10.91,10.91,0,0,1,1,4.81Z" class="cls-1"/></g></g></g></g></g></g></g></svg>
\ No newline at end of file
# Programming with Julia
## March 2023
<div style="top: 6em; left: 0%; position: absolute;">
<img src="theme/img/lcsb_bg.png">
</div>
<div style="top: 1em; left: 60%; position: absolute;">
<img src="slides/img/julia.svg" height="200px">
<h1 style="margin-top:3ex; margin-bottom:3ex;">3: Numeric&scientific computing</h1>
<h4>
Miroslav Kratochvíl<br>
Laurent Heirendt<br>
LCSB, DSSE<br>
</h4>
</div>
<link rel="stylesheet" href="https://lcsb-biocore.github.io/icons-mirror/twemoji-amazing.css">
<style>
code {border: 2pt dotted #f80; padding: .4ex; border-radius: .7ex; color:#444; }
.reveal pre code {border: 0; font-size: 18pt; line-height:27pt;}
em {color: #e02;}
li {margin-bottom: 1ex;}
div.leader {font-size:400%; line-height:120%; font-weight:bold; margin: 1em;}
section {padding-bottom: 10em;}
</style>
[
{ "filename": "index.md" },
{ "filename": "2a-details.md" },
{ "filename": "1e-exercises.md" },
{ "filename": "3a-num-exercises.md" }
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment