Search in sources :

Example 6 with AstNum

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

the class AstColNames method apply.

@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Frame fr = stk.track(asts[1].exec(env)).getFrame();
    if (asts[2] instanceof AstNumList) {
        if (!(asts[3] instanceof AstStrList))
            throw new IllegalArgumentException("Column naming requires a string-list, but found a " + asts[3].getClass());
        AstNumList cols = ((AstNumList) asts[2]);
        AstStrList nams = ((AstStrList) asts[3]);
        int[] d = cols.expand4();
        if (d.length != nams._strs.length)
            throw new IllegalArgumentException("Must have the same number of column choices as names");
        for (int i = 0; i < d.length; i++) fr._names[d[i]] = nams._strs[i];
    } else if ((asts[2] instanceof AstNum)) {
        int col = (int) (asts[2].exec(env).getNum());
        String name = asts[3].exec(env).getStr();
        fr._names[col] = name;
    } else
        throw new IllegalArgumentException("Column naming requires a number-list, but found a " + asts[2].getClass());
    // Update names in DKV
    if (fr._key != null)
        DKV.put(fr);
    return new ValFrame(fr);
}
Also used : AstStrList(water.rapids.ast.params.AstStrList) ValFrame(water.rapids.vals.ValFrame) AstNum(water.rapids.ast.params.AstNum) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) AstNumList(water.rapids.ast.params.AstNumList)

Example 7 with AstNum

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

the class AstColPySlice method apply.

@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Val v = stk.track(asts[1].exec(env));
    AstParameter colList = (AstParameter) asts[2];
    if (v instanceof ValRow) {
        ValRow vv = (ValRow) v;
        return vv.slice(colList.columns(vv.getNames()));
    }
    Frame fr = v.getFrame();
    int[] cols = colList.columns(fr.names());
    Frame fr2 = new Frame();
    if (// Empty inclusion list?
    cols.length == 0)
        return new ValFrame(fr2);
    if (// Negative cols have number of cols added
    cols[0] < 0)
        for (int i = 0; i < cols.length; i++) cols[i] += fr.numCols();
    if (// Singletons must be in-range
    asts[2] instanceof AstNum && (cols[0] < 0 || cols[0] >= fr.numCols()))
        throw new IllegalArgumentException("Column must be an integer from 0 to " + (fr.numCols() - 1));
    for (// For all included columns
    int col : // For all included columns
    cols) if (// Ignoring out-of-range ones
    col >= 0 && col < fr.numCols())
        fr2.add(fr.names()[col], fr.vecs()[col]);
    return new ValFrame(fr2);
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) AstNum(water.rapids.ast.params.AstNum) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) ValRow(water.rapids.vals.ValRow) AstParameter(water.rapids.ast.AstParameter)

Aggregations

AstNum (water.rapids.ast.params.AstNum)7 ValFrame (water.rapids.vals.ValFrame)7 Frame (water.fvec.Frame)6 AstNumList (water.rapids.ast.params.AstNumList)6 AstStr (water.rapids.ast.params.AstStr)3 AstStrList (water.rapids.ast.params.AstStrList)3 MRTask (water.MRTask)2 Chunk (water.fvec.Chunk)2 Vec (water.fvec.Vec)2 AstParameter (water.rapids.ast.AstParameter)2 AstRoot (water.rapids.ast.AstRoot)2 QuantileModel (hex.quantile.QuantileModel)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 NewChunk (water.fvec.NewChunk)1 BufferedString (water.parser.BufferedString)1 Val (water.rapids.Val)1 AstExec (water.rapids.ast.AstExec)1 AstFrame (water.rapids.ast.AstFrame)1 AstId (water.rapids.ast.params.AstId)1