Search in sources :

Example 1 with ValFun

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

the class AstGroup method apply.

@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Frame fr = stk.track(asts[1].exec(env)).getFrame();
    int ncols = fr.numCols();
    AstNumList groupby = check(ncols, asts[2]);
    final int[] gbCols = groupby.expand4();
    // Count of aggregates; knock off the first 4 ASTs (GB data [group-by] [order-by]...),
    // then count by triples.
    int naggs = (asts.length - 3) / 3;
    final AGG[] aggs = new AGG[naggs];
    for (int idx = 3; idx < asts.length; idx += 3) {
        Val v = asts[idx].exec(env);
        String fn = v instanceof ValFun ? v.getFun().str() : v.getStr();
        FCN fcn = FCN.valueOf(fn);
        AstNumList col = check(ncols, asts[idx + 1]);
        if (col.cnt() != 1)
            throw new IllegalArgumentException("Group-By functions take only a single column");
        // Aggregate column
        int agg_col = (int) col.min();
        if (fcn == FCN.mode && !fr.vec(agg_col).isCategorical())
            throw new IllegalArgumentException("Mode only allowed on categorical columns");
        NAHandling na = NAHandling.valueOf(asts[idx + 2].exec(env).getStr().toUpperCase());
        aggs[(idx - 3) / 3] = new AGG(fcn, agg_col, na, (int) fr.vec(agg_col).max() + 1);
    }
    // do the group by work now
    IcedHashMap<G, String> gss = doGroups(fr, gbCols, aggs);
    final G[] grps = gss.keySet().toArray(new G[gss.size()]);
    // apply an ORDER by here...
    if (gbCols.length > 0)
        Arrays.sort(grps, new java.util.Comparator<G>() {

            // Compare 2 groups.  Iterate down _gs, stop when _gs[i] > that._gs[i],
            // or _gs[i] < that._gs[i].  Order by various columns specified by
            // gbCols.  NaN is treated as least
            @Override
            public int compare(G g1, G g2) {
                for (int i = 0; i < gbCols.length; i++) {
                    if (Double.isNaN(g1._gs[i]) && !Double.isNaN(g2._gs[i]))
                        return -1;
                    if (!Double.isNaN(g1._gs[i]) && Double.isNaN(g2._gs[i]))
                        return 1;
                    if (g1._gs[i] != g2._gs[i])
                        return g1._gs[i] < g2._gs[i] ? -1 : 1;
                }
                return 0;
            }

            // I do not believe sort() calls equals() at this time, so no need to implement
            @Override
            public boolean equals(Object o) {
                throw H2O.unimpl();
            }
        });
    // Build the output!
    String[] fcnames = new String[aggs.length];
    for (int i = 0; i < aggs.length; i++) fcnames[i] = aggs[i]._fcn.toString() + "_" + fr.name(aggs[i]._col);
    MRTask mrfill = new MRTask() {

        @Override
        public void map(Chunk[] c, NewChunk[] ncs) {
            int start = (int) c[0].start();
            for (int i = 0; i < c[0]._len; ++i) {
                // One Group per row
                G g = grps[i + start];
                int j;
                for (// The Group Key, as a row
                j = 0; // The Group Key, as a row
                j < g._gs.length; // The Group Key, as a row
                j++) ncs[j].addNum(g._gs[j]);
                for (int a = 0; a < aggs.length; a++) ncs[j++].addNum(aggs[a]._fcn.postPass(g._dss[a], g._ns[a]));
            }
        }
    };
    Frame f = buildOutput(gbCols, naggs, fr, fcnames, grps.length, mrfill);
    return new ValFrame(f);
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) ValFrame(water.rapids.vals.ValFrame) ValFun(water.rapids.vals.ValFun) MRTask(water.MRTask) AstNumList(water.rapids.ast.params.AstNumList)

Example 2 with ValFun

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

the class Env method lookup.

// ----
// Variable lookup
public Val lookup(String id) {
    // Lexically scoped functions first
    Val val = _scope == null ? null : _scope.lookup(id);
    if (val != null)
        return val;
    // disallow TRUE/FALSE/NA to be overwritten by keys in the DKV... just way way saner this way
    if (CONSTS.containsKey(id)) {
        return CONSTS.get(id).exec(this);
    }
    // Now the DKV
    Value value = DKV.get(Key.make(expand(id)));
    if (value != null) {
        if (value.isFrame())
            return addGlobals((Frame) value.get());
        // Only understand Frames right now
        throw new IllegalArgumentException("DKV name lookup of " + id + " yielded an instance of type " + value.className() + ", but only Frame is supported");
    }
    // Now the built-ins
    AstPrimitive ast = PRIMS.get(id);
    if (ast != null)
        return new ValFun(ast);
    throw new IllegalArgumentException("Name lookup of '" + id + "' failed");
}
Also used : ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) ValFun(water.rapids.vals.ValFun)

Aggregations

Frame (water.fvec.Frame)2 ValFrame (water.rapids.vals.ValFrame)2 ValFun (water.rapids.vals.ValFun)2 MRTask (water.MRTask)1 Val (water.rapids.Val)1 AstNumList (water.rapids.ast.params.AstNumList)1