From 30a83667ee1b4097f63feca3bdbd4672e3910a8f Mon Sep 17 00:00:00 2001 From: Matti Pastell Date: Fri, 29 Apr 2016 16:41:18 +0300 Subject: [PATCH 1/3] Add autodetection of plotting library, and pre and post chunk hooks, prehook for setting plot size with plots.jl --- src/Weave.jl | 14 +++++++++++++- src/config.jl | 2 +- src/plots.jl | 11 +++++++++++ src/run.jl | 41 +++++++++++++++++++++++++++++++++++------ 4 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 src/plots.jl diff --git a/src/Weave.jl b/src/Weave.jl index d8b5810..602758c 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -64,7 +64,7 @@ Weave an input document to output file. **Note:** Run Weave from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output. """ -function weave(source ; doctype = :auto, plotlib="Gadfly", +function weave(source ; doctype = :auto, plotlib=:auto, informat=:auto, out_path=:doc, fig_path = "figures", fig_ext = nothing, cache_path = "cache", cache=:off) @@ -101,6 +101,18 @@ function weave(doc::AbstractString, doctype::AbstractString) weave(doc, doctype=doctype) end +#Hooks to run before and after chunks, this is form IJulia, +#but note that Weave hooks take the chunk as input +const preexecute_hooks = Function[] +push_preexecute_hook(f::Function) = push!(preexecute_hooks, f) +pop_preexecute_hook(f::Function) = splice!(preexecute_hooks, findfirst(pretexecute_hooks, f)) + +const postexecute_hooks = Function[] +push_postexecute_hook(f::Function) = push!(postexecute_hooks, f) +pop_postexecute_hook(f::Function) = splice!(postexecute_hooks, findfirst(postexecute_hooks, f)) + +const plotlib_set = false + export weave, list_out_formats, tangle, set_chunk_defaults, get_chunk_defaults, restore_chunk_defaults diff --git a/src/config.jl b/src/config.jl index 97810a5..dc254bc 100644 --- a/src/config.jl +++ b/src/config.jl @@ -2,7 +2,7 @@ #Default options const defaultParams = - @compat Dict{Symbol,Any}(:plotlib => "Gadfly", + @compat Dict{Symbol,Any}(:plotlib => nothing, :storeresults => false, :doc_number => 0, :chunk_defaults => diff --git a/src/plots.jl b/src/plots.jl new file mode 100644 index 0000000..aa6d4d7 --- /dev/null +++ b/src/plots.jl @@ -0,0 +1,11 @@ +import Plots + +"""Pre-execute hooks to set the plot size for the chunk """ +function plots_set_size(chunk) + w = chunk.options[:fig_width] * chunk.options[:dpi] + h = chunk.options[:fig_height] * chunk.options[:dpi] + Plots.default(size = (w,h)) + return chunk +end + +push_preexecute_hook(plots_set_size) diff --git a/src/run.jl b/src/run.jl index d85f250..b9e11ad 100644 --- a/src/run.jl +++ b/src/run.jl @@ -18,7 +18,7 @@ Run code chunks and capture output from parsed document. **Note:** Run command from terminal and not using IJulia, Juno or ESS, they tend to mess with capturing output. """ -function Base.run(doc::WeaveDoc; doctype = :auto, plotlib="Gadfly", +function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto, out_path=:doc, fig_path = "figures", fig_ext = nothing, cache_path = "cache", cache = :off) #cache :all, :user, :off, :refresh @@ -43,8 +43,10 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib="Gadfly", mimetypes = default_mime_types end + #Reset plotting + plotlib_set = false + plotlib == :auto || init_plotting(plotlib) - init_plotting(plotlib) report = Report(doc.cwd, doc.basename, doc.format.formatdict, mimetypes) pushdisplay(report) @@ -64,6 +66,7 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib="Gadfly", if typeof(chunk) == CodeChunk options = merge(rcParams[:chunk_defaults], chunk.options) merge!(chunk.options, options) + plotlib_set || detect_plotlib() #Try to autodetect plotting library end restore = (cache ==:user && typeof(chunk) == CodeChunk && chunk.options[:cache]) @@ -71,8 +74,10 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib="Gadfly", if cached != nothing && (cache == :all || restore) result_chunks = restore_chunk(chunk, cached) else - result_chunks = run_chunk(chunk, report, SandBox) + + result_chunks = run_chunk(chunk, report, SandBox) end + executed = [executed; result_chunks] end @@ -198,6 +203,11 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module) return chunk end + #Run preexecute_hooks + for hook in preexecute_hooks + chunk = hook(chunk) + end + report.fignum = 1 report.cur_chunk = chunk @@ -206,6 +216,12 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module) end chunk.result = run_code(chunk, report, SandBox) + + #Run post_execute chunks + for hook in postexecute_hooks + chunk = hook(chunk) + end + if chunk.options[:term] chunks = collect_results(chunk, TermResult()) elseif chunk.options[:hold] @@ -214,6 +230,8 @@ function eval_chunk(chunk::CodeChunk, report::Report, SandBox::Module) chunks = collect_results(chunk, ScriptResult()) end + + #else # chunk.options[:fig] && (chunk.figures = copy(report.figures)) #end @@ -252,24 +270,27 @@ end function init_plotting(plotlib) if plotlib == nothing - #rcParams[:chunk_defaults][:fig] = false rcParams[:plotlib] = nothing else l_plotlib = lowercase(plotlib) rcParams[:chunk_defaults][:fig] = true - if l_plotlib == "winston" eval(parse("""include(Pkg.dir("Weave","src","winston.jl"))""")) rcParams[:plotlib] = "Winston" elseif l_plotlib == "pyplot" eval(parse("""include(Pkg.dir("Weave","src","pyplot.jl"))""")) rcParams[:plotlib] = "PyPlot" + elseif l_plotlib == "plots" + eval(parse("""include(Pkg.dir("Weave","src","plots.jl"))""")) + rcParams[:plotlib] = "Plots" elseif l_plotlib == "gadfly" eval(parse("""include(Pkg.dir("Weave","src","gadfly.jl"))""")) rcParams[:plotlib] = "Gadfly" end end - return nothing + plotlib_set = true + info(rcParams[:plotlib]) + return true end function get_cwd(doc::WeaveDoc, out_path) @@ -386,3 +407,11 @@ function collect_results(chunk::CodeChunk, fmt::CollectResult) end return [chunk] end + +function detect_plotlib() + info("Detecting plotting library") + isdefined(:Plots) && init_plotting("Plots") && return + isdefined(:PyPlot) && init_plotting("PyPlot") && return + isdefined(:Gadfly) && init_plotting("Gadfly") && return + isdefined(:Winston) && init_plotting("Winston") && return +end From 699f5b46e7a4daa4ccd7a73bc2dec4150514caa6 Mon Sep 17 00:00:00 2001 From: Matti Pastell Date: Fri, 29 Apr 2016 16:56:05 +0300 Subject: [PATCH 2/3] Move plotlib_set flag to rcParams --- src/Weave.jl | 2 +- src/config.jl | 1 + src/run.jl | 9 +++++---- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Weave.jl b/src/Weave.jl index 602758c..04d5e09 100644 --- a/src/Weave.jl +++ b/src/Weave.jl @@ -111,7 +111,7 @@ const postexecute_hooks = Function[] push_postexecute_hook(f::Function) = push!(postexecute_hooks, f) pop_postexecute_hook(f::Function) = splice!(postexecute_hooks, findfirst(postexecute_hooks, f)) -const plotlib_set = false + export weave, list_out_formats, tangle, set_chunk_defaults, get_chunk_defaults, restore_chunk_defaults diff --git a/src/config.jl b/src/config.jl index dc254bc..c6f9ce8 100644 --- a/src/config.jl +++ b/src/config.jl @@ -3,6 +3,7 @@ #Default options const defaultParams = @compat Dict{Symbol,Any}(:plotlib => nothing, + :plotlib_set => false, :storeresults => false, :doc_number => 0, :chunk_defaults => diff --git a/src/run.jl b/src/run.jl index b9e11ad..0203c7a 100644 --- a/src/run.jl +++ b/src/run.jl @@ -44,8 +44,8 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto, end #Reset plotting - plotlib_set = false - plotlib == :auto || init_plotting(plotlib) + rcParams[:plotlib_set] = false + #plotlib == :auto || init_plotting(plotlib) report = Report(doc.cwd, doc.basename, doc.format.formatdict, mimetypes) pushdisplay(report) @@ -66,7 +66,8 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto, if typeof(chunk) == CodeChunk options = merge(rcParams[:chunk_defaults], chunk.options) merge!(chunk.options, options) - plotlib_set || detect_plotlib() #Try to autodetect plotting library + + rcParams[:plotlib_set] || detect_plotlib() #Try to autodetect plotting library end restore = (cache ==:user && typeof(chunk) == CodeChunk && chunk.options[:cache]) @@ -269,6 +270,7 @@ end function init_plotting(plotlib) + rcParams[:plotlib_set] = true if plotlib == nothing rcParams[:plotlib] = nothing else @@ -288,7 +290,6 @@ function init_plotting(plotlib) rcParams[:plotlib] = "Gadfly" end end - plotlib_set = true info(rcParams[:plotlib]) return true end From 5e929b7ef64625047c7a7bd291f43941538a4c35 Mon Sep 17 00:00:00 2001 From: = Date: Mon, 2 May 2016 23:15:24 +0300 Subject: [PATCH 3/3] Make plotting library autodetection work in first chunk --- src/run.jl | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/run.jl b/src/run.jl index 0203c7a..a35e27d 100644 --- a/src/run.jl +++ b/src/run.jl @@ -45,7 +45,7 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto, #Reset plotting rcParams[:plotlib_set] = false - #plotlib == :auto || init_plotting(plotlib) + plotlib == :auto || init_plotting(plotlib) report = Report(doc.cwd, doc.basename, doc.format.formatdict, mimetypes) pushdisplay(report) @@ -67,7 +67,7 @@ function Base.run(doc::WeaveDoc; doctype = :auto, plotlib=:auto, options = merge(rcParams[:chunk_defaults], chunk.options) merge!(chunk.options, options) - rcParams[:plotlib_set] || detect_plotlib() #Try to autodetect plotting library + end restore = (cache ==:user && typeof(chunk) == CodeChunk && chunk.options[:cache]) @@ -133,6 +133,7 @@ function run_code(chunk::CodeChunk, report::Report, SandBox::Module) for (str_expr, expr) = expressions reset_report(report) lastline = (result_no == N) + rcParams[:plotlib_set] || detect_plotlib(chunk) #Try to autodetect plotting library (obj, out) = capture_output(expr, SandBox, chunk.options[:term], rcParams[:plotlib], lastline) figures = report.figures #Captured figures @@ -290,7 +291,6 @@ function init_plotting(plotlib) rcParams[:plotlib] = "Gadfly" end end - info(rcParams[:plotlib]) return true end @@ -409,9 +409,13 @@ function collect_results(chunk::CodeChunk, fmt::CollectResult) return [chunk] end -function detect_plotlib() - info("Detecting plotting library") - isdefined(:Plots) && init_plotting("Plots") && return +function detect_plotlib(chunk::CodeChunk) + if isdefined(:Plots) + init_plotting("Plots") + #Need to set size before plots are created + plots_set_size(chunk) + return + end isdefined(:PyPlot) && init_plotting("PyPlot") && return isdefined(:Gadfly) && init_plotting("Gadfly") && return isdefined(:Winston) && init_plotting("Winston") && return