Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Dec 4, 2022
1 parent 8db25c7 commit 4b6595c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 95 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jobs:
GKS_ENCODING: "utf8"
GKSwstype: "nul"
PYTHON: "Conda" # for `PyPlot`
JULIA_PYTHONCALL_EXE: "@PyCall" # for `PythonCall`
name: Julia ${{ matrix.version }} - ${{ matrix.os }}
runs-on: ${{ matrix.os }}
continue-on-error: ${{ matrix.version == 'nightly' }}
Expand Down Expand Up @@ -94,11 +93,6 @@ jobs:
end
Conda.add([libgcc..., "matplotlib"])
Conda.list()
# configure `CondPkg` to use `Conda` setup
open(ENV["GITHUB_ENV"], "a") do io
println(io, "JULIA_CONDAPKG_EXE=", Conda.CONDA_EXE)
println(io, "JULIA_CONDAPKG_BACKEND=Null")
end
Pkg.add("PyCall"); Pkg.build("PyCall"; verbose=true)
- uses: julia-actions/julia-runtest@latest
Expand Down
23 changes: 13 additions & 10 deletions src/backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -745,14 +745,14 @@ const _plotlyjs_scale = _plotly_scale
# pyplot

_post_init(::PyPlotBackend) = @eval begin
pycolors = PyPlot.pyimport("matplotlib.colors")
pypath = PyPlot.pyimport("matplotlib.path")
mplot3d = PyPlot.pyimport("mpl_toolkits.mplot3d")
axes_grid1 = PyPlot.pyimport("mpl_toolkits.axes_grid1")
pypatches = PyPlot.pyimport("matplotlib.patches")
pyticker = PyPlot.pyimport("matplotlib.ticker")
pycmap = PyPlot.pyimport("matplotlib.cm")
pynp = PyPlot.pyimport("numpy")
pycolors = PyCall.pyimport("matplotlib.colors")
pypath = PyCall.pyimport("matplotlib.path")
mplot3d = PyCall.pyimport("mpl_toolkits.mplot3d")
axes_grid1 = PyCall.pyimport("mpl_toolkits.axes_grid1")
pypatches = PyCall.pyimport("matplotlib.patches")
pyticker = PyCall.pyimport("matplotlib.ticker")
pycmap = PyCall.pyimport("matplotlib.cm")
pynp = PyCall.pyimport("numpy")

pynp."seterr"(invalid = "ignore")

Expand All @@ -765,7 +765,10 @@ function _initialize_backend(pkg::PyPlotBackend)
export PyPlot
$(_check_compat)(PyPlot)
end
@eval const PyPlot = Main.PyPlot
@eval begin
const PyPlot = Main.PyPlot
const PyCall = Main.PyPlot.PyCall
end
_post_init(pkg)
end

Expand Down Expand Up @@ -897,7 +900,7 @@ _post_init(::PythonPlotBackend) = @eval begin
mpl = PythonCall.pyimport("matplotlib")
numpy = PythonCall.pyimport("numpy")

PythonPlot.pyimport("mpl_toolkits.axes_grid1")
PythonCall.pyimport("mpl_toolkits.axes_grid1")
numpy.seterr(invalid = "ignore")

PythonPlot.ioff() # we don't want every command to update the figure
Expand Down
2 changes: 1 addition & 1 deletion src/examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1349,7 +1349,7 @@ function test_examples(
)
@info "Testing plot: $pkgname:$i:$(_examples[i].header)"

m = Module(:PlotsExamplesModule)
m = Module(Symbol(:PlotsExamples, pkgname))

# prevent leaking variables (esp. functions) directly into Plots namespace
Base.eval(m, quote
Expand Down
8 changes: 3 additions & 5 deletions src/init.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ end
function _plots_plotly_defaults()
if bool_env("PLOTS_HOST_DEPENDENCY_LOCAL", "false")
_plotly_local_file_path[] =
joinpath(@get_scratch!("plotly"), _plotly_min_js_filename)
isfile(_plotly_local_file_path[]) || Downloads.download(
"https://cdn.plot.ly/$(_plotly_min_js_filename)",
_plotly_local_file_path[],
)
fn = joinpath(@get_scratch!("plotly"), _plotly_min_js_filename)
isfile(fn) ||
Downloads.download("https://cdn.plot.ly/$(_plotly_min_js_filename)", fn)
_use_local_plotlyjs[] = true
end
_use_local_dependencies[] = _use_local_plotlyjs[]
Expand Down
147 changes: 74 additions & 73 deletions test/test_backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ function image_comparison_tests(
example = Plots._examples[idx]
@info "Testing plot: $pkg:$idx:$(example.header)"

reffn = reference_file(pkg, Plots._current_plots_version, idx)
newfn =
joinpath(reference_path(pkg, Plots._current_plots_version), ref_name(idx) * ".png")
ver = Plots._current_plots_version
ver = VersionNumber(ver.major, ver.minor, ver.patch)
reffn = reference_file(pkg, ver, idx)
newfn = joinpath(reference_path(pkg, ver), ref_name(idx) * ".png")

imports = something(example.imports, :())
exprs = quote
Expand Down Expand Up @@ -142,64 +143,6 @@ with(:pgfplotsx) do
end
=#

@testset "Preferences" begin
Plots.set_default_backend!() # start with empty preferences

withenv("PLOTS_DEFAULT_BACKEND" => "invalid") do
@test_logs (:warn, r".*is not a supported backend") Plots.load_default_backend()
end
@test_logs (:warn, r".*is not a supported backend") backend(:invalid)

@test Plots.load_default_backend() == Plots.GRBackend()

withenv("PLOTS_DEFAULT_BACKEND" => "unicodeplots") do
@test_logs (:info, r".*environment variable") Plots.diagnostics(devnull)
@test Plots.load_default_backend() == Plots.UnicodePlotsBackend()
end

@test Plots.load_default_backend() == Plots.GRBackend()
@test Plots.backend_package_name() === :GR
@test Plots.backend_name() === :gr

@test_logs (:info, r".*fallback") Plots.diagnostics(devnull)

@test Plots.merge_with_base_supported([:annotations, :guide]) isa Set
@test Plots.CurrentBackend(:gr).sym === :gr

@test_logs (:warn, r".*is not compatible with") Plots.set_default_backend!(:invalid)

@testset "simple restart" begin
# this test mimics a restart, which is needed after a preferences change
Plots.set_default_backend!(:unicodeplots)
script = tempname()
write(
script,
"""
ENV["PLOTS_PRECOMPILE"] = false
using Pkg, Test; io = (devnull, stdout)[1] # toggle for debugging
Pkg.activate(; temp = true, io)
Pkg.develop(; path = "$(escape_string(pkgdir(Plots)))", io)
Pkg.add("UnicodePlots"; io) # checked by Plots
using Plots
res = @testset "Prefs" begin
@test_logs (:info, r".*Preferences") Plots.diagnostics(io)
@test backend() == Plots.UnicodePlotsBackend()
end
exit(res.n_passed == 2 ? 0 : 1)
""",
)
@test success(run(```$(Base.julia_cmd()) $script```))
end

is_pkgeval() || for be in CONCRETE_BACKENDS
(Sys.isapple() && be === :gaston) && continue # FIXME: hangs
@test_logs Plots.set_default_backend!(be) # test the absence of warnings
@test Base.compilecache(Base.module_keys[Plots]) isa String # test default precompilation
end

Plots.set_default_backend!() # clear `Preferences` key
end

@testset "UnicodePlots" begin
with(:unicodeplots) do
@test backend() == Plots.UnicodePlotsBackend()
Expand Down Expand Up @@ -262,6 +205,64 @@ end
end
end

@testset "Preferences" begin
Plots.set_default_backend!() # start with empty preferences

withenv("PLOTS_DEFAULT_BACKEND" => "invalid") do
@test_logs (:warn, r".*is not a supported backend") Plots.load_default_backend()
end
@test_logs (:warn, r".*is not a supported backend") backend(:invalid)

@test Plots.load_default_backend() == Plots.GRBackend()

withenv("PLOTS_DEFAULT_BACKEND" => "unicodeplots") do
@test_logs (:info, r".*environment variable") Plots.diagnostics(devnull)
@test Plots.load_default_backend() == Plots.UnicodePlotsBackend()
end

@test Plots.load_default_backend() == Plots.GRBackend()
@test Plots.backend_package_name() === :GR
@test Plots.backend_name() === :gr

@test_logs (:info, r".*fallback") Plots.diagnostics(devnull)

@test Plots.merge_with_base_supported([:annotations, :guide]) isa Set
@test Plots.CurrentBackend(:gr).sym === :gr

@test_logs (:warn, r".*is not compatible with") Plots.set_default_backend!(:invalid)

@testset "persistent backend" begin
# this test mimics a restart, which is needed after a preferences change
Plots.set_default_backend!(:unicodeplots)
script = tempname()
write(
script,
"""
ENV["PLOTS_PRECOMPILE"] = false
using Pkg, Test; io = (devnull, stdout)[1] # toggle for debugging
Pkg.activate(; temp = true, io)
Pkg.develop(; path = "$(escape_string(pkgdir(Plots)))", io)
Pkg.add("UnicodePlots"; io) # checked by Plots
using Plots
res = @testset "Prefs" begin
@test_logs (:info, r".*Preferences") Plots.diagnostics(io)
@test backend() == Plots.UnicodePlotsBackend()
end
exit(res.n_passed == 2 ? 0 : 1)
""",
)
@test success(run(```$(Base.julia_cmd()) $script```))
end

is_pkgeval() || for be in CONCRETE_BACKENDS
(Sys.isapple() && be === :gaston) && continue # FIXME: hangs
@test_logs Plots.set_default_backend!(be) # test the absence of warnings
@test Base.compilecache(Base.module_keys[Plots]) isa String # test default precompilation
end

Plots.set_default_backend!() # clear `Preferences` key
end

is_pkgeval() || @testset "PlotlyJS" begin
with(:plotlyjs) do
@test backend() == Plots.PlotlyJSBackend()
Expand All @@ -272,18 +273,18 @@ is_pkgeval() || @testset "PlotlyJS" begin
end

is_pkgeval() || @testset "Examples" begin
if Sys.islinux()
callback(m, pkgname, i) = begin
pl = m.Plots.current()
save_func = (; pgfplotsx = m.Plots.pdf, unicodeplots = m.Plots.txt) # fastest `savefig` for each backend
fn = Base.invokelatest(
get(save_func, pkgname, m.Plots.png),
pl,
tempname() * ref_name(i),
)
@test filesize(fn) > 1_000
end
for be in CONCRETE_BACKENDS
callback(m, pkgname, i) = begin
pl = m.Plots.current()
save_func = (; pgfplotsx = m.Plots.pdf, unicodeplots = m.Plots.txt) # fastest `savefig` for each backend
fn = Base.invokelatest(
get(save_func, pkgname, m.Plots.png),
pl,
tempname() * ref_name(i),
)
@test filesize(fn) > 1_000
end
Sys.islinux() && for be in CONCRETE_BACKENDS
withenv("JULIA_PYTHONCALL_EXE" => "@PyCall", "JULIA_CONDAPKG_BACKEND" => "Null") do
skip = vcat(Plots._backend_skips[be], blacklist)
Plots.test_examples(be; skip, callback, disp = is_ci(), strict = true) # `ci` display for coverage
closeall()
Expand Down

0 comments on commit 4b6595c

Please sign in to comment.