jw3126 / UnitfulRecipes.jl

Plots.jl recipes for Unitful.jl arrays
MIT License
37 stars 10 forks source link

New Recipes to support StatsPlots.jl #76

Open jmtlawrie opened 2 years ago

jmtlawrie commented 2 years ago

Hello!

It would be nice to be able to use UnitfulRecipes.jl with the functions (e.g. groupedbar() and kde()) from StatsPlots.jl.

Given that StatsPlots.jl is very closely related to Plots.jl (according to its own documentation), I wonder if this is something that is not too tricky to do?

If this has been posted to the wrong place (perhaps it belongs to StatsPlots instead?) then please let me know!

Many thanks! J


Minimum working example, based on this example from StatsPlots.jl documentation

gr()

Basic example without using Unitful.jl or UnitfulRecipes.jl

x = randn(1024); y = randn(1024); marginalkde(x, x+y)

using Unitful

x_u = x . u"m"; y1_u= y . u"m"

using UnitfulRecipes

marginalkde(x_u, x_u+y_u) # This errors

____
#### Error message
- Please note: it seems that the same error message is produced regardless of whether or not `using UnitfulRecipes` has already been executed in the REPL

julia> marginalkde(x_u, x_u+y_u) ERROR: MethodError: no method matching kde(::Tuple{Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}, Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}}) Closest candidates are: kde(::AbstractVector{T} where T<:Real; bandwidth, kernel, npoints, boundary, weights) at ~/.julia/packages/KernelDensity/bNBAQ/src/univariate.jl:169 kde(::AbstractVector{T} where T<:Real, ::Distributions.UnivariateDistribution; boundary, npoints, weights) at ~/.julia/packages/KernelDensity/bNBAQ/src/univariate.jl:155 kde(::AbstractVector{T} where T<:Real, ::R; bandwidth, kernel, weights) where R<:AbstractRange at ~/.julia/packages/KernelDensity/bNBAQ/src/univariate.jl:162 ... Stacktrace: [1] macro expansion @ ~/.julia/packages/StatsPlots/LlHWB/src/marginalkde.jl:24 [inlined] [2] apply_recipe(plotattributes::AbstractDict{Symbol, Any}, kc::StatsPlots.MarginalKDE) @ StatsPlots ~/.julia/packages/RecipesBase/qpxEX/src/RecipesBase.jl:289 [3] _process_userrecipes!(plt::Any, plotattributes::Any, args::Any) @ RecipesPipeline ~/.julia/packages/RecipesPipeline/OXGmH/src/user_recipe.jl:36 [4] recipe_pipeline!(plt::Any, plotattributes::Any, args::Any) @ RecipesPipeline ~/.julia/packages/RecipesPipeline/OXGmH/src/RecipesPipeline.jl:70 [5] _plot!(plt::Plots.Plot, plotattributes::Any, args::Any) @ Plots ~/.julia/packages/Plots/lW9ll/src/plot.jl:209 [6] plot(args::Any; kw::Base.Pairs{Symbol, V, Tuple{Vararg{Symbol, N}}, NamedTuple{names, T}} where {V, N, names, T<:Tuple{Vararg{Any, N}}}) @ Plots ~/.julia/packages/Plots/lW9ll/src/plot.jl:91 [7] plot @ ~/.julia/packages/Plots/lW9ll/src/plot.jl:82 [inlined] [8] marginalkde(::Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}, ::Vararg{Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}}; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}) @ StatsPlots ~/.julia/packages/RecipesBase/qpxEX/src/RecipesBase.jl:364 [9] marginalkde(::Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}, ::Vararg{Vector{Quantity{Float64, 𝐋, Unitful.FreeUnits{(m,), 𝐋, nothing}}}}) @ StatsPlots ~/.julia/packages/RecipesBase/qpxEX/src/RecipesBase.jl:364 [10] top-level scope @ REPL[13]:1

____

julia> versioninfo() Julia Version 1.8.0 Commit 5544a0fab76 (2022-08-17 13:38 UTC) Platform Info: OS: macOS (x86_64-apple-darwin21.4.0) CPU: 4 Γ— Intel(R) Core(TM) i5-5250U CPU @ 1.60GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-13.0.1 (ORCJIT, broadwell) Threads: 1 on 2 virtual cores Environment: JULIA_EDITOR = code JULIA_NUM_THREADS =

(@v1.8) pkg> st # manually edited to only show relevant packages Status ~/.julia/environments/v1.8/Project.toml [f3b207a7] StatsPlots v0.15.1 [1986cc42] Unitful v1.11.0 [42071c24] UnitfulRecipes v1.5.3

jw3126 commented 2 years ago

I am not really familiar with StatsPlots.jl, but if you want to tackle this issue I am happy to help with the UnitfulRecipes.jl side. My guess is that kde first performs a calculation (estimating kernel density) and then does some plotting stuff. And the calculation is probably not compatible with units? Then one approach would be to make the calculation compatible with Unitful.jl I think and afterwards unitful plotting magically works hopefully.

gustaphe commented 2 years ago

The issue is that KernelDensity.kde() only accepts Real, while Unitful.Quantity isa Number but not a Real.

One solution to this and several similar bugs is a pretty invasive rework of Unitful with

struct RealQuantity{T<:Real, D, U} <: Real
    val::T
end
RealQuantity(q::Q) where {Q<:Unitful.Quantity} = RealQuantity{Q.parameters...}(q.val)

Another is to make kde more permissive, using isreal rather than dispatching with Real if that's important.

jmtlawrie commented 2 years ago

I am not really familiar with StatsPlots.jl, but if you want to tackle this issue I am happy to help with the UnitfulRecipes.jl side.

  • Thank you for the kind offer. Step one for me is to learn how generic Plots.jl recipes work, then UnitfulRecipes, so it might be a while yet πŸ˜„

My guess is that kde first performs a calculation (estimating kernel density) and then does some plotting stuff. And the calculation is probably not compatible with units?

  • I think this is also the case. Does this mean that the Plots.jl plot types all have methods which permit Unitful data?

Then one approach would be to make the calculation compatible with Unitful.jl I think and afterwards unitful plotting magically works hopefully.

  • If this is the right thing to do, I think this will be a thing to take to StatsPlots.jl in the first instance, and then return to UnitfulRecipes if needed afterward.
jmtlawrie commented 2 years ago

One solution to this and several similar bugs is a pretty invasive rework of Unitful

  • Thanks for your feedback!
  • Good to know, though such a step is definitely beyond me (I'm an engineer rather than programmer) and I am unsure if anyone else is interested in using the StatsPlots functionality with Unitful data directly.
  • Is this something that should be mentioned to the people who maintain Unitful, do you think?

In the meantime, I was able to plot what I was trying to, by, for example, doing a groupedbar() plot with data without any units, then I needed to plot a hline() on to this plot, which works very nicely with UnitfulRecipes, and lets the axes be scaled and the units be put onto the axes automatically with unitformat=:square or similar.

I intend to learn more about how this works in the future, at which point perhaps I can contribute something 🀞

gustaphe commented 2 years ago

Does this mean that the Plots.jl plot types all have methods which permit Unitful data?

It's rather that through our recipes, we are telling Plots how to convert Unitful data to unitless data before any plotting is done. I can't quite figure out why no such transformation is applied for StatsPlots.