use of water.rapids.Val in project h2o-3 by h2oai.
the class AstSumAxisTest method testRowwisesumOnEmptyFrame.
@Test
public void testRowwisesumOnEmptyFrame() {
Frame fr = register(new Frame(Key.<Frame>make()));
Val val = Rapids.exec("(sumaxis " + fr._key + " 0 1)");
assertTrue(val instanceof ValFrame);
Frame res = register(val.getFrame());
assertEquals(res.numCols(), 0);
assertEquals(res.numRows(), 0);
}
use of water.rapids.Val in project h2o-3 by h2oai.
the class GLRMTest method testSubset.
@Ignore
@Test
public void testSubset() throws InterruptedException, ExecutionException {
//Analogous to pyunit_subset_glrm.py
GLRM job = null;
GLRMModel model = null;
Frame train;
InputStream is;
try {
is = new FileInputStream(FileUtils.getFile("bigdata/laptop/census/ACS_13_5YR_DP02_cleaned.zip"));
UploadFileVec.ReadPutStats stats = new UploadFileVec.ReadPutStats();
UploadFileVec.readPut("train", is, stats);
} catch (Exception e) {
e.printStackTrace();
}
ParseDataset.parse(Key.make("train_parsed"), Key.make("train"));
train = DKV.getGet("train_parsed");
try {
Log.info("num chunks: ", train.anyVec().nChunks());
Vec[] acs_zcta_vec = { train.vec(0).toCategoricalVec() };
Frame acs_zcta_fr = new Frame(Key.<Frame>make("acs_zcta_fr"), new String[] { "name" }, acs_zcta_vec);
DKV.put(acs_zcta_fr);
train.remove(0).remove();
DKV.put(train);
GLRMParameters parms = new GLRMParameters();
parms._train = train._key;
parms._gamma_x = 0.25;
parms._gamma_y = 0.5;
parms._regularization_x = GlrmRegularizer.Quadratic;
parms._regularization_y = GlrmRegularizer.L1;
parms._k = 10;
parms._transform = DataInfo.TransformType.STANDARDIZE;
parms._max_iterations = 1;
parms._loss = GlrmLoss.Quadratic;
try {
Scope.enter();
job = new GLRM(parms);
model = job.trainModel().get();
String s = "(tmp= py_4 (rows (cols_py " + model._output._representation_key + " [0 1]) (tmp= py_3 (| (| (| (| (| (== (tmp= py_2 " + acs_zcta_fr._key + ") \"10065\") (== py_2 \"11219\")) (== py_2 \"66753\")) (== py_2 \"84104\")) (== py_2 \"94086\")) (== py_2 \"95014\")))))";
Val val = Rapids.exec(s);
} catch (Throwable t) {
t.printStackTrace();
throw new RuntimeException(t);
} finally {
acs_zcta_fr.delete();
Scope.exit();
}
} catch (Throwable t) {
t.printStackTrace();
throw new RuntimeException(t);
} finally {
if (train != null)
train.delete();
if (model != null)
model.delete();
}
}
use of water.rapids.Val in project h2o-3 by h2oai.
the class AstAppend method apply.
@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
Frame dst = stk.track(asts[1].exec(env)).getFrame();
Val vsrc = stk.track(asts[2].exec(env));
String newColName = asts[3].exec(env).getStr();
Vec vec = dst.anyVec();
switch(vsrc.type()) {
case Val.NUM:
vec = vec.makeCon(vsrc.getNum());
break;
case Val.STR:
throw H2O.unimpl();
case Val.FRM:
if (vsrc.getFrame().numCols() != 1)
throw new IllegalArgumentException("Can only append one column");
vec = vsrc.getFrame().anyVec();
break;
default:
throw new IllegalArgumentException("Source must be a Frame or Number, but found a " + vsrc.getClass());
}
dst = new Frame(dst._names.clone(), dst.vecs().clone());
dst.add(newColName, vec);
return new ValFrame(dst);
}
use of water.rapids.Val 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.Val in project h2o-3 by h2oai.
the class RapidsHandler method exec.
public RapidsSchemaV3 exec(int version, RapidsSchemaV3 rapids) {
if (rapids == null)
return null;
if (!StringUtils.isNullOrEmpty(rapids.id))
throw new H2OIllegalArgumentException("Field RapidsSchemaV3.id is deprecated and should not be set " + rapids.id);
if (StringUtils.isNullOrEmpty(rapids.ast))
return rapids;
if (StringUtils.isNullOrEmpty(rapids.session_id))
rapids.session_id = "_specialSess";
Session ses = RapidsHandler.SESSIONS.get(rapids.session_id);
if (ses == null) {
ses = new Session(rapids.session_id);
RapidsHandler.SESSIONS.put(rapids.session_id, ses);
}
Val val;
try {
// This call is synchronized on the session instance
val = Rapids.exec(rapids.ast, ses);
} catch (IllegalArgumentException e) {
throw e;
} catch (Throwable e) {
Log.err(e);
e.printStackTrace();
throw e;
}
switch(val.type()) {
case Val.NUM:
return new RapidsNumberV3(val.getNum());
case Val.NUMS:
return new RapidsNumbersV3(val.getNums());
case Val.ROW:
return new RapidsNumbersV3(val.getRow());
case Val.STR:
return new RapidsStringV3(val.getStr());
case Val.STRS:
return new RapidsStringsV3(val.getStrs());
case Val.FRM:
return new RapidsFrameV3(val.getFrame());
case Val.FUN:
return new RapidsFunctionV3(val.getFun().toString());
default:
throw H2O.fail();
}
}
Aggregations