Skip to content

Commit

Permalink
various updates - improve test suite (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg authored Dec 25, 2023
1 parent b5539fb commit b653837
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 189 deletions.
1 change: 0 additions & 1 deletion .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
always_for_in = true
import_to_using = false
align_pair_arrow = true
align_assignment = true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
matrix:
version:
- '1.6' # LTS (lowest supported `julia` version declared in `Project.toml`)
- '1.8' # latest stable
- '1' # latest stable
os: [ubuntu-latest, windows-latest, macos-latest]
arch: [x64, x86]
exclude:
Expand Down
11 changes: 7 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,20 @@ PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[compat]
PrecompileTools = "1"
ColorSchemes = "3.19"
ColorSchemes = "≥3.19"
Colors = "0.12"
Dates = "<0.0.1, 1"
Printf = "<0.0.1, 1"
Random = "<0.0.1, 1"
Reexport = "1"
PrecompileTools = "1"
Statistics = "<0.0.1, 1"
julia = "1.6"

[extras]
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Pkg", "StableRNGs", "Statistics", "Test"]
test = ["Pkg", "StableRNGs", "Test"]
2 changes: 1 addition & 1 deletion src/PlotUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const _default_colorscheme = generate_colorscheme()

if VERSION v"1.8.0"
@compile_workload begin
for T in (Int, Float64)
for T (Int, Float64)
optimize_ticks(-one(T), one(T))
optimize_ticks(-one(T), one(T); k_min = 2, k_max = 10)
adapted_grid(sin, (-one(T), one(T)))
Expand Down
22 changes: 11 additions & 11 deletions src/adapted_grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function adapted_grid(
# Wiggle interior points a bit to prevent aliasing and other degenerate cases
rng = MersenneTwister(1337)
rand_factor = 0.05
for i in 2:(length(xs) - 1)
for i 2:(length(xs) - 1)
xs[i] += 2rand_factor * (rand(rng) - 0.5) * (xs[i + 1] - xs[i - 1])
end

Expand Down Expand Up @@ -67,7 +67,7 @@ function adapted_grid(
# Guard against division by zero later
(f_range == 0 || !isfinite(f_range)) && (f_range = one(f_range))
# Skip first and last interval
for interval in 1:n_intervals
for interval 1:n_intervals
p = 2interval
if n_tot_refinements[interval] max_recursions
# Skip intervals that have been refined too much
Expand All @@ -77,7 +77,7 @@ function adapted_grid(
else
tot_w = 0.0
# Do a small convolution
for (q, w) in ((-1, 0.25), (0, 0.5), (1, 0.25))
for (q, w) ((-1, 0.25), (0, 0.5), (1, 0.25))
interval == 1 && q == -1 && continue
interval == n_intervals && q == 1 && continue
tot_w += w
Expand All @@ -99,7 +99,7 @@ function adapted_grid(
end
end
# Approximate end intervals as being the same curvature as those next to it.
# This avoids computing the function in the end points
# This avoids computing the function the end points
curvatures[1] = curvatures[2]
active[1] = active[2]
curvatures[end] = curvatures[end - 1]
Expand All @@ -121,10 +121,10 @@ function adapted_grid(
new_fs = zeros(eltype(fs), n_points + n_new_points)
new_tot_refinements = zeros(Int, n_intervals + n_intervals_to_refine)
k = kk = 0
for i in 1:n_points
if iseven(i) # This is a point in an interval
for i 1:n_points
if iseven(i) # This is a point an interval
interval = i ÷ 2
if interval in intervals_to_refine
if interval intervals_to_refine
kk += 1
new_tot_refinements[interval - 1 + kk] = n_tot_refinements[interval] + 1
new_tot_refinements[interval + kk] = n_tot_refinements[interval] + 1
Expand Down Expand Up @@ -170,10 +170,10 @@ Tries to call the callable `F` (which must accept one real argument)
and determine when it executes without error.
If `F` is an `AbstractArray`, it will find the first element of `vec`
for which all callables in `F` execute.
for which all callables `F` execute.
"""
function tryrange(F, vec)
for v in vec
for v vec
try
tmp = F(v)
return v
Expand All @@ -186,8 +186,8 @@ end
# try some intervals over which the function may be defined

function tryrange(F::AbstractArray, vec)
rets = [tryrange(f, vec) for f in F] # get the preferred for each
rets = [tryrange(f, vec) for f F] # get the preferred for each
maxind = maximum(indexin(rets, vec)) # get the last attempt that succeeded (most likely to fit all)
rets .= [tryrange(f, vec[maxind:maxind]) for f in F] # ensure that all functions compute there
rets .= [tryrange(f, vec[maxind:maxind]) for f F] # ensure that all functions compute there
rets[1]
end
8 changes: 4 additions & 4 deletions src/color_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ function getpctrange(n::Integer)
n > 0 || error()
n == 1 && return zeros(1)
zs = [0.0, 1.0]
for i in 3:n
for i 3:n
sorted = sort(zs)
diffs = diff(sorted)
widestj = 0
widest = 0.0
for (j, d) in enumerate(diffs)
for (j, d) enumerate(diffs)
if d > widest
widest = d
widestj = j
Expand All @@ -61,7 +61,7 @@ function get_zvalues(n::Integer)
offsets = getpctrange(ceil(Int, n / 4) + 1) / 4
offsets = vcat(offsets[1], offsets[3:end])
zvalues = Float64[]
for offset in offsets
for offset offsets
append!(zvalues, offset .+ [0.0, 0.5, 0.25, 0.75])
end
vcat(zvalues[1], 1.0, zvalues[2:(n - 1)])
Expand All @@ -85,7 +85,7 @@ isdark(c::Colorant)::Bool = lightness_level(c) < 0.5
islight(c::Colorant)::Bool = !isdark(c)

Base.convert(::Type{RGB}, h::Unsigned) =
RGB([(x & 0x0000FF) / 0xFF for x in (h >> 16, h >> 8, h)]...)
RGB([(x & 0x0000FF) / 0xFF for x (h >> 16, h >> 8, h)]...)

make255(x)::Int = round(Int, 255 * x)

Expand Down
16 changes: 8 additions & 8 deletions src/colors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ plot_color(b::Val{false}) = invisible()
plot_color(b::Bool) = plot_color(Val(b))
plot_color(::Nothing) = invisible()
plot_color(c::Colorant) = convert(RGBA{Float64}, c)
# plot_color(cs::AbstractVector) = RGBA{Float64}[plot_color(c) for c in cs]
# plot_color(cs::AbstractVector) = RGBA{Float64}[plot_color(c) for c cs]
# plot_color(cs::AbstractArray) = map(plot_color, cs)

# no alpha override
Expand All @@ -29,16 +29,16 @@ plot_color(s::Symbol, α::Number) = (

function plot_color(cs::AbstractArray)
a = Array{RGBA{Float64}}(undef, size(cs)...)
for i in eachindex(cs)
for i eachindex(cs)
a[i] = plot_color(cs[i])
end
a
end

# plot_color(cs::AbstractVector, α::Number) = RGBA{Float64}[plot_color(c,α) for c in cs]
# plot_color(cs::AbstractVector, α::Number) = RGBA{Float64}[plot_color(c,α) for c cs]
function plot_color(cs::AbstractArray, α::Number)
a = Array{RGBA{Float64}}(undef, size(cs)...)
for i in eachindex(cs)
for i eachindex(cs)
a[i] = plot_color(cs[i], α)
end
a
Expand All @@ -50,26 +50,26 @@ end
# function plot_color(zs::AbstractVector{T}) where T<:Number
# grad = cgrad()
# zmin, zmax = extrema(zs)
# RGBA{Float64}[grad[(z-zmin)/(zmax-zmin)] for z in zs]
# RGBA{Float64}[grad[(z-zmin)/(zmax-zmin)] for z zs]
# end
function plot_color(zs::AbstractArray{T}) where {T<:Number}
grad = cgrad()
zmin, zmax = extrema(zs[isfinite.(zs)])
a = Array{RGBA{Float64}}(undef, size(zs)...)
for i in eachindex(zs)
for i eachindex(zs)
a[i] = grad[(zs[i] - zmin) / (zmax - zmin)]
end
a
end

# function plot_color(zs::AbstractVector{T}, α::Number) where T<:Number
# cs = plot_color(zs)
# RGBA{Float64}[RGBA{Float64}(convert(RGB, c), α) for c in cs]
# RGBA{Float64}[RGBA{Float64}(convert(RGB, c), α) for c cs]
# end
function plot_color(zs::AbstractArray{T}, α::Number) where {T<:Number}
cs = plot_color(zs)
a = Array{RGBA{Float64}}(undef, size(zs)...)
for i in eachindex(zs)
for i eachindex(zs)
a[i] = RGBA{Float64}(convert(RGB, cs[i]), α)
end
a
Expand Down
58 changes: 30 additions & 28 deletions src/colorschemes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract type ColorGradient <: AbstractColorList end
Base.getindex(cg::ColorGradient, x::Union{AbstractFloat,AbstractVector{<:AbstractFloat}}) =
get(cg, x)
function Base.get(cg::ColorGradient, v::AbstractArray, rangescale = (0.0, 1.0))
rangescale === :extrema && (rangescale = extrema(v))
rangescale :extrema && (rangescale = extrema(v))
map(x -> get(cg, x, rangescale), v)
end

Expand Down Expand Up @@ -53,7 +53,7 @@ Base.show(io::IO, m::MIME"image/svg+xml", cg::ContinuousColorGradient) =

function sample_color(cg::ContinuousColorGradient, x::AbstractFloat)
c, v = cg.colors, cg.values
if (index = findfirst(==(x), v)) === nothing
if (index = findfirst(==(x), v)) nothing
nm1 = length(v) - 1
i = min(nm1, findlast(<(x), v))
r = (x - v[i]) / (v[i + 1] - v[i])
Expand Down Expand Up @@ -81,7 +81,7 @@ function ColorSchemes.getinverse(cg::ContinuousColorGradient, c)
alpha(c) == 0 && return NaN
z = getinverse(to_rgb(get_colorscheme(cg)), to_rgb(c))
cr = get_range(cg.colors)
if (index = findfirst(==(z), cr)) !== nothing
if (index = findfirst(==(z), cr)) nothing
cg.values[index]
else
i = min(length(cr) - 1, findlast(<(z), cr))
Expand All @@ -91,15 +91,15 @@ end

function prepare_continuous_cgrad_colors(c, v)
v = sort(unique(clamp.(v, 0, 1)))
0 in v || pushfirst!(v, 0)
1 in v || push!(v, 1)
0 v || pushfirst!(v, 0)
1 v || push!(v, 1)
nv = length(v)
nc = length(c)
c, v = if nc != nv
value_range = get_range(nv)
color_range = get_range(nc)
values = [0.0]
for i in 2:nv
for i 2:nv
inds = findall(x -> value_range[i - 1] < x < value_range[i], color_range)
isempty(inds) || append!(
values,
Expand Down Expand Up @@ -161,10 +161,12 @@ end

function prepare_categorical_cgrad_colors(c, v)
v = sort(unique(clamp.(v, 0, 1)))
0 in v || pushfirst!(v, 0)
1 in v || push!(v, 1)
c = ColorScheme(plot_color(c[get_range(length(v) - 1)]))
c, v
0 v || pushfirst!(v, 0)
1 v || push!(v, 1)
colors = map(c[get_range(length(v) - 1)]) do col
RGBA(RGB(col), clamp(alpha(col), 0, 1))
end
ColorScheme(plot_color(colors)), v
end

## cgrad
Expand Down Expand Up @@ -193,22 +195,22 @@ function cgrad(
rev = false,
alpha = nothing,
)
if categorical !== nothing && categorical
if categorical nothing && categorical
colors, values = prepare_categorical_cgrad_colors(colors, values)
end

if alpha !== nothing
if alpha nothing
rgbs = convert.(RGB, colors.colors)
colors = ColorScheme(RGBA.(rgbs, alpha))
end
rev && (colors = reverse(colors))
values = if scale in (:log, :log10) || scale isa typeof(log10)
values = if scale (:log, :log10) || scale log10
log10.(ColorSchemes.remap(values, 0, 1, 1, 10))
elseif scale === :log2 || scale isa typeof(log2)
elseif scale :log2 || scale log2
log2.(ColorSchemes.remap(values, 0, 1, 1, 2))
elseif scale === :ln || scale isa typeof(log)
elseif scale :ln || scale log
log.(ColorSchemes.remap(values, 0, 1, 1, ℯ))
elseif scale in (:exp, :exp10) || scale isa typeof(exp10) || scale isa typeof(exp)
elseif scale (:exp, :exp10) || scale exp10 || scale exp
ColorSchemes.remap(exp10.(values), 1, 10, 0, 1)
elseif scale isa Function
v = scale.(values)
Expand All @@ -217,7 +219,7 @@ function cgrad(
values
end

if categorical !== nothing && categorical
if categorical nothing && categorical
CategoricalColorGradient(colors, values)
else
ContinuousColorGradient(colors, values)
Expand All @@ -230,12 +232,12 @@ function cgrad(
categorical = nothing,
kwargs...,
)
values = get_range(n + (categorical !== nothing))
values = get_range(n + (categorical nothing))
cgrad(colors, values; categorical = categorical, kwargs...)
end

function cgrad(colors, args...; kwargs...)
colors === :default && (colors = :inferno)
colors :default && (colors = :inferno)
cgrad(get_colorscheme(colors), args...; kwargs...)
end

Expand All @@ -244,8 +246,8 @@ cgrad(; kw...) = cgrad(DEFAULT_COLOR_GRADIENT[]; kw...)
default_cgrad(cg; kw...) = DEFAULT_COLOR_GRADIENT[] = cgrad(cg; kw...)

function get_rangescale(rangescale)
rangescale === :clamp && return (0.0, 1.0)
rangescale === :extrema && return extrema(x)
rangescale :clamp && return (0.0, 1.0)
rangescale :extrema && return extrema(x)
(rangescale isa NTuple{2,Number}) || error(
"rangescale ($rangescale) not supported, should be :clamp, :extrema or tuple (minVal, maxVal). Got $(rangescale).",
)
Expand All @@ -272,7 +274,7 @@ If `rev` is `true` colors are reversed.
"""
function palette(cs; rev = false, alpha = nothing)
cs = get_colorscheme(cs)
if alpha !== nothing
if alpha nothing
rgbs = convert.(RGB, cs.colors)
cs = ColorScheme(RGBA.(rgbs, alpha))
end
Expand All @@ -288,15 +290,15 @@ end

## Utils

get_range(n::Int) = range(0, stop = 1, length = n)
get_range(n::Int) = range(0, stop = n == 1 ? 0 : 1, length = n)
get_range(cs) = get_range(length(cs))

get_colorscheme(v::AbstractVector{<:Colorant}) = ColorScheme(v)
get_colorscheme(v::AbstractVector) = ColorScheme(parse.(Colorant, v))
function get_colorscheme(sym::Symbol)
haskey(MISC_COLORSCHEMES, sym) && return MISC_COLORSCHEMES[sym]
sym = get(COLORSCHEME_ALIASES, sym, sym)
if sym === :default || sym === :auto
if sym :default || sym :auto
_default_colorscheme
elseif haskey(ColorSchemes.colorschemes, sym)
ColorSchemes.colorschemes[sym]
Expand All @@ -310,7 +312,7 @@ get_colorscheme(cs::ColorScheme) = cs

function cvec(cs, n = 10; kw...)
cg = cgrad(cs; kw...)
RGBA{Float64}[cg[z] for z in get_range(n)]
RGBA{Float64}[cg[z] for z get_range(n)]
end

color_list(c) = get_colorscheme(c).colors
Expand All @@ -329,9 +331,9 @@ rgba_string(cg::T) where {T<:Union{ColorScheme,ColorGradient,ColorPalette}} =
rgba_string(cg[1])

is_colorscheme(sym) =
sym in keys(ColorSchemes.colorschemes) ||
sym in keys(COLORSCHEME_ALIASES) ||
sym in keys(MISC_COLORSCHEMES)
sym keys(ColorSchemes.colorschemes) ||
sym keys(COLORSCHEME_ALIASES) ||
sym keys(MISC_COLORSCHEMES)

const DEFAULT_COLOR_GRADIENT = Ref(cgrad(ColorSchemes.colorschemes[:inferno]))

Expand Down
Loading

0 comments on commit b653837

Please sign in to comment.