Search in sources :

Example 1 with Val

use of water.rapids.Val 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 Val

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

the class AstScale 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();
    // Peel out the bias/shift/mean
    double[] means;
    if (asts[2] instanceof AstNumList) {
        means = ((AstNumList) asts[2]).expand();
        if (means.length != ncols)
            throw new IllegalArgumentException("Numlist must be the same length as the columns of the Frame");
    } else {
        double d = asts[2].exec(env).getNum();
        if (// No change on means, so zero-filled
        d == 0)
            // No change on means, so zero-filled
            means = new double[ncols];
        else if (d == 1)
            means = fr.means();
        else
            throw new IllegalArgumentException("Only true or false allowed");
    }
    // Peel out the scale/stddev
    double[] mults;
    if (asts[3] instanceof AstNumList) {
        mults = ((AstNumList) asts[3]).expand();
        if (mults.length != ncols)
            throw new IllegalArgumentException("Numlist must be the same length as the columns of the Frame");
    } else {
        Val v = asts[3].exec(env);
        if (v instanceof ValFrame) {
            mults = toArray(v.getFrame().anyVec());
        } else {
            double d = v.getNum();
            if (d == 0)
                // No change on mults, so one-filled
                Arrays.fill(mults = new double[ncols], 1.0);
            else if (d == 1)
                mults = fr.mults();
            else
                throw new IllegalArgumentException("Only true or false allowed");
        }
    }
    // Update in-place.
    // Make final copy for closure
    final double[] fmeans = means;
    // Make final copy for closure
    final double[] fmults = mults;
    new MRTask() {

        @Override
        public void map(Chunk[] cs) {
            for (int i = 0; i < cs.length; i++) for (int row = 0; row < cs[i]._len; row++) cs[i].set(row, (cs[i].atd(row) - fmeans[i]) * fmults[i]);
        }
    }.doAll(fr);
    return new ValFrame(fr);
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) MRTask(water.MRTask) Chunk(water.fvec.Chunk) AstNumList(water.rapids.ast.params.AstNumList)

Example 3 with Val

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

the class AstRepLen method apply.

@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Val v = asts[1].exec(env);
    long length = (long) asts[2].exec(env).getNum();
    Frame ff;
    if (v instanceof ValFrame)
        ff = stk.track(v).getFrame();
    else
        return new ValFrame(new Frame(Vec.makeCon(v.getNum(), length)));
    final Frame fr = ff;
    if (fr.numCols() == 1) {
        Vec vec = Vec.makeRepSeq(length, fr.numRows());
        new MRTask() {

            @Override
            public void map(Chunk c) {
                for (int i = 0; i < c._len; ++i) c.set(i, fr.anyVec().at((long) c.atd(i)));
            }
        }.doAll(vec);
        vec.setDomain(fr.anyVec().domain());
        return new ValFrame(new Frame(vec));
    } else {
        Frame f = new Frame();
        for (int i = 0; i < length; ++i) f.add(Frame.defaultColName(f.numCols()), fr.vec(i % fr.numCols()));
        return new ValFrame(f);
    }
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) Vec(water.fvec.Vec) MRTask(water.MRTask) Chunk(water.fvec.Chunk)

Example 4 with Val

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

the class AstReducerOp method apply.

@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    // NOTE: no *initial* value needed for the reduction.  Instead, the
    // reduction op is used between pairs of actual values, and never against
    // the empty list.  NaN is returned if there are *no* values in the
    // reduction.
    double d = Double.NaN;
    for (int i = 1; i < asts.length; i++) {
        Val val = asts[i].exec(env);
        double d2 = val.isFrame() ? new AstReducerOp.RedOp().doAll(stk.track(val).getFrame())._d : val.getNum();
        if (i == 1)
            d = d2;
        else
            d = op(d, d2);
    }
    return new ValNum(d);
}
Also used : Val(water.rapids.Val) ValNum(water.rapids.vals.ValNum)

Example 5 with Val

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

the class AstRollupOp method apply.

@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Val arg1 = asts[1].exec(env);
    if (arg1.isRow()) {
        // Row-wise operation
        double[] ds = arg1.getRow();
        double d = ds[0];
        for (int i = 1; i < ds.length; i++) d = op(d, ds[i]);
        return new ValRow(new double[] { d }, null);
    }
    // Normal column-wise operation
    Frame fr = stk.track(arg1).getFrame();
    Vec[] vecs = fr.vecs();
    if (vecs.length == 0 || vecs[0].naCnt() > 0)
        return new ValNum(Double.NaN);
    double d = rup(vecs[0]);
    for (int i = 1; i < vecs.length; i++) {
        if (vecs[i].naCnt() > 0)
            return new ValNum(Double.NaN);
        d = op(d, rup(vecs[i]));
    }
    return new ValNum(d);
}
Also used : Val(water.rapids.Val) Frame(water.fvec.Frame) ValRow(water.rapids.vals.ValRow) Vec(water.fvec.Vec) ValNum(water.rapids.vals.ValNum)

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