Skip to content

Commit

Permalink
Merge pull request #40 from fredrikekre/fe/0.7
Browse files Browse the repository at this point in the history
fixes for julia 0.7
  • Loading branch information
daschw authored Jun 28, 2018
2 parents 1403d9a + 1ba3142 commit 61eaaef
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 52 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ os:
- linux
- osx
julia:
- 0.5
- 0.6
- 0.7
- nightly
notifications:
email: true
Expand Down
3 changes: 1 addition & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
julia 0.5

julia 0.7-alpha
Reexport
Colors
13 changes: 6 additions & 7 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.6/julia-0.6-latest-win64.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.7/julia-0.7-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.7/julia-0.7-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

matrix:
allow_failures:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.6/julia-0.6-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
# matrix:
# allow_failures:
# - JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
# - JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"

branches:
only:
Expand Down
2 changes: 2 additions & 0 deletions src/PlotUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ __precompile__()

module PlotUtils

using Dates
using Reexport
using Printf
@reexport using Colors
import Base: getindex

Expand Down
4 changes: 2 additions & 2 deletions src/adapted_grid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function adapted_grid(f, minmax::Tuple{Real, Real}; max_recursions = 7)
n_intervals = n_points ÷ 2
@assert isodd(n_points)

xs = collect(linspace(minmax[1], minmax[2], n_points))
xs = collect(range(minmax[1]; stop=minmax[2], length=n_points))
# Move the first and last interior points a bit closer to the end points
xs[2] = xs[1] + (xs[2] - xs[1]) * 0.25
xs[end-1] = xs[end] - (xs[end] - xs[end-1]) * 0.25
Expand All @@ -34,7 +34,7 @@ function adapted_grid(f, minmax::Tuple{Real, Real}; max_recursions = 7)
rng = MersenneTwister(1337)
rand_factor = 0.05
for i in 2:length(xs)-1
xs[i] += rand_factor * 2 * (rand(rng) - 0.5) * (xs[i+1] - xs[i-1])
xs[i] += rand_factor * 2 * (rand(rng) - 0.5) * (xs[i+1] - xs[i-1])
end

n_tot_refinements = zeros(Int, n_intervals)
Expand Down
28 changes: 14 additions & 14 deletions src/color_gradients.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

type ColorLibrary
mutable struct ColorLibrary
defaults::Dict{Symbol, Symbol}
lib::Dict{Symbol, Vector{RGBA{Float64}}}
ColorLibrary(defaults = Dict(:default => :sequential), lib = Dict{Symbol, Vector{RGBA{Float64}}}()) = new(defaults, lib)
Expand Down Expand Up @@ -43,7 +43,7 @@ end

getindex(cl::ColorLibrary, key::Symbol) = getgradient(key, cl)

function register_gradient_colors{C<:Colorant}(name::Symbol, colors::AbstractVector{C}, color_library::Symbol = :default)
function register_gradient_colors(name::Symbol, colors::AbstractVector{C}, color_library::Symbol = :default) where {C<:Colorant}
haskey(color_libraries, color_library) || register_color_library(color_library, ColorLibrary())
color_libraries[color_library].lib[name] = colors
end
Expand Down Expand Up @@ -103,7 +103,7 @@ cgradients(color_library::Symbol = _gradients[1]) = collect(keys(color_libraries


"Continuous gradient between values. Wraps a list of bounding colors and the values they represent."
immutable ColorGradient
struct ColorGradient
colors::Vector{RGBA{Float64}}
values::Vector{Float64}
end
Expand Down Expand Up @@ -173,7 +173,7 @@ cgrad_colors(grad::ColorGradient) = copy(grad.colors)
cgrad_colors(cs::Vector{RGBA{Float64}}) = cs
cgrad_colors(cs::AbstractVector) = RGBA{Float64}[plot_color(c) for c in cs]

function _color_list(arg, ::Void)
function _color_list(arg, ::Nothing)
cgrad_colors(arg)
end

Expand All @@ -194,10 +194,10 @@ function cgrad(arg, values; alpha = nothing)
values
else
# merge values into the default range, then recompute color list and return vals
# vals = merge(collect(linspace(0, 1, length(colors))), collect(values))
vals = sort(unique(vcat(linspace(0, 1, length(colors)), values)))
# vals = merge(collect(range(0; stop=1, length=length(colors))), collect(values))
vals = sort(unique(vcat(range(0; stop=1, length=length(colors)), values)))
grad = ColorGradient(colors)
colors = RGBA{Float64}[grad[z] for z in linspace(0, 1, length(vals))]
colors = RGBA{Float64}[grad[z] for z in range(0; stop=1, length=length(vals))]
vals
end

Expand All @@ -211,17 +211,17 @@ cgrad(arg::Symbol, cl::Symbol; kw...) = cgrad(cgrad_colors(arg, color_library =
function cgrad(arg; alpha = nothing, scale = :identity)
colors = _color_list(arg, alpha)
values = if scale in (:log, :log10)
log10(linspace(1,10,length(colors)))
log10(range(1; stop=10, length=length(colors)))
elseif scale == :log2
log2(linspace(1,2,length(colors)))
log2(range(1; stop=2, length=length(colors)))
elseif scale == :ln
log(linspace(1,pi,length(colors)))
log(range(1; stop=pi, length=length(colors)))
elseif scale in (:exp, :exp10)
(exp10(linspace(0,1,length(colors))) - 1) / 9
(exp10(range(0; stop=1, length=length(colors))) - 1) / 9
elseif isa(arg, ColorGradient)
arg.values
else
linspace(0, 1, length(colors))
range(0; stop=1, length=length(colors))
end

# construct and return the gradient
Expand All @@ -235,10 +235,10 @@ cgrad(; kw...) = cgrad(:default; kw...)


cvec(s::Symbol, n::Integer = 10; kw...) = cvec(cgrad(s; kw...), n)
cvec(grad::ColorGradient, n::Integer = 10; kw...) = RGBA{Float64}[grad[z] for z in linspace(0,1,n)]
cvec(grad::ColorGradient, n::Integer = 10; kw...) = RGBA{Float64}[grad[z] for z in range(0; stop=1, length=n)]

function sample_evenly(v::AbstractVector, n::Integer = length(v))
idx = Int[round(Int, x) for x in linspace(1, length(v), n)]
idx = Int[round(Int, x) for x in range(1; stop=length(v), length=n)]
v[idx]
end

Expand Down
4 changes: 2 additions & 2 deletions src/color_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function generate_colorgradient(bgcolor = plot_color(:white);
seed_colors,
lchoices=Float64[lightness],
cchoices=Float64[chroma],
hchoices=linspace(0, 340, 20)
hchoices=range(0; stop=340, length=20)
)[2:end]
gradient_from_list(colors)
end
Expand All @@ -70,7 +70,7 @@ function get_color_palette(palette, bgcolor::Colorant, numcolors::Integer)
RGBA{Float64}[grad[z] for z in zrng]
end

get_color_palette{C<:Colorant}(palette::Vector{C}, bgcolor::Colorant, numcolors::Integer) = palette
get_color_palette(palette::Vector{C}, bgcolor::Colorant, numcolors::Integer) where {C<:Colorant} = palette


# ----------------------------------------------------------------------------------
Expand Down
18 changes: 9 additions & 9 deletions src/colors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ invisible() = RGBA{Float64}(0.,0.,0.,0.)
plot_color(s::AbstractString) = parse(RGBA{Float64}, s)
plot_color(s::Symbol) = (iscgrad_symbol(s) ? cgrad(s) : parse(RGBA{Float64}, s))
plot_color(b::Bool) = b ? error("plot_color(true) not allowed.") : invisible()
plot_color(::Void) = invisible()
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::AbstractArray) = map(plot_color, cs)
plot_color(grad::ColorGradient) = grad

# no alpha override
plot_color(x, ::Void) = plot_color(x)
plot_color(x, ::Nothing) = plot_color(x)

# alpha override
plot_color(x, α::Number) = RGBA{Float64}(convert(RGB, plot_color(x)), α)
Expand All @@ -30,7 +30,7 @@ end

# plot_color(cs::AbstractVector, α::Number) = RGBA{Float64}[plot_color(c,α) for c in cs]
function plot_color(cs::AbstractArray, α::Number)
a = Array{RGBA{Float64}}(size(cs))
a = Array{RGBA{Float64}}(undef, size(cs))
for i in eachindex(cs)
a[i] = plot_color(cs[i], α)
end
Expand All @@ -41,28 +41,28 @@ end

# convenience conversions from numeric arrays to gradient values
# note: we need the first version because of dispatch
# function plot_color{T<:Number}(zs::AbstractVector{T})
# 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]
# end
function plot_color{T<:Number}(zs::AbstractArray{T})
function plot_color(zs::AbstractArray{T}) where T<:Number
grad = cgrad()
zmin, zmax = extrema(zs[isfinite.(zs)])
a = Array{RGBA{Float64}}(size(zs))
a = Array{RGBA{Float64}}(undef, size(zs))
for i in eachindex(zs)
a[i] = grad[(zs[i]-zmin)/(zmax-zmin)]
end
a
end

# function plot_color{T<:Number}(zs::AbstractVector{T}, α::Number)
# 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]
# end
function plot_color{T<:Number}(zs::AbstractArray{T}, α::Number)
function plot_color(zs::AbstractArray{T}, α::Number) where T<:Number
cs = plot_color(zs)
a = Array{RGBA{Float64}}(size(zs))
a = Array{RGBA{Float64}}(undef, size(zs))
for i in eachindex(zs)
a[i] = RGBA{Float64}(convert(RGB, cs[i]), α)
end
Expand Down
22 changes: 11 additions & 11 deletions src/ticks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Find the smallest order of magnitude that is larger than xspan This is a
# little opaque because I want to avoid assuming the log function is defined
# over typeof(xspan)
function bounding_order_of_magnitude{DT}(xspan::DT)
function bounding_order_of_magnitude(xspan::DT) where DT
one_dt = convert(DT, one(DT))

a = 1
Expand Down Expand Up @@ -53,39 +53,39 @@ optimize_ticks() = Any[]
# Returns:
# A Float64 vector containing tick marks.
#
function optimize_ticks{T}(x_min::T, x_max::T; extend_ticks::Bool=false,
function optimize_ticks(x_min::T, x_max::T; extend_ticks::Bool=false,
Q=[(1.0,1.0), (5.0, 0.9), (2.0, 0.7), (2.5, 0.5), (3.0, 0.2)],
k_min::Int=2, k_max::Int=10, k_ideal::Int=5,
granularity_weight::Float64=1/4, simplicity_weight::Float64=1/6,
coverage_weight::Float64=1/3, niceness_weight::Float64=1/4,
strict_span=true, span_buffer = nothing)
strict_span=true, span_buffer = nothing) where T

Qv = [(Float64(q[1]), Float64(q[2])) for q in Q]
optimize_ticks_typed(x_min, x_max, extend_ticks, Qv, k_min, k_max, k_ideal,
granularity_weight, simplicity_weight,
coverage_weight, niceness_weight, strict_span, span_buffer)
end

function optimize_ticks_typed{T}(x_min::T, x_max::T, extend_ticks,
function optimize_ticks_typed(x_min::T, x_max::T, extend_ticks,
Q::Vector{Tuple{Float64,Float64}}, k_min,
k_max, k_ideal,
granularity_weight::Float64, simplicity_weight::Float64,
coverage_weight::Float64, niceness_weight::Float64,
strict_span, span_buffer)
strict_span, span_buffer) where T
one_t = convert(T, one(T))
if x_max - x_min < eps()*one_t
R = typeof(1.0 * one_t)
return R[x_min], x_min - one_t, x_min + one_t
end

const n = length(Q)
n = length(Q)

# generalizing "order of magnitude"
xspan = x_max - x_min
z = bounding_order_of_magnitude(xspan)

high_score = -Inf
S_best = Array{typeof(1.0 * one_t)}(0)
S_best = Array{typeof(1.0 * one_t)}(undef, )
viewmin_best, viewmax_best = x_min, x_max

while 2k_max * 10.0^(z+1) * one_t > xspan
Expand All @@ -112,7 +112,7 @@ function optimize_ticks_typed{T}(x_min::T, x_max::T, extend_ticks,
end
viewmin, viewmax = S[k + 1], S[2 * k]
else
S = Array{typeof(1.0 * one_t)}(k)
S = Array{typeof(1.0 * one_t)}(undef, k)
for i in 0:(k - 1)
S[i+1] = (r + i) * tickspan
end
Expand Down Expand Up @@ -210,7 +210,7 @@ function optimize_ticks(x_min::DateTime, x_max::DateTime; extend_ticks::Bool=fal
if year(x_max) == year(x_min) && month(x_max) - month(x_min) <= 1 && scale != :month
ticks = DateTime[]

const scales = [
scales = [
Day(1), Hour(1), Minute(1), Second(1), Millisecond(100),
Millisecond(10), Millisecond(1)
]
Expand Down Expand Up @@ -303,8 +303,8 @@ end


# Generate ticks suitable for multiple scales.
function multilevel_ticks{T}(viewmin::T, viewmax::T;
scales=[0.5, 5.0, 10.0])
function multilevel_ticks(viewmin::T, viewmax::T;
scales=[0.5, 5.0, 10.0]) where T

ticks = Dict()
for scale in scales
Expand Down
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using PlotUtils
using Base.Test
using Test

# TODO: real tests

Expand All @@ -26,11 +26,11 @@ const C = RGBA{Float64}

grad = cgrad([:red, "blue"])
@test grad.colors == C[colorant"red", colorant"blue"]
@test grad.values == collect(linspace(0,1,2))
@test grad.values == collect(range(0,stop=1,length=2))

grad = cgrad([:red, "blue"], alpha = 0.5)
@test grad.colors == C[C(1,0,0,0.5), C(0,0,1,0.5)]
@test grad.values == collect(linspace(0,1,2))
@test grad.values == collect(range(0,stop=1,length=2))

grad = cgrad([:red,:blue], [0,0.1,1])
@test grad.colors == C[C(1,0,0), C(0.5,0,0.5), C(0,0,1)]
Expand Down

0 comments on commit 61eaaef

Please sign in to comment.