recipe overhaul; groups, annotations, SliceIt

This commit is contained in:
Thomas Breloff 2016-05-13 00:17:58 -04:00
parent 7e1706f700
commit 4f429fb536
5 changed files with 99 additions and 52 deletions

View File

@ -584,6 +584,10 @@ function preprocessArgs!(d::KW)
processLineArg(d, arg) processLineArg(d, arg)
end end
if haskey(d, :linetype) && haskey(_typeAliases, d[:linetype])
d[:linetype] = _typeAliases[d[:linetype]]
end
# handle marker args... default to ellipse if shape not set # handle marker args... default to ellipse if shape not set
anymarker = false anymarker = false
for arg in wraptuple(get(d, :marker, ())) for arg in wraptuple(get(d, :marker, ()))
@ -689,6 +693,22 @@ function extractGroupArgs{T, V<:AVec{Int}}(idxmap::Dict{T,V}, args...)
GroupBy(groupLabels, groupIds) GroupBy(groupLabels, groupIds)
end end
filter_data(v::AVec, idxfilter::AVec{Int}) = v[idxfilter]
filter_data(v, idxfilter) = v
function filter_data!(d::KW, idxfilter)
for s in (:x, :y, :z)
d[s] = filter_data(get(d, s, nothing), idxfilter)
end
end
function _filter_input_data!(d::KW)
idxfilter = pop!(d, :idxfilter, nothing)
if idxfilter != nothing
filter_data!(d, idxfilter)
end
end
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------

View File

@ -338,7 +338,6 @@ end
function fix_xy_lengths!(plt::Plot{PyPlotBackend}, d::KW) function fix_xy_lengths!(plt::Plot{PyPlotBackend}, d::KW)
x, y = d[:x], d[:y] x, y = d[:x], d[:y]
@show x, y
nx, ny = length(x), length(y) nx, ny = length(x), length(y)
if !isa(get(d, :z, nothing), Surface) && nx != ny if !isa(get(d, :z, nothing), Surface) && nx != ny
if nx < ny if nx < ny

View File

@ -93,9 +93,20 @@ function _plot!(plt::Plot, d::KW, args...)
# we simply add the GroupBy object to the front of the args list to allow # we simply add the GroupBy object to the front of the args list to allow
# the recipe to be applied # the recipe to be applied
if haskey(d, :group) if haskey(d, :group)
args = vcat(extractGroupArgs(d[:group], args...), args) args = tuple(extractGroupArgs(d[:group], args...), args...)
end end
# initialize the annotations list with what was passed in
# TODO: there must be cleaner way to handle this!
anns = annotations(get(d, :annotation, NTuple{3}[]))
if typeof(anns) <: AVec{PlotText}
anns = NTuple{3}[]
else
delete!(d, :annotation)
end
# anns = annotations(pop!(d, :annotation, [])
# for plotting recipes, swap out the args and update the parameter dictionary # for plotting recipes, swap out the args and update the parameter dictionary
# we are keeping a queue of series that still need to be processed. # we are keeping a queue of series that still need to be processed.
# each pass through the loop, we pop one off and apply the recipe. # each pass through the loop, we pop one off and apply the recipe.
@ -109,8 +120,9 @@ function _plot!(plt::Plot, d::KW, args...)
series_list = RecipesBase.apply_recipe(next_series.d, next_series.args...) series_list = RecipesBase.apply_recipe(next_series.d, next_series.args...)
for series in series_list for series in series_list
if isempty(series.args) if isempty(series.args)
# apply markershape_to_add and then warn if there's anything left unsupported # finish processing and add to the kw_list
_add_markershape(series.d) _add_markershape(series.d)
_filter_input_data!(series.d)
warnOnUnsupportedArgs(plt.backend, series.d) warnOnUnsupportedArgs(plt.backend, series.d)
warnOnUnsupportedScales(plt.backend, series.d) warnOnUnsupportedScales(plt.backend, series.d)
push!(kw_list, series.d) push!(kw_list, series.d)
@ -130,22 +142,27 @@ function _plot!(plt::Plot, d::KW, args...)
# nothing # nothing
# end # end
# now include any annotations which were added during recipes
# anns = NTuple{3,Any}[]
for kw in kw_list
append!(anns, annotations(pop!(kw, :annotation, [])))
end
# @show anns
# TODO: why do i need to check for the subplot key? # TODO: why do i need to check for the subplot key?
# merge plot args # merge plot args
if !haskey(d, :subplot) if !haskey(d, :subplot)
# merge the plot args from the recipes, then update the plot colors # merge the plot args from the recipes, then update the plot colors
for kw in vcat(kw_list, d) for kw in vcat(kw_list, d)
# @show kw
# append!(anns, annotations(pop!(kw, :annotation, [])))
_add_plotargs!(plt, kw) _add_plotargs!(plt, kw)
end end
# for k in keys(_plotDefaults)
# for kw in kw_list
# if haskey(kw, k)
# plt.plotargs[k] = pop!(kw, k)
# end
# end
# end
# merge!(plt.plotargs, d)
handlePlotColors(plt.backend, plt.plotargs) handlePlotColors(plt.backend, plt.plotargs)
end end
@ -176,18 +193,18 @@ function _plot!(plt::Plot, d::KW, args...)
# merge!(plt.plotargs, plotarg_overrides) # merge!(plt.plotargs, plotarg_overrides)
# end # end
dumpdict(kw, "before add defaults", true) # dumpdict(kw, "before add defaults", true)
_add_defaults!(kw, plt, i) _add_defaults!(kw, plt, i)
dumpdict(kw, "after add defaults", true) # dumpdict(kw, "after add defaults", true)
# getSeriesArgs(plt.backend, getplotargs(plt, n), d, commandIndex, convertSeriesIndex(plt, n), n) # getSeriesArgs(plt.backend, getplotargs(plt, n), d, commandIndex, convertSeriesIndex(plt, n), n)
# TODO: apply idxfilter to x/y/z
_replace_linewidth(kw) _replace_linewidth(kw)
_add_series(plt.backend, plt, kw) _add_series(plt.backend, plt, kw)
end end
_add_annotations(plt, anns)
# _add_annotations(plt, d) # TODO
# add title, axis labels, ticks, etc # add title, axis labels, ticks, etc
if !haskey(d, :subplot) if !haskey(d, :subplot)
@ -220,14 +237,6 @@ end
# end # end
# end # end
filter_data(v::AVec, idxfilter::AVec{Int}) = v[idxfilter]
filter_data(v, idxfilter) = v
function filter_data!(d::KW, idxfilter)
for s in (:x, :y, :z)
d[s] = filter_data(get(d, s, nothing), idxfilter)
end
end
function _replace_linewidth(d::KW) function _replace_linewidth(d::KW)
# get a good default linewidth... 0 for surface and heatmaps # get a good default linewidth... 0 for surface and heatmaps
@ -372,6 +381,17 @@ annotations(v::AVec) = map(PlotText, v)
annotations(anns) = error("Expecting a tuple (or vector of tuples) for annotations: ", annotations(anns) = error("Expecting a tuple (or vector of tuples) for annotations: ",
"(x, y, annotation)\n got: $(typeof(anns))") "(x, y, annotation)\n got: $(typeof(anns))")
function annotations(plt::Plot, anns)
anns = annotations(anns)
# if we just have a list of PlotText objects, then create (x,y,text) tuples
if typeof(anns) <: AVec{PlotText}
x, y = plt[plt.n]
anns = Tuple{Float64,Float64,PlotText}[(x[i], y[i], t) for (i,t) in enumerate(anns)]
end
anns
end
function _add_annotations(plt::Plot, d::KW) function _add_annotations(plt::Plot, d::KW)
anns = annotations(get(d, :annotation, nothing)) anns = annotations(get(d, :annotation, nothing))
if !isempty(anns) if !isempty(anns)

View File

@ -44,6 +44,11 @@ if is_installed("DataFrames")
end end
end end
function extractGroupArgs(group::Symbol, df::DataFrames.AbstractDataFrame, args...)
extractGroupArgs(collect(df[group]))
end
function handle_group(df::DataFrames.AbstractDataFrame, d::KW) function handle_group(df::DataFrames.AbstractDataFrame, d::KW)
if haskey(d, :group) if haskey(d, :group)
g = d[:group] g = d[:group]

View File

@ -25,9 +25,9 @@ function _add_defaults!(d::KW, plt::Plot, commandIndex::Int)
# end # end
# end # end
if haskey(_typeAliases, d[:linetype]) # if haskey(_typeAliases, d[:linetype])
d[:linetype] = _typeAliases[d[:linetype]] # d[:linetype] = _typeAliases[d[:linetype]]
end # end
aliasesAndAutopick(d, :axis, _axesAliases, supportedAxes(pkg), plotIndex) aliasesAndAutopick(d, :axis, _axesAliases, supportedAxes(pkg), plotIndex)
aliasesAndAutopick(d, :linestyle, _styleAliases, supportedStyles(pkg), plotIndex) aliasesAndAutopick(d, :linestyle, _styleAliases, supportedStyles(pkg), plotIndex)
@ -87,10 +87,12 @@ end
# instead of process_inputs: # instead of process_inputs:
# ensure we dispatch to the slicer
immutable SliceIt end
# the catch-all recipes # the catch-all recipes
# @recipe function f(x, y, z) @recipe function f(::Type{SliceIt}, x, y, z)
function slice_and_dice(d, x, y, z) # @show "HERE", typeof((x,y,z))
@show "HERE", typeof((x,y,z))
xs, _ = convertToAnyVector(x, d) xs, _ = convertToAnyVector(x, d)
ys, _ = convertToAnyVector(y, d) ys, _ = convertToAnyVector(y, d)
zs, _ = convertToAnyVector(z, d) zs, _ = convertToAnyVector(z, d)
@ -111,22 +113,23 @@ function slice_and_dice(d, x, y, z)
# add a new series # add a new series
di = copy(d) di = copy(d)
xi, yi, zi = xs[mod1(i,mx)], ys[mod1(i,my)], zs[mod1(i,mz)] xi, yi, zi = xs[mod1(i,mx)], ys[mod1(i,my)], zs[mod1(i,mz)]
@show i, typeof((xi, yi, zi)) # @show i, typeof((xi, yi, zi))
di[:x], di[:y], di[:z] = compute_xyz(xi, yi, zi) di[:x], di[:y], di[:z] = compute_xyz(xi, yi, zi)
# handle fillrange # handle fillrange
fr = fillranges[mod1(i,mf)] fr = fillranges[mod1(i,mf)]
d[:fillrange] = isa(fr, Function) ? map(fr, di[:x]) : fr di[:fillrange] = isa(fr, Function) ? map(fr, di[:x]) : fr
@show i, di[:x], di[:y], di[:z] # @show i, di[:x], di[:y], di[:z]
push!(series_list, RecipeData(di, ())) push!(series_list, RecipeData(di, ()))
end end
nothing # don't add a series for the main block nothing # don't add a series for the main block
end end
@recipe f(x, y, z) = slice_and_dice(d, x, y, z) # pass these through to the slicer
@recipe f(x, y) = x, y, nothing @recipe f(x, y, z) = SliceIt, x, y, z
@recipe f(y) = nothing, y, nothing @recipe f(x, y) = SliceIt, x, y, nothing
@recipe f(y) = SliceIt, nothing, y, nothing
# # -------------------------------------------------------------------- # # --------------------------------------------------------------------
@ -312,7 +315,7 @@ end
elseif !(lt in _3dTypes) elseif !(lt in _3dTypes)
d[:linetype] = :path3d d[:linetype] = :path3d
end end
slice_and_dice(d, x, y, z) SliceIt, x, y, z
end end
# #
@ -328,7 +331,7 @@ end
@recipe function f{X,Y}(x::AVec{X}, y::AVec{Y}, zf::Function) @recipe function f{X,Y}(x::AVec{X}, y::AVec{Y}, zf::Function)
x = TX <: Number ? sort(x) : x x = TX <: Number ? sort(x) : x
y = TY <: Number ? sort(y) : y y = TY <: Number ? sort(y) : y
slice_and_dice(d, x, y, Surface(zf, x, y)) # TODO: replace with SurfaceFunction when supported SliceIt, x, y, Surface(zf, x, y) # TODO: replace with SurfaceFunction when supported
end end
# #
@ -353,7 +356,7 @@ end
if !like_surface(get(d, :linetype, :none)) if !like_surface(get(d, :linetype, :none))
d[:linetype] = :contour d[:linetype] = :contour
end end
slice_and_dice(d, x, y, Surface{Matrix{Z}}(z)) SliceIt, x, y, Surface{Matrix{Z}}(z)
end end
# #
@ -390,9 +393,9 @@ end
# process_inputs{T<:Number}(plt::AbstractPlot, d::KW, u::AVec{T}, fx::FuncOrFuncs, fy::FuncOrFuncs) = process_inputs(plt, d, mapFuncOrFuncs(fx, u), mapFuncOrFuncs(fy, u)) # process_inputs{T<:Number}(plt::AbstractPlot, d::KW, u::AVec{T}, fx::FuncOrFuncs, fy::FuncOrFuncs) = process_inputs(plt, d, mapFuncOrFuncs(fx, u), mapFuncOrFuncs(fy, u))
# process_inputs(plt::AbstractPlot, d::KW, fx::FuncOrFuncs, fy::FuncOrFuncs, umin::Number, umax::Number, numPoints::Int = 1000) = process_inputs(plt, d, fx, fy, linspace(umin, umax, numPoints)) # process_inputs(plt::AbstractPlot, d::KW, fx::FuncOrFuncs, fy::FuncOrFuncs, umin::Number, umax::Number, numPoints::Int = 1000) = process_inputs(plt, d, fx, fy, linspace(umin, umax, numPoints))
@recipe f(f::FuncOrFuncs, xmin::Number, xmax::Number) = linspace(xmin, xmax, 100), f @recipe f(f::FuncOrFuncs, xmin::Number, xmax::Number) = linspace(xmin, xmax, 100), f
@recipe f{T<:Number}(fx::FuncOrFuncs, fy::FuncOrFuncs, u::AVec{T}) = mapFuncOrFuncs(fx, u), mapFuncOrFuncs(fy, u) @recipe f(fx::FuncOrFuncs, fy::FuncOrFuncs, u::AVec) = mapFuncOrFuncs(fx, u), mapFuncOrFuncs(fy, u)
@recipe f{T<:Number}(u::AVec{T}, fx::FuncOrFuncs, fy::FuncOrFuncs) = mapFuncOrFuncs(fx, u), mapFuncOrFuncs(fy, u) # @recipe f(u::AVec, fx::FuncOrFuncs, fy::FuncOrFuncs) = mapFuncOrFuncs(fx, u), mapFuncOrFuncs(fy, u)
@recipe f(fx::FuncOrFuncs, fy::FuncOrFuncs, umin::Number, umax::Number, n = 200) = fx, fy, linspace(umin, umax, n) @recipe f(fx::FuncOrFuncs, fy::FuncOrFuncs, umin::Number, umax::Number, n = 200) = fx, fy, linspace(umin, umax, n)
# #
@ -401,12 +404,12 @@ end
# process_inputs{T<:Number}(plt::AbstractPlot, d::KW, u::AVec{T}, fx::FuncOrFuncs, fy::FuncOrFuncs, fz::FuncOrFuncs) = process_inputs(plt, d, mapFuncOrFuncs(fx, u), mapFuncOrFuncs(fy, u), mapFuncOrFuncs(fz, u)) # process_inputs{T<:Number}(plt::AbstractPlot, d::KW, u::AVec{T}, fx::FuncOrFuncs, fy::FuncOrFuncs, fz::FuncOrFuncs) = process_inputs(plt, d, mapFuncOrFuncs(fx, u), mapFuncOrFuncs(fy, u), mapFuncOrFuncs(fz, u))
# process_inputs(plt::AbstractPlot, d::KW, fx::FuncOrFuncs, fy::FuncOrFuncs, fz::FuncOrFuncs, umin::Number, umax::Number, numPoints::Int = 1000) = process_inputs(plt, d, fx, fy, fz, linspace(umin, umax, numPoints)) # process_inputs(plt::AbstractPlot, d::KW, fx::FuncOrFuncs, fy::FuncOrFuncs, fz::FuncOrFuncs, umin::Number, umax::Number, numPoints::Int = 1000) = process_inputs(plt, d, fx, fy, fz, linspace(umin, umax, numPoints))
@recipe function f{T<:Number}(fx::FuncOrFuncs, fy::FuncOrFuncs, fz::FuncOrFuncs, u::AVec{T}) @recipe function f(fx::FuncOrFuncs, fy::FuncOrFuncs, fz::FuncOrFuncs, u::AVec)
mapFuncOrFuncs(fx, u), mapFuncOrFuncs(fy, u), mapFuncOrFuncs(fz, u)
end
@recipe function f{T<:Number}(u::AVec{T}, fx::FuncOrFuncs, fy::FuncOrFuncs, fz::FuncOrFuncs)
mapFuncOrFuncs(fx, u), mapFuncOrFuncs(fy, u), mapFuncOrFuncs(fz, u) mapFuncOrFuncs(fx, u), mapFuncOrFuncs(fy, u), mapFuncOrFuncs(fz, u)
end end
# @recipe function f(u::AVec, fx::FuncOrFuncs, fy::FuncOrFuncs, fz::FuncOrFuncs)
# mapFuncOrFuncs(fx, u), mapFuncOrFuncs(fy, u), mapFuncOrFuncs(fz, u)
# end
@recipe function f(fx::FuncOrFuncs, fy::FuncOrFuncs, fz::FuncOrFuncs, umin::Number, umax::Number, numPointsn = 200) @recipe function f(fx::FuncOrFuncs, fy::FuncOrFuncs, fz::FuncOrFuncs, umin::Number, umax::Number, numPointsn = 200)
fx, fy, fz, linspace(umin, umax, numPoints) fx, fy, fz, linspace(umin, umax, numPoints)
end end
@ -434,7 +437,7 @@ end
# end # end
@recipe f{R1<:Number,R2<:Number}(xy::AVec{Tuple{R1,R2}}) = unzip(xy) @recipe f{R1<:Number,R2<:Number}(xy::AVec{Tuple{R1,R2}}) = unzip(xy)
@recipe f{R1<:Number,R2<:Number}(xy::Tuple{R1,R2}) = [xy[1]], [xy[2]] @recipe f{R1<:Number,R2<:Number}(xy::Tuple{R1,R2}) = [xy[1]], [xy[2]]
# #
# # (x,y,z) tuples # # (x,y,z) tuples
@ -446,7 +449,7 @@ end
# end # end
@recipe f{R1<:Number,R2<:Number,R3<:Number}(xyz::AVec{Tuple{R1,R2,R3}}) = unzip(xyz) @recipe f{R1<:Number,R2<:Number,R3<:Number}(xyz::AVec{Tuple{R1,R2,R3}}) = unzip(xyz)
@recipe f{R1<:Number,R2<:Number,R3<:Number}(xyz::Tuple{R1,R2,R3}) = [xyz[1]], [xyz[2]], [xyz[3]] @recipe f{R1<:Number,R2<:Number,R3<:Number}(xyz::Tuple{R1,R2,R3}) = [xyz[1]], [xyz[2]], [xyz[3]]
# #
# # 2D FixedSizeArrays # # 2D FixedSizeArrays
@ -458,7 +461,7 @@ end
# end # end
@recipe f{T<:Number}(xy::AVec{FixedSizeArrays.Vec{2,T}}) = unzip(xy) @recipe f{T<:Number}(xy::AVec{FixedSizeArrays.Vec{2,T}}) = unzip(xy)
@recipe f{T<:Number}(xy::FixedSizeArrays.Vec{2,T}) = [xy[1]], [xy[2]] @recipe f{T<:Number}(xy::FixedSizeArrays.Vec{2,T}) = [xy[1]], [xy[2]]
# #
# # 3D FixedSizeArrays # # 3D FixedSizeArrays
@ -470,7 +473,7 @@ end
# end # end
@recipe f{T<:Number}(xyz::AVec{FixedSizeArrays.Vec{3,T}}) = unzip(xyz) @recipe f{T<:Number}(xyz::AVec{FixedSizeArrays.Vec{3,T}}) = unzip(xyz)
@recipe f{T<:Number}(xyz::FixedSizeArrays.Vec{3,T}) = [xyz[1]], [xyz[2]], [xyz[3]] @recipe f{T<:Number}(xyz::FixedSizeArrays.Vec{3,T}) = [xyz[1]], [xyz[2]], [xyz[3]]
# #
# # -------------------------------------------------------------------- # # --------------------------------------------------------------------
@ -495,8 +498,8 @@ end
# create a new series, with the label of the group, and an idxfilter (to be applied in slice_and_dice) # create a new series, with the label of the group, and an idxfilter (to be applied in slice_and_dice)
# TODO: use @series instead # TODO: use @series instead
di = copy(d) di = copy(d)
label --> string(glab) get!(di, :label, string(glab))
idxfilter --> groupby.groupIds[i] get!(di, :idxfilter, groupby.groupIds[i])
push!(series_list, RecipeData(di, args)) push!(series_list, RecipeData(di, args))
end end
nothing nothing