use of water.rapids.vals.ValRow in project h2o-3 by h2oai.
the class AstRollupOp method apply.
@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Val arg1 = asts[1].exec(env);
if (arg1.isRow()) {
// Row-wise operation
double[] ds = arg1.getRow();
double d = ds[0];
for (int i = 1; i < ds.length; i++) d = op(d, ds[i]);
return new ValRow(new double[] { d }, null);
}
// Normal column-wise operation
Frame fr = stk.track(arg1).getFrame();
Vec[] vecs = fr.vecs();
if (vecs.length == 0 || vecs[0].naCnt() > 0)
return new ValNum(Double.NaN);
double d = rup(vecs[0]);
for (int i = 1; i < vecs.length; i++) {
if (vecs[i].naCnt() > 0)
return new ValNum(Double.NaN);
d = op(d, rup(vecs[i]));
}
return new ValNum(d);
}
use of water.rapids.vals.ValRow in project h2o-3 by h2oai.
the class AstGetrowTest method TestGetrow3.
/** Test columns of various types */
@Test
public void TestGetrow3() {
Frame f = null;
Vec[] vv = null;
try {
f = ArrayUtils.frame(ar("D1", "D2"), ard(0, 1));
vv = f.vec(0).makeCons(5, 0, ar(ar("N", "Y"), ar("a", "b", "c"), null, null, null), ar(Vec.T_CAT, Vec.T_CAT, Vec.T_TIME, Vec.T_STR, Vec.T_UUID));
f.add(ar("C1", "C2", "T1", "S1", "U1"), vv);
Val v = Rapids.exec("(getrow " + f._key + ")");
assertTrue(v instanceof ValRow);
double[] row = v.getRow();
assertEquals(7, row.length);
assertArrayEquals(ard(0, 1, Double.NaN, Double.NaN, 0, Double.NaN, Double.NaN), row, 1e-8);
} finally {
if (f != null)
f.delete();
if (vv != null)
for (Vec v : vv) v.remove();
}
}
use of water.rapids.vals.ValRow in project h2o-3 by h2oai.
the class AstUniOp method exec.
@Override
public Val exec(Val... args) {
Val val = args[1];
switch(val.type()) {
case Val.NUM:
return new ValNum(op(val.getNum()));
case Val.FRM:
Frame fr = val.getFrame();
for (int i = 0; i < fr.numCols(); i++) if (!fr.vec(i).isNumeric())
throw new IllegalArgumentException("Operator " + str() + "() cannot be applied to non-numeric column " + fr.name(i));
// Get length of columns in fr and append `op(colName)`. For example, a column named "income" that had
// a log transformation would now be changed to `log(income)`.
String[] newNames = new String[fr.numCols()];
for (int i = 0; i < newNames.length; i++) {
newNames[i] = str() + "(" + fr.name(i) + ")";
}
return new ValFrame(new MRTask() {
@Override
public void map(Chunk[] cs, NewChunk[] ncs) {
for (int col = 0; col < cs.length; col++) {
Chunk c = cs[col];
NewChunk nc = ncs[col];
for (int i = 0; i < c._len; i++) nc.addNum(op(c.atd(i)));
}
}
}.doAll(fr.numCols(), Vec.T_NUM, fr).outputFrame(newNames, null));
case Val.ROW:
double[] ds = new double[val.getRow().length];
for (int i = 0; i < ds.length; ++i) ds[i] = op(val.getRow()[i]);
String[] names = ((ValRow) val).getNames().clone();
return new ValRow(ds, names);
default:
throw H2O.unimpl("unop unimpl: " + val.getClass());
}
}
use of water.rapids.vals.ValRow in project h2o-3 by h2oai.
the class AstGetrowTest method TestGetrow.
/** Test that in normal case the result has the correct type and value. */
@Test
public void TestGetrow() {
Frame f = null;
try {
f = ArrayUtils.frame(ar("A", "B", "C", "D", "E"), ard(1.0, -3, 12, 1000000, Double.NaN));
Val v = Rapids.exec("(getrow " + f._key + ")");
assertTrue(v instanceof ValRow);
double[] row = v.getRow();
assertEquals(row.length, 5);
assertArrayEquals(ard(1.0, -3, 12, 1000000, Double.NaN), row, 1e-8);
} finally {
if (f != null)
f.delete();
}
}
use of water.rapids.vals.ValRow in project h2o-3 by h2oai.
the class AstColPySlice method apply.
@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Val v = stk.track(asts[1].exec(env));
AstParameter colList = (AstParameter) asts[2];
if (v instanceof ValRow) {
ValRow vv = (ValRow) v;
return vv.slice(colList.columns(vv.getNames()));
}
Frame fr = v.getFrame();
int[] cols = colList.columns(fr.names());
Frame fr2 = new Frame();
if (// Empty inclusion list?
cols.length == 0)
return new ValFrame(fr2);
if (// Negative cols have number of cols added
cols[0] < 0)
for (int i = 0; i < cols.length; i++) cols[i] += fr.numCols();
if (// Singletons must be in-range
asts[2] instanceof AstNum && (cols[0] < 0 || cols[0] >= fr.numCols()))
throw new IllegalArgumentException("Column must be an integer from 0 to " + (fr.numCols() - 1));
for (// For all included columns
int col : // For all included columns
cols) if (// Ignoring out-of-range ones
col >= 0 && col < fr.numCols())
fr2.add(fr.names()[col], fr.vecs()[col]);
return new ValFrame(fr2);
}
Aggregations