use of water.fvec.Chunk in project h2o-3 by h2oai.
the class TypedFrame method makeVec.
protected Vec makeVec() throws IOException {
final Vec vec0 = buildZeroVec();
MRTask task = new MRTask() {
@Override
public void map(Chunk[] cs) {
for (Chunk c : cs) {
DataChunk<X> tc = factory.apply(c);
for (int r = 0; r < c._len; r++) {
long i = r + c.start();
tc.set(r, function.apply(i));
}
}
}
};
MRTask mrTask = task.doAll(vec0);
return mrTask._fr.vecs()[0];
}
use of water.fvec.Chunk in project h2o-3 by h2oai.
the class AstReLevel method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
if (fr.numCols() != 1)
throw new IllegalArgumentException("`setLevel` works on a single column at a time.");
String[] doms = fr.anyVec().domain().clone();
if (doms == null)
throw new IllegalArgumentException("Cannot set the level on a non-factor column!");
String lvl = asts[2].exec(env).getStr();
final int idx = Arrays.asList(doms).indexOf(lvl);
if (idx == -1)
throw new IllegalArgumentException("Did not find level `" + lvl + "` in the column.");
if (idx == 0)
return new ValFrame(new Frame(fr.names(), new Vec[] { fr.anyVec().makeCopy() }));
String[] srcDom = fr.anyVec().domain();
final String[] dom = new String[srcDom.length];
dom[0] = srcDom[idx];
int j = 1;
for (int i = 0; i < srcDom.length; ++i) if (i != idx)
dom[j++] = srcDom[i];
return new ValFrame(new MRTask() {
@Override
public void map(Chunk c, NewChunk nc) {
int[] vals = new int[c._len];
c.getIntegers(vals, 0, c._len, -1);
for (int i = 0; i < vals.length; ++i) if (vals[i] == -1)
nc.addNA();
else if (vals[i] == idx)
nc.addNum(0);
else
nc.addNum(vals[i] + (vals[i] < idx ? 1 : 0));
}
}.doAll(1, Vec.T_CAT, fr).outputFrame(fr.names(), new String[][] { dom }));
}
use of water.fvec.Chunk in project h2o-3 by h2oai.
the class AstSetLevel method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
if (fr.numCols() != 1)
throw new IllegalArgumentException("`setLevel` works on a single column at a time.");
String[] doms = fr.anyVec().domain().clone();
if (doms == null)
throw new IllegalArgumentException("Cannot set the level on a non-factor column!");
String lvl = asts[2].exec(env).getStr();
final int idx = Arrays.asList(doms).indexOf(lvl);
if (idx == -1)
throw new IllegalArgumentException("Did not find level `" + lvl + "` in the column.");
// COW semantics
Frame fr2 = new MRTask() {
@Override
public void map(Chunk c, NewChunk nc) {
for (int i = 0; i < c._len; ++i) nc.addNum(idx);
}
}.doAll(new byte[] { Vec.T_NUM }, fr.anyVec()).outputFrame(null, fr.names(), fr.domains());
return new ValFrame(fr2);
}
use of water.fvec.Chunk in project h2o-3 by h2oai.
the class AstBinOp method frame_op_scalar.
/**
* Auto-widen the scalar to every element of the frame
*/
private ValFrame frame_op_scalar(Frame fr, final String str) {
Frame res = new MRTask() {
@Override
public void map(Chunk[] chks, NewChunk[] cress) {
BufferedString vstr = new BufferedString();
for (int c = 0; c < chks.length; c++) {
Chunk chk = chks[c];
NewChunk cres = cress[c];
Vec vec = chk.vec();
// String Vectors: apply str_op as BufferedStrings to all elements
if (vec.isString()) {
final BufferedString conStr = new BufferedString(str);
for (int i = 0; i < chk._len; i++) cres.addNum(str_op(chk.atStr(vstr, i), conStr));
} else if (vec.isCategorical()) {
// categorical Vectors: convert string to domain value; apply op (not
// str_op). Not sure what the "right" behavior here is, can
// easily argue that should instead apply str_op to the categorical
// string domain value - except that this whole operation only
// makes sense for EQ/NE, and is much faster when just comparing
// doubles vs comparing strings. Note that if the string is not
// part of the categorical domain, the find op returns -1 which is never
// equal to any categorical dense integer (which are always 0+).
final double d = (double) ArrayUtils.find(vec.domain(), str);
for (int i = 0; i < chk._len; i++) cres.addNum(op(chk.atd(i), d));
} else {
// mixing string and numeric
// false or true only
final double d = op(1, 2);
for (int i = 0; i < chk._len; i++) cres.addNum(d);
}
}
}
}.doAll(fr.numCols(), Vec.T_NUM, fr).outputFrame(fr._names, null);
return new ValFrame(res);
}
Aggregations