Search in sources :

Example 31 with Val

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);
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) Test(org.junit.Test)

Example 32 with Val

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();
    }
}
Also used : Val(water.rapids.Val) Frame(water.fvec.Frame) UploadFileVec(water.fvec.UploadFileVec) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) GLRMParameters(hex.glrm.GLRMModel.GLRMParameters) FileInputStream(java.io.FileInputStream) ExecutionException(java.util.concurrent.ExecutionException) UploadFileVec(water.fvec.UploadFileVec) Vec(water.fvec.Vec) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 33 with Val

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);
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) Vec(water.fvec.Vec)

Example 34 with Val

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());
    }
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) ValNum(water.rapids.vals.ValNum) Chunk(water.fvec.Chunk) NewChunk(water.fvec.NewChunk) NewChunk(water.fvec.NewChunk) ValFrame(water.rapids.vals.ValFrame) ValRow(water.rapids.vals.ValRow) MRTask(water.MRTask)

Example 35 with Val

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();
    }
}
Also used : Val(water.rapids.Val) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) Session(water.rapids.Session)

Aggregations

Val (water.rapids.Val)76 Frame (water.fvec.Frame)65 Test (org.junit.Test)56 ValFrame (water.rapids.vals.ValFrame)52 Vec (water.fvec.Vec)14 ValRow (water.rapids.vals.ValRow)8 MRTask (water.MRTask)5 Session (water.rapids.Session)5 Chunk (water.fvec.Chunk)4 TestFrameBuilder (water.fvec.TestFrameBuilder)4 ValNum (water.rapids.vals.ValNum)4 NewChunk (water.fvec.NewChunk)2 AstParameter (water.rapids.ast.AstParameter)2 AstNumList (water.rapids.ast.params.AstNumList)2 CreateFrame (hex.CreateFrame)1 GLRMParameters (hex.glrm.GLRMModel.GLRMParameters)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1