Skip to content

Commit

Permalink
Merge pull request #1229 from non-Jedi/benchmarking
Browse files Browse the repository at this point in the history
Tweak mapping to improve plotting performance
  • Loading branch information
bjarthur authored Dec 19, 2018
2 parents 2dfff47 + a70f656 commit 8e155b8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
9 changes: 3 additions & 6 deletions src/Gadfly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,9 @@ plot(ls..., Guide.title("layer example"))
"""
function layer(data_source,
elements::ElementOrFunction...; mapping...)
mapping = Dict{Symbol, Any}(mapping)
lyr = Layer()
lyr.data_source = data_source
lyr.mapping = cleanmapping(mapping)
lyr.mapping = cleanmapping(Dict(mapping))
if haskey(mapping, :order)
lyr.order = mapping[:order]
end
Expand Down Expand Up @@ -284,8 +283,7 @@ plot(my_matrix, x=Col.value(1), y=Col.value(2), Geom.line,
"""
function plot(data_source,
elements::ElementOrFunctionOrLayers...; mapping...)
mappingdict = Dict{Symbol, Any}(mapping)
return plot(data_source, mappingdict, elements...)
return plot(data_source, Dict(mapping), elements...)
end

"""
Expand All @@ -304,8 +302,7 @@ plot(x=collect(1917:2018), y=1.02.^(0:101), Geom.line)
```
"""
function plot(elements::ElementOrFunctionOrLayers...; mapping...)
mappingdict = Dict{Symbol, Any}(mapping)
plot(nothing, mappingdict, elements...)
plot(nothing, Dict(mapping), elements...)
end

"""
Expand Down
41 changes: 27 additions & 14 deletions src/mapping.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,25 @@ end # module Row
# A new mapping with aliases evaluated and unrecognized aesthetics removed.
#
function cleanmapping(mapping::Dict)
valid_aesthetics = fieldnames(Aesthetics)
cleaned = Dict{Symbol, Any}()
for (key, val) in mapping
# skip the "order" pesudo-aesthetic, used to order layers
key == :order && continue

if haskey(aesthetic_aliases, key)
key = aesthetic_aliases[key]
elseif !in(key, fieldnames(Aesthetics))
# redefining key could have performance impacts
newkey = aesthetic_aliases[key]
elseif in(key, valid_aesthetics)
newkey = key
else
@warn "$(string(key)) is not a recognized aesthetic. Ignoring."
continue
end

if val == Col.value || val == Col.index || val == Row.index
val = val()
end
newval = val == Col.value || val == Col.index || val == Row.index ? val() : val

cleaned[key] = val
cleaned[newkey] = newval
end
cleaned
end
Expand Down Expand Up @@ -200,7 +202,21 @@ evalmapping(source::MeltedData, arg::AbstractString) =
evalmapping(source::MeltedData, arg::Colon) = source.melted_data


# Share common functionality between general and melted-data-specific methods
function _evalmapping!(mapping::Dict, data_source, data::Data)
for (k, v) in mapping
setfield!(data, k, evalmapping(data_source, v))
data.titles[k] = isa(v, AbstractString) || isa(v, Symbol) ? string(v) : string(k)
end

return data_source
end

# Evalute aesthetic mappings producting a Data instance.
evalmapping!(mapping::Dict, data_source::MeltedData, data::Data) =
_evalmapping!(mapping, data_source, data)

# Need to do additional work only if data_source is not MeltedData
function evalmapping!(mapping::Dict, data_source, data::Data)
# Are we doing implicit reshaping?
colgroups = Col.GroupedColumn[]
Expand All @@ -212,16 +228,13 @@ function evalmapping!(mapping::Dict, data_source, data::Data)
end
end

if !isempty(colgroups) && !isa(data_source, MeltedData)
data_source = meltdata(data_source, colgroups)
end

for (k, v) in mapping
setfield!(data, k, evalmapping(data_source, v))
data.titles[k] = isa(v, AbstractString) || isa(v, Symbol) ? string(v) : string(k)
transformed_data_source = if !isempty(colgroups)
meltdata(data_source, colgroups)
else
data_source
end

return data_source
return _evalmapping!(mapping, transformed_data_source, data)
end

function link_dataframes()
Expand Down

0 comments on commit 8e155b8

Please sign in to comment.