Search in sources :

Example 86 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class AstBinOp method frame_op_scalar.

/**
   * Auto-widen the scalar to every element of the frame
   */
private ValFrame frame_op_scalar(Frame fr, final String str) {
    Frame res = new MRTask() {

        @Override
        public void map(Chunk[] chks, NewChunk[] cress) {
            BufferedString vstr = new BufferedString();
            for (int c = 0; c < chks.length; c++) {
                Chunk chk = chks[c];
                NewChunk cres = cress[c];
                Vec vec = chk.vec();
                // String Vectors: apply str_op as BufferedStrings to all elements
                if (vec.isString()) {
                    final BufferedString conStr = new BufferedString(str);
                    for (int i = 0; i < chk._len; i++) cres.addNum(str_op(chk.atStr(vstr, i), conStr));
                } else if (vec.isCategorical()) {
                    // categorical Vectors: convert string to domain value; apply op (not
                    // str_op).  Not sure what the "right" behavior here is, can
                    // easily argue that should instead apply str_op to the categorical
                    // string domain value - except that this whole operation only
                    // makes sense for EQ/NE, and is much faster when just comparing
                    // doubles vs comparing strings.  Note that if the string is not
                    // part of the categorical domain, the find op returns -1 which is never
                    // equal to any categorical dense integer (which are always 0+).
                    final double d = (double) ArrayUtils.find(vec.domain(), str);
                    for (int i = 0; i < chk._len; i++) cres.addNum(op(chk.atd(i), d));
                } else {
                    // mixing string and numeric
                    // false or true only
                    final double d = op(1, 2);
                    for (int i = 0; i < chk._len; i++) cres.addNum(d);
                }
            }
        }
    }.doAll(fr.numCols(), Vec.T_NUM, fr).outputFrame(fr._names, null);
    return new ValFrame(res);
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) Vec(water.fvec.Vec) MRTask(water.MRTask) BufferedString(water.parser.BufferedString) Chunk(water.fvec.Chunk) NewChunk(water.fvec.NewChunk) NewChunk(water.fvec.NewChunk)

Example 87 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class AstColSlice method apply.

@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Val v = stk.track(asts[1].exec(env));
    AstParameter col_list = (AstParameter) asts[2];
    if (v instanceof ValRow) {
        ValRow vv = (ValRow) v;
        return vv.slice(col_list.columns(vv.getNames()));
    }
    Frame src = v.getFrame();
    int[] cols = col_select(src.names(), col_list);
    Frame dst = new Frame();
    Vec[] vecs = src.vecs();
    for (int col : cols) dst.add(src._names[col], vecs[col]);
    return new ValFrame(dst);
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) ValRow(water.rapids.vals.ValRow) Vec(water.fvec.Vec) AstParameter(water.rapids.ast.AstParameter)

Example 88 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class AstRStrip method apply.

@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Frame fr = stk.track(asts[1].exec(env)).getFrame();
    String set = asts[2].exec(env).getStr();
    // Type check
    for (Vec v : fr.vecs()) if (!(v.isCategorical() || v.isString()))
        throw new IllegalArgumentException("trim() requires a string or categorical column. " + "Received " + fr.anyVec().get_type_str() + ". Please convert column to a string or categorical first.");
    // Transform each vec
    Vec[] nvs = new Vec[fr.numCols()];
    int i = 0;
    for (Vec v : fr.vecs()) {
        if (v.isCategorical())
            nvs[i] = rstripCategoricalCol(v, set);
        else
            nvs[i] = rstripStringCol(v, set);
        i++;
    }
    return new ValFrame(new Frame(nvs));
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) BufferedString(water.parser.BufferedString)

Example 89 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class AstStrLength method apply.

@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Frame fr = stk.track(asts[1].exec(env)).getFrame();
    // Type check
    for (Vec v : fr.vecs()) if (!(v.isCategorical() || v.isString()))
        throw new IllegalArgumentException("length() requires a string or categorical column. " + "Received " + fr.anyVec().get_type_str() + ". Please convert column to a string or categorical first.");
    // Transform each vec
    Vec[] nvs = new Vec[fr.numCols()];
    int i = 0;
    for (Vec v : fr.vecs()) {
        if (v.isCategorical())
            nvs[i] = lengthCategoricalCol(v);
        else
            nvs[i] = lengthStringCol(v);
        i++;
    }
    return new ValFrame(new Frame(nvs));
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame)

Example 90 with ValFrame

use of water.rapids.vals.ValFrame in project h2o-3 by h2oai.

the class AstStrSplit method apply.

@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Frame fr = stk.track(asts[1].exec(env)).getFrame();
    String splitRegEx = asts[2].exec(env).getStr();
    // Type check
    for (Vec v : fr.vecs()) if (!(v.isCategorical() || v.isString()))
        throw new IllegalArgumentException("strsplit() requires a string or categorical column. " + "Received " + fr.anyVec().get_type_str() + ". Please convert column to a string or categorical first.");
    // Transform each vec
    ArrayList<Vec> vs = new ArrayList<>(fr.numCols());
    for (Vec v : fr.vecs()) {
        Vec[] splits;
        if (v.isCategorical()) {
            splits = strSplitCategoricalCol(v, splitRegEx);
            for (Vec split : splits) vs.add(split);
        } else {
            splits = strSplitStringCol(v, splitRegEx);
            for (Vec split : splits) vs.add(split);
        }
    }
    return new ValFrame(new Frame(vs.toArray(new Vec[vs.size()])));
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) ArrayList(java.util.ArrayList) BufferedString(water.parser.BufferedString)

Aggregations

ValFrame (water.rapids.vals.ValFrame)132 Frame (water.fvec.Frame)98 Val (water.rapids.Val)48 Vec (water.fvec.Vec)43 Test (org.junit.Test)38 MRTask (water.MRTask)32 Chunk (water.fvec.Chunk)24 NewChunk (water.fvec.NewChunk)23 BufferedString (water.parser.BufferedString)16 AstNumList (water.rapids.ast.params.AstNumList)11 AstNum (water.rapids.ast.params.AstNum)7 ValNum (water.rapids.vals.ValNum)7 AstRoot (water.rapids.ast.AstRoot)6 ValRow (water.rapids.vals.ValRow)6 ArrayList (java.util.ArrayList)5 Key (water.Key)5 AstStrList (water.rapids.ast.params.AstStrList)5 Futures (water.Futures)4 AstParameter (water.rapids.ast.AstParameter)4 Random (java.util.Random)3