# Julia for Scientists: summary slides

**URL:** <https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397>\
**Category:** Teaching & Outreach\
**Tags:** plotting, outreach, teaching, ttfx, advent-of-code\
**Created:** [December 8, 2022, 1:15am UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397 "2022-12-08T01:15:01Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Post date:** [December 12, 2022, 6:47pm UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/21 "2022-12-12T18:47:04Z")

</div>

> [@t-bltg](#):
>
> I miss the `numpy` context manager [`np.printoptions`](https://numpy.org/doc/stable/reference/generated/numpy.printoptions.html), really handy.

I wrote some slightly monstrous code a while back (listed below), to have such a print-precision ‘context manager’.

Example usage:

```julia
julia> ℯ^99
9.889030319346946e+42

julia> @with_print_precision 3 begin
           display(ℯ^99)
       end
9.89E+42

julia> ℯ^99
9.889030319346946e+42

```

You can also set the format globally:

```julia
julia> set_print_precision("3f")

julia> ℯ^-9
0.000

```

* * *

The code behind the above:

```julia
using Printf

const default_float_fmt = "%.16g"
# This is approximately (but not entirely)
# what the default `show(::Float64)` does.

const float_fmt = Ref(default_float_fmt)

function set_float_print_fmt(fmt_str)
    float_fmt[] = fmt_str
    fmt = Printf.Format(fmt_str)
    Main.eval(:( 
        Base.show(io::IO, x::Float64) = Printf.format(io, $fmt, x) 
    ))
    return nothing
end
# We don't specify `::MIME"text/plain"` in `show`, so 
# that we also get compact floats in composite types
# (like when displaying a NamedTuple).
# Disadvantage is that we cannot use `show(x)`
# to see full float repr anymore.

set_print_precision(digits::Integer = 3) = set_float_print_fmt("%.$(digits)G")
set_print_precision(digits_and_type) = set_float_print_fmt("%.$(digits_and_type)")

# Something like `with_print_precision(3) do … end` wouldn't work:
# it can't be a function, must be a macro.
macro with_print_precision(p, expr)
    oldfmt = float_fmt[]
    return quote
        set_print_precision($p)
        $(esc(expr))
        set_float_print_fmt($oldfmt)
    end
end

```

It’s missing documentation.  
And I’m sure Julia experts could come up with sth better.  
`eval` ing type-pirating `show` methods in `Main` is not ideal 😛

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [December 13, 2022, 6:29am UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/22 "2022-12-13T06:29:42Z")

</div>

What did you use to generate the slides?

---

<div class="post-metadata">

**Author:** ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Post date:** [December 13, 2022, 9:12am UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/23 "2022-12-13T09:12:18Z")

</div>

Good old PowerPoint.  
The title slide font is [Alegreya ht :: Huerta Tipográfica](https://www.huertatipografica.com/en/fonts/alegreya-ht-pro)

Code is copy-pasted from VS Code – which amazingly keeps the syntax highlighting.

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [December 13, 2022, 10:43am UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/24 "2022-12-13T10:43:59Z")

</div>

I see your point.  
The easiness of the layout in PowerPoint is unbeatable. This is where I struggle with Quarto and Reveal.JS.

Though [Slides](https://slides.com/) gives you the PowerPoint experience with Reveal.JS.

---

<div class="post-metadata">

**Author:** ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Post date:** [December 13, 2022, 11:02am UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/25 "2022-12-13T11:02:00Z")

</div>

Yeah. Though the declarativity, semanticity, and ease-of-use (when just adding text) of Beamer and browser-based slides is very attractive too.

I dream of a combination of both  
Similar to

> [@tfiers](#):
>
> Edit your plots by point and click. Plots are graphic – it’s silly to tweak them with code.
> 
> - [Pylustrator](https://pylustrator.readthedocs.io) (for Matplotlib) is a great experiment that combines GUI-editing of plots with code/reproducibility (though it’s too buggy for practical use atm for me).

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [December 13, 2022, 11:12am UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/26 "2022-12-13T11:12:33Z")

</div>

Indeed.  
I think if Quarto had few grids work with and better way to control the dimensions of elements they will be half way there.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [December 13, 2022, 5:31pm UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/27 "2022-12-13T17:31:54Z")

</div>

I thought this was kind of cool, using the Makie layout api to make slides [GitHub - fatteneder/MakieSlides.jl](https://github.com/fatteneder/MakieSlides.jl)

---

<div class="post-metadata">

**Author:** ![kjohnsen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kjohnsen/32/27419_2.png) [@kjohnsen](https://discourse.julialang.org/u/kjohnsen)\
**Post date:** [December 23, 2022, 7:41pm UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/28 "2022-12-23T19:41:15Z")

</div>

What do you mean? Quarto+reveal has [grids](https://quarto.org/docs/authoring/figures.html#multiple-rows). I’ve been using it a bit recently and I’m liking it. Of course some things are harder than in normal PowerPoint but overall I think it’s saving me time. With a little extra web-dev CSS knowledge, it’s easier to make custom formatting consistent too (e.g., make a CSS class and stick it on divs/spans)

---

<div class="post-metadata">

**Author:** ![kjohnsen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kjohnsen/32/27419_2.png) [@kjohnsen](https://discourse.julialang.org/u/kjohnsen)\
**Post date:** [December 23, 2022, 7:48pm UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/29 "2022-12-23T19:48:28Z")

</div>

@tfiers You say you switched to Julia to use units in neuron simulations…but there’s already Brian in Python. Is there a reason you don’t use Brian? I’ve toyed with the idea of using Julia if I want to do something big that would be slow with Brian, but then I’d have to implement a bunch of things from scratch. Any thoughts?

---

<div class="post-metadata">

**Author:** ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Post date:** [December 23, 2022, 10:54pm UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/30 "2022-12-23T22:54:50Z")

</div>

I love Brian and the people that made it!  
It’s high quality software and extremely well documented.  
I don’t personally use it for my own work because:

- I wanted to develop a spiking neural network simulator myself, to understand how they work (queue Feynman’s “what I cannot create, I do not understand”). It’s also excellent procrastination bait while doing a PhD
- Brian’s DSL (for specifying e.g. differential equations) is input via strings, i.e. is not syntax highlighted. The equations and functions you specify can also not be easily tested and re-used on their own. Not a big issue, but I still found it important. With fast native Julia functions (and optionally, macros) you can specify your model in ‘real’ code. Compare Hodgkin-Huxley [in Brian](https://brian2.readthedocs.io/en/stable/examples/COBAHH.html) and [this Julia HH specificiation](https://github.com/tfiers/Firework.jl/blob/main/test/HH.jl)

Coincidentally, that last linked file is part of a spiking neural network lib I’m developing. Absolutely not ready for use yet, but feel free to check it out or get in touch if interested (I wanna know people’s use cases and struggles). [GitHub - tfiers/Firework.jl: Helping you build spiking neural network simulations in Julia](https://github.com/tfiers/Firework.jl/)

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [December 23, 2022, 10:59pm UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/31 "2022-12-23T22:59:47Z")

</div>

> [@tfiers](#):
>
> Compare Hodgkin-Huxley [in Brian](https://brian2.readthedocs.io/en/stable/examples/COBAHH.html) and [this Julia HH specificiation](https://github.com/tfiers/Firework.jl/blob/main/test/HH.jl)

I’m away from computer and can’t check it, but I have reasons to think these lines [https://github.com/tfiers/Firework.jl/blob/512da62d8b45d80fbce1dea00c1d495c3bebd895/test/HH.jl#L37-L39](https://github.com/tfiers/Firework.jl/blob/512da62d8b45d80fbce1dea00c1d495c3bebd895/test/HH.jl#L37-L39) may not be doing what you believe they do.

---

<div class="post-metadata">

**Author:** ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Post date:** [December 23, 2022, 11:01pm UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/32 "2022-12-23T23:01:04Z")

</div>

Ah yes very true.  
That file is not actually live code yet (it’s more of a sketch of what an end-user API could look like)

This sibling file does run: [Firework.jl/Nto1sim.jl at main · tfiers/Firework.jl · GitHub](https://github.com/tfiers/Firework.jl/blob/main/test/Nto1sim.jl)

---

<div class="post-metadata">

**Author:** ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Post date:** [December 23, 2022, 11:19pm UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/33 "2022-12-23T23:19:56Z")

</div>

I tried btw to use DifferentialEquations.jl first, and also the neuron simulator Conductor.jl that is based on it (with ModellingToolkit.jl). But it seems those libraries are not designed with very many discrete events (spikes) in mind.  
My implementation with Callbacks was slow, even after reading and watching a bunch of SciML tutorials on performance. (Plus, there’s the long-ish package load times, and the frequent long re-pre-compiles of the big SciML dependency tree – neither of which you have with a small bespoke package).  
A dumb handwritten Euler integration loop (similar to what Brian does) gave much faster results that where good enough

---

<div class="post-metadata">

**Author:** ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)\
**Post date:** [December 24, 2022, 6:26am UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/34 "2022-12-24T06:26:21Z")

</div>

What I’m after is more like being able to put text, figure, image, callout, or any other block anywhere at any size. If you used [Slides](https://slides.com/), you may see how it can be achieved there in Reveal.js.  
It is not doable programmatically, so I think 99% of the effect would be using grids (Like the guides in PP, etc…). There is the ability to use RevealJS guides, but the problem is the size. You can’t chose arbitrary size for blocks.

I hope I made what I’m after more clear.

---

<div class="post-metadata">

**Author:** ![kjohnsen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kjohnsen/32/27419_2.png) [@kjohnsen](https://discourse.julialang.org/u/kjohnsen)\
**Post date:** [December 24, 2022, 6:13pm UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/35 "2022-12-24T18:13:54Z")

</div>

Interesting…I was going to ask about this. I was excited about ModelingToolkit/Conductor.jl, so that’s disappointing. I can relate with the PhD procrastination…if I were ruthless in my time management I probably wouldn’t be dabbling with Julia at all 😅

If my Brian work gets cumbersome enough, I may reach out about Firework.jl. Out of curiosity, how much faster has it usually been than Brian?

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [December 24, 2022, 7:19pm UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/36 "2022-12-24T19:19:37Z")

</div>

Spiking neuron models are stiff, right? You’d probably benefit from using an implicit integration method. And 2nd- and higher-order integrators are a pretty marginal increase of complexity.

I have ultra-simple 2nd, 3rd, and 4th-order implicit and explicit Runge-Kutta integrators I wrote for teaching if you want them.

---

<div class="post-metadata">

**Author:** ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Post date:** [December 25, 2022, 9:39pm UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/37 "2022-12-25T21:39:55Z")

</div>

Hi John, thank you for your input. That sounds interesting!

I am no differential equations expert. But I think only Hodgkin-Huxley-type models are really stiff? (Namely, during a spike; they are quite linear when below the spiking threshold).

Simpler models ‘fake’ the spike. Two common 2D neuron models are [AdEx](http://www.scholarpedia.org/article/Adaptive_exponential_integrate-and-fire_model) and the [Izhikevich](https://www.izhikevich.org/publications/spikes.htm) neuron (implemented above). They both run away to ∞ above their spiking threshold, modelling the spike upstroke. But, an artificial reset is introduced at a certain point: the voltage is reset, discontinously, to well below the threshold again.

The simplest and most common neuron model (the leaky-integrate-and-fire or LIF neuron) does not even model the upstroke, and is completely linear.

* * *

@kjohnsen, I haven’t done a performance comparison yet. Would be interesting to see!  
Also, if you are modelling small neuron networks, definitely try Conductor.jl!

---

<div class="post-metadata">

**Author:** ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)\
**Post date:** [December 25, 2022, 11:21pm UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/38 "2022-12-25T23:21:57Z")

</div>

Thanks for the info and links. I’m interested and will try my hand at simulating these, if only to expand my range of applications in teaching!

FWIW I often find myself deciding between roll-my-own implementations of algorithms because oddities in the problem (like discontinuities in the ODE) challenge library algorithms. Or because the need to share algorithm internals back to the calling function, or learning a general framework and conventions of a library is harder than implementing a small subset myself.

But I do far less rolling-my-own since switching from C++ to Julia, due to Julia’s vast improvement in interoperability of libraries.

(getting a bit far from OP intent, perhaps should split off 🙂 )

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 26, 2022, 12:01am UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/39 "2022-12-26T00:01:18Z")

</div>

> [@tfiers](#):
>
> I am no differential equations expert. But I think only Hodgkin-Huxley-type models are really stiff? (Namely, during a spike; they are quite linear when below the spiking threshold).
> 
> Simpler models ‘fake’ the spike. Two common 2D neuron models are [AdEx](http://www.scholarpedia.org/article/Adaptive_exponential_integrate-and-fire_model) and the [Izhikevich](https://www.izhikevich.org/publications/spikes.htm) neuron (implemented above). They both run away to ∞ above their spiking threshold, modelling the spike upstroke. But, an artificial reset is introduced at a certain point: the voltage is reset, discontinously, to well below the spiking threshold again.
> 
> The simplest (and most common) leaky-integrate-and-fire (LIF) neuron does not even model the upstroke, and is completely linear.

That right there is the crux of it. When you say “A dumb handwritten Euler integration loop (similar to what Brian does) gave much faster results that where good enough”, I’m sure that’s correct. That makes no contradiction of course to DifferentialEquations.jl being efficient, the issue of course is that any definition of efficiency has some implicit definition of accuracy. If ones definition of accuracy is loose, then a simple Euler implementation will win every single time because that will take the least number of calculations.

When I say you shouldn’t use Euler’s method ([Why you shouldn't use Euler's method to solve ODEs - Nextjournal](https://nextjournal.com/ChrisRackauckas/why-you-shouldnt-use-eulers-method-to-solve-odes#:~:text=The%20Euler%20method%2C%20being%20a,error%20down%20to%20satisfying%20levels.)), it’s because it has a very difficult time hitting even two digits of accuracy on simple ODEs:

![image](https://global.discourse-cdn.com/julialang/original/3X/6/a/6a03f5e6eae8d5c63454d3f7b33a7a0a09d5f0df.png)

But in a lot of cases for spiking neural networks, 2 digits of accuracy isn’t even required. “It looks vaguely reasonable when I plot it”, the eyeball test, is many times the test for accuracy, and if that’s the case then yeah you cannot do better than Euler.

So it’s more of a modeling choice.

> [@tfiers](#):
>
> Simpler models ‘fake’ the spike. Two common 2D neuron models are [AdEx](http://www.scholarpedia.org/article/Adaptive_exponential_integrate-and-fire_model) and the [Izhikevich](https://www.izhikevich.org/publications/spikes.htm) neuron (implemented above). They both run away to ∞ above their spiking threshold, modelling the spike upstroke. But, an artificial reset is introduced at a certain point: the voltage is reset, discontinously, to well below the spiking threshold again.
> 
> The simplest (and most common) leaky-integrate-and-fire (LIF) neuron does not even model the upstroke, and is completely linear.

These now are example tutorials in the docs:

> **[Spiking Neural Systems · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/dev/examples/spiking_neural_systems/)**
>
> Documentation for DifferentialEquations.jl.

And that said…

> [@John\_Gibson](#):
>
> Spiking neuron models are stiff, right? You’d probably benefit from using an implicit integration method. And 2nd- and higher-order integrators are a pretty marginal increase of complexity.

What’s going on with ModelingToolkit.jl/Conductor.jl is that indeed there is a lot to offer once you get to this part of it. The nonlinear tearing can improve the solve in some nice ways. We’re creating new primitive problems, the `ImplicitDiscreteProblem`, for `f(u_{n+1},u_n,p,t_n) = 0` which is then a good primitive to build a lot of the simple methods in a way that can use all of the implicit integrator tricks. That and Rosenbrock23 are a nice combo, so there will be more to say here soon.

> [@John\_Gibson](#):
>
> I have ultra-simple 2nd, 3rd, and 4th-order implicit and explicit Runge-Kutta integrators I wrote for teaching if you want them.

BTW, you might want to check out the SimpleDiffEq.jl code. It’s ultra low overhead implementations, but also good for teaching. See for example the GPUATsit5 (GPU-kernel compatible adaptive Tsit5):

> <https://github.com/SciML/SimpleDiffEq.jl/blob/master/src/tsit5/gpuatsit5.jl#L104-L223>

Of course it’s missing a lot (for example, no callbacks), but it’s nice way to see a full correct implementation with adaptivity and all. There’s an RK4 too:

> <https://github.com/SciML/SimpleDiffEq.jl/blob/master/src/rk4/gpurk4.jl#L6-L47>

We should probably add a SimpleEuler, for completeness and because sometimes a 0-overhead Euler can be useful.

Anyways, there’s still a lot more libraries can offer by offering new primitives and new forms.

---

<div class="post-metadata">

**Author:** ![tfiers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tfiers/32/32427_2.png) [@tfiers](https://discourse.julialang.org/u/tfiers)\
**Post date:** [December 26, 2022, 9:22am UTC](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397/40 "2022-12-26T09:22:47Z")

</div>

> [@ChrisRackauckas](#):
>
> These now are example tutorials in the docs:
> 
> [Spiking Neural Systems · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/dev/examples/spiking_neural_systems/)

This is great!  
The epfl book ([Table of contents | Neuronal Dynamics online book](https://neuronaldynamics.epfl.ch/online/)) is indeed a good resource. (It uses latexml btw (latex-to-html conversion), same as what ar5iv uses).

On accuracy: very true that ‘the eyeball test’ is often the only requirement.  
Something more involved that’s often done is to try and reproduce the voltage trace of a real neuron that’s injected with a known current signal.  
Would be interesting to see how different integration schemes compare here.  
(I’m sure there’s literature about this somewhere – but that’s not my PhD topic alas so I have no refs to provide here).

EDIT: the more obvious comparison is a ‘ground truth’ integration with an advanced algorithm and minuscule timestep, I suppose

> [@ChrisRackauckas](#):
>
> The nonlinear tearing can improve the solve in some nice ways. We’re creating new primitive problems, the `ImplicitDiscreteProblem`, for `f(u_{n+1},u_n,p,t_n) = 0` which is then a good primitive to build a lot of the simple methods in a way that can use all of the implicit integrator tricks. That and Rosenbrock23 are a nice combo, so there will be more to say here soon.

This is exciting. Looking forward!

* * *

> [@John\_Gibson](#):
>
> (getting a bit far from OP intent, perhaps should split off 🙂 )

Agree. (Maybe from @kjohnsen 's initial question about Brian)

[Previous page](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397.md?page=1)

[Next page](https://discourse.julialang.org/t/julia-for-scientists-summary-slides/91397.md?page=3)
