Skip to content

Commit

Permalink
enhance coverage - fix throw syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Feb 6, 2022
1 parent 7cc0d5b commit 8c0539d
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 40 deletions.
8 changes: 4 additions & 4 deletions src/canvas/lookupcanvas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ function CreateLookupCanvas(
visible,
pixel_width,
pixel_height,
Float64(origin_x),
Float64(origin_y),
Float64(width),
Float64(height),
float(origin_x),
float(origin_y),
float(width),
float(height),
xscale,
yscale,
)
Expand Down
13 changes: 8 additions & 5 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ function char_marker(marker::MarkerType)::Char
if marker isa Symbol
get(MARKERS, marker, MARKERS[:circle])
else
length(marker) == 1 || throw(error("`marker` keyword has a non unit length"))
length(marker) == 1 ||
throw(ArgumentError("`marker` keyword has a non unit length"))
marker[1]
end
end
Expand All @@ -208,6 +209,8 @@ function transform_name(tr, basename = "")
string(basename, " [", name, "]")
end

as_float(x) = eltype(x) <: AbstractFloat ? x : float.(x)

roundable(num::Number) = isinteger(num) & (typemin(Int) <= num < typemax(Int))
compact_repr(num::Number) = repr(num, context = :compact => true)

Expand Down Expand Up @@ -257,22 +260,22 @@ function plotting_range(xmin, xmax)
diff = xmax - xmin
xmax = round_up_tick(xmax, diff)
xmin = round_down_tick(xmin, diff)
Float64(xmin), Float64(xmax)
float(xmin), float(xmax)
end

function plotting_range_narrow(xmin, xmax)
diff = xmax - xmin
xmax = round_up_subtick(xmax, diff)
xmin = round_down_subtick(xmin, diff)
Float64(xmin), Float64(xmax)
float(xmin), float(xmax)
end

extend_limits(vec, limits) = extend_limits(vec, limits, :identity)

function extend_limits(vec, limits, scale::Union{Symbol,Function})
mi, ma = map(Float64, extrema(limits))
mi, ma = as_float(extrema(limits))
if mi == 0 && ma == 0
mi, ma = map(Float64, extrema(vec))
mi, ma = as_float(extrema(vec))
end
if ma - mi == 0
ma = mi + 1
Expand Down
12 changes: 6 additions & 6 deletions src/interface/boxplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ function boxplot(

mean_x = (min_x + max_x) / 2
min_x_str = compact_repr(
roundable(min_x) ? round(Int, Float64(min_x), RoundNearestTiesUp) : min_x,
roundable(min_x) ? round(Int, float(min_x), RoundNearestTiesUp) : min_x,
)
mean_x_str = compact_repr(
roundable(mean_x) ? round(Int, Float64(mean_x), RoundNearestTiesUp) : mean_x,
roundable(mean_x) ? round(Int, float(mean_x), RoundNearestTiesUp) : mean_x,
)
max_x_str = compact_repr(
roundable(max_x) ? round(Int, Float64(max_x), RoundNearestTiesUp) : max_x,
roundable(max_x) ? round(Int, float(max_x), RoundNearestTiesUp) : max_x,
)
label!(plot, :bl, min_x_str, color = :light_black)
label!(plot, :b, mean_x_str, color = :light_black)
Expand Down Expand Up @@ -129,13 +129,13 @@ function boxplot!(
max_x = plot.graphics.max_x
mean_x = (min_x + max_x) / 2
min_x_str = compact_repr(
roundable(min_x) ? round(Int, Float64(min_x), RoundNearestTiesUp) : min_x,
roundable(min_x) ? round(Int, float(min_x), RoundNearestTiesUp) : min_x,
)
mean_x_str = compact_repr(
roundable(mean_x) ? round(Int, Float64(mean_x), RoundNearestTiesUp) : mean_x,
roundable(mean_x) ? round(Int, float(mean_x), RoundNearestTiesUp) : mean_x,
)
max_x_str = compact_repr(
roundable(max_x) ? round(Int, Float64(max_x), RoundNearestTiesUp) : max_x,
roundable(max_x) ? round(Int, float(max_x), RoundNearestTiesUp) : max_x,
)
label!(plot, :bl, min_x_str)
label!(plot, :b, mean_x_str)
Expand Down
2 changes: 1 addition & 1 deletion src/interface/contourplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function contourplot!(
name == "" || label!(plot, :r, string(name))

plot.colormap = callback = colormap_callback(colormap)
mA, MA = NaNMath.extrema(A)
mA, MA = NaNMath.extrema(as_float(A))

for cl in Contour.levels(Contour.contours(y, x, A, levels))
color = callback(Contour.level(cl), mA, MA)
Expand Down
4 changes: 2 additions & 2 deletions src/interface/heatmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ function heatmap(
end
kw = (; kw..., colorbar = colorbar)

xs = length(X) > 0 ? [X[1], X[end]] : Float64[0, 0]
ys = length(Y) > 0 ? [Y[1], Y[end]] : Float64[0, 0]
xs = length(X) > 0 ? [X[1], X[end]] : [0.0, 0.0]
ys = length(Y) > 0 ? [Y[1], Y[end]] : [0.0, 0.0]
plot = Plot(
xs,
ys,
Expand Down
2 changes: 1 addition & 1 deletion src/interface/isosurface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ $(arguments(
(
V = "`Array` (volume) of interest for which a surface is extracted, or `Function` evaluated as `f(x, y, z)`",
isovalue = "chosen surface isovalue",
legacy = "use the legacy Marching Cubes algorithm instead of the topology enhanced algorithm",
cull = "cull (hide) back faces",
legacy = "use the legacy Marching Cubes algorithm instead of the topology enhanced algorithm",
centroid = "display triangulation centroid instead of triangle vertices",
); add = (Z_DESCRIPTION..., PROJ_DESCRIPTION..., :x, :y, :z, :canvas), remove = (:blend, :grid)
))
Expand Down
4 changes: 2 additions & 2 deletions src/interface/lineplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ function lineplot(
ylabel = "f(x)",
kw...,
)
y = Float64[f(i) for i in x]
y = [float(f(i)) for i in x]
name = name == "" ? string(nameof(f), "(x)") : name
plot = lineplot(x, y; name = name, xlabel = xlabel, ylabel = ylabel, kw...)
end
Expand All @@ -196,7 +196,7 @@ end
lineplot(f::Function; kw...) = lineplot(f, -10, 10; kw...)

function lineplot!(plot::Plot{<:Canvas}, f::Function, x::AbstractVector; name = "", kw...)
y = Float64[f(i) for i in x]
y = [float(f(i)) for i in x]
name = name == "" ? string(nameof(f), "(x)") : name
lineplot!(plot, x, y; name = name, kw...)
end
Expand Down
2 changes: 1 addition & 1 deletion src/interface/scatterplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function scatterplot!(
if marker (:pixel, :auto)
points!(plot, x, y, z, color)
else
z === nothing || throw(error("unsupported scatter with 3D data"))
z === nothing || throw(ArgumentError("unsupported scatter with 3D data"))
for (xi, yi, mi, ci) in zip(x, y, iterable(marker), iterable(color))
annotate!(plot, xi, yi, char_marker(mi); color = ci)
end
Expand Down
16 changes: 9 additions & 7 deletions src/interface/surfaceplot.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
surfaceplot(x, y, A; kw...)
Draws a 3D surface plot on a new canvas. Values can be masked using `NaN`s. Pass
Draws a 3D surface plot on a new canvas. Values can be masked using `NaN`s.
# Usage
Expand All @@ -13,6 +13,8 @@ $(arguments(
(
A = "`Matrix` of surface heights, or `Function` evaluated as `f(x, y)`",
lines = "use `lineplot` instead of `scatterplot`",
zscale = "scale z (`:identity`, `:aspect` or arbitrary scale function)",
zlim = "scale z in the given range (`(0, 0)` stands for automatic)"
); add = (Z_DESCRIPTION..., PROJ_DESCRIPTION..., :x, :y, :canvas), remove = (:blend, :grid)
))
Expand Down Expand Up @@ -73,24 +75,24 @@ function surfaceplot(

mx, Mx = extrema(x)
my, My = extrema(y)
mh, Mh = NaNMath.extrema(H)
mh, Mh = NaNMath.extrema(as_float(H))

if zscale === :identity
mz, Mz = mh, Mh
Z = H
elseif zscale === :aspect
if zscale === :aspect || zlim != (0, 0)
mz, Mz = if zlim == (0, 0)
min(mx, my), max(Mx, My)
else
zlim
end
Z = (H .- mh) .* ((Mz - mz) / (Mh - mh)) .+ mz
elseif zscale === :identity
mz, Mz = mh, Mh
Z = H
elseif zscale isa Function
# e.g. plotting a slice, pass zscale = x -> slice_pos
mz, Mz = zscale(mh), zscale(Mh)
Z = zscale.(H)
else
throw(error("zscale=$zscale not understood"))
throw(ArgumentError("zscale=$zscale not understood"))
end

length(X) == length(Y) == length(Z) == length(H) ||
Expand Down
6 changes: 3 additions & 3 deletions src/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ function Plot(

if projection !== nothing # 3D
(xscale !== :identity || yscale !== :identity) &&
throw(error("xscale or yscale are unsupported in 3D"))
throw(ArgumentError("xscale or yscale are unsupported in 3D"))

projection isa Symbol && (projection = MVP(x, y, z; kw...))

Expand All @@ -211,12 +211,12 @@ function Plot(
mx, Mx = if xlim == (0, 0)
-1.0, 1.0
else
float.(xlim)
as_float(xlim)
end
my, My = if ylim == (0, 0)
-1.0, 1.0
else
float.(ylim)
as_float(ylim)
end

grid = blend = false
Expand Down
8 changes: 4 additions & 4 deletions src/volume.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ end
Computes data center, minimum and maximum points, and cube diagonal.
"""
function ctr_len_diag(x, y, z)
mx, Mx = NaNMath.extrema(float.(x))
my, My = NaNMath.extrema(float.(y))
mz, Mz = NaNMath.extrema(float.(z))
mx, Mx = NaNMath.extrema(as_float(x))
my, My = NaNMath.extrema(as_float(y))
mz, Mz = NaNMath.extrema(as_float(z))

lx = Mx - mx
ly = My - my
Expand Down Expand Up @@ -187,7 +187,7 @@ function view_matrix(center, distance, elevation, azimuth, up)
elseif up_axis === :z
2
else
throw(error("$up not understood"))
throw(ArgumentError("up=$up not understood"))
end
# we support :x -> +x, :px -> +x or :mx -> -x
up_vector = circshift(
Expand Down
4 changes: 2 additions & 2 deletions test/tst_common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
@testset "types" begin
@test UnicodePlots.plotting_range(0, 1) === (0.0, 1.0)
@test UnicodePlots.plotting_range(0.0, 1) === (0.0, 1.0)
@test UnicodePlots.plotting_range(0, 1.0f0) === (0.0, 1.0)
@test UnicodePlots.plotting_range(0, 1.0f0) === (0.0, 1.0f0)
@test UnicodePlots.plotting_range(0x0, 0x1) === (0.0, 1.0)
end

Expand All @@ -18,7 +18,7 @@ end
@testset "types" begin
@test UnicodePlots.plotting_range_narrow(0, 1) === (0.0, 1.0)
@test UnicodePlots.plotting_range_narrow(0.0, 1) === (0.0, 1.0)
@test UnicodePlots.plotting_range_narrow(0, 1.0f0) === (0.0, 1.0)
@test UnicodePlots.plotting_range_narrow(0, 1.0f0) === (0.0, 1.0f0)
@test UnicodePlots.plotting_range_narrow(0x0, 0x1) === (0.0, 1.0)
end

Expand Down
4 changes: 3 additions & 1 deletion test/tst_surfaceplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

p = surfaceplot(-8:0.5:8, -8:0.5:8, sombrero, zscale = h -> h / 2)
test_ref("references/surfaceplot/sombrero_zscale.txt", @show_col(p))

@test_throws ArgumentError surfaceplot([1 2; 3 4], zscale = :not_supported)
end

@testset "single color - no colormap" begin
Expand All @@ -17,6 +19,6 @@ end
end

@testset "matrix" begin
p = surfaceplot(collect(1:10) * collect(.1:.1:1)')
p = surfaceplot(collect(1:10) * collect(0.1:0.1:1)')
test_ref("references/surfaceplot/matrix.txt", @show_col(p))
end
12 changes: 11 additions & 1 deletion test/tst_volume.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
]
@test dir == [1, 0, 0]

@test_throws ArgumentError UnicodePlots.view_matrix([0, 0, 0], 1, 0, 0, :not_supported)

@test UnicodePlots.scale_4x4([1, 1, 1]) I
@test UnicodePlots.translate_4x4([0, 0, 0]) I
@test UnicodePlots.rotd_x(0) I
Expand All @@ -55,7 +57,15 @@ end

x, y, z = ellipsoid()
for (plane, az, el) in [("yz", 0, 0), ("xz", -90, 0), ("xy", -90, 90)]
p = Plot(x, y, z, projection = :orthographic, elevation = el, azimuth = az, title = "plane=$plane")
p = Plot(
x,
y,
z,
projection = :orthographic,
elevation = el,
azimuth = az,
title = "plane=$plane",
)
scatterplot!(p, x, y, z)

test_ref("references/volume/ellipsoid_$plane.txt", @show_col(p))
Expand Down

0 comments on commit 8c0539d

Please sign in to comment.