Skip to content

Commit

Permalink
Fix violin indexing error (#4682)
Browse files Browse the repository at this point in the history
* safeguard against idx = 0 (and nothing)

* include the actual fix

* update changelog [skip ci]

* actually another CI run would be nice
  • Loading branch information
ffreyer authored Jan 2, 2025
1 parent d6fdf25 commit 7a95dbc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## [Unreleased]

- Fixed indexing error edge case in violin median code [#4682](https://github.com/MakieOrg/Makie.jl/pull/4682)

## [0.22.0] - 2024-12-12

- Updated to GeometryBasics 0.5: [GeometryBasics#173](https://github.com/JuliaGeometry/GeometryBasics.jl/pull/173), [GeometryBasics#219](https://github.com/JuliaGeometry/GeometryBasics.jl/pull/219) [#4319](https://github.com/MakieOrg/Makie.jl/pull/4319)
Expand Down
6 changes: 3 additions & 3 deletions src/stats/violin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ function plot!(plot::Violin)
if show_median
# interpolate median bounds between corresponding points
xm = spec.median
ip = findfirst(>(xm), spec.kde.x)
ym₋, ym₊ = spec.kde.density[ip-1], spec.kde.density[ip]
xm₋, xm₊ = spec.kde.x[ip-1], spec.kde.x[ip]
ip = Base.max(2, something(findfirst(>(xm), spec.kde.x), length(spec.kde.x)))
ym₋, ym₊ = spec.kde.density[Base.max(1, ip-1)], spec.kde.density[ip]
xm₋, xm₊ = spec.kde.x[Base.max(1, ip-1)], spec.kde.x[ip]
ym = (xm * (ym₊ - ym₋) + xm₊ * ym₋ - xm₋ * ym₊) / (xm₊ - xm₋)
median_left = point_func(spec.side == 1 ? spec.x : spec.x - ym * scale, xm)
median_right = point_func(spec.side == -1 ? spec.x : spec.x + ym * scale, xm)
Expand Down
6 changes: 6 additions & 0 deletions test/statistical_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,10 @@ end
@test p2.plots[2] isa LineSegments
@test p2.plots[2][:color][] === :white
@test p2.plots[2][:visible][] === :false

# median edge case #4675
@test violin(fill(1, 1000), push!(fill(0, 999), 1), show_median=true, datalimits=(-0.001,Inf)) !== nothing
# And some others
@test violin(fill(1, 1000),fill(0, 1000), show_median=true) !== nothing
@test violin([1], [1], show_median=true) !== nothing
end

0 comments on commit 7a95dbc

Please sign in to comment.