Julia Environment

This notebook describes and creates the default Julia environments in Nextjournal. It is based on the Bash environment. Check out the showcase if you want to see what the environment contains. To see how it’s built, see setup.

Showcase

These packages are included in Nextjournal's Julia 1.4 environment.

]st
0.9s
Julia Tests (Julia)
Julia 1.5.2

System Packages and Basics

A wide variety of support libraries are installed, as well as gcc v7 and ImageMagick.

Version

"1.1.0"
of the SoftGlobalScope package is available—this adjusts scope-handling to be more like Julia 0.6, providing a more REPL-like interface.

Julia packages are installed normally, using Pkg in a Julia cell. Please refer to the Julia section of Installing Software and Packages for more detailed information.

Plotting

The default environment comes with GR v

"0.52.0"
, PlotlyBase
"0.4.1"
, and PlotlyJS
"0.14.0"
, as well as version
"1.7.3"
of the Plots framework. Makie version
"0.12.18"
is also installed.

Plots

The JuliaPlots framework provides a unified interface to multiple graphical backends. The default backend is GR, which efficiently generates static plots and animations.

using Plots; gr()
# define the Lorenz attractor
mutable struct Lorenz
    dt; σ; ρ; β; x; y; z
end
function step!(l::Lorenz)
    dx = l.σ*(l.y - l.x)       ; l.x += l.dt * dx
    dy = l.x*(l.ρ - l.z) - l.y ; l.y += l.dt * dy
    dz = l.x*l.y - l.β*l.z     ; l.z += l.dt * dz
end
attractor = 
  Lorenz((dt = 0.02, σ = 10., ρ = 28., β = 8//3, x = 1., y = 1., z = 1.)...)
# initialize a 3D plot with 1 empty series
plt = plot3d(1, xlim=(-25,25), ylim=(-25,25), zlim=(0,50),
             title = "Lorenz Attractor", marker = 2)
# build an animated gif by pushing new points to the plot, saving every 10th frame
@gif for i=1:1500
    step!(attractor)
    push!(plt, attractor.x, attractor.y, attractor.z)
end every 10
41.5s
Julia Tests (Julia)
Julia 1.5.2

Switching to the Plotly backend adds some interactivity to the output.

plotly(lw=3)
x = -100:100
Plots.plot(x, 100x.^2)
Plots.plot!(x, x.^3 - x.^2)
17.7s
Julia Tests (Julia)
Julia 1.5.2

Makie

The WGLMakie package can generate beautiful 3D visualizations with WebGL.

using WGLMakie, AbstractPlotting, LinearAlgebra
Attributes(font = "Open Sans", resolution = (600, 500))
n = 20
set_theme!(resolution = (600, 500))
f   = (x,y,z) -> x*exp(cos(y)*z)
∇f  = (x,y,z) -> Point3f0(exp(cos(y)*z), -sin(y)*z*x*exp(cos(y)*z), x*cos(y)*exp(cos(y)*z))
∇ˢf = (x,y,z) -> ∇f(x,y,z) - Point3f0(x,y,z)*dot(Point3f0(x,y,z), ∇f(x,y,z))
θ = [0;(0.5:n-0.5)/n;1]
φ = [(0:2n-2)*2/(2n-1);2]
x = [cospi(φ)*sinpi(θ) for θ in θ, φ in φ]
y = [sinpi(φ)*sinpi(θ) for θ in θ, φ in φ]
z = [cospi(θ) for θ in θ, φ in φ]
pts = vec(Point3f0.(x, y, z))
∇ˢF = vec(∇ˢf.(x, y, z)) .* 0.1f0
AbstractPlotting.surface(x, y, z)
AbstractPlotting.arrows!(pts, ∇ˢF, arrowsize = 0.03, linecolor = (:white, 0.6), linewidth = 3)
110.1s
Julia Tests (Julia)
Julia 1.5.2

VegaLite

VegaLite can also provide interactive plotting.

using VegaLite, VegaDatasets
vega_plot = dataset("cars") |>
@vlplot(
    :point,
    x = :Horsepower,
    y = :Miles_per_Gallon,
    color = :Origin,
    width = 650,
    height = 400
) |> VegaLite.interactive()
28.0s
Julia Tests (Julia)
Julia 1.5.2

Data Structures

Several data-related packages are installed in the default environment.

  • For handling their relative datatypes and I/O for associated files, we install a variety of packages, including JLD v

    "0.10.0"
    , CSV v
    "0.7.7"
    , HDF5 v
    "0.13.6"
    , and JSON v
    "0.21.1"
    .

  • The FileIO v

    "1.4.4"
    framework provides a unified interface for loading files of many types via multiple backends, including ImageMagick.

  • DataFrames v

    "0.10.0"
    defines and handles objects similar to those found in R and the Python pandas toolkit, with a comparable interface.

File Handling

The HDF5 package provides a Julia interface to the HDF5 library. It is also used by the JLD and MAT packages. Let's look at some example temperature data.

NEONDSTowerTemperatureData.hdf5
using HDF5
# Some methods to traverse the first path in an h5 file.
dd(node::HDF5File) = dd(node[names(node)[1]])
dd(node::HDF5Group) = dd(node[names(node)[1]])
dd(node::HDF5Dataset) = node
dspath = h5open(
NEONDSTowerTemperatureData.hdf5
) do h5
  name(dd(h5))
end
print("First path: $dspath.")
data = h5read(
NEONDSTowerTemperatureData.hdf5
, dspath)
using Plots
Plots.plot([x.date for x in data],[y.mean for y in data], 
  label="Temperature (Min/Max)",
  ribbon=[(y.min,y.max) for y in data], xrotation=45)
39.7s
Julia Tests (Julia)
Julia 1.5.2

The JLD package provides a type-preserving way to save Julia objects to file.

using JLD
struct testData
  x::Int64
  y::String
end
foo = testData(7,"test")
save("/tmp/blah.jld", "foo", foo)
load("/tmp/blah.jld")
12.2s
Julia Tests (Julia)
Julia 1.5.2
Dict{String,Any} with 1 entry: "foo" => testData(7, "test")

FileIO

FileIO provides a unified set of methods to access data in files: query(), load(), and save(), as well as loadstreaming() and savestreaming() for large files. The functions can automatically recognize files using headers or extensions, but you can also provide format information as below, where we load the Iris Dataset from a CSV file.

iris.csv
using FileIO
load(File(format"CSV",
iris.csv
))
8.1s
Julia Tests (Julia)
Julia 1.5.2

DataFrames

DataFrame objects represent tabular data as a set of vectors.

using DataFrames
df = DataFrame(A = 1:5, B = 1:2:10, C = ["a","b","c","d","q"])
7.6s
Julia Tests (Julia)
Julia 1.5.2
ABC
11a
23b
35c
47d
59q
5 items

Column names are referenced via accesing the fields.

df.A .+ df.B
1.2s
Julia Tests (Julia)
Julia 1.5.2
5-element Array{Int64,1}: 2 5 8 11 14

JSON

Import and export JSON using the JSON package, which is always loaded on Nextjournal. In the example below, a Julia data structure input results in JSON output. The change from nothing to null is a clear indicator.

json(["foo", Dict("bar" => ("baz", nothing, 1.0, 2))])
0.8s
Julia Tests (Julia)
Julia 1.5.2
"[\"foo\",{\"bar\":[\"baz\",null,1.0,2]}]"

Setup

Julia 1.x

Build a Minimal Julia 1.x Environment

We'll base our environment off of our Minimal Python image, which has a small conda Python setup. Note that the Julia version is set as an environment variable on the runtime.

Minimal Julia 1.5.2
Minimal Julia 1.5.2 (Bash)
exporting environment
Type: Nextjournal
Environment:
Machine Type:
Environment Variables:
PATH/usr/local/julia/bin:/opt/conda/bin:/usr/local/nvidia/bin:/usr/local/cuda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
JULIA_PATH/usr/local/julia
JULIA_VERSION1.5.2
CONDA_JL_HOME/opt/conda
JSSERVE_LISTEN_URL0.0.0.0
Download this environment as a Docker image from:

The exact version we're installing is

"1.5.2"
. Here we download the archive and signatures, verify, and save. Cell is locked to prevent redownload.

tarArch="x86_64"
dirArch="x64"
BASEURL="https://julialang-s3.julialang.org/bin"
tarfile="julia-${JULIA_VERSION}-linux-${tarArch}.tar.gz"
dir="${dirArch}/${JULIA_VERSION%[.-]*}"
tarurl="${BASEURL}/linux/${dir}/${tarfile}"
sigfile="${tarfile}.asc"
sigurl="${tarurl}.asc"
shafile="julia-${JULIA_VERSION}.sha256"
shaurl="${BASEURL}/checksums/${shafile}"
keyfile="juliareleases.asc"
keyurl="https://julialang.org/assets/${keyfile}"
wget -q --show-progress --progress=bar:force \
  $tarurl $sigurl $shaurl $keyurl
sha256sum -c --ignore-missing $shafile
export GNUPGHOME="$(mktemp -d)"
gpg --import $keyfile
gpg --batch --verify $sigfile $tarfile
gpgconf --kill all
cp $tarfile /results/
rm -rf "$GNUPGHOME" $tarfile $sigfile $shafile $keyfile
8.5s
Minimal Julia 1.5.2 (Bash)
julia-1.5.2-linux-x86_64.tar.gz
105.32 MB

Install from saved archive.

mkdir -p "$JULIA_PATH"
tar -xzf 
julia-1.5.2-linux-x86_64.tar.gz
-C "$JULIA_PATH" --strip-components 1
4.8s
Minimal Julia 1.5.2 (Bash)

And verify it runs.

julia -v
0.7s
Minimal Julia 1.5.2 (Bash)

The JSON package is required to run on Nextjournal.

julia -e 'using Pkg; pkg"up; add JSON; precompile; test JSON"'
43.7s
Minimal Julia 1.5.2 (Bash)

Install Conda.jl, which is set to use the existing Conda installation via the CONDA_JL_HOME environment variable.

julia -e 'using Pkg; pkg"add Conda; build Conda; precompile"'
7.3s
Minimal Julia 1.5.2 (Bash)

Check size.

du -hsx /
6.2s
Minimal Julia 1.5.2 (Bash)

Build the Default Julia 1.x Environment

We'll add a number of packages as well as the libraries and support programs required by them. The major packages installed are JuliaPlots along with the GR and PlotlyJS backends, WGLMakie, and the DataFrames, CSV, and HDF5 data-handling packages.

First we'll check that the minimal Julia env works.

"$VERSION"
0.4s
Julia 1.x versionJulia 1.5.2 (Julia)
Minimal Julia 1.5.2
"1.5.2"

Install a few system tools and libraries.

apt-get -qq update
DEBIAN_FRONTEND=noninteractive apt-get install --no-install-recommends \
  imagemagick libhdf5-dev hdf5-tools mesa-utils \
  libxt6 libxrender1 libgl1-mesa-glx libqt5widgets5 `# for GR` \
  libhttp-parser2.7.1 `# for PlotlyJS`
apt-get clean
rm -r /var/lib/apt/lists/* # Clear package list so it isn't stale
33.3s
Julia 1.5.2 (Bash in Julia)
Minimal Julia 1.5.2

Next the Julia package installs.

]up
7.7s
Julia 1.5.2 (Julia)
Minimal Julia 1.5.2
]add SoftGlobalScope DataFrames JLD CSV CSVFiles BSON NRRD MeshIO HDF5 MAT FileIO JSExpr CSSUtil StatsBase StatsPlots Observables Parameters Unitful Interact WebSockets HTTP Blink WebIO PlotlyBase PlotlyJS RecipesBase GR Plots ImageCore ImageShow ImageMagick Colors ProgressMeter BenchmarkTools PackageCompiler FixedPointNumbers OffsetArrays IJulia WGLMakie AbstractPlotting JSCall VegaLite VegaDatasets RDatasets GeometryBasics
87.2s
Julia 1.5.2 (Julia)
Minimal Julia 1.5.2

Build all packages.

]build
25.9s
Julia 1.5.2 (Julia)
Minimal Julia 1.5.2

Precompile any qualifying packages.

]precompile
707.9s
Julia 1.5.2 (Julia)
Minimal Julia 1.5.2

Finally, add font defaults for JuliaPlots and Makie. These named Code Listings will be mounted as files to the runtime's filesystem, and saved with the environment.

PLOTS_DEFAULTS = Dict(:fontfamily => "Open Sans")
.juliarc.jl
Julia

Makie caches some fonts on first using, so we make sure that they're already cached.

using AbstractPlotting, WGLMakie
for res in (AbstractPlotting.High, AbstractPlotting.Low)
  AbstractPlotting.set_glyph_resolution!(res)
  AbstractPlotting.cached_load();
end
35.8s
Julia 1.5.2 (Julia)
Minimal Julia 1.5.2

Test.

julia -v
du -hsx /
6.1s
Julia 1.5.2 (Bash in Julia)
Minimal Julia 1.5.2

Test Default 1.x Env

Test-load all packages.

using SoftGlobalScope, DataFrames, JLD, CSV, CSVFiles, BSON, NRRD, MeshIO, HDF5, MAT, FileIO, JSExpr, CSSUtil, StatsBase, StatsPlots, Observables, Parameters, Unitful, Interact, WebSockets, HTTP, Blink, WebIO, PlotlyBase, PlotlyJS, RecipesBase, GR, Plots, ImageCore, ImageShow, ImageMagick, Colors, ProgressMeter, BenchmarkTools, PackageCompiler, FixedPointNumbers, OffsetArrays, IJulia, WGLMakie, AbstractPlotting, JSCall, VegaLite, VegaDatasets, RDatasets, GeometryBasics
IJulia.verbose
17.3s
Julia Tests (Julia)
Julia 1.5.2
false

Update, and add some mixed packages to test dependency resolution and installation.

]up
22.7s
Julia Tests (Julia)
Julia 1.5.2
]add Flux ArrayFire FFTW Images ImageFiltering Adapt GPUArrays CUDA NNlib QuantumOptics LinearAlgebra DifferentialEquations BoundaryValueDiffEq DelayDiffEq DiffEqBase DiffEqCallbacks DiffEqFinancial DiffEqJump DiffEqNoiseProcess DiffEqPhysics DimensionalPlotRecipes OrdinaryDiffEq SteadyStateDiffEq StochasticDiffEq 
26.7s
Julia Tests (Julia)
Julia 1.5.2

Print package versions.

"$(Pkg.installed()["SoftGlobalScope"])"
1.4s
SoftGlobalScope versionJulia Tests (Julia)
Julia 1.5.2
"1.1.0"
"$(Pkg.installed()["GR"])"
0.3s
GR versionJulia Tests (Julia)
Julia 1.5.2
"0.52.0"
"$(Pkg.installed()["PlotlyBase"])"
0.3s
PlotlyBase versionJulia Tests (Julia)
Julia 1.5.2
"0.4.1"
"$(Pkg.installed()["PlotlyJS"])"
0.3s
PlotlyJS VersionJulia Tests (Julia)
Julia 1.5.2
"0.14.0"
"$(Pkg.installed()["Plots"])"
0.3s
Plots versionJulia Tests (Julia)
Julia 1.5.2
"1.7.3"
"$(Pkg.installed()["AbstractPlotting"])"
0.2s
Makie versionJulia Tests (Julia)
Julia 1.5.2
"0.12.18"
"$(Pkg.installed()["JLD"])"
0.3s
JLD versionJulia Tests (Julia)
Julia 1.5.2
"0.10.0"
"$(Pkg.installed()["CSV"])"
0.2s
CSV versionJulia Tests (Julia)
Julia 1.5.2
"0.7.7"
"$(Pkg.installed()["HDF5"])"
0.3s
HDF5 versionJulia Tests (Julia)
Julia 1.5.2
"0.13.6"
"$(Pkg.installed()["JSON"])"
0.3s
JSON versionJulia Tests (Julia)
Julia 1.5.2
"0.21.1"
"$(Pkg.installed()["FileIO"])"
0.3s
FileIO versionJulia Tests (Julia)
Julia 1.5.2
"1.4.4"
"$(Pkg.installed()["DataFrames"])"
0.3s
DataFrames versionJulia Tests (Julia)
Julia 1.5.2
"0.21.8"
Runtimes (3)