group on a tuple of vectors

This commit is contained in:
Pietro Vertechi 2017-08-27 18:56:10 +01:00
parent 5a7ed24078
commit 5157089d87

View File

@ -840,16 +840,24 @@ 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...)