Search in sources :

Example 96 with Frame

use of water.fvec.Frame 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 97 with Frame

use of water.fvec.Frame in project h2o-3 by h2oai.

the class AstMatch method apply.

@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Frame fr = stk.track(asts[1].exec(env)).getFrame();
    if ((fr.numCols() != 1) || !(fr.anyVec().isCategorical() || fr.anyVec().isString()))
        throw new IllegalArgumentException("can only match on a single categorical/string column.");
    final MRTask<?> matchTask;
    double noMatch = asts[3].exec(env).getNum();
    if (asts[2] instanceof AstNumList) {
        matchTask = new NumMatchTask(((AstNumList) asts[2]).sort().expand(), noMatch);
    } else if (asts[2] instanceof AstNum) {
        matchTask = new NumMatchTask(new double[] { asts[2].exec(env).getNum() }, noMatch);
    } else if (asts[2] instanceof AstStrList) {
        String[] values = ((AstStrList) asts[2])._strs;
        Arrays.sort(values);
        matchTask = fr.anyVec().isString() ? new StrMatchTask(values, noMatch) : new CatMatchTask(values, noMatch);
    } else if (asts[2] instanceof AstStr) {
        String[] values = new String[] { asts[2].exec(env).getStr() };
        matchTask = fr.anyVec().isString() ? new StrMatchTask(values, noMatch) : new CatMatchTask(values, noMatch);
    } else
        throw new IllegalArgumentException("Expected numbers/strings. Got: " + asts[2].getClass());
    Frame result = matchTask.doAll(Vec.T_NUM, fr.anyVec()).outputFrame();
    return new ValFrame(result);
}
Also used : AstStrList(water.rapids.ast.params.AstStrList) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) BufferedString(water.parser.BufferedString) ValFrame(water.rapids.vals.ValFrame) AstNum(water.rapids.ast.params.AstNum) AstStr(water.rapids.ast.params.AstStr) AstNumList(water.rapids.ast.params.AstNumList)

Example 98 with Frame

use of water.fvec.Frame in project h2o-3 by h2oai.

the class AstGrep method exec.

@Override
protected Val exec(Val[] args) {
    Frame fr = args[1].getFrame();
    String regex = args[2].getStr();
    boolean ignoreCase = args[3].getNum() == 1;
    boolean invert = args[4].getNum() == 1;
    boolean outputLogical = args[5].getNum() == 1;
    GrepHelper grepHelper = new GrepHelper(regex, ignoreCase, invert, outputLogical);
    if ((fr.numCols() != 1) || !(fr.anyVec().isCategorical() || fr.anyVec().isString()))
        throw new IllegalArgumentException("can only grep on a single categorical/string column.");
    Vec v = fr.anyVec();
    assert v != null;
    Frame result;
    if (v.isCategorical()) {
        int[] filtered = grepDomain(grepHelper, v);
        Arrays.sort(filtered);
        result = new GrepCatTask(grepHelper, filtered).doAll(Vec.T_NUM, v).outputFrame();
    } else {
        result = new GrepStrTask(grepHelper).doAll(Vec.T_NUM, v).outputFrame();
    }
    return new ValFrame(result);
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) Vec(water.fvec.Vec) BufferedString(water.parser.BufferedString)

Example 99 with Frame

use of water.fvec.Frame in project h2o-3 by h2oai.

the class AstAsDate method apply.

@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    Frame fr = stk.track(asts[1].exec(env)).getFrame();
    Vec vec = fr.vecs()[0];
    if (fr.vecs().length != 1 || !(vec.isCategorical() || vec.isString()))
        throw new IllegalArgumentException("as.Date requires a single column of factors or strings");
    final String format = asts[2].exec(env).getStr();
    if (format.isEmpty())
        throw new IllegalArgumentException("as.Date requires a non-empty format string");
    // check the format string more?
    final String[] dom = vec.domain();
    final boolean isStr = dom == null && vec.isString();
    assert isStr || dom != null : "as.Date error: domain is null, but vec is not String";
    Frame fr2 = new MRTask() {

        private transient DateTimeFormatter _fmt;

        @Override
        public void setupLocal() {
            _fmt = ParseTime.forStrptimePattern(format).withZone(ParseTime.getTimezone());
        }

        @Override
        public void map(Chunk c, NewChunk nc) {
            //done on each node in lieu of rewriting DateTimeFormatter as Iced
            String date;
            BufferedString tmpStr = new BufferedString();
            for (int i = 0; i < c._len; ++i) {
                if (!c.isNA(i)) {
                    if (isStr)
                        date = c.atStr(tmpStr, i).toString();
                    else
                        date = dom[(int) c.at8(i)];
                    nc.addNum(DateTime.parse(date, _fmt).getMillis(), 0);
                } else
                    nc.addNA();
            }
        }
    }.doAll(1, Vec.T_NUM, fr).outputFrame(fr._names, null);
    return new ValFrame(fr2);
}
Also used : ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) BufferedString(water.parser.BufferedString) Chunk(water.fvec.Chunk) NewChunk(water.fvec.NewChunk) NewChunk(water.fvec.NewChunk) ValFrame(water.rapids.vals.ValFrame) Vec(water.fvec.Vec) MRTask(water.MRTask) BufferedString(water.parser.BufferedString) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Example 100 with Frame

use of water.fvec.Frame in project h2o-3 by h2oai.

the class AstListTimeZones method apply.

@Override
public ValFrame apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    String[] domain = ParseTime.listTimezones().split("\n");
    double[] ds = new double[domain.length];
    for (int i = 0; i < domain.length; i++) ds[i] = i;
    Vec vec = Vec.makeVec(ds, Vec.VectorGroup.VG_LEN1.addVec());
    vec.setDomain(domain);
    return new ValFrame(new Frame(new String[] { "Timezones" }, new Vec[] { vec }));
}
Also used : ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) Vec(water.fvec.Vec)

Aggregations

Frame (water.fvec.Frame)782 Test (org.junit.Test)435 Vec (water.fvec.Vec)215 ValFrame (water.rapids.vals.ValFrame)132 NFSFileVec (water.fvec.NFSFileVec)66 Val (water.rapids.Val)65 SplitFrame (hex.SplitFrame)59 Key (water.Key)56 DeepLearningParameters (hex.deeplearning.DeepLearningModel.DeepLearningParameters)54 Chunk (water.fvec.Chunk)50 NewChunk (water.fvec.NewChunk)37 MRTask (water.MRTask)33 ShuffleSplitFrame (hex.splitframe.ShuffleSplitFrame)31 Ignore (org.junit.Ignore)28 Random (java.util.Random)26 File (java.io.File)25 BufferedString (water.parser.BufferedString)21 H2OIllegalArgumentException (water.exceptions.H2OIllegalArgumentException)19 HashMap (java.util.HashMap)17 hex (hex)16