From 6c548f025e1e0dea95fd9249f4644efbb804d143 Mon Sep 17 00:00:00 2001 From: Pietro Vertechi Date: Thu, 31 Aug 2017 18:55:30 +0100 Subject: [PATCH 1/4] wip groupedbar --- src/series.jl | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/series.jl b/src/series.jl index f3499e22..246764d5 100644 --- a/src/series.jl +++ b/src/series.jl @@ -519,16 +519,31 @@ split_kw(key, val::Tuple, indices) = Tuple(split_kw(key, v, indices) for v in va # split the group into 1 series per group, and set the label and idxfilter for each @recipe function f(groupby::GroupBy, args...) lengthGroup = maximum(union(groupby.groupIds...)) - for (i,glab) in enumerate(groupby.groupLabels) - @series begin - label --> string(glab) - idxfilter --> groupby.groupIds[i] - for (key,val) in d - if splittable_kw(key, val, lengthGroup) - :($key) := split_kw(key, val, groupby.groupIds[i]) + if !(RecipesBase.group_as_matrix(args[1])) + for (i,glab) in enumerate(groupby.groupLabels) + @series begin + label --> string(glab) + 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 end - args end + else + g = args[1] + x, y = g.args + x_u = unique(x) + y_mat = Array{eltype(y)}(length(x_u), length(groupby.groupLabels)) + for i in 1:length(groupby.groupLabels) + xi = x[groupby.groupIds[i]] + yi = y[groupby.groupIds[i]] + x2y = Dict(zip(xi,yi)) + y_mat[:, i] = [get(x2y, s, NaN) for s in x_u] + end + label --> reshape(groupby.groupLabels, 1, :) + typeof(g)((x_u, y_mat)) end end From e2795341d96729dc2ec3d59287ec63500002bcd6 Mon Sep 17 00:00:00 2001 From: Pietro Vertechi Date: Mon, 4 Sep 2017 09:48:49 +0100 Subject: [PATCH 2/4] moved group_as_matrix to Plots --- src/series.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/series.jl b/src/series.jl index 246764d5..99feb861 100644 --- a/src/series.jl +++ b/src/series.jl @@ -516,10 +516,12 @@ splittable_kw(key, val::Tuple, lengthGroup) = all(splittable_kw.(key, val, lengt 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) +group_as_matrix(t) = false + # split the group into 1 series per group, and set the label and idxfilter for each @recipe function f(groupby::GroupBy, args...) lengthGroup = maximum(union(groupby.groupIds...)) - if !(RecipesBase.group_as_matrix(args[1])) + if !(group_as_matrix(args[1])) for (i,glab) in enumerate(groupby.groupLabels) @series begin label --> string(glab) From 0f28b59c5f1467a6293b36efb8932d7c46708075 Mon Sep 17 00:00:00 2001 From: Pietro Vertechi Date: Mon, 4 Sep 2017 09:57:33 +0100 Subject: [PATCH 3/4] added support for 1 or 3 arguments to group as matrix --- src/series.jl | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/series.jl b/src/series.jl index 99feb861..f4cb4b8f 100644 --- a/src/series.jl +++ b/src/series.jl @@ -516,6 +516,16 @@ splittable_kw(key, val::Tuple, lengthGroup) = all(splittable_kw.(key, val, lengt 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) +function build_arg_mat(x_ind, x, y, groupby) + y_mat = fill(NaN, length(keys(x_ind)), length(groupby.groupLabels)) + for i in 1:length(groupby.groupLabels) + xi = x[groupby.groupIds[i]] + yi = y[groupby.groupIds[i]] + y_mat[getindex.(x_ind, xi), i] = yi + end + return y_mat +end + group_as_matrix(t) = false # split the group into 1 series per group, and set the label and idxfilter for each @@ -536,16 +546,19 @@ group_as_matrix(t) = false end else g = args[1] - x, y = g.args - x_u = unique(x) - y_mat = Array{eltype(y)}(length(x_u), length(groupby.groupLabels)) - for i in 1:length(groupby.groupLabels) - xi = x[groupby.groupIds[i]] - yi = y[groupby.groupIds[i]] - x2y = Dict(zip(xi,yi)) - y_mat[:, i] = [get(x2y, s, NaN) for s in x_u] + if length(g.args) == 1 + x = zeros(Int64, lengthGroup) + for indexes in groupby.groupIds + x[indexes] = 1:length(indexes) + end + last_args = g.args + else + x = g.args[1] + last_args = g.args[2:end] end + x_u = unique(x) + x_ind = Dict(zip(x_u, 1:length(x_u))) label --> reshape(groupby.groupLabels, 1, :) - typeof(g)((x_u, y_mat)) + typeof(g)((x_u, (build_arg_mat(x_ind, x, arg, groupby) for arg in last_args)...)) end end From dee24e6f08e1f0f76c18d8bb1e85f281bed28f86 Mon Sep 17 00:00:00 2001 From: Pietro Vertechi Date: Mon, 4 Sep 2017 11:49:18 +0100 Subject: [PATCH 4/4] add kw support --- src/series.jl | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/series.jl b/src/series.jl index f4cb4b8f..31191fd2 100644 --- a/src/series.jl +++ b/src/series.jl @@ -516,8 +516,9 @@ splittable_kw(key, val::Tuple, lengthGroup) = all(splittable_kw.(key, val, lengt 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) -function build_arg_mat(x_ind, x, y, groupby) - y_mat = fill(NaN, length(keys(x_ind)), length(groupby.groupLabels)) +function groupedvec2mat(x_ind, x, y::AbstractArray, groupby, def_val = y[1]) + y_mat = Array{promote_type(eltype(y), typeof(def_val))}(length(keys(x_ind)), length(groupby.groupLabels)) + fill!(y_mat, def_val) for i in 1:length(groupby.groupLabels) xi = x[groupby.groupIds[i]] yi = y[groupby.groupIds[i]] @@ -526,6 +527,8 @@ function build_arg_mat(x_ind, x, y, groupby) return y_mat end +groupedvec2mat(x_ind, x, y::Tuple, groupby) = Tuple(groupedvec2mat(x_ind, x, v, groupby) for v in y) + group_as_matrix(t) = false # split the group into 1 series per group, and set the label and idxfilter for each @@ -558,7 +561,12 @@ group_as_matrix(t) = false end x_u = unique(x) x_ind = Dict(zip(x_u, 1:length(x_u))) + for (key,val) in d + if splittable_kw(key, val, lengthGroup) + :($key) := groupedvec2mat(x_ind, x, val, groupby) + end + end label --> reshape(groupby.groupLabels, 1, :) - typeof(g)((x_u, (build_arg_mat(x_ind, x, arg, groupby) for arg in last_args)...)) + typeof(g)((x_u, (groupedvec2mat(x_ind, x, arg, groupby, NaN) for arg in last_args)...)) end end