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 floating point equality comparison in layouts #4972

Merged
merged 3 commits into from
Aug 23, 2024
Merged
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
5 changes: 5 additions & 0 deletions .zenodo.json
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,11 @@
"name": "Syver Døving Agdestein",
"orcid": "0000-0002-1589-2916",
"type": "Other"
},
{
"affiliation": "The Alan Turing Institute",
"name": "Penelope Yong",
"type": "Other"
}
],
"upload_type": "software"
Expand Down
19 changes: 7 additions & 12 deletions src/layouts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,23 +216,18 @@ function GridLayout(
kw...,
)
# Check the values for heights and widths if values are provided
all_between_one(xs) = all(x -> 0 < x < 1, xs)
if heights !== nothing
if sum(heights) != 1
error("The sum of heights must be 1!")
end
if all(x -> 0 < x < 1, heights) == false
error("Values for heights must be in the range (0, 1)!")
end
sum(heights) ≈ 1 || error("The heights provided ($(heights)) must sum to 1.")
all_between_one(heights) ||
error("The heights provided ($(heights)) must be in the range (0, 1).")
else
heights = zeros(dims[1])
end
if widths !== nothing
if sum(widths) != 1
error("The sum of widths must be 1!")
end
if all(x -> 0 < x < 1, widths) == false
error("Values for widths must be in the range (0, 1)!")
end
sum(widths) ≈ 1 || error("The widths provided ($(widths)) must sum to 1.")
all_between_one(widths) ||
error("The widths provided ($(widths)) must be in the range (0, 1).")
else
widths = zeros(dims[2])
end
Expand Down
11 changes: 11 additions & 0 deletions test/test_layouts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ end
@test_throws ErrorException plot(map(_ -> plot(1:2), 1:5)...; layout = grid(2, 2))
end

@testset "Allowed grid widths/heights" begin
@test_nowarn grid(2, 1, heights = [0.5, 0.5])
@test_nowarn grid(4, 1, heights = [0.3, 0.3, 0.3, 0.1])
@test_nowarn grid(1, 2, widths = [0.5, 0.5])
@test_nowarn grid(1, 4, widths = [0.3, 0.3, 0.3, 0.1])
@test_throws ErrorException grid(2, 1, heights = [0.5, 0.4])
@test_throws ErrorException grid(4, 1, heights = [1.5, -0.5])
@test_throws ErrorException grid(1, 2, widths = [0.5, 0.4])
@test_throws ErrorException grid(1, 4, widths = [1.5, -0.5])
end

@testset "Invalid viewport" begin
# github.com/JuliaPlots/Plots.jl/issues/2804
pl = plot(1, layout = (10, 2))
Expand Down
Loading