Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix hexbin corner case - reorganize test files #3506

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 9 additions & 28 deletions src/stats/hexbin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ Plots a heatmap with hexagonal bins for the observations `xs` and `ys`.
strokecolor=:black)
end

function spacings_offsets_nbins(bins::Tuple{Int,Int}, cellsize::Nothing, xmi, xma, ymi, yma)
function spacings_offsets_nbins(bins::Tuple{Int,Int}, cellsize::Nothing, xmi::T, xma::T, ymi::T, yma::T) where T
any(<(2), bins) && error("Minimum number of bins in one direction is 2, got $bins.")
x_diff = xma - xmi
y_diff = yma - ymi
x_diff = max(eps(float(T)), xma - xmi)
y_diff = max(eps(float(T)), yma - ymi)

xspacing, yspacing = (x_diff, y_diff) ./ (bins .- 1)
return xspacing, yspacing, xmi, ymi, bins...
Expand Down Expand Up @@ -70,7 +70,6 @@ function data_limits(hb::Hexbin)
ms = 2 .* fn(hb.plots[1].markersize[])
nw = widths(bb) .+ (ms..., 0.0f0)
no = bb.origin .- ((ms ./ 2.0f0)..., 0.0f0)

return Rect3f(no, nw)
end

Expand All @@ -91,25 +90,20 @@ function plot!(hb::Hexbin{<:Tuple{<:AbstractVector{<:Point2}}})

isempty(xy) && return

sqrt3 = sqrt(3)

# enclose data in limits
_expand((lo, hi)) = prevfloat(lo), nextfloat(hi)

xmi, xma = _expand(extrema((p[1] for p in xy)))
ymi, yma = _expand(extrema((p[2] for p in xy)))

x_diff = xma - xmi
y_diff = yma - ymi

xspacing, yspacing, xoff, yoff, nbinsx, nbinsy =
spacings_offsets_nbins(bins, cellsize, xmi, xma, ymi, yma)

ysize = yspacing / 3 * 4
ry = ysize / 2

xsize = xspacing * 2
rx = xsize / sqrt3
rx = xsize / √3

d = Dict{Tuple{Int,Int}, Float64}()

Expand All @@ -126,11 +120,7 @@ function plot!(hb::Hexbin{<:Tuple{<:AbstractVector{<:Point2}}})
d1 = ((_x - nx)^2 + (yweight * (_y - ny))^2)
d2 = ((_x - nxs)^2 + (yweight * (_y - nys))^2)

is_grid1 = d1 < d2
t-bltg marked this conversation as resolved.
Show resolved Hide resolved

# _xy = is_grid1 ? (nx, ny) : (nxs, nys)

id = if is_grid1
id = if (is_grid1 = d1 < d2)
(
cld(dvx, 2),
iseven(dvy) ? dvy : dvy+1
Expand All @@ -142,7 +132,7 @@ function plot!(hb::Hexbin{<:Tuple{<:AbstractVector{<:Point2}}})
)
end

d[id] = get(d, id, 0) + (get_weight(weights, i))
d[id] = get(d, id, 0.0) + get_weight(weights, i)
i += 1
end

Expand Down Expand Up @@ -187,11 +177,7 @@ function plot!(hb::Hexbin{<:Tuple{<:AbstractVector{<:Point2}}})
# and every cell has only 1 entry, then we set the minimum to 0 so we do not get
# a singular colorrange error down the line.
if mi == ma
if ma == 0
(0, 1)
else
(0, ma)
end
(0, ma == 0 ? one(ma) : ma)
else
(mi, ma)
end
Expand Down Expand Up @@ -220,13 +206,8 @@ function plot!(hb::Hexbin{<:Tuple{<:AbstractVector{<:Point2}}})
strokecolor=hb.strokecolor)
end

function center_value(dv, spacing, offset, is_grid1)
if is_grid1
offset + spacing * (dv + isodd(dv))
else
offset + spacing * (dv + iseven(dv))
end
end
center_value(dv, spacing, offset, is_grid1) =
offset + spacing * (dv + is_grid1 ? isodd(dv) : iseven(dv))

function nearest_center(val, spacing, offset)
dv = Int(fld(val - offset, spacing))
Expand Down
5 changes: 2 additions & 3 deletions test/bezier.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Makie, Test

# nice reference: https://www.nan.fyi/svg-paths
@testset "BezierPath construction" begin
@test_nowarn BezierPath("m 0,9 L 0,5138 0,9 z")
@test_broken BezierPath("m 0,1e-5 L 0,5138 0,9 z") isa BezierPath
@test_broken BezierPath("m 0,1e-5 L 0,5138 0,9 z") isa BezierPath # `Parsing e is not implemented`
@test_nowarn BezierPath("M 100,100 C 100,200 200,100 200,200 z")
@test_broken BezierPath("M 100,100 Q 50,150,100,100 z") isa BezierPath
@test_broken BezierPath("M 100,100 Q 50,150,100,100 z") isa BezierPath # `Parsing Q is not implemented`
@test_broken BezierPath("M 3.0 10.0 A 10.0 7.5 0.0 0.0 0.0 20.0 15.0 z") isa BezierPath
end
2 changes: 1 addition & 1 deletion test/boundingboxes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ end
@test bb.origin ≈ Point3f(-0.2)
@test bb.widths ≈ Vec3f(2.4)

fig, ax, p = volume(rand(5, 5, 5))
fig, ax, p = Makie.volume(rand(5, 5, 5))
bb = boundingbox(p)
@test bb.origin ≈ Point3f(0)
@test bb.widths ≈ Vec3f(5)
Expand Down
8 changes: 4 additions & 4 deletions test/conversions.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using Makie:
import Makie:
NoConversion,
convert_arguments,
conversion_trait,
convert_single_argument,
to_vertices,
categorical_colors
categorical_colors,
check_line_pattern,
line_diff_pattern

@testset "Conversions" begin
# NoConversion
Expand Down Expand Up @@ -131,8 +133,6 @@ end
end
end

using Makie: check_line_pattern, line_diff_pattern

@testset "Linetype" begin
@test isnothing(check_line_pattern("-."))
@test isnothing(check_line_pattern("--"))
Expand Down
3 changes: 1 addition & 2 deletions test/events.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Makie: MouseButtonEvent, KeyEvent, Figure, Textbox
using Makie: Not, And, Or
import Makie: MouseButtonEvent, KeyEvent, Figure, Textbox, Not, And, Or
using InteractiveUtils

# rudimentary equality for tests
Expand Down
21 changes: 21 additions & 0 deletions test/issues.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@testset "Reported issues" begin

@testset "Volume errors if data is not a cube (#659)" begin
fig, ax, vplot = Makie.volume(1:8, 1:8, 1:10, rand(8, 8, 10))
lims = Makie.data_limits(vplot)
lo, hi = extrema(lims)
@test all(lo .<= 1)
@test all(hi .>= (8,8,10))
end

@testset "Hexbin singleton (#3357)" begin
# degenerate case with singleton 0
hexbin([0, 0], [1, 2])
hexbin([1, 2], [0, 0])

# degenerate case with singleton 1
hexbin([1, 1], [1, 2])
hexbin([1, 2], [1, 1])
end

end
File renamed without changes.
47 changes: 20 additions & 27 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,31 @@ using Makie.GeometryBasics
using Makie.PlotUtils
using Makie.FileIO
using Makie.IntervalSets
using GeometryBasics: Pyramid

using Makie: volume
using LinearAlgebra

@testset "Unit tests" begin
@testset "#659 Volume errors if data is not a cube" begin
Copy link
Collaborator Author

@t-bltg t-bltg Dec 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this test is moved to an added issues.jl file to keep runtests.jl minimal.

fig, ax, vplot = volume(1:8, 1:8, 1:10, rand(8, 8, 10))
lims = Makie.data_limits(vplot)
lo, hi = extrema(lims)
@test all(lo .<= 1)
@test all(hi .>= (8,8,10))
end

include("barplot.jl")
include("bezier.jl")
include("boundingboxes.jl")
include("conversions.jl")
include("deprecated.jl")
include("specapi.jl")
include("primitives.jl")
include("events.jl")
include("figures.jl")
include("hist.jl")
include("issues.jl")
include("liftmacro.jl")
include("makielayout.jl")
include("pipeline.jl")
include("polaraxis.jl")
include("primitives.jl")
include("projection_math.jl")
include("quaternions.jl")
include("ray_casting.jl")
include("record.jl")
include("scenes.jl")
include("conversions.jl")
include("quaternions.jl")
include("projection_math.jl")
include("liftmacro.jl")
include("makielayout.jl")
include("figures.jl")
include("transformations.jl")
include("events.jl")
include("specapi.jl")
# include("statistical_tests.jl") # FIXME: untested and broken ?
include("text.jl")
include("boundingboxes.jl")
include("ray_casting.jl")
include("PolarAxis.jl")
include("barplot.jl")
include("bezier.jl")
include("hist.jl")
include("transformations.jl")
# include("zoom_pan.jl") # FIXME: untested and broken ?
end
8 changes: 4 additions & 4 deletions test/statistical_tests.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using StatsBase: Histogram
using Makie, StatsBase
import Distributions
using KernelDensity

using Random: seed!
using GeometryBasics: Rect2f
import GeometryBasics: Rect2f
import StatsBase: Histogram
import Distributions
import Random: seed!

seed!(0)

Expand Down
3 changes: 1 addition & 2 deletions test/transformations.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Makie: PointTrans, apply_transform
using LinearAlgebra
import Makie: PointTrans, apply_transform

function xyz_boundingbox(trans, points)
bb_ref = Base.RefValue(Rect3f())
Expand Down
Loading