Search in sources :

Example 71 with Val

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

the class AstLAnd method apply.

@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Val left = stk.track(asts[1].exec(env));
    // If the left is zero, just return the left
    if (left.isNum()) {
        double d = left.getNum();
        if (d == 0)
            return left;
    }
    Val rite = stk.track(asts[2].exec(env));
    return prim_apply(left, rite);
}
Also used : Val(water.rapids.Val)

Example 72 with Val

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

the class AstLOr method apply.

@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Val left = stk.track(asts[1].exec(env));
    // If the left is 1, just return the left
    if (left.isNum()) {
        double d = left.getNum();
        if (d == 1)
            return left;
    }
    Val rite = stk.track(asts[2].exec(env));
    return prim_apply(left, rite);
}
Also used : Val(water.rapids.Val)

Example 73 with Val

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

the class AstMoment method exec.

@Override
protected ValFrame exec(Val[] args) {
    // Parse the input arguments, verifying their validity.
    boolean naResult = false;
    long numRows = -1;
    int[] timeparts = new int[7];
    ArrayList<Integer> chunksmap = new ArrayList<>(7);
    ArrayList<Vec> timevecs = new ArrayList<>(7);
    for (int i = 0; i < 7; i++) {
        Val vi = args[i + 1];
        if (vi.isFrame()) {
            Frame fr = vi.getFrame();
            if (fr.numCols() != 1)
                throw new IllegalArgumentException("Argument " + i + " is a frame with " + fr.numCols() + " columns");
            if (!fr.vec(0).isNumeric())
                throw new IllegalArgumentException("Argument " + i + " is not a numeric column");
            if (fr.numRows() == 0)
                throw new IllegalArgumentException("Column " + i + " has 0 rows");
            if (fr.numRows() == 1) {
                double d = fr.vec(0).at(0);
                if (Double.isNaN(d))
                    naResult = true;
                else
                    timeparts[i] = (int) d;
            } else {
                if (numRows == -1)
                    numRows = fr.numRows();
                if (fr.numRows() != numRows)
                    throw new IllegalArgumentException("Incompatible vec " + i + " having " + fr.numRows() + " rows, whereas " + "other vecs have " + numRows + " rows.");
                timevecs.add(fr.vec(0));
                chunksmap.add(i);
            }
        } else if (vi.isNum()) {
            double d = vi.getNum();
            if (Double.isNaN(d))
                naResult = true;
            else
                timeparts[i] = (int) d;
        } else {
            throw new IllegalArgumentException("Argument " + i + " is neither a number nor a frame");
        }
    }
    // If all arguments are scalars, return a 1x1 frame
    if (timevecs.isEmpty()) {
        double val = Double.NaN;
        if (!naResult) {
            try {
                val = ISOChronology.getInstanceUTC().getDateTimeMillis(timeparts[0], timeparts[1], timeparts[2], timeparts[3], timeparts[4], timeparts[5], timeparts[6]);
            } catch (IllegalFieldValueException ignored) {
            }
        }
        return make1x1Frame(val);
    }
    // If the result is all-NAs, make a constant NA vec
    if (naResult) {
        long n = timevecs.get(0).length();
        Vec v = Vec.makeCon(Double.NaN, n, Vec.T_TIME);
        Frame fr = new Frame(Key.<Frame>make(), new String[] { "time" }, new Vec[] { v });
        return new ValFrame(fr);
    }
    // Some arguments are vecs -- create a frame of the same size
    Vec[] vecs = timevecs.toArray(new Vec[timevecs.size()]);
    int[] cm = ArrayUtils.toPrimitive(chunksmap);
    Frame fr = new SetTimeTask(timeparts, cm).doAll(Vec.T_TIME, vecs).outputFrame(new String[] { "time" }, null);
    return new ValFrame(fr);
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) IllegalFieldValueException(org.joda.time.IllegalFieldValueException) ArrayList(java.util.ArrayList) ValFrame(water.rapids.vals.ValFrame) Vec(water.fvec.Vec)

Example 74 with Val

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

the class AstSumAxis method apply.

@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Val val1 = asts[1].exec(env);
    if (val1 instanceof ValFrame) {
        Frame fr = stk.track(val1).getFrame();
        boolean na_rm = asts[2].exec(env).getNum() == 1;
        boolean axis = asts.length == 4 && (asts[3].exec(env).getNum() == 1);
        return axis ? rowwiseSum(fr, na_rm) : colwisesum(fr, na_rm);
    } else if (val1 instanceof ValRow) {
        // This may be called from AstApply when doing per-row computations.
        double[] row = val1.getRow();
        boolean na_rm = asts[2].exec(env).getNum() == 1;
        double d = 0;
        int n = 0;
        for (double r : row) {
            if (Double.isNaN(r)) {
                if (!na_rm)
                    return new ValRow(new double[] { Double.NaN }, null);
            } else {
                d += r;
                n++;
            }
        }
        return new ValRow(new double[] { d }, null);
    } else
        throw new IllegalArgumentException("Incorrect argument to (sum): expected a frame or a row, received " + val1.getClass());
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) ValRow(water.rapids.vals.ValRow)

Example 75 with Val

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

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