Merge pull request #1033 from piever/group
Group now groups kw arguments as well
This commit is contained in:
commit
ca5310025f
14
src/args.jl
14
src/args.jl
@ -856,21 +856,29 @@ end
|
|||||||
|
|
||||||
|
|
||||||
# this is when given a vector-type of values to group by
|
# this is when given a vector-type of values to group by
|
||||||
function extractGroupArgs(v::AVec, args...)
|
function extractGroupArgs(v::AVec, args...; legendEntry = string)
|
||||||
groupLabels = sort(collect(unique(v)))
|
groupLabels = sort(collect(unique(v)))
|
||||||
n = length(groupLabels)
|
n = length(groupLabels)
|
||||||
if n > 100
|
if n > 100
|
||||||
warn("You created n=$n groups... Is that intended?")
|
warn("You created n=$n groups... Is that intended?")
|
||||||
end
|
end
|
||||||
groupIds = Vector{Int}[filter(i -> v[i] == glab, 1:length(v)) for glab in groupLabels]
|
groupIds = Vector{Int}[filter(i -> v[i] == glab, 1:length(v)) for glab in groupLabels]
|
||||||
GroupBy(map(string, groupLabels), groupIds)
|
GroupBy(map(legendEntry, groupLabels), groupIds)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
legendEntryFromTuple(ns::Tuple) = string(("$n " for n in ns)...)
|
||||||
|
|
||||||
|
# this is when given a tuple of vectors of values to group by
|
||||||
|
function extractGroupArgs(vs::Tuple, args...)
|
||||||
|
(vs == ()) && return GroupBy([""], [1:size(args[1],1)])
|
||||||
|
v = collect(zip(vs...))
|
||||||
|
extractGroupArgs(v, args...; legendEntry = legendEntryFromTuple)
|
||||||
|
end
|
||||||
|
|
||||||
# expecting a mapping of "group label" to "group indices"
|
# expecting a mapping of "group label" to "group indices"
|
||||||
function extractGroupArgs{T, V<:AVec{Int}}(idxmap::Dict{T,V}, args...)
|
function extractGroupArgs{T, V<:AVec{Int}}(idxmap::Dict{T,V}, args...)
|
||||||
groupLabels = sortedkeys(idxmap)
|
groupLabels = sortedkeys(idxmap)
|
||||||
groupIds = VecI[collect(idxmap[k]) for k in groupLabels]
|
groupIds = Vector{Int}[collect(idxmap[k]) for k in groupLabels]
|
||||||
GroupBy(groupLabels, groupIds)
|
GroupBy(groupLabels, groupIds)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -509,12 +509,25 @@ end
|
|||||||
# nothing
|
# nothing
|
||||||
# end
|
# end
|
||||||
|
|
||||||
|
splittable_kw(key, val, lengthGroup) = false
|
||||||
|
splittable_kw(key, val::AbstractArray, lengthGroup) = (key != :group) && size(val,1) == lengthGroup
|
||||||
|
splittable_kw(key, val::Tuple, lengthGroup) = all(splittable_kw.(key, val, lengthGroup))
|
||||||
|
|
||||||
|
split_kw(key, val::AbstractArray, indices) = val[indices, fill(Colon(), ndims(val)-1)...]
|
||||||
|
split_kw(key, val::Tuple, indices) = Tuple(split_kw(key, v, indices) for v in val)
|
||||||
|
|
||||||
# split the group into 1 series per group, and set the label and idxfilter for each
|
# split the group into 1 series per group, and set the label and idxfilter for each
|
||||||
@recipe function f(groupby::GroupBy, args...)
|
@recipe function f(groupby::GroupBy, args...)
|
||||||
|
lengthGroup = maximum(union(groupby.groupIds...))
|
||||||
for (i,glab) in enumerate(groupby.groupLabels)
|
for (i,glab) in enumerate(groupby.groupLabels)
|
||||||
@series begin
|
@series begin
|
||||||
label --> string(glab)
|
label --> string(glab)
|
||||||
idxfilter --> groupby.groupIds[i]
|
idxfilter --> groupby.groupIds[i]
|
||||||
|
for (key,val) in d
|
||||||
|
if splittable_kw(key, val, lengthGroup)
|
||||||
|
:($key) := split_kw(key, val, groupby.groupIds[i])
|
||||||
|
end
|
||||||
|
end
|
||||||
args
|
args
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user