Skip to content

Commit

Permalink
Merge pull request #27 from Julia-Tempering/gen-pdf
Browse files Browse the repository at this point in the history
Support generating a PDF instead of a webpage
  • Loading branch information
alexandrebouchard authored Apr 15, 2024
2 parents 51f50e3 + 1c1566e commit 22a7b28
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 27 deletions.
10 changes: 9 additions & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ See `Examples` in the left side bar to see examples of such reports.

See [`report`](@ref) for more information on the options available.

You may also generate a TeX/PDF version using
Documenter.jl's [TeX support](https://documenter.juliadocs.org/stable/man/other-formats/)
as follows:

```@example pigeons
report(pt; writer=InferenceReport.Documenter.LaTeX(platform = "docker"))
```


### From MCMCChains

Expand Down Expand Up @@ -238,4 +246,4 @@ See our [Documenter.jl make file](https://github.com/Julia-Tempering/InferenceRe
integrate InferenceReport into a broader documentation page.
The key functions used are [`headless`](@ref),
[`report_to_docs`](@ref) and
[`as_doc_page`](@ref).
[`as_doc_page`](@ref).
14 changes: 11 additions & 3 deletions src/InferenceReport.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module InferenceReport

using CairoMakie
CairoMakie.activate!(type = "png", px_per_unit = 2)

using Documenter
using MCMCChains
using PairPlots
Expand Down Expand Up @@ -121,8 +123,14 @@ as_doc_page(context) = "`$(target_name(context))`" => "generated/$(basename(dirn
"""
$SIGNATURES
"""
view_webpage(exec_folder) = open_in_default_browser("$exec_folder/build/index.html")

function view_webpage(exec_folder)
# Try to autodect if we built HTML or a PDF
if isfile("$exec_folder/build/InferenceReport.pdf")
open_in_default_browser("$exec_folder/build/InferenceReport.pdf")
else
open_in_default_browser("$exec_folder/build/index.html")
end
end
"""
$SIGNATURES
"""
Expand All @@ -131,7 +139,7 @@ render(context) =
root = dirname(context.output_directory),
sitename = "InferenceReport",
repo="https://github.com/Julia-Tempering/Pigeons.jl/blob/{commit}{path}#{line}",
format = Documenter.HTML(size_threshold=nothing),
format = context.options.writer,
pages = ["`$(target_name(context))`" => "index.md"])

# Controls defaults such as whether to render and open webpage right away
Expand Down
6 changes: 6 additions & 0 deletions src/ReportOptions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ $FIELDS
"""
reproducibility_command::Union{String, Nothing} = nothing

"""
A Documenter.Writer instance used to render the document. Default is an HTML
page, but other options including Documenter.LaTeX() are also possible.
"""
writer::Documenter.Writer=Documenter.HTML(size_threshold=nothing)

"""
Useful for debugging, false by default.
"""
Expand Down
34 changes: 22 additions & 12 deletions src/building_blocks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,28 @@ end
$SIGNATURES
"""
function add_plot(context; file, title, url_help = nothing, description = "", movie = nothing)
info_link = isnothing(url_help) ? "" : """⏐<a href="$url_help">🔗 Info </a>"""
movie_link = isnothing(movie) ? "" : """⏐<a href="$movie">🍿 Movie </a>"""
add_markdown(context;
title,
contents = """
$description
```@raw html
<iframe src="$file" style="height:500px;width:100%;"></iframe>
<a href="$file"> 🔍 Full page </a> $movie_link $info_link
```
"""
)
if context.options.writer isa Documenter.HTML
info_link = isnothing(url_help) ? "" : """⏐<a href="$url_help">🔗 Info </a>"""
movie_link = isnothing(movie) ? "" : """⏐<a href="$movie">🍿 Movie </a>"""
add_markdown(context;
title,
contents = """
$description
```@raw html
<img src="$file" style="display: block; max-width:100%; max-height:500px; width:auto; height:auto;"/>
<a href="$file"> 🔍 Full page </a> $movie_link $info_link
```
"""
)
else
add_markdown(context;
title,
contents = """
$description
![]($file)
"""
)
end
end

"""
Expand Down
22 changes: 11 additions & 11 deletions src/processors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Create a pair plot using [PairPlot.jl](https://github.com/sefffal/PairPlots.jl).
"""
function pair_plot(context)
plot = PairPlots.pairplot(get_chains(context))
file = output_file(context, "pair_plot", "svg")
CairoMakie.save(file, plot)
file = output_file(context, "pair_plot", "png")
CairoMakie.save(file, plot, px_per_unit=2)

description = """
Diagonal entries show estimates of the marginal
Expand All @@ -25,7 +25,7 @@ function pair_plot(context)
end

add_plot(context;
file = "pair_plot.svg",
file = "pair_plot.png",
title = "Pair plot",
url_help = "https://sefffal.github.io/PairPlots.jl",
movie = moving_pair_plot(context),
Expand Down Expand Up @@ -127,10 +127,10 @@ function trace_plot(context, cumulative)
resize_to_layout!(fig)

name = cumulative ? "cumulative_trace_plot" : "trace_plot"
file = output_file(context, name, "svg")
CairoMakie.save(file, fig, size= (800, 200 * n_params))
file = output_file(context, name, "png")
CairoMakie.save(file, fig, size= (800, 200 * n_params), px_per_unit=2)
add_plot(context;
file = "$name.svg",
file = "$name.png",
title = cumulative ? "Cumulative traces" : "Trace plots",
description = cumulative ? """
For each iteration ``i``, shows the running average up to ``i``,
Expand Down Expand Up @@ -268,10 +268,10 @@ function lcb(context)
ax.xlabel = "β"
ax.ylabel = "λ(β)"
name = "local_barrier"
file = output_file(context, name, "svg")
CairoMakie.save(file, f)
file = output_file(context, name, "png")
CairoMakie.save(file, f, px_per_unit=2)
add_plot(context;
file = "$name.svg",
file = "$name.png",
title = "Local communication barrier",
url_help = "https://pigeons.run/dev/output-pt/#Local-communication-barrier",
description = """
Expand Down Expand Up @@ -319,10 +319,10 @@ function pigeons_progress(context; property, title, args...)
mapping(:round, property) *
visual(Lines)
plot = draw(recipe)
file = output_file(context, "$(property)_progress", "svg")
file = output_file(context, "$(property)_progress", "png")
CairoMakie.save(file, plot)
add_plot(context;
file = "$(property)_progress.svg",
file = "$(property)_progress.png",
title,
args...)
end
28 changes: 28 additions & 0 deletions test/test_pdf.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@testset "PDF output" begin

if (get(ENV, "CI", "false") == "true") && !Sys.islinux()

@warn "on github action CI, docker available out of the box on linux only"

else

inputs, reproducibility_command =
@reproducible Inputs(
target = toy_mvn_target(2),
n_rounds = 4,
record = [traces; round_trip; record_default()])

pt = pigeons(inputs)

context = report(pt;
view = false,
writer=InferenceReport.Documenter.LaTeX(platform = "docker"),
target_description = "Description",
reproducibility_command)

@test isfile("$(context.options.exec_folder)/build/InferenceReport.pdf")

@test length(context.generated_markdown) ==
length(InferenceReport.default_postprocessors()) - 1 # MPI output
end
end

0 comments on commit 22a7b28

Please sign in to comment.