# Introduction to Julia This document: https://hackmd.io/@jarno/julia-October-2022 Zoom room: 625 6817 1445 (no password) Zoom link: https://aalto.zoom.us/j/62568171445 Materials: https://github.com/AaltoRSE/julia-introduction#cloning-the-materials Code of conduct: https://www.aalto.fi/en/aalto-university/code-of-conduct-values-into-practice Scicomp Garage: https://scicomp.aalto.fi/help/garage/ More on Julia packaging: https://www.youtube.com/watch?v=oHsLmaHSHd8&ab_channel=Nordic-RSE JuMP optimization library: https://jump.dev/ Day 1: https://hackmd.io/@jarno/intro_to_julia_4_10_2021 # Day 2: ### Ice breaker question - What is your field? - Biostatistics and epidemiology - IT support for research - Quantum computing/ condensed matter - Computational condensed matter physics, x2 - Computational biology, x2 - Geoscience and as application expert at an HPC - Mathematics and systems analysis - Mathematics, control and robotics - Photonics - Machine learning and cryptography - What kind of Julia packages could be useful to you? - That's what I'm trying to work out this week :) - One that allows for integration with R (the most common language around here) - Mathematics, statistics - . - Packages for numerical methods (like numpy/scipy in Python)+1 - checkout the sciml organisation :) https://sciml.ai/ - numpy/scipy, plots and maps, netcdf/hdf5/grib and other special formats - Plots.jl and Makie.jl are a couple of popular packages for plotting - ... - ... - ... ## Ask questions here - Is this a question? - Yes, and this is an answer - You said yesterday that these recordings will probably be posted on Youtube. On what channel will they be posted? - Aalto scicomp channel: https://www.youtube.com/channel/UCNErdFO1_GzSkDx0bLKWXOA - How does a function defined in that form `Base.show` work again? - if you have a custom type `MyType` and you want to define a pretty printing for it, you can define a new method for `Base.show` with - ``` function Base.show(io::IO, x::MyType) # your code here print(io, resulting_string) end ``` for example ``` struct Point x::Float64 y::Float64 end Base.show(io::IO, p::Point) = print(io, "($(p.x), $(p.y))") julia> Point(1, 2) (1.0, 2.0) ``` - Example about `copy` ``` julia> A = [1 2;3 4] 2×2 Matrix{Int64}: 1 2 3 4 julia> B = [1 2;3 4] 2×2 Matrix{Int64}: 1 2 3 4 julia> A1 = A 2×2 Matrix{Int64}: 1 2 3 4 julia> A1[1, 1] = 0 0 julia> A 2×2 Matrix{Int64}: 0 2 3 4 julia> A1 2×2 Matrix{Int64}: 0 2 3 4 julia> B1 = copy(B) 2×2 Matrix{Int64}: 1 2 3 4 julia> B1[1, 1] = 0 0 julia> B 2×2 Matrix{Int64}: 1 2 3 4 julia> B1 2×2 Matrix{Int64}: 0 2 3 4 ``` - If we find time, could we discuss a bit when it is worth to produce code in Julia if you are already fluent in Python? Implementing low level functions and call in Python? :::danger BREAK UNTIL 10:05 EEST ::: - Is there some sort of replicate function to run that `update!(...)` cell several times without manually re-running it (or doing a for loop)? - I think you could write a macro for it, but I think writing something like ``` for _ in 1:N update!(...) end ``` should not be that lengthy to write, or did I misunderstand your question? - For example, in R one could write `replicate(5, print("Hi"))` to get `print("hi!")` (or whatever proper function) run 5 times, so instead of putting `update!(...)` inside a for loop I was wondering if there was a one-liner solution, a wrapper function to run it _n_ times. - Ah, I'm not sure if there's something similar in Julia - but I guess practically the main use of that function is to do a list comprehension? In julia you can do `[fun(x, y) for _ in 1:N]` - Can you specify that you want to (only) import the hello function from Example? Like you'd do `import hello from Example` in python (edit I see Jarno is talking about that now) - `import Example: hello` or `using Example: hello` - more info about `using` vs `import` here: https://docs.julialang.org/en/v1/manual/modules/ - For more about interoperability: check out [BinaryBuilder.jl](https://github.com/JuliaPackaging/BinaryBuilder.jl) (kind of more advanced, it's a very powerful tool but sometimes can be a little tricky to get the library compiled.) - Why there's no Jupyter hub for Julia on triton? (there's just the command line) - How about the named arguments in Julia? Can they come in different order? - yes, when you define a function, named arguments are separated from a semicolon, e.g. ``` function myfunction(x, y; kw1=1, kw2=3, kw3="hi") # blah blah blah end ``` and after that you can give the named arguments in any order, e.g. ``` myfunction(1, 2; kw2=5, kw1=3) ``` you have probably already noticed from Jarno's example that when you call the function you don't need the `;`, so you could do ``` myfunction(1, 2, kw2=5, kw1=3) ``` however some people consider good practice to have the `;` also when you call the function :) - thanks! I believe that the multiple dispatcher only looks at the first arguments? (before ';') - yes, different methods are distinguished based on so-called "positional arguments", which are the arguments before `;`. :::danger BREAK UNTILL 11:15 ::: - is `using Plots` activating PyPlot or is it using Julia's Plot package? And why are we relying on Python packages for plotting? - `using Plots` imports the package and the function names to plot. `pyplot`, `gr`, `plotlyjs` are all backends for plotting (that is, what happens under the hood when you call teh functions). If you just do `using Plots` without specifying the backend, it will by default use `gr`. If you want to use PyPlot as backend, you have to explicitly activate it by doing `pyplot()`. - I get an error trying to use to_colors: `MethodError: no method matching getproperty(::Char, ::String)` - can you paste the whole line where you are calling `to_colors`? - it happens already in the predefined example call: `plot(to_colors.(plants),legend=false, border=:none)` - it sounds like your definition of `to_colors` or `plants` is slightly different, but that's quite difficult to debug without looking at the whole code. Would you like me to open a break out room to investigate more? - no it's ok I'll just follow the rest :) - The **best** resource to setup a new package is [PkgTemplates.jl](https://github.com/invenia/PkgTemplates.jl), it setup the structure already for docs, tests, it allows you to automatically setup CI workflows etc. etc. - There's also a full workshop about package development in Julia from a few weeks back: https://www.youtube.com/watch?v=oHsLmaHSHd8 - Does Julia have a sandbox environment for publishing packages (like [PyPI](https://test.pypi.org/) does)? - Julia has the [general registry](https://github.com/JuliaRegistries/General) where you can register the packages. I don't think there's a sandbox registry HOWEVER if you put the packages in your github, people can add that from git with - `Pkg.add("url/to/package.git")`, for example `Pkg.add("https://github.com/myname/MyPackage.jl.git")` - This kind of nullifies the need for a sandbox registry, I think - May we get a quick example about properly formatting package function documentation? - You add a docstring with the triple quotation mark """ """ on top of the function, e.g. ``` """" myfun(x, y, z) This function does something with some inputs. """ function myfun(x, y, z) # blah blah blah end ``` then you could just do `?myfun` to get that docstring. There's not a general "proper" formatting, different people have different opinions about how to format a docstring. I wrote some guidelines for a package I'm maintaining [here](https://juliaintervals.github.io/IntervalLinearAlgebra.jl/stable/CONTRIBUTING/#Docstrings) but of course it's very opinionated whether it's good or not :) - Anyways, once you have written some documentation somehown, Documenter.jl is a fantastic package to build the documentatino (automatically collect docstrings, write tutorials with julia code examples, build the docs page etc. etc.) :::danger BREAK UNTIL 12:40 ::: After the package exercise, we have optional sections. Vote here for your favourite: - Perfomance: oo - Testing: o - Multiprocessing: ooooo - Macros and metaprogramming: ooooooo - Strings and IO: I vote for metaprogramming because I guess it's something special about Julia (?), the other things are interesting as well, but I guess probably easier to google by oneself - yes metaprogramming is one of the things that makes Julia special. Although to be fair Julia is **not** the only language in the universe supporting metaprogramming, e.g. Lisp also supports it and (I think) Rust to som extend. - Also C++, but not as well I just wanna vote on everything :laughing: - I know the feeling.... Julia is so fantastic and so cool that choosing only one thing to talk about is hard :( - [Revise.jl](https://github.com/timholy/Revise.jl): always use this when you are seriously developing packages or you will be in troubles!!! :::danger BREAK UNTILL 13:25 ::: - when I call the “to_colors.(plants)” I get UndefVarError: RGB not defined — was there something to “add” or “use” first? should I change the “Epidemic.jl” ? - You may be missing "Using Plots" in Epidemic.jl. That should define "RGB" - ok, that fixed it :) ## Feedback for day 2: 1. What did you like about today? - Good pacing, regular breaks +2 - Luca's accent +2 - quick answers to questions - Really good to have two teachers to keep track of everything! 2. What did you not like about today? - I would rather have exchanged packages for any of the other topics :P - today felt a bit chaotic. Maybe I was tired - I would also like to have more about performance and multiprocessing (why is Julia fast), as it is mostly why I come to learn - I actually would also enjoy to hear more about concepts, advatages and good use-cases of Julia (there can be some minutes without coding) 3. section 4, Epidemic simulation wrap-up, was - too fast: - too slow: - just right: oooo - other comments: - ... - ... 4. section 5, Packages and plotting, was - too fast: - too slow: - just right: ooo 5. section 6, Creating packages, was - too fast: o - too slow: - just right: oooo - other comments: - ... - ... 6. section 7, Metaprogramming, was - too fast: o - too slow: - just right: ooo - other comments: - ... - ...