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

Adapt to Meshes v0.44 #69

Merged
merged 9 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fis issues with colorbar and units -> strip them out of the attribute…
… and colorrange
  • Loading branch information
VEZY committed Jun 12, 2024
commit 4de65e7c397655fab2c817d4fe0ff0abebc22b93
1 change: 1 addition & 0 deletions ext/PlantGeomMakie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import MultiScaleTreeGraph
import MultiScaleTreeGraph: get_attributes
import ColorSchemes: get, rainbow, colorschemes, ColorScheme
import UUIDs
import Unitful

MeshesMakieExt = Base.get_extension(Meshes, :MeshesMakieExt)

Expand Down
11 changes: 6 additions & 5 deletions ext/makie_recipes/colorbar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,20 @@ function PlantGeom.colorbar(parent, plotobject; kwargs...)

# Because we extend the `Viz` type, we need to check if the user has given a color range.
# If we defined our own e.g. `PlantViz` type, we could have defined a `colorrange` field in it directly.
if hasproperty(plotobject.attributes, :colorrange)

if hasproperty(plotobject.attributes, :colorrange) && (!isa(plotobject.attributes.colorrange, Observables.Observable) || plotobject.attributes.colorrange[] !== nothing)
if isa(plotobject.attributes.colorrange, Observables.Observable)
colorbar_limits = plotobject.attributes.colorrange
# colorbar_limits = Unitful.ustrip.(plotobject.attributes.colorrange[])
colorbar_limits = Makie.lift(x -> Unitful.ustrip.(x), plotobject.attributes.colorrange)
else
colorbar_limits = Observables.Observable(plotobject.attributes.colorrange)
end
else
# Get the attribute values without nothing values:
colorbar_limits = Makie.@lift PlantGeom.attribute_range($mtg, $color)
colorbar_limits = Makie.@lift PlantGeom.attribute_range($mtg, $color, ustrip=true)
end

colormap = Makie.lift(get_colormap, plotobject.attributes.colormap)
println(colorbar_limits)

Makie.Colorbar(
parent,
colormap=colormap,
Expand Down
2 changes: 1 addition & 1 deletion ext/makie_recipes/opf/plot_opf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ function plot_opf(colorant::Observables.Observable{AttributeColorant}, plot)
color_range = plot[:colorrange]
else
# Get the attribute values without nothing values:
color_range = Makie.@lift PlantGeom.attribute_range($opf, $colorant)
color_range = Makie.@lift PlantGeom.attribute_range($opf, $colorant, ustrip=true)
end

if hasproperty(plot, :index)
Expand Down
6 changes: 4 additions & 2 deletions src/colors/colors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,20 @@ get_color(1:2, 1:10, colormap = colorschemes[:viridis]) # returns RGB{N0f8}(0.26
get_color(1:2, 1:10, 1, colormap = colorschemes[:viridis]) # returns RGB{N0f8}(0.267004,0.00487433,0.329415)
"""
function get_color(var::T, range_var, index::Nothing=nothing; colormap=colorschemes[:viridis]) where {T<:AbstractArray}
range_var = Unitful.ustrip.(range_var)
x2 = (range_var[2] - range_var[1])
# get the color based on a colormap and the normalized attribute value
[get(colormap, (i - range_var[1]) / x2) for i in var]
[get(colormap, (i - range_var[1]) / x2) for i in Unitful.ustrip.(var)]
end

function get_color(var::T, range_var, index::I; colormap=colorschemes[:viridis]) where {T<:AbstractArray,I<:Integer}
get_color(var[index], range_var; colormap=colormap)
end

function get_color(var, range_var, index::I=1; colormap=colorschemes[:viridis]) where {I<:Integer}
range_var = Unitful.ustrip.(range_var)
# get the color based on a colormap and the normalized attribute value
get(colormap, (var - range_var[1]) / (range_var[2] - range_var[1]))
get(colormap, (Unitful.ustrip(var) - range_var[1]) / (range_var[2] - range_var[1]))
end


Expand Down
6 changes: 5 additions & 1 deletion src/opf/mtg_recipe_helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function mtg_coordinates_df!(mtg, attr=:YY; force=false)
DataFrame(mtg, unique([:XX, :YY, :ZZ, :XX_from, :YY_from, :ZZ_from, attr]))
end

function attribute_range(mtg, attr)
function attribute_range(mtg, attr; ustrip=false)
attr_name = attr_colorant_name(attr)
vals =
MultiScaleTreeGraph.descendants(
Expand All @@ -183,6 +183,10 @@ function attribute_range(mtg, attr)
range_val = (minimum(minimum.(vals_no_nothing)), maximum(maximum.(vals_no_nothing)))
end

if ustrip
return Unitful.ustrip.(range_val)
end

return range_val
end

Expand Down