# Importing CairoMakie prevents sampling

**URL:** https://discourse.julialang.org/t/importing-cairomakie-prevents-sampling/59501
**Category:** Probabilistic Programming
**Tags:** question, gaussian-process, makie, dynamichmc
**Created:** [April 17, 2021, 9:10pm UTC](https://discourse.julialang.org/t/importing-cairomakie-prevents-sampling/59501 "2021-04-17T21:10:36Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [April 17, 2021, 9:10pm UTC](https://discourse.julialang.org/t/importing-cairomakie-prevents-sampling/59501/1 "2021-04-17T21:10:36Z")

</div>

I have a script that runs fine unless I have `import CairoMakie` at the top. Then it hangs. How can I diagnose this?

It hangs at `mcmc_with_warmup`. The same thing happens whether I have `using CairoMakie` or `import CairoMakie`. However, if I run the script before importing `CairoMakie`, then import it and run the script again, it works fine.

It seems like it’s hanging before it starts executing the line.

```julia
using AbstractGPs
using Distributions
using StatsFuns
using DynamicHMC
using LogDensityProblems
# import CairoMakie
using Random
Random.seed!(1234)

n = 100
x_train = collect(range(-5.0, 5.0; length=n))
y_train = rand(n) + sin.(x_train .* 2);

function gp_loglikelihood(x, y)
    function loglikelihood(params)
        kernel =
            softplus(params[1]) * (Matern52Kernel() ∘ ScaleTransform(softplus(params[2])))
        f = GP(kernel)
        fx = f(x, 0.1)
        return logpdf(fx, y)
    end
    return loglikelihood
end

function gp_posterior(x, y, p)
    kernel = softplus(p[1]) * (Matern52Kernel() ∘ ScaleTransform(softplus(p[2])))
    f = GP(kernel)
    return posterior(f(x, 0.1), y)
end

loglik_train = gp_loglikelihood(x_train, y_train)
logprior(params) = logpdf(MvNormal(2, 1), params)

n_samples = 2_000
n_adapts = 1_000

# Log joint density
function LogDensityProblems.logdensity(ℓ::typeof(loglik_train), params)
    return ℓ(params) + logprior(params)
end

# The parameter space is two-dimensional
LogDensityProblems.dimension(::typeof(loglik_train)) = 2

# `loglik_train` does not allow to evaluate derivatives of
# the log-likelihood function
function LogDensityProblems.capabilities(::Type{<:typeof(loglik_train)})
    return LogDensityProblems.LogDensityOrder{0}()
end

mcmc_result = mcmc_with_warmup(
        Random.GLOBAL_RNG,
        ADgradient(:ForwardDiff, loglik_train),
        n_samples;
        reporter=NoProgressReport(),
)

samples = mcmc_result.chain

samples_constrained = [map(softplus, p) for p in samples]
mean_samples = mean(samples_constrained)

```

Julia 1.5.3  
Linux

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [April 17, 2021, 9:46pm UTC](https://discourse.julialang.org/t/importing-cairomakie-prevents-sampling/59501/2 "2021-04-17T21:46:01Z")

</div>

Does `using `CairoMakie` hang when it is the only line? What happens when you interrupt it with Ctrl-C?

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [April 17, 2021, 9:57pm UTC](https://discourse.julialang.org/t/importing-cairomakie-prevents-sampling/59501/3 "2021-04-17T21:57:23Z")

</div>

`using CairoMakie` by itself works fine. The part that hangs is the `mcmc_with_warmup` line. Ctrl+C doesn’t do anything. I have to Ctrl+\ to get out.

---

<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: [April 18, 2021, 7:31pm UTC](https://discourse.julialang.org/t/importing-cairomakie-prevents-sampling/59501/4 "2021-04-18T19:31:14Z")

</div>

Could you try this with just Cairo.jl? If it still hangs that would point to something outside of our control.

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [April 18, 2021, 8:02pm UTC](https://discourse.julialang.org/t/importing-cairomakie-prevents-sampling/59501/5 "2021-04-18T20:02:01Z")

</div>

Works fine with just `import Cairo`.

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [April 22, 2021, 4:35am UTC](https://discourse.julialang.org/t/importing-cairomakie-prevents-sampling/59501/6 "2021-04-22T04:35:20Z")

</div>

It seems CairoMakie prevents the code from running, when Cairo alone doesn’t cause problems. I can import CairoMakie _after_ doing the computation, but that’s a hack.

---

<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: [April 22, 2021, 5:07am UTC](https://discourse.julialang.org/t/importing-cairomakie-prevents-sampling/59501/7 "2021-04-22T05:07:45Z")

</div>

@sdanisch any idea what this could be? Cairomakie doesn’t have a renderloop right, and I don’t see what could be happening there in the background

---

<div class="post-metadata">

### Author: ![sdanisch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdanisch/32/1406_2.png) [@sdanisch](https://discourse.julialang.org/u/sdanisch)
#### Post date: [April 22, 2021, 8:47am UTC](https://discourse.julialang.org/t/importing-cairomakie-prevents-sampling/59501/8 "2021-04-22T08:47:42Z")

</div>

I can’t reproduce it on windows & julia 1.6…
