91 lines
3.0 KiB
C#
91 lines
3.0 KiB
C#
namespace bts.SilverPine {
|
|
using BCG;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
using ZstdSharp.Unsafe;
|
|
|
|
public enum GroupByBundleOp {
|
|
Mean,
|
|
Median,
|
|
Mode,
|
|
StdDev,
|
|
Max,
|
|
Min,
|
|
First,
|
|
Last,
|
|
Sum
|
|
}
|
|
public class GroupByBundle {
|
|
public readonly List<Tuple<string, GroupByBundleOp>> operations;
|
|
|
|
public static GroupByBundle parse(string text) {
|
|
// text is inp_col: op, inp_col: op, inp_col:op
|
|
List<Tuple<string, GroupByBundleOp>> rv = new List<Tuple<string, GroupByBundleOp>>();
|
|
string[] calcs = text.Split(',');
|
|
foreach (string c in calcs) {
|
|
string[] toks = c.Split(':');
|
|
string inp_col = toks[0].Trim();
|
|
string op_txt = toks[1].Trim();
|
|
GroupByBundleOp op = Enum.Parse<GroupByBundleOp>(op_txt, true);
|
|
rv.Add(Tuple.Create(inp_col, op));
|
|
}
|
|
return new GroupByBundle(rv);
|
|
}
|
|
|
|
public GroupByBundle(List<Tuple<string, GroupByBundleOp>> operations_) {
|
|
operations = operations_;
|
|
}
|
|
|
|
public static IBlueRow group_by(List<IBlueRow> rows, GroupByBundle parameters) {
|
|
if (rows.Count() == 0) {
|
|
return null;
|
|
}
|
|
|
|
IBlueRow ir = rows.ElementAt(0);
|
|
IBlueTableSchema in_schema = ir.schema;
|
|
IBlueTableSchema out_schema = in_schema;
|
|
IDictionary<string, Tuple<string, GroupByBundleOp>> out_col_map =
|
|
new Dictionary<string, Tuple<string, GroupByBundleOp>>();
|
|
foreach (var t in parameters.operations) {
|
|
string out_col_name = $"{t.Item1}_{t.Item2}".ToLower();
|
|
out_schema = out_schema.add_column(TypeCode.Double, out_col_name);
|
|
out_col_map[out_col_name] = t;
|
|
}
|
|
MutableBlueRow or = new MutableBlueRow(out_schema, ir);
|
|
foreach (var kv in out_col_map) {
|
|
string out_col_name = kv.Key;
|
|
string in_col_name = kv.Value.Item1;
|
|
GroupByBundleOp op = kv.Value.Item2;
|
|
IEnumerable<double> col_vals = BlueDataUtility.get_colvalues<double>(rows, in_col_name);
|
|
double comp_res = compute_op(col_vals, op);
|
|
or.setValue(out_col_name, comp_res);
|
|
}
|
|
return or;
|
|
}
|
|
|
|
public static double compute_op(IEnumerable<double> col_vals, GroupByBundleOp op) {
|
|
switch (op) {
|
|
case GroupByBundleOp.Max:
|
|
return col_vals.Max<double>();
|
|
case GroupByBundleOp.Min:
|
|
return col_vals.Min<double>();
|
|
case GroupByBundleOp.Mean:
|
|
return col_vals.Sum() / col_vals.Count();
|
|
case GroupByBundleOp.Sum:
|
|
return col_vals.Sum();
|
|
case GroupByBundleOp.First:
|
|
return col_vals.First();
|
|
case GroupByBundleOp.Last:
|
|
return col_vals.Last();
|
|
default:
|
|
// TODO
|
|
Log.assert(false);
|
|
return Double.NaN;
|
|
}
|
|
}
|
|
|
|
}
|
|
} |