Skip to content

Commit

Permalink
use InverseFunctions to invert functions
Browse files Browse the repository at this point in the history
Both at once: simplify the code and support much more functions
  • Loading branch information
aplavin committed Sep 6, 2024
1 parent 8617742 commit d542a02
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ GridLayoutBase = "3955a311-db13-416c-9275-1d80ed98e5e9"
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
IntervalSets = "8197267c-284f-5f27-9208-e0e47529a953"
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"
Isoband = "f1662d9f-8043-43de-a69a-05efc1cc6ff4"
KernelDensity = "5ab0869b-81aa-558d-bb23-cbf5423bbe9b"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
Expand Down
2 changes: 2 additions & 0 deletions src/Makie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ using Base.Iterators: repeated, drop
import Base: getindex, setindex!, push!, append!, parent, get, get!, delete!, haskey
using Observables: listeners, to_value, notify

import InverseFunctions

using MakieCore: SceneLike, MakieScreen, ScenePlot, AbstractScene, AbstractPlot, Transformable, Attributes, Plot, Theme, Plot
using MakieCore: Arrows, Heatmap, Image, Lines, LineSegments, Mesh, MeshScatter, Poly, Scatter, Surface, Text, Volume, Wireframe
using MakieCore: ConversionTrait, NoConversion, PointBased, GridBased, VertexGrid, CellGrid, ImageLike, VolumeLike
Expand Down
11 changes: 4 additions & 7 deletions src/layouting/transformation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -417,15 +417,12 @@ function Symlog10(lo, hi)
return ReversibleScale(forward, inverse; limits=(0.0f0, 3.0f0), name=:Symlog10)
end

inverse_transform(::typeof(identity)) = identity
inverse_transform(::typeof(log10)) = exp10
inverse_transform(::typeof(log2)) = exp2
inverse_transform(::typeof(log)) = exp
inverse_transform(::typeof(sqrt)) = x -> x ^ 2
function inverse_transform(f)
f⁻¹ = InverseFunctions.inverse(f)
return f⁻¹ isa InverseFunctions.NoInverse ? nothing : f⁻¹ # nothing is for backwards compatibility

Check warning on line 422 in src/layouting/transformation.jl

View check run for this annotation

Codecov / codecov/patch

src/layouting/transformation.jl#L420-L422

Added lines #L420 - L422 were not covered by tests
end
inverse_transform(F::Tuple) = map(inverse_transform, F)
inverse_transform(::typeof(logit)) = logistic
inverse_transform(s::ReversibleScale) = s.inverse
inverse_transform(::Any) = nothing

function is_identity_transform(t)
return t === identity || t isa Tuple && all(x-> x === identity, t)
Expand Down
1 change: 1 addition & 0 deletions test/makielayout.jl
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ end
@testset "ReversibleScale" begin
@test ReversibleScale(identity).inverse === identity
@test ReversibleScale(log).inverse === exp
@test ReversibleScale(cbrt).inverse(2) == 8
@test_throws ArgumentError ReversibleScale(x -> log10(x)) # missing inverse scale
@test_throws ArgumentError ReversibleScale(sqrt, exp10) # incorrect inverse scale
end
Expand Down

0 comments on commit d542a02

Please sign in to comment.