Search in sources :

Example 11 with AstRoot

use of water.rapids.ast.AstRoot in project h2o-3 by h2oai.

the class Rapids method exec.

/**
   * Compute and return a value in this session.  Any returned frame shares
   * Vecs with the session (is not deep copied), and so must be deleted by the
   * caller (with a Rapids "rm" call) or will disappear on session exit, or is
   * a normal global frame.
   * @param rapids expression to parse
   */
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
public static Val exec(String rapids, Session session) {
    AstRoot ast = Rapids.parse(rapids);
    // on the same session, which Flow sometimes does
    synchronized (session) {
        Val val = session.exec(ast, null);
        // named temp, the ref cnts are accounted for by being in the temp table.
        if (val.isFrame()) {
            Frame frame = val.getFrame();
            assert frame._key != null : "Returned frame has no key";
            session.addRefCnt(frame, -1);
        }
        return val;
    }
}
Also used : Frame(water.fvec.Frame) AstRoot(water.rapids.ast.AstRoot)

Example 12 with AstRoot

use of water.rapids.ast.AstRoot in project h2o-3 by h2oai.

the class Rapids method parseFunctionApplication.

/**
   * Parse "function application" expression, i.e. pattern of the form "(func ...args)"
   */
private AstExec parseFunctionApplication() {
    eatChar('(');
    ArrayList<AstRoot> asts = new ArrayList<>();
    while (skipWS() != ')') asts.add(parseNext());
    eatChar(')');
    AstExec res = new AstExec(asts);
    if (peek(0) == '-') {
        eatChar('-');
        eatChar('>');
        AstId tmpid = new AstId(token());
        res = new AstExec(new AstRoot[] { new AstId("tmp="), tmpid, res });
    }
    return res;
}
Also used : AstExec(water.rapids.ast.AstExec) ArrayList(java.util.ArrayList) AstRoot(water.rapids.ast.AstRoot)

Example 13 with AstRoot

use of water.rapids.ast.AstRoot in project h2o-3 by h2oai.

the class AstApply method colwise.

// --------------------------------------------------------------------------
private ValFrame colwise(Env env, Env.StackHelp stk, Frame fr, AstPrimitive fun) {
    // Break each column into it's own Frame, then execute the function passing
    // the 1 argument.  All columns are independent, and this loop should be
    // parallized over each column.
    Vec[] vecs = fr.vecs();
    Val[] vals = new Val[vecs.length];
    AstRoot[] asts = new AstRoot[] { fun, null };
    for (int i = 0; i < vecs.length; i++) {
        asts[1] = new AstFrame(new Frame(new String[] { fr._names[i] }, new Vec[] { vecs[i] }));
        try (Env.StackHelp stk_inner = env.stk()) {
            vals[i] = fun.apply(env, stk_inner, asts);
        }
    }
    // All the resulting Vals must be the same scalar type (and if ValFrames,
    // the columns must be the same count and type).  Build a Frame result with
    // 1 row column per applied function result (per column), and as many rows
    // as there are columns in the returned Frames.
    Val v0 = vals[0];
    Vec[] ovecs = new Vec[vecs.length];
    switch(v0.type()) {
        case Val.NUM:
            for (int i = 0; i < vecs.length; i++) // Since the zero column is a number, all must be numbers
            ovecs[i] = Vec.makeCon(vals[i].getNum(), 1L);
            break;
        case Val.FRM:
            long nrows = v0.getFrame().numRows();
            for (int i = 0; i < vecs.length; i++) {
                // Since the zero column is a frame, all must be frames
                Frame res = vals[i].getFrame();
                if (res.numCols() != 1)
                    throw new IllegalArgumentException("apply result Frames must have one column, found " + res.numCols() + " cols");
                if (res.numRows() != nrows)
                    throw new IllegalArgumentException("apply result Frames must have all the same rows, found " + nrows + " rows and " + res.numRows());
                ovecs[i] = res.vec(0);
            }
            break;
        case Val.NUMS:
            for (int i = 0; i < vecs.length; i++) ovecs[i] = Vec.makeCon(vals[i].getNums()[0], 1L);
            break;
        case Val.STRS:
            throw H2O.unimpl();
        case Val.FUN:
            throw water.H2O.unimpl();
        case Val.STR:
            throw water.H2O.unimpl();
        default:
            throw water.H2O.unimpl();
    }
    return new ValFrame(new Frame(fr._names, ovecs));
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) AstRoot(water.rapids.ast.AstRoot)

Example 14 with AstRoot

use of water.rapids.ast.AstRoot in project h2o-3 by h2oai.

the class AstIsax method apply.

@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Frame fr = stk.track(asts[1].exec(env)).getFrame();
    AstRoot n = asts[2];
    AstRoot mc = asts[3];
    boolean optm_card = asts[4].exec(env).getNum() == 1;
    //Check vecs are numeric
    for (Vec v : fr.vecs()) {
        if (!v.isNumeric()) {
            throw new IllegalArgumentException("iSax only applies to numeric columns!");
        }
    }
    int numWords = (int) n.exec(env).getNum();
    int maxCardinality = (int) mc.exec(env).getNum();
    //Check numWords and maxCardinality are >=0
    if (numWords < 0) {
        throw new IllegalArgumentException("numWords must be greater than 0!");
    }
    if (maxCardinality < 0) {
        throw new IllegalArgumentException("maxCardinality must be greater than 0!");
    }
    ArrayList<String> columns = new ArrayList<>();
    for (int i = 0; i < numWords; i++) {
        columns.add("c" + i);
    }
    Frame fr2 = new AstIsax.IsaxTask(numWords, maxCardinality).doAll(numWords, Vec.T_NUM, fr).outputFrame(null, columns.toArray(new String[numWords]), null);
    int[] maxCards = new int[numWords];
    if (optm_card) {
        _domain_hm = new double[numWords][maxCardinality];
        for (double[] r : _domain_hm) Arrays.fill(r, Double.NaN);
        // see if we can reduce the cardinality by checking all unique tokens in all series in a word
        for (int i = 0; i < fr2.numCols(); i++) {
            String[] domains = fr2.vec(i).toCategoricalVec().domain();
            for (int j = 0; j < domains.length; j++) {
                _domain_hm[i][j] = Double.valueOf(domains[j]);
            }
        }
        // get the cardinalities of each word
        for (int i = 0; i < numWords; i++) {
            int cnt = 0;
            for (double d : _domain_hm[i]) {
                if (Double.isNaN(d))
                    break;
                else
                    cnt++;
            }
            maxCards[i] = cnt;
        }
        Frame fr2_reduced = new AstIsax.IsaxReduceCard(_domain_hm, maxCardinality).doAll(numWords, Vec.T_NUM, fr2).outputFrame(null, columns.toArray(new String[numWords]), null);
        Frame fr3 = new AstIsax.IsaxStringTask(maxCards).doAll(1, Vec.T_STR, fr2_reduced).outputFrame(null, new String[] { "iSax_index" }, null);
        //Not needed anymore
        fr2.delete();
        fr3.add(fr2_reduced);
        return new ValFrame(fr3);
    }
    for (int i = 0; i < numWords; ++i) {
        maxCards[i] = maxCardinality;
    }
    Frame fr3 = new AstIsax.IsaxStringTask(maxCards).doAll(1, Vec.T_STR, fr2).outputFrame(null, new String[] { "iSax_index" }, null);
    fr3.add(fr2);
    return new ValFrame(fr3);
}
Also used : ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) ArrayList(java.util.ArrayList) ValFrame(water.rapids.vals.ValFrame) Vec(water.fvec.Vec) AstRoot(water.rapids.ast.AstRoot)

Aggregations

AstRoot (water.rapids.ast.AstRoot)14 ValFrame (water.rapids.vals.ValFrame)6 Frame (water.fvec.Frame)5 ArrayList (java.util.ArrayList)4 MRTask (water.MRTask)4 Vec (water.fvec.Vec)4 Chunk (water.fvec.Chunk)3 AstNumList (water.rapids.ast.params.AstNumList)3 AstStr (water.rapids.ast.params.AstStr)3 NewChunk (water.fvec.NewChunk)2 AstNum (water.rapids.ast.params.AstNum)2 QuantileModel (hex.quantile.QuantileModel)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 RapidsExpressionV3 (water.api.schemas3.RapidsHelpV3.RapidsExpressionV3)1 AstExec (water.rapids.ast.AstExec)1 AstFrame (water.rapids.ast.AstFrame)1 AstFunction (water.rapids.ast.AstFunction)1 AstStrList (water.rapids.ast.params.AstStrList)1 AstGroup (water.rapids.ast.prims.mungers.AstGroup)1