use of water.MRTask in project h2o-3 by h2oai.
the class AstGroup method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
int ncols = fr.numCols();
AstNumList groupby = check(ncols, asts[2]);
final int[] gbCols = groupby.expand4();
// Count of aggregates; knock off the first 4 ASTs (GB data [group-by] [order-by]...),
// then count by triples.
int naggs = (asts.length - 3) / 3;
final AGG[] aggs = new AGG[naggs];
for (int idx = 3; idx < asts.length; idx += 3) {
Val v = asts[idx].exec(env);
String fn = v instanceof ValFun ? v.getFun().str() : v.getStr();
FCN fcn = FCN.valueOf(fn);
AstNumList col = check(ncols, asts[idx + 1]);
if (col.cnt() != 1)
throw new IllegalArgumentException("Group-By functions take only a single column");
// Aggregate column
int agg_col = (int) col.min();
if (fcn == FCN.mode && !fr.vec(agg_col).isCategorical())
throw new IllegalArgumentException("Mode only allowed on categorical columns");
NAHandling na = NAHandling.valueOf(asts[idx + 2].exec(env).getStr().toUpperCase());
aggs[(idx - 3) / 3] = new AGG(fcn, agg_col, na, (int) fr.vec(agg_col).max() + 1);
}
// do the group by work now
IcedHashMap<G, String> gss = doGroups(fr, gbCols, aggs);
final G[] grps = gss.keySet().toArray(new G[gss.size()]);
// apply an ORDER by here...
if (gbCols.length > 0)
Arrays.sort(grps, new java.util.Comparator<G>() {
// Compare 2 groups. Iterate down _gs, stop when _gs[i] > that._gs[i],
// or _gs[i] < that._gs[i]. Order by various columns specified by
// gbCols. NaN is treated as least
@Override
public int compare(G g1, G g2) {
for (int i = 0; i < gbCols.length; i++) {
if (Double.isNaN(g1._gs[i]) && !Double.isNaN(g2._gs[i]))
return -1;
if (!Double.isNaN(g1._gs[i]) && Double.isNaN(g2._gs[i]))
return 1;
if (g1._gs[i] != g2._gs[i])
return g1._gs[i] < g2._gs[i] ? -1 : 1;
}
return 0;
}
// I do not believe sort() calls equals() at this time, so no need to implement
@Override
public boolean equals(Object o) {
throw H2O.unimpl();
}
});
// Build the output!
String[] fcnames = new String[aggs.length];
for (int i = 0; i < aggs.length; i++) fcnames[i] = aggs[i]._fcn.toString() + "_" + fr.name(aggs[i]._col);
MRTask mrfill = new MRTask() {
@Override
public void map(Chunk[] c, NewChunk[] ncs) {
int start = (int) c[0].start();
for (int i = 0; i < c[0]._len; ++i) {
// One Group per row
G g = grps[i + start];
int j;
for (// The Group Key, as a row
j = 0; // The Group Key, as a row
j < g._gs.length; // The Group Key, as a row
j++) ncs[j].addNum(g._gs[j]);
for (int a = 0; a < aggs.length; a++) ncs[j++].addNum(aggs[a]._fcn.postPass(g._dss[a], g._ns[a]));
}
}
};
Frame f = buildOutput(gbCols, naggs, fr, fcnames, grps.length, mrfill);
return new ValFrame(f);
}
use of water.MRTask in project h2o-3 by h2oai.
the class AstScale method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
int ncols = fr.numCols();
// Peel out the bias/shift/mean
double[] means;
if (asts[2] instanceof AstNumList) {
means = ((AstNumList) asts[2]).expand();
if (means.length != ncols)
throw new IllegalArgumentException("Numlist must be the same length as the columns of the Frame");
} else {
double d = asts[2].exec(env).getNum();
if (// No change on means, so zero-filled
d == 0)
// No change on means, so zero-filled
means = new double[ncols];
else if (d == 1)
means = fr.means();
else
throw new IllegalArgumentException("Only true or false allowed");
}
// Peel out the scale/stddev
double[] mults;
if (asts[3] instanceof AstNumList) {
mults = ((AstNumList) asts[3]).expand();
if (mults.length != ncols)
throw new IllegalArgumentException("Numlist must be the same length as the columns of the Frame");
} else {
Val v = asts[3].exec(env);
if (v instanceof ValFrame) {
mults = toArray(v.getFrame().anyVec());
} else {
double d = v.getNum();
if (d == 0)
// No change on mults, so one-filled
Arrays.fill(mults = new double[ncols], 1.0);
else if (d == 1)
mults = fr.mults();
else
throw new IllegalArgumentException("Only true or false allowed");
}
}
// Update in-place.
// Make final copy for closure
final double[] fmeans = means;
// Make final copy for closure
final double[] fmults = mults;
new MRTask() {
@Override
public void map(Chunk[] cs) {
for (int i = 0; i < cs.length; i++) for (int row = 0; row < cs[i]._len; row++) cs[i].set(row, (cs[i].atd(row) - fmeans[i]) * fmults[i]);
}
}.doAll(fr);
return new ValFrame(fr);
}
use of water.MRTask in project h2o-3 by h2oai.
the class AstSetDomain method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame f = stk.track(asts[1].exec(env)).getFrame();
String[] _domains = ((AstStrList) asts[2])._strs;
if (f.numCols() != 1)
throw new IllegalArgumentException("Must be a single column. Got: " + f.numCols() + " columns.");
Vec v = f.anyVec();
if (!v.isCategorical())
throw new IllegalArgumentException("Vector must be a factor column. Got: " + v.get_type_str());
if (_domains != null && _domains.length != v.domain().length) {
// in this case we want to recollect the domain and check that number of levels matches _domains
VecUtils.CollectDomainFast t = new VecUtils.CollectDomainFast((int) v.max());
t.doAll(v);
final long[] dom = t.domain();
if (dom.length != _domains.length)
throw new IllegalArgumentException("Number of replacement factors must equal current number of levels. Current number of levels: " + dom.length + " != " + _domains.length);
new MRTask() {
@Override
public void map(Chunk c) {
for (int i = 0; i < c._len; ++i) {
if (!c.isNA(i)) {
long num = Arrays.binarySearch(dom, c.at8(i));
if (num < 0)
throw new IllegalArgumentException("Could not find the categorical value!");
c.set(i, num);
}
}
}
}.doAll(v);
}
v.setDomain(_domains);
DKV.put(v);
return new ValFrame(f);
}
use of water.MRTask in project h2o-3 by h2oai.
the class AstBinOp method frame_op_frame.
/**
* Auto-widen: If one frame has only 1 column, auto-widen that 1 column to
* the rest. Otherwise the frames must have the same column count, and
* auto-widen element-by-element. Short-cut if one frame has zero
* columns.
*/
private ValFrame frame_op_frame(Frame lf, Frame rt) {
if (lf.numRows() != rt.numRows()) {
// special case for broadcasting a single row of data across a frame
if (lf.numRows() == 1 || rt.numRows() == 1) {
if (lf.numCols() != rt.numCols())
throw new IllegalArgumentException("Frames must have same columns, found " + lf.numCols() + " columns and " + rt.numCols() + " columns.");
return frame_op_row(lf, rt);
} else
throw new IllegalArgumentException("Frames must have same rows, found " + lf.numRows() + " rows and " + rt.numRows() + " rows.");
}
if (lf.numCols() == 0)
return new ValFrame(lf);
if (rt.numCols() == 0)
return new ValFrame(rt);
if (lf.numCols() == 1 && rt.numCols() > 1)
return vec_op_frame(lf.vecs()[0], rt);
if (rt.numCols() == 1 && lf.numCols() > 1)
return frame_op_vec(lf, rt.vecs()[0]);
if (lf.numCols() != rt.numCols())
throw new IllegalArgumentException("Frames must have same columns, found " + lf.numCols() + " columns and " + rt.numCols() + " columns.");
Frame res = new MRTask() {
@Override
public void map(Chunk[] chks, NewChunk[] cress) {
BufferedString lfstr = new BufferedString();
BufferedString rtstr = new BufferedString();
assert (cress.length << 1) == chks.length;
for (int c = 0; c < cress.length; c++) {
Chunk clf = chks[c];
Chunk crt = chks[c + cress.length];
NewChunk cres = cress[c];
if (clf.vec().isString())
for (int i = 0; i < clf._len; i++) cres.addNum(str_op(clf.atStr(lfstr, i), crt.atStr(rtstr, i)));
else
for (int i = 0; i < clf._len; i++) cres.addNum(op(clf.atd(i), crt.atd(i)));
}
}
}.doAll(lf.numCols(), Vec.T_NUM, new Frame(lf).add(rt)).outputFrame(lf._names, null);
// Cleanup categorical misuse
return cleanCategorical(lf, res);
}
use of water.MRTask in project h2o-3 by h2oai.
the class AstBinOp method frame_op_vec.
private ValFrame frame_op_vec(Frame fr, Vec vec) {
// Already checked for same rows, non-zero frame
Frame lf = new Frame(fr);
lf.add("", vec);
Frame res = new MRTask() {
@Override
public void map(Chunk[] chks, NewChunk[] cress) {
assert cress.length == chks.length - 1;
Chunk crt = chks[cress.length];
for (int c = 0; c < cress.length; c++) {
Chunk clf = chks[c];
NewChunk cres = cress[c];
for (int i = 0; i < clf._len; i++) cres.addNum(op(clf.atd(i), crt.atd(i)));
}
}
}.doAll(fr.numCols(), Vec.T_NUM, lf).outputFrame(fr._names, null);
// Cleanup categorical misuse
return cleanCategorical(fr, res);
}
Aggregations