From d542a026b5b0ee8d12f424914dac88c17f159d14 Mon Sep 17 00:00:00 2001 From: Alexander Plavin Date: Fri, 6 Sep 2024 12:07:05 +0200 Subject: [PATCH] use InverseFunctions to invert functions Both at once: simplify the code and support much more functions --- Project.toml | 1 + src/Makie.jl | 2 ++ src/layouting/transformation.jl | 11 ++++------- test/makielayout.jl | 1 + 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Project.toml b/Project.toml index ee3eb0aa82f..c4132099110 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/Makie.jl b/src/Makie.jl index 2a1545280ca..6d6a6452068 100644 --- a/src/Makie.jl +++ b/src/Makie.jl @@ -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 diff --git a/src/layouting/transformation.jl b/src/layouting/transformation.jl index e4b96dbabb8..9aa2f8b9534 100644 --- a/src/layouting/transformation.jl +++ b/src/layouting/transformation.jl @@ -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 +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) diff --git a/test/makielayout.jl b/test/makielayout.jl index bff143e1c89..6030c5a0dac 100644 --- a/test/makielayout.jl +++ b/test/makielayout.jl @@ -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