improvement: Make MultiRowRollingMathStream more performant
Avoid creating extra lists and o(n) enumeration
This commit is contained in:
1 parent
52f35cc421
commit
ea0246929a
2 files changed
+26
-24
No files matched your search
@@ -1,6 +1,8 @@
|
||||
namespace bts.BlueOrb {
|
||||
using BCG;
|
||||
|
||||
using bts.collections;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -73,30 +75,20 @@ namespace bts.BlueOrb {
|
||||
}
|
||||
|
||||
// aggregates multiple rows into one, used by MultiRowRollingMathStream
|
||||
public static IBlueRow group_by_compute(List<IBlueRow> rows, int start_idx, int n_rows, MultiRowOpHelper parameters) {
|
||||
if (rows.Count() == 0) {
|
||||
return null;
|
||||
}
|
||||
IBlueRow ir = rows.ElementAt(start_idx);
|
||||
IBlueTableSchema in_schema = ir.schema;
|
||||
IBlueTableSchema out_schema = in_schema;
|
||||
// out_col vs inp_cal + type
|
||||
IDictionary<string, Tuple<string, MultiRowOp>> out_col_map =
|
||||
new Dictionary<string, Tuple<string, MultiRowOp>>();
|
||||
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);
|
||||
public static IBlueRow group_by_compute(
|
||||
IBlueRow row,
|
||||
int start_idx,
|
||||
int n_rows,
|
||||
IBlueTableSchema out_schema,
|
||||
IDictionary<string, (MultiRowOp, IList<double>)> out_col_map) {
|
||||
MutableBlueRow or = new MutableBlueRow(out_schema, row);
|
||||
foreach (var kv in out_col_map) {
|
||||
string out_col_name = kv.Key;
|
||||
string in_col_name = kv.Value.Item1;
|
||||
MultiRowOp op = kv.Value.Item2;
|
||||
IEnumerable<double> col_vals = BlueDataUtility.get_colvalues<double>(rows, start_idx, n_rows, in_col_name);
|
||||
MultiRowOp op = kv.Value.Item1;
|
||||
var col_vals = kv.Value.Item2.WrapSubRange(start_idx, Math.Min(n_rows, kv.Value.Item2.Count - start_idx));
|
||||
double comp_res = compute_op(col_vals, op);
|
||||
or.setValue(out_col_name, comp_res);
|
||||
or.setValue("id", ir["id"]);
|
||||
or.setValue("id", row["id"]);
|
||||
}
|
||||
return or;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
namespace bts.BlueOrb {
|
||||
using BCG;
|
||||
|
||||
using bts.collections;
|
||||
|
||||
using mbts.utils;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net;
|
||||
|
||||
[DataStreamUserRespresentation("MultiRowRollingMathStream")]
|
||||
public class MultiRowRollingMathStream : ViewStream {
|
||||
@@ -27,10 +29,18 @@
|
||||
|
||||
private void _parent_str_onchange(object dp_obj) {
|
||||
var dp = ExceptionAssert.Argument.Is<IDataPacket>(dp_obj);
|
||||
var rows = dp.newRows.to_list();
|
||||
BlueTable? out_table = null;
|
||||
for (int i = 0; i < rows.Count; i++) {
|
||||
var out_r = MultiRowOpHelper.group_by_compute(rows, i, _rolling_window_sz, _op);
|
||||
IBlueTableSchema out_schema = dp.newRows.schema;
|
||||
// out_col vs inp_cal + type
|
||||
var out_col_map = new Dictionary<string, (MultiRowOp, IList<double>)>();
|
||||
foreach (var t in _op.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.Item2, dp.newRows.GetColumn(t.Item1).WrapSelector(o => (double)o, null));
|
||||
}
|
||||
int i = 0;
|
||||
foreach (IBlueRow row in dp.newRows) {
|
||||
var out_r = MultiRowOpHelper.group_by_compute(row, i++, _rolling_window_sz, out_schema, out_col_map);
|
||||
if (out_table == null)
|
||||
out_table = new BlueTable(out_r.schema);
|
||||
out_table.addRow(out_r);
|
||||
|
||||
Reference in new issue
Block a user