Skip to content

Commit 043992c

Browse files
authored
Unitful and weakdep handling for v2 extensions (#5143)
* Catch some fixes from #5098 and #5127 for v2 * Copypaste whitespace formatting * EOF whitespace format * Load latexify for Unitful tests * Add improvements from #5158 * PlotsBase, not Plots * Fully excise UnitfulLatexify, and drop Latexify as a dep for Unitful * no need for brackets * Fix unitful latexify tests * Make Latexify and LaTeXStrings full deps of PlotsBase, remove hack * Add Colors and Contour as full deps of PlotsBase, so PGFPlotsXExt triggers with no extra imports * Allow Colors, Contour, LaTeXStrings, Latexify to be stale deps for PlotsBase * Add a test to improve coverage * Another test suggested by coverage * do that test right, whoops * Drop "WEAKDEPS" for triggering extensions
1 parent c4132f2 commit 043992c

File tree

12 files changed

+57
-48
lines changed

12 files changed

+57
-48
lines changed

PlotsBase/Project.toml

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ version = "0.1"
44

55
[deps]
66
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
7+
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
8+
Contour = "d38c429a-6771-53c6-b99e-75d170b6e991"
79
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
810
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
911
FFMPEG = "c87230d0-a227-11e9-1b43-d7ebe4e7570a"
1012
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
1113
JLFzf = "1019f520-868f-41f5-a6de-eb00f4b6a39c"
1214
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
15+
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
16+
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
1317
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1418
Measures = "442fdcdd-2543-5da2-b0f3-8c86c306513e"
1519
NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
@@ -40,24 +44,19 @@ RecipesBase = {path = "../RecipesBase"}
4044
RecipesPipeline = {path = "../RecipesPipeline"}
4145

4246
[weakdeps]
43-
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
44-
Contour = "d38c429a-6771-53c6-b99e-75d170b6e991"
4547
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
4648
GR = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71"
4749
Gaston = "4b11ee91-296f-5714-9832-002c20994614"
4850
GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326"
4951
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
5052
IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
5153
ImageInTerminal = "d8c32880-2388-543b-8c61-d9f865259254"
52-
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
53-
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
5454
PGFPlotsX = "8314cec4-20b6-5062-9cdb-752b83310925"
5555
PlotlyJS = "f0f68f2c-4968-5e81-91da-67840de0976a"
5656
PlotlyKaleido = "f2990250-8cf9-495f-b13a-cce12b45703c"
5757
PythonPlot = "274fc56d-3b97-40fa-a1cd-1b4a50311bf9"
5858
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
5959
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
60-
UnitfulLatexify = "45397f5d-5981-4c77-b2b3-fc36d6e9b728"
6160

6261
[extensions]
6362
FileIOExt = "FileIO"
@@ -67,12 +66,12 @@ GeometryBasicsExt = "GeometryBasics"
6766
HDF5Ext = "HDF5"
6867
IJuliaExt = "IJulia"
6968
ImageInTerminalExt = "ImageInTerminal"
70-
PGFPlotsXExt = ["Colors", "Contour", "Latexify", "LaTeXStrings", "PGFPlotsX"]
69+
PGFPlotsXExt = "PGFPlotsX"
7170
PlotlyJSExt = "PlotlyJS"
7271
PlotlyKaleidoExt = "PlotlyKaleido"
7372
PythonPlotExt = "PythonPlot"
7473
UnicodePlotsExt = "UnicodePlots"
75-
UnitfulExt = ["Latexify", "LaTeXStrings", "UnitfulLatexify", "Unitful"]
74+
UnitfulExt = "Unitful"
7675

7776
[compat]
7877
Base64 = "1"
@@ -88,7 +87,7 @@ HDF5 = "0.17"
8887
JLFzf = "0.1"
8988
JSON = "0.21, 1"
9089
LaTeXStrings = "1"
91-
Latexify = "0.16"
90+
Latexify = "0.16.8"
9291
Measures = "0.3"
9392
NaNMath = "1"
9493
PGFPlotsX = "1"
@@ -114,7 +113,7 @@ StatsBase = "0.34"
114113
TableOperations = "1"
115114
UnicodeFun = "0.4"
116115
UnicodePlots = "3"
117-
UnitfulLatexify = "1"
116+
Unitful = "1.25"
118117
Unzip = "0.2"
119118
UUIDs = "1"
120119
julia = "1.10"

PlotsBase/ext/UnitfulExt.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,10 @@ import Unitful:
1818
Gain,
1919
uconvert
2020
import PlotsBase: PlotsBase, @recipe, PlotText, Subplot, AVec, AMat, Axis
21+
using Latexify: latexify
22+
using LaTeXStrings
2123
import PlotsBase.Axes: format_unit_label
2224
import RecipesBase
23-
import LaTeXStrings: LaTeXString
24-
import Latexify
25-
26-
using UnitfulLatexify
2725

2826
const MissingOrQuantity = Union{Missing, <:Quantity, <:LogScaled}
2927

@@ -255,7 +253,7 @@ function append_cbar_unit_if_needed!(attr, label::Nothing, u)
255253
return attr[:colorbar_title] = UnitfulString("", u)
256254
end
257255
return attr[:colorbar_title] = if PlotsBase.backend_name() :pgfplotsx
258-
UnitfulString(LaTeXString(Latexify.latexify(u)), u)
256+
UnitfulString(LaTeXString(latexify(u)), u)
259257
else
260258
UnitfulString(string(u), u)
261259
end
@@ -267,7 +265,7 @@ function append_cbar_unit_if_needed!(attr, label::S, u) where {S <: AbstractStri
267265
LaTeXString(
268266
format_unit_label(
269267
label,
270-
Latexify.latexify(u),
268+
latexify(u),
271269
get(attr, :zunitformat, :round),
272270
),
273271
),

PlotsBase/src/Axes.jl

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export Axis, Extrema, tickfont, guidefont, widen_factor, scale_inverse_scale_fun
44
export sort_3d_axes, axes_letters, process_axis_arg!, has_ticks, get_axis, get_guide
55

66
import ..PlotsBase: PlotsBase, Subplot, DefaultsDict
7+
using Latexify: latexify
78

89
using ..RecipesPipeline
910
using ..Commons
@@ -347,22 +348,20 @@ function PlotsBase.attr!(axis::Axis, args...; kw...)
347348
return axis
348349
end
349350

350-
function get_guide(axis::Axis)
351-
if isnothing(axis[:unit]) || axis[:unitformat] :nounit
352-
return axis[:guide]
351+
get_guide(axis::Axis) = if isnothing(axis[:guide])
352+
""
353+
elseif isnothing(axis[:unit]) || axis[:unitformat] :nounit
354+
axis[:guide]
355+
else
356+
unit = if axis[:unitformat] isa Function
357+
axis[:unit]
358+
elseif PlotsBase.backend_name() :pgfplotsx
359+
latexify(axis[:unit])
353360
else
354-
ustr = if PlotsBase.backend_name() :pgfplotsx
355-
pgext = Base.get_extension(PlotsBase, :PGFPlotsXExt)
356-
isnothing(pgext) && error("PGFPlotsX extension not loaded. Problems")
357-
pgext.Latexify.latexify(axis[:unit])
358-
else
359-
string(axis[:unit])
360-
end
361-
if isempty(axis[:guide])
362-
return ustr
363-
end
364-
return format_unit_label(axis[:guide], ustr, axis[:unitformat])
361+
string(axis[:unit])
365362
end
363+
isempty(string(axis[:guide])) && return unit
364+
format_unit_label(axis[:guide], unit, axis[:unitformat])
366365
end
367366

368367
# Keyword options for unit formats

PlotsBase/src/PlotsBase.jl

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,6 @@ if isdefined(Base, :Experimental) && isdefined(Base.Experimental, Symbol("@max_m
77
@eval Base.Experimental.@max_methods 1
88
end
99

10-
# multiple weakdeps triggers (keep in sync with Project.toml !)
11-
const WEAKDEPS = Expr(
12-
:block,
13-
:(import UnitfulLatexify),
14-
:(import LaTeXStrings),
15-
:(import Latexify),
16-
:(import Contour),
17-
:(import Colors),
18-
)
19-
2010
using Base.Meta
2111

2212
import PrecompileTools

PlotsBase/test/Project.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4040
TestImages = "5e47fb64-e119-507b-a336-dd2b206d9990"
4141
UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228"
4242
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
43-
UnitfulLatexify = "45397f5d-5981-4c77-b2b3-fc36d6e9b728"
4443
VisualRegressionTests = "34922c18-7c2a-561c-bac1-01e79b2c4c92"

PlotsBase/test/runtests.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ get!(ENV, "MPLBACKEND", "agg")
1414
get!(ENV, "PLOTSBASE_PLOTLYJS_UNSAFE_ELECTRON", "true")
1515

1616
using PlotsBase
17-
eval(PlotsBase.WEAKDEPS)
1817

1918
# initialize all backends
2019
for pkg in TEST_PACKAGES

PlotsBase/test/test_axes.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,20 @@ end
281281
end
282282
end
283283
end
284+
285+
@testset "axis guides (labels)" begin
286+
yguide(pl, idx = length(pl.subplots)) = PlotsBase.get_guide(pl.subplots[idx].attr[:yaxis])
287+
288+
@test yguide(plot(1:3, ylabel = "hello")) == "hello"
289+
@test yguide(plot(1:3, ylabel = L"hello")) == L"hello"
290+
@test yguide(plot(1:3, ylabel = :hello)) == :hello
291+
292+
@test yguide(plot(1:3, yunit = 1)) == "1"
293+
@test yguide(plot(1:3, yunit = 1, ylabel = :hello)) == "hello (1)"
294+
@test yguide(plot(1:3, yunit = 1, ylabel = :hello, unitformat = :square)) == "hello [1]"
295+
@test yguide(plot(1:3, yunit = 1, ylabel = nothing, unitformat = :square)) == ""
296+
@test yguide(plot(1:3, yunit = 1, ylabel = L"hello")) == L"hello" * " (1)"
297+
298+
uf = (l, u) -> Symbol(l, "_symb_", u)
299+
@test yguide(plot(1:3, yunit = 1, ylabel = :hello, unitformat = uf)) == :hello_symb_1
300+
end

PlotsBase/test/test_preferences.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ end
7272
write(
7373
script,
7474
"""
75-
$(PlotsBase.WEAKDEPS)
76-
7775
import $pkg
7876
using Test, PlotsBase
7977

PlotsBase/test/test_quality.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# :CondaPkg stale deps show up when running CI
44
Aqua.test_all(
55
PlotsBase;
6-
stale_deps = (; ignore = [:CondaPkg]),
6+
stale_deps = (; ignore = [:Colors, :Contour, :LaTeXStrings, :Latexify, :CondaPkg]),
77
persistent_tasks = false,
88
ambiguities = false,
99
deps_compat = false, # FIXME: fails `CondaPkg`

PlotsBase/test/test_unitful.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using PlotsBase, Test
22
using Unitful
3+
using Latexify
34
using Unitful: m, cm, s, DimensionError
45
# Some helper functions to access the subplot labels and the series inside each test plot
56
xguide(pl, idx = length(pl.subplots)) = PlotsBase.get_guide(pl.subplots[idx].attr[:xaxis])
@@ -292,6 +293,17 @@ end
292293
@test contourf(x, y, z) isa PlotsBase.Plot
293294
end
294295
296+
@testset "latexify as unitformat" begin
297+
y = rand(10) * u"m^-1"
298+
Latexify.set_default(labelformat = :slash)
299+
@test yguide(plot(y, ylabel = "hello", unitformat = latexify)) == "\$hello\\;\\left/\\;\\mathrm{m}^{-1}\\right.\$"
300+
301+
uf = (l, u) -> l * " (" * latexify(u) * ")"
302+
@test yguide(plot(y, ylabel = "hello", unitformat = uf)) == "hello (\$\\mathrm{m}^{-1}\$)"
303+
Latexify.set_default(labelformat = :square)
304+
@test yguide(plot(y, ylabel = "hello", unitformat = latexify)) == "\$hello\\;\\left[\\mathrm{m}^{-1}\\right]\$"
305+
end
306+
295307
@testset "colorbar title" begin
296308
x, y = (1:0.01:2) * m, (1:0.02:2) * s
297309
z = x' ./ y
@@ -301,6 +313,8 @@ end
301313
@test ctitle(pl) ["km hr^-1", "km hr⁻¹"]
302314
pl = heatmap(x, y, z, zunit = u"cm/s", zunitformat = :square, colorbar_title = "v")
303315
@test ctitle(pl) ["v [cm s^-1]", "v [cm s⁻¹]"]
316+
pl = heatmap(x, y, z, zunit = u"cm/s", zunitformat = :nounit, colorbar_title = nothing)
317+
@test ctitle(pl) == ""
304318
end
305319

306320
@testset "twinx (#4750)" begin

0 commit comments

Comments
 (0)