use of water.fvec.Frame in project h2o-3 by h2oai.
the class AstRepLen method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Val v = asts[1].exec(env);
long length = (long) asts[2].exec(env).getNum();
Frame ff;
if (v instanceof ValFrame)
ff = stk.track(v).getFrame();
else
return new ValFrame(new Frame(Vec.makeCon(v.getNum(), length)));
final Frame fr = ff;
if (fr.numCols() == 1) {
Vec vec = Vec.makeRepSeq(length, fr.numRows());
new MRTask() {
@Override
public void map(Chunk c) {
for (int i = 0; i < c._len; ++i) c.set(i, fr.anyVec().at((long) c.atd(i)));
}
}.doAll(vec);
vec.setDomain(fr.anyVec().domain());
return new ValFrame(new Frame(vec));
} else {
Frame f = new Frame();
for (int i = 0; i < length; ++i) f.add(Frame.defaultColName(f.numCols()), fr.vec(i % fr.numCols()));
return new ValFrame(f);
}
}
use of water.fvec.Frame in project h2o-3 by h2oai.
the class AstMatch 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) || !(fr.anyVec().isCategorical() || fr.anyVec().isString()))
throw new IllegalArgumentException("can only match on a single categorical/string column.");
final MRTask<?> matchTask;
double noMatch = asts[3].exec(env).getNum();
if (asts[2] instanceof AstNumList) {
matchTask = new NumMatchTask(((AstNumList) asts[2]).sort().expand(), noMatch);
} else if (asts[2] instanceof AstNum) {
matchTask = new NumMatchTask(new double[] { asts[2].exec(env).getNum() }, noMatch);
} else if (asts[2] instanceof AstStrList) {
String[] values = ((AstStrList) asts[2])._strs;
Arrays.sort(values);
matchTask = fr.anyVec().isString() ? new StrMatchTask(values, noMatch) : new CatMatchTask(values, noMatch);
} else if (asts[2] instanceof AstStr) {
String[] values = new String[] { asts[2].exec(env).getStr() };
matchTask = fr.anyVec().isString() ? new StrMatchTask(values, noMatch) : new CatMatchTask(values, noMatch);
} else
throw new IllegalArgumentException("Expected numbers/strings. Got: " + asts[2].getClass());
Frame result = matchTask.doAll(Vec.T_NUM, fr.anyVec()).outputFrame();
return new ValFrame(result);
}
use of water.fvec.Frame in project h2o-3 by h2oai.
the class AstGrep method exec.
@Override
protected Val exec(Val[] args) {
Frame fr = args[1].getFrame();
String regex = args[2].getStr();
boolean ignoreCase = args[3].getNum() == 1;
boolean invert = args[4].getNum() == 1;
boolean outputLogical = args[5].getNum() == 1;
GrepHelper grepHelper = new GrepHelper(regex, ignoreCase, invert, outputLogical);
if ((fr.numCols() != 1) || !(fr.anyVec().isCategorical() || fr.anyVec().isString()))
throw new IllegalArgumentException("can only grep on a single categorical/string column.");
Vec v = fr.anyVec();
assert v != null;
Frame result;
if (v.isCategorical()) {
int[] filtered = grepDomain(grepHelper, v);
Arrays.sort(filtered);
result = new GrepCatTask(grepHelper, filtered).doAll(Vec.T_NUM, v).outputFrame();
} else {
result = new GrepStrTask(grepHelper).doAll(Vec.T_NUM, v).outputFrame();
}
return new ValFrame(result);
}
use of water.fvec.Frame in project h2o-3 by h2oai.
the class AstAsDate method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame fr = stk.track(asts[1].exec(env)).getFrame();
Vec vec = fr.vecs()[0];
if (fr.vecs().length != 1 || !(vec.isCategorical() || vec.isString()))
throw new IllegalArgumentException("as.Date requires a single column of factors or strings");
final String format = asts[2].exec(env).getStr();
if (format.isEmpty())
throw new IllegalArgumentException("as.Date requires a non-empty format string");
// check the format string more?
final String[] dom = vec.domain();
final boolean isStr = dom == null && vec.isString();
assert isStr || dom != null : "as.Date error: domain is null, but vec is not String";
Frame fr2 = new MRTask() {
private transient DateTimeFormatter _fmt;
@Override
public void setupLocal() {
_fmt = ParseTime.forStrptimePattern(format).withZone(ParseTime.getTimezone());
}
@Override
public void map(Chunk c, NewChunk nc) {
//done on each node in lieu of rewriting DateTimeFormatter as Iced
String date;
BufferedString tmpStr = new BufferedString();
for (int i = 0; i < c._len; ++i) {
if (!c.isNA(i)) {
if (isStr)
date = c.atStr(tmpStr, i).toString();
else
date = dom[(int) c.at8(i)];
nc.addNum(DateTime.parse(date, _fmt).getMillis(), 0);
} else
nc.addNA();
}
}
}.doAll(1, Vec.T_NUM, fr).outputFrame(fr._names, null);
return new ValFrame(fr2);
}
use of water.fvec.Frame in project h2o-3 by h2oai.
the class AstListTimeZones method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
String[] domain = ParseTime.listTimezones().split("\n");
double[] ds = new double[domain.length];
for (int i = 0; i < domain.length; i++) ds[i] = i;
Vec vec = Vec.makeVec(ds, Vec.VectorGroup.VG_LEN1.addVec());
vec.setDomain(domain);
return new ValFrame(new Frame(new String[] { "Timezones" }, new Vec[] { vec }));
}
Aggregations