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

[WIP] work towards updating to DataFrames v0.11+ framework #1088

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ Compat 0.18.0
Compose 0.5.2
Contour 0.1.1
CoupledFields
DataFrames 0.4.2
DataArrays
DataFrames 0.11.0
DataArrays 0.7.0
CategoricalArrays
DataStructures
Distributions
Hexagons
Expand Down
1 change: 1 addition & 0 deletions src/Gadfly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Colors
using Compat
using Compose
using DataArrays
using CategoricalArrays
using DataFrames
using DataStructures
using JSON
Expand Down
28 changes: 6 additions & 22 deletions src/aesthetics.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const NumericalOrCategoricalAesthetic =
Union{(Void), Vector, DataArray, PooledDataArray}
Union{(Void), Vector, DataArray, CategoricalArray}

const CategoricalAesthetic =
Union{(Void), PooledDataArray}
Union{(Void), CategoricalArray}

const NumericalAesthetic =
Union{(Void), Matrix, Vector, DataArray}
Expand Down Expand Up @@ -287,18 +287,8 @@ function cat_aes_var!{T, U}(a::AbstractArray{T}, b::AbstractArray{U})
return ab
end

function cat_aes_var!{T}(xs::PooledDataVector{T}, ys::PooledDataVector{T})
newpool = T[x for x in union(Set(xs.pool), Set(ys.pool))]
newdata = vcat(T[x for x in xs], T[y for y in ys])
PooledDataArray(newdata, newpool, [false for _ in newdata])
end

function cat_aes_var!{T, U}(xs::PooledDataVector{T}, ys::PooledDataVector{U})
V = promote_type(T, U)
newpool = V[x for x in union(Set(xs.pool), Set(ys.pool))]
newdata = vcat(V[x for x in xs], V[y for y in ys])
PooledDataArray(newdata, newpool, [false for _ in newdata])
end
cat_aes_var!{T}(xs::CategoricalArray{T}, ys::CategoricalArray{T}) = vcat(xs, ys)
cat_aes_var!{T, U}(xs::CategoricalArray{T}, ys::CategoricalArray{U}) = vcat(xs, ys)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, cat_aes_var{T}(...) is pre-0.6 syntax. Since DataFrames 0.11 requires 0.6, it's better write it as cat_aes_var(...) where {T}, although in this particular case it could be as simple as (replaces both definitions)

cat_aes_var!(xs::CategoricalArray, ys::CategoricalArray) = vcat(xs, ys)



# Summarizing aesthetics
Expand Down Expand Up @@ -333,11 +323,6 @@ function by_xy_group{T <: Union{Data, Aesthetics}}(aes::T, xgroup, ygroup,

xgroup === nothing && ygroup === nothing && return aes_grid

make_pooled_data_array{T,U,V}(::Type{PooledDataArray{T,U,V}}, arr::AbstractArray) =
PooledDataArray(convert(Array{T}, arr))
make_pooled_data_array{T,U,V}(::Type{PooledDataArray{T,U,V}},
arr::PooledDataArray{T,U,V}) = arr

for var in fieldnames(T)
# Skipped aesthetics. Don't try to partition aesthetics for which it
# makes no sense to pass on to subplots.
Expand Down Expand Up @@ -366,9 +351,8 @@ function by_xy_group{T <: Union{Data, Aesthetics}}(aes::T, xgroup, ygroup,
end

for i in 1:n, j in 1:m
if typeof(vals) <: PooledDataArray
setfield!(aes_grid[i, j], var,
make_pooled_data_array(typeof(vals), staging[i, j]))
if typeof(vals) <: CategoricalArray
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vals isa CategoricalArray or isa(vals, CategoricalArray) is shorter

setfield!(aes_grid[i, j], var, CategoricalArray(staging[i, j]))
else
if !applicable(convert, typeof(vals), staging[i, j])
T2 = eltype(vals)
Expand Down
4 changes: 2 additions & 2 deletions src/geom/bar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ function render_stacked_bar(geom::BarGeometry,
aes::Gadfly.Aesthetics,
orientation::Symbol)
# preserve factor orders of pooled data arrays
if isa(aes.color, PooledDataArray)
if isa(aes.color, CategoricalArray)
idxs = sortperm(aes.color.refs, rev=true)
else
idxs = 1:length(aes.color)
Expand Down Expand Up @@ -152,7 +152,7 @@ function render_dodged_bar(geom::BarGeometry,
aes::Gadfly.Aesthetics,
orientation::Symbol)
# preserve factor orders of pooled data arrays
if isa(aes.color, PooledDataArray)
if isa(aes.color, CategoricalArray)
idxs = sortperm(aes.color.refs, rev=true)
else
idxs = 1:length(aes.color)
Expand Down
2 changes: 1 addition & 1 deletion src/geom/beeswarm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function render(geom::BeeswarmGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthet
Gadfly.assert_aesthetics_equal_length("Geom.point", aes,
element_aesthetics(geom)...)
default_aes = Gadfly.Aesthetics()
default_aes.color = PooledDataArray(RGBA{Float32}[theme.default_color])
default_aes.color = CategoricalArray(RGBA{Float32}[theme.default_color])
default_aes.size = Measure[theme.point_size]
aes = inherit(aes, default_aes)
padding = 1.0mm
Expand Down
2 changes: 1 addition & 1 deletion src/geom/boxplot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function render(geom::BoxplotGeometry, theme::Gadfly.Theme, aes::Gadfly.Aestheti
:upper_hinge, :upper_fence, :outliers)

default_aes = Gadfly.Aesthetics()
default_aes.color = PooledDataArray(RGB{Float32}[theme.default_color])
default_aes.color = CategoricalArray(RGB{Float32}[theme.default_color])
default_aes.x = Float64[0.5]
aes = inherit(aes, default_aes)

Expand Down
4 changes: 2 additions & 2 deletions src/geom/errorbar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function render(geom::YErrorBarGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthe
element_aesthetics(geom)...)

default_aes = Gadfly.Aesthetics()
default_aes.color = PooledDataArray(RGB{Float32}[theme.default_color])
default_aes.color = CategoricalArray(RGB{Float32}[theme.default_color])
aes = inherit(aes, default_aes)
caplen = theme.errorbar_cap_length/2
ttc, teb, tbc = subtags(geom.tag, :top_cap, :error_bar, :bottom_cap)
Expand Down Expand Up @@ -90,7 +90,7 @@ function render(geom::XErrorBarGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthe

colored = aes.color != nothing
default_aes = Gadfly.Aesthetics()
default_aes.color = PooledDataArray(RGB{Float32}[theme.default_color])
default_aes.color = CategoricalArray(RGB{Float32}[theme.default_color])
aes = inherit(aes, default_aes)
caplen = theme.errorbar_cap_length/2
tlc, teb, trc = subtags(geom.tag, :left_cap, :error_bar, :right_cap)
Expand Down
4 changes: 2 additions & 2 deletions src/geom/hexbin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ element_aesthetics(geom::HexagonalBinGeometry) = [:x, :y, :xsize, :ysize, :color

function render(geom::HexagonalBinGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics)
default_aes = Gadfly.Aesthetics()
default_aes.color = PooledDataArray(RGB{Float32}[theme.default_color])
default_aes.color = CategoricalArray(RGB{Float32}[theme.default_color])
default_aes.xsize = [1.0]
default_aes.ysize = [1.0]
aes = inherit(aes, default_aes)
Expand All @@ -25,7 +25,7 @@ function render(geom::HexagonalBinGeometry, theme::Gadfly.Theme, aes::Gadfly.Aes
Gadfly.assert_aesthetics_equal_length("Geom.hexbin", aes, :x, :y)

n = length(aes.x)
visibility = Bool[!isna(c) for c in takestrict(cycle(aes.color), n)]
visibility = Bool[!ismissing(c) for c in takestrict(cycle(aes.color), n)]
xs = aes.x[visibility]
ys = aes.y[visibility]
xsizes = collect(eltype(aes.xsize), takestrict(cycle(aes.xsize), n))[visibility]
Expand Down
4 changes: 2 additions & 2 deletions src/geom/line.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function render(geom::LineGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics)
element_aesthetics(geom)...)

default_aes = Gadfly.Aesthetics()
default_aes.color = PooledDataArray(RGBA{Float32}[theme.default_color])
default_aes.color = CategoricalArray(RGBA{Float32}[theme.default_color])
aes = inherit(aes, default_aes)

ctx = context(order=geom.order)
Expand Down Expand Up @@ -131,7 +131,7 @@ function render(geom::LineGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics)
svgclass(classes))

elseif length(aes.color) == 1 &&
!(isa(aes.color, PooledDataArray) && length(levels(aes.color)) > 1)
!(isa(aes.color, CategoricalArray) && length(levels(aes.color)) > 1)
T = (Tuple{eltype(aes.x), eltype(aes.y)})
points = T[(x, y) for (x, y) in zip(aes.x, aes.y)]
geom.preserve_order || sort!(points, by=first)
Expand Down
2 changes: 1 addition & 1 deletion src/geom/point.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function render(geom::PointGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics

default_aes = Gadfly.Aesthetics()
default_aes.shape = Function[Shape.circle]
default_aes.color = PooledDataArray(RGBA{Float32}[theme.default_color])
default_aes.color = CategoricalArray(RGBA{Float32}[theme.default_color])
default_aes.size = Measure[theme.point_size]
aes = inherit(aes, default_aes)

Expand Down
4 changes: 2 additions & 2 deletions src/geom/polygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function render(geom::PolygonGeometry, theme::Gadfly.Theme,
Gadfly.assert_aesthetics_defined("Geom.polygon", aes, :x, :y)

default_aes = Gadfly.Aesthetics()
default_aes.color = PooledDataArray(RGBA{Float32}[theme.default_color])
default_aes.color = CategoricalArray(RGBA{Float32}[theme.default_color])
aes = inherit(aes, default_aes)

ctx = context(order=geom.order)
Expand All @@ -56,7 +56,7 @@ function render(geom::PolygonGeometry, theme::Gadfly.Theme,
compose!(ctx, fill(nothing), stroke(cs))
end
elseif length(aes.color) == 1 &&
!(isa(aes.color, PooledDataArray) && length(levels(aes.color)) > 1)
!(isa(aes.color, CategoricalArray) && length(levels(aes.color)) > 1)
compose!(ctx, Compose.polygon(polygon_points(aes.x, aes.y, geom.preserve_order), geom.tag))
if geom.fill
compose!(ctx, fill(aes.color[1]),
Expand Down
2 changes: 1 addition & 1 deletion src/geom/rectbin.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ element_aesthetics(::RectangularBinGeometry) =
function render(geom::RectangularBinGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetics)

default_aes = Gadfly.Aesthetics()
default_aes.color = PooledDataArray(RGBA{Float32}[theme.default_color])
default_aes.color = CategoricalArray(RGBA{Float32}[theme.default_color])
aes = inherit(aes, default_aes)

Gadfly.assert_aesthetics_defined("RectangularBinGeometry", aes, :xmin, :xmax, :ymin, :ymax)
Expand Down
4 changes: 2 additions & 2 deletions src/geom/ribbon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ function render(geom::RibbonGeometry, theme::Gadfly.Theme, aes::Gadfly.Aesthetic
element_aesthetics(geom)...)

default_aes = Gadfly.Aesthetics()
default_aes.color = PooledDataArray(RGB{Float32}[theme.default_color])
default_aes.color = CategoricalArray(RGB{Float32}[theme.default_color])
aes = inherit(aes, default_aes)

aes_x, aes_ymin, aes_ymax = concretize(aes.x, aes.ymin, aes.ymax)

if length(aes.color) == 1 &&
!(isa(aes.color, PooledDataArray) && length(levels(aes.color)) > 1)
!(isa(aes.color, CategoricalArray) && length(levels(aes.color)) > 1)
max_points = collect(zip(aes_x, aes_ymax))
sort!(max_points, by=first)

Expand Down
1 change: 1 addition & 0 deletions src/geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using Colors
using Compat
using Compose
using DataArrays
using CategoricalArrays
using DataStructures
using Gadfly
using Measures
Expand Down
6 changes: 3 additions & 3 deletions src/misc.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#Is this usable data?
isconcrete{T<:Number}(x::T) = !isna(x) && isfinite(x)
isconcrete{T<:Number}(x::T) = !ismissing(x) && isfinite(x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it should be something like:

isconcrete(::Missing) = false
isconcrete(x::Number) = isfinite(x)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. This should fix #1083 too, I think

isconcrete(x::(Irrational)) = true
isconcrete(x) = !isna(x)
isconcrete(x) = !ismissing(x)

hasna(xs) = false

function hasna(xs::AbstractDataArray)
for x in xs
if isna(x)
if ismissing(x)
return true
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/poetry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ A plot object.
"""

function spy(M::AbstractMatrix, elements::ElementOrFunction...; mapping...)
is, js, values = _findnz(x->!isnan(x), M)
is, js, values = _findnz(x->!ismissingn(x), M)
n,m = size(M)
df = DataFrames.DataFrame(i=is, j=js, value=values)
plot(df, x=:j, y=:i, color=:value,
Expand Down
66 changes: 13 additions & 53 deletions src/scale.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using Colors
using Compat
using Compose
using DataArrays
using CategoricalArrays
using DataStructures
using Gadfly
using Showoff
Expand Down Expand Up @@ -250,67 +251,26 @@ function apply_scale_typed!(ds, field, scale::ContinuousScale)
end
end

# Reorder the levels of a pooled data array
function reorder_levels(da::PooledDataArray, order::AbstractVector)
# Reorder the levels of a categorical array
function reorder_levels(da::CategoricalArray, order::AbstractVector)
level_values = levels(da)
length(order) != length(level_values) &&
error("Discrete scale order is not of the same length as the data's levels.")
permute!(level_values, order)
return PooledDataArray(da, level_values)
end

function discretize_make_pda(values::Vector, levels=nothing)
if levels == nothing
return PooledDataArray(values)
else
return PooledDataArray(convert(Vector{eltype(levels)}, values), levels)
end
end

function discretize_make_pda(values::DataArray, levels=nothing)
if levels == nothing
return PooledDataArray(values)
else
return PooledDataArray(convert(DataArray{eltype(levels)}, values), levels)
end
end

function discretize_make_pda(values::Range, levels=nothing)
if levels == nothing
return PooledDataArray(collect(values))
else
return PooledDataArray(collect(values), levels)
end
end

function discretize_make_pda(values::PooledDataArray, levels=nothing)
if levels == nothing
return values
else
return PooledDataArray(values, convert(Vector{eltype(values)}, levels))
end
levels!(da, level_values)
end

function discretize(values, levels=nothing, order=nothing, preserve_order=true)
if levels == nothing
if preserve_order
levels = OrderedSet()
for value in values
push!(levels, value)
end
da = discretize_make_pda(values, collect(eltype(values), levels))
else
da = discretize_make_pda(values)
end
if preserve_order
da = CategoricalArray(values, ordered=true)
else
da = discretize_make_pda(values, levels)
da = CategoricalArray(values)
end

if order != nothing
return reorder_levels(da, order)
else
return da
reorder_levels(da, order)
end
return da
end


Expand Down Expand Up @@ -364,7 +324,7 @@ function apply_scale(scale::DiscreteScale, aess::Vector{Gadfly.Aesthetics}, data
getfield(data, var) === nothing && continue

disc_data = discretize(getfield(data, var), scale.levels, scale.order)
setfield!(aes, var, PooledDataArray([round(Int64,x) for x in disc_data.refs]))
setfield!(aes, var, CategoricalArray([round(Int64,x) for x in disc_data.refs]))

# The leveler for discrete scales is a closure over the discretized data.
if scale.labels === nothing
Expand Down Expand Up @@ -418,7 +378,7 @@ function apply_scale(scale::IdentityColorScale,
aess::Vector{Gadfly.Aesthetics}, datas::Gadfly.Data...)
for (aes, data) in zip(aess, datas)
data.color === nothing && continue
aes.color = PooledDataArray(data.color)
aes.color = CategoricalArray(data.color)
aes.color_key_colors = Dict()
end
end
Expand Down Expand Up @@ -488,7 +448,7 @@ function apply_scale(scale::DiscreteColorScale,
for (aes, data) in zip(aess, datas)
data.color === nothing && continue
for d in data.color
isna(d) || push!(levelset, d)
ismissing(d) || push!(levelset, d)
end
end

Expand Down Expand Up @@ -517,7 +477,7 @@ function apply_scale(scale::DiscreteColorScale,
end
end

colored_ds = PooledDataArray(colorvals, colors)
colored_ds = CategoricalArray(colorvals)
aes.color = colored_ds

aes.color_label = labeler
Expand Down
Loading