use of water.rapids.ast.params.AstId in project h2o-3 by h2oai.
the class AstRowSlice method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
Frame returningFrame;
long nrows = fr.numRows();
if (asts[2] instanceof AstNumList) {
final AstNumList nums = (AstNumList) asts[2];
if (!nums._isSort && !nums.isEmpty() && nums._bases[0] >= 0)
throw new IllegalArgumentException("H2O does not currently reorder rows, please sort your row selection first");
long[] rows = (nums._isList || nums.min() < 0) ? nums.expand8Sort() : null;
if (rows != null) {
if (rows.length == 0) {
// Empty inclusion list?
} else if (rows[0] >= 0) {
// Positive (inclusion) list
if (rows[rows.length - 1] > nrows)
throw new IllegalArgumentException("Row must be an integer from 0 to " + (nrows - 1));
} else {
// Negative (exclusion) list
if (rows[rows.length - 1] >= 0)
throw new IllegalArgumentException("Cannot mix negative and postive row selection");
// Invert the list to make a positive list, ignoring out-of-bounds values
BitSet bs = new BitSet((int) nrows);
for (long row : rows) {
// The positive index
int idx = (int) (-row - 1);
if (idx >= 0 && idx < nrows)
// Set column to EXCLUDE
bs.set(idx);
}
rows = new long[(int) nrows - bs.cardinality()];
for (int i = bs.nextClearBit(0), j = 0; i < nrows; i = bs.nextClearBit(i + 1)) rows[j++] = i;
}
}
final long[] ls = rows;
returningFrame = new MRTask() {
@Override
public void map(Chunk[] cs, NewChunk[] ncs) {
if (nums.cnt() == 0)
return;
if (ls != null && ls.length == 0)
return;
long start = cs[0].start();
long end = start + cs[0]._len;
// exclusive max to inclusive max when stride == 1
long min = ls == null ? (long) nums.min() : ls[0], max = ls == null ? (long) nums.max() - 1 : ls[ls.length - 1];
//5 [ nums ] nums run rite: start <= nums.min() && end < nums.max()
if (!(max < start || min > end)) {
// not situation 1 or 2 above
// situation 4 and 5 => min > start;
long startOffset = (min > start ? min : start);
for (int i = (int) (startOffset - start); i < cs[0]._len; ++i) {
if ((ls == null && nums.has(start + i)) || (ls != null && Arrays.binarySearch(ls, start + i) >= 0)) {
for (int c = 0; c < cs.length; ++c) {
if (cs[c] instanceof CStrChunk)
ncs[c].addStr(cs[c], i);
else if (cs[c] instanceof C16Chunk)
ncs[c].addUUID(cs[c], i);
else if (cs[c].isNA(i))
ncs[c].addNA();
else
ncs[c].addNum(cs[c].atd(i));
}
}
}
}
}
}.doAll(fr.types(), fr).outputFrame(fr.names(), fr.domains());
} else if ((asts[2] instanceof AstNum)) {
long[] rows = new long[] { (long) (((AstNum) asts[2]).getNum()) };
returningFrame = fr.deepSlice(rows, null);
} else if ((asts[2] instanceof AstExec) || (asts[2] instanceof AstId)) {
Frame predVec = stk.track(asts[2].exec(env)).getFrame();
if (predVec.numCols() != 1)
throw new IllegalArgumentException("Conditional Row Slicing Expression evaluated to " + predVec.numCols() + " columns. Must be a boolean Vec.");
returningFrame = fr.deepSlice(predVec, null);
} else
throw new IllegalArgumentException("Row slicing requires a number-list as the last argument, but found a " + asts[2].getClass());
return new ValFrame(returningFrame);
}
use of water.rapids.ast.params.AstId in project h2o-3 by h2oai.
the class H2OColSelect method transformImpl.
@Override
protected Frame transformImpl(Frame f) {
_ast._asts[1] = new AstId(f);
// throw water.H2O.unimpl();
Session ses = new Session();
Frame fr = ses.exec(_ast, null).getFrame();
if (fr._key == null)
fr = new Frame(Key.<Frame>make("H2OColSelect_" + f._key.toString()), fr.names(), fr.vecs());
DKV.put(fr);
return fr;
}
use of water.rapids.ast.params.AstId in project h2o-3 by h2oai.
the class H2OColOp method transformImpl.
@Override
protected Frame transformImpl(Frame f) {
((AstExec) _ast._asts[1])._asts[1] = new AstId(f);
Session ses = new Session();
Frame fr = ses.exec(_ast, null).getFrame();
_newCol = _newNames == null ? new String[fr.numCols()] : _newNames;
_newColTypes = toJavaPrimitive(fr.anyVec().get_type_str());
if ((_multiColReturn = fr.numCols() > 1)) {
for (int i = 0; i < _newCol.length; i++) {
if (_newNames == null)
_newCol[i] = f.uniquify(i > 0 ? _newCol[i - 1] : _oldCol);
f.add(_newCol[i], fr.vec(i));
}
if (_inplace)
f.remove(f.find(_oldCol)).remove();
} else {
_newCol = _newNames == null ? new String[] { _inplace ? _oldCol : f.uniquify(_oldCol) } : _newCol;
if (_inplace)
f.replace(f.find(_oldCol), fr.anyVec()).remove();
else
f.add(_newNames == null ? _newCol[0] : _newNames[0], fr.anyVec());
}
DKV.put(f);
return f;
}
use of water.rapids.ast.params.AstId in project h2o-3 by h2oai.
the class AstMktime method apply.
@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
// Seven args, all required. See if any are arrays.
Frame[] fs = new Frame[nargs() - 1];
int[] is = new int[nargs() - 1];
// Sample frame (for auto-expanding constants)
Frame x = null;
for (int i = 1; i < nargs(); i++) if (asts[i] instanceof AstId || asts[i] instanceof AstExec)
fs[i - 1] = x = stk.track(asts[i].exec(env)).getFrame();
else
is[i - 1] = (int) asts[i].exec(env).getNum();
if (x == null) {
// Single point
long msec = new MutableDateTime(// year
is[0], // month
is[1] + 1, // day
is[2] + 1, // hour
is[3], // minute
is[4], // second
is[5], // msec
is[6]).getMillis();
return new ValNum(msec);
}
// Make constant Vecs for the constant args. Commonly, they'll all be zero
Vec[] vecs = new Vec[7];
for (int i = 0; i < 7; i++) {
if (fs[i] == null) {
vecs[i] = x.anyVec().makeCon(is[i]);
} else {
if (fs[i].numCols() != 1)
throw new IllegalArgumentException("Expect single column");
vecs[i] = fs[i].anyVec();
}
}
// Convert whole column to epoch msec
Frame fr2 = new MRTask() {
@Override
public void map(Chunk[] chks, NewChunk[] nchks) {
MutableDateTime dt = new MutableDateTime(0);
NewChunk n = nchks[0];
int rlen = chks[0]._len;
for (int r = 0; r < rlen; r++) {
dt.setDateTime(// year
(int) chks[0].at8(r), // month
(int) chks[1].at8(r) + 1, // day
(int) chks[2].at8(r) + 1, // hour
(int) chks[3].at8(r), // minute
(int) chks[4].at8(r), // second
(int) chks[5].at8(r), // msec
(int) chks[6].at8(r));
n.addNum(dt.getMillis());
}
}
}.doAll(new byte[] { Vec.T_NUM }, vecs).outputFrame(new String[] { "msec" }, null);
// Clean up the constants
for (int i = 0; i < nargs() - 1; i++) if (fs[i] == null)
vecs[i].remove();
return new ValFrame(fr2);
}
Aggregations