use of water.MRTask2 in project h2o-2 by h2oai.
the class MRUtils method add.
public static Frame add(Frame fr, final double d) {
Frame r = new MRTask2() {
@Override
public void map(Chunk[] cs, NewChunk[] ncs) {
for (int i = 0; i < ncs.length; i++) {
NewChunk nc = ncs[i];
Chunk c = cs[i];
for (int r = 0; r < c._len; r++) nc.addNum(c.at0(r) + d);
}
}
}.doAll(fr.numCols(), fr).outputFrame(fr.names(), fr.domains());
return r;
}
use of water.MRTask2 in project h2o-2 by h2oai.
the class MRUtils method pow.
public static Frame pow(Frame fr, final double d) {
Frame r = new MRTask2() {
@Override
public void map(Chunk[] cs, NewChunk[] ncs) {
for (int i = 0; i < ncs.length; i++) {
NewChunk nc = ncs[i];
Chunk c = cs[i];
for (int r = 0; r < c._len; r++) nc.addNum(Math.pow(c.at0(r), d));
}
}
}.doAll(fr.numCols(), fr).outputFrame(fr.names(), fr.domains());
return r;
}
use of water.MRTask2 in project h2o-2 by h2oai.
the class MRUtils method mul.
public static Frame mul(Frame fr, final double d) {
Frame r = new MRTask2() {
@Override
public void map(Chunk[] cs, NewChunk[] ncs) {
for (int i = 0; i < ncs.length; i++) {
NewChunk nc = ncs[i];
Chunk c = cs[i];
for (int r = 0; r < c._len; r++) nc.addNum(c.at0(r) * d);
}
}
}.doAll(fr.numCols(), fr).outputFrame(fr.names(), fr.domains());
;
return r;
}
use of water.MRTask2 in project h2o-2 by h2oai.
the class ASTUnique method apply.
@Override
void apply(Env env, int argcnt, ASTApply apply) {
// Peek everything from the stack
// ary->dblary but better be ary[,1]->dblary[,1]
final ASTOp op = env.fcn(-1);
// MARGIN: ROW=1, COLUMN=2 selector
double d = env.dbl(-2);
// The Frame to work on
Frame fr = env.ary(-3);
if (d == 2 || d == -1) {
// Work on columns?
int ncols = fr.numCols();
// If results are doubles, gather in small array
double[][] ds = null;
// If the results are Vecs, gather them in this Frame
Frame fr2 = null;
String err = "apply requires that " + op + " return 1 column";
if (op._t.ret().isDbl())
ds = new double[ncols][1];
else
fr2 = new Frame(new String[0], new Vec[0]);
// Apply the function across columns
try {
Vec[] vecs = fr.vecs();
for (int i = 0; i < ncols; i++) {
env.push(op);
env.push(new Frame(new String[] { fr._names[i] }, new Vec[] { vecs[i] }));
env.fcn(-2).apply(env, 2, null);
if (ds != null) {
// Doubles or Frame results?
ds[i][0] = env.popDbl();
} else {
// Frame results
fr2.add(fr._names[i], env.popXAry().theVec(err));
}
}
} catch (IllegalArgumentException iae) {
env.subRef(fr2, null);
throw iae;
}
env.pop(4);
if (ds != null)
env.push(FrameUtils.frame(new String[] { "C1" }, ds));
else
env.push(fr2);
assert env.isAry();
return;
}
if (d == 1 || d == -2) {
// Work on rows
// apply on rows is essentially a map function
Type[] ts = new Type[2];
ts[0] = Type.unbound();
ts[1] = Type.ARY;
Type ft1 = Type.fcn(ts);
// Should be a function type
Type ft2 = op._t.find();
if (!ft1.union(ft2)) {
if (ft2._ts.length != 2)
throw new IllegalArgumentException("FCN " + op.toString() + " cannot accept one argument.");
if (!ft2._ts[1].union(ts[1]))
throw new IllegalArgumentException("Arg " + op._vars[1] + " typed " + ft2._ts[1].find() + " but passed as " + ts[1]);
assert false;
}
// find out return type
double[] rowin = new double[fr.vecs().length];
for (int c = 0; c < rowin.length; c++) rowin[c] = fr.vecs()[c].at(0);
final int outlen = op.map(env, rowin, null).length;
final Env env0 = env;
MRTask2 mrt = new MRTask2() {
@Override
public void map(Chunk[] cs, NewChunk[] ncs) {
double[] rowin = new double[cs.length];
double[] rowout = new double[outlen];
for (int row = 0; row < cs[0]._len; row++) {
for (int c = 0; c < cs.length; c++) rowin[c] = cs[c].at0(row);
op.map(env0, rowin, rowout);
for (int c = 0; c < ncs.length; c++) ncs[c].addNum(rowout[c]);
}
}
};
String[] names = new String[outlen];
for (int i = 0; i < names.length; i++) names[i] = "C" + (i + 1);
Frame res = mrt.doAll(outlen, fr).outputFrame(names, null);
env.poppush(4, res, null);
return;
}
throw new IllegalArgumentException("MARGIN limited to 1 (rows) or 2 (cols)");
}
use of water.MRTask2 in project h2o-2 by h2oai.
the class MRUtils method div.
public static Frame div(Frame fr, final double d) {
Frame r = new MRTask2() {
@Override
public void map(Chunk[] cs, NewChunk[] ncs) {
for (int i = 0; i < ncs.length; i++) {
NewChunk nc = ncs[i];
Chunk c = cs[i];
for (int r = 0; r < c._len; r++) if (d != 0)
nc.addNum(c.at0(r) * d);
else
nc.addNA();
}
}
}.doAll(fr.numCols(), fr).outputFrame(fr.names(), fr.domains());
return r;
}