use of water.fvec.NewChunk in project h2o-3 by h2oai.
the class AstMad method mad.
public static double mad(Frame f, QuantileModel.CombineMethod cm, double constant) {
// need Frames everywhere because of QuantileModel demanding a Frame...
Key tk = null;
if (f._key == null) {
DKV.put(tk = Key.make(), f = new Frame(tk, f.names(), f.vecs()));
}
final double median = AstMedian.median(f, cm);
Frame abs_dev = new MRTask() {
@Override
public void map(Chunk c, NewChunk nc) {
for (int i = 0; i < c._len; ++i) nc.addNum(Math.abs(c.at8(i) - median));
}
}.doAll(1, Vec.T_NUM, f).outputFrame();
if (abs_dev._key == null) {
DKV.put(tk = Key.make(), abs_dev = new Frame(tk, abs_dev.names(), abs_dev.vecs()));
}
double mad = AstMedian.median(abs_dev, cm);
// drp mapping, keep vec
DKV.remove(f._key);
DKV.remove(abs_dev._key);
return constant * mad;
}
use of water.fvec.NewChunk in project h2o-3 by h2oai.
the class AstDiffLag1 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("Expected a single column for diff. Got: " + fr.numCols() + " columns.");
if (!fr.anyVec().isNumeric())
throw new IllegalArgumentException("Expected a numeric column for diff. Got: " + fr.anyVec().get_type_str());
final double[] lastElemPerChk = GetLastElemPerChunkTask.get(fr.anyVec());
return new ValFrame(new MRTask() {
@Override
public void map(Chunk c, NewChunk nc) {
if (c.cidx() == 0)
nc.addNA();
else
nc.addNum(c.atd(0) - lastElemPerChk[c.cidx() - 1]);
for (int row = 1; row < c._len; ++row) nc.addNum(c.atd(row) - c.atd(row - 1));
}
}.doAll(fr.types(), fr).outputFrame(fr.names(), fr.domains()));
}
use of water.fvec.NewChunk 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);
}
use of water.fvec.NewChunk in project h2o-3 by h2oai.
the class AstSeq method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
double from = asts[1].exec(env).getNum();
double to = asts[2].exec(env).getNum();
double by = asts[3].exec(env).getNum();
double delta = to - from;
if (delta == 0 && to == 0)
throw new IllegalArgumentException("Expected `to` and `from` to have nonzero difference.");
else {
double n = delta / by;
if (n < 0)
throw new IllegalArgumentException("wrong sign in 'by' argument");
else if (n > Double.MAX_VALUE)
throw new IllegalArgumentException("'by' argument is much too small");
Futures fs = new Futures();
AppendableVec av = new AppendableVec(Vec.newKey(), Vec.T_NUM);
NewChunk nc = new NewChunk(av, 0);
int len = (int) n + 1;
for (int r = 0; r < len; r++) nc.addNum(from + r * by);
// May need to adjust values = by > 0 ? min(values, to) : max(values, to)
nc.close(0, fs);
Vec vec = av.layout_and_close(fs);
fs.blockForPending();
return new ValFrame(new Frame(vec));
}
}
use of water.fvec.NewChunk 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 }));
}
Aggregations