Search in sources :

Example 1 with Session

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

the class AstMomentTest method testOneRowFrame.

@Test
public void testOneRowFrame() {
    Scope.enter();
    try {
        Session s = new Session();
        new TestFrameBuilder().withName("$frame1", s).withColNames("day", "hour").withDataForCol(0, ar(1)).withDataForCol(1, ard(Double.NaN)).build();
        new TestFrameBuilder().withName("$month", s).withColNames("month").withDataForCol(0, ar(2, 3)).build();
        Val result = Rapids.exec("(moment 2010 $month (cols $frame1 'day') 0 0 0 0)->$res1", s);
        assertTrue(result.isFrame());
        Frame fr = result.getFrame();
        Scope.track(fr);
        assertEquals(1, fr.numCols());
        assertEquals(2, fr.numRows());
        assertEquals(Vec.T_TIME, fr.vec(0).get_type());
        result = Rapids.exec("(moment 2010 $month 1 (cols $frame1 'hour') 0 0 0)->$res2", s);
        assertTrue(result.isFrame());
        fr = result.getFrame();
        Scope.track(fr);
        assertEquals(1, fr.numCols());
        assertEquals(2, fr.numRows());
        assertEquals(Vec.T_TIME, fr.vec(0).get_type());
        assertTrue(Double.isNaN(fr.vec(0).at(0)));
        assertTrue(Double.isNaN(fr.vec(0).at(1)));
    } finally {
        Scope.exit();
    }
}
Also used : Val(water.rapids.Val) Frame(water.fvec.Frame) TestFrameBuilder(water.fvec.TestFrameBuilder) Session(water.rapids.Session) Test(org.junit.Test)

Example 2 with Session

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

the class AstTmpAssignTest method testDollarIds.

@Test
public void testDollarIds() {
    Frame f = null, v, w;
    try {
        Session sess = new Session();
        String expid1 = "id1~" + sess.id();
        f = ArrayUtils.frame(Key.<Frame>make(), ar("a", "b"), ard(1, -1), ard(2, 0), ard(3, 1));
        v = Rapids.exec("(, " + f._key + ")->$id1", sess).getFrame();
        w = DKV.get(expid1).get();
        assertArrayEquals(f._names, v._names);
        assertEquals(expid1, v._key.toString());
        assertEquals(expid1, new Env(sess).expand("$id1"));
        assertNotEquals(f._key, v._key);
        assertEquals(w, v);
        String expid2 = "foo~" + sess.id();
        Rapids.exec("(rename '$id1' '$foo')", sess);
        DKV.get(expid2).get();
        assertEquals(DKV.get(expid1), null);
        Rapids.exec("(rm $foo)", sess);
        assertEquals(DKV.get(expid2), null);
    } finally {
        if (f != null)
            f.delete();
    }
}
Also used : Frame(water.fvec.Frame) Env(water.rapids.Env) Session(water.rapids.Session) Test(org.junit.Test)

Example 3 with Session

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

the class RapidsHandler method exec.

public RapidsSchemaV3 exec(int version, RapidsSchemaV3 rapids) {
    if (rapids == null)
        return null;
    if (!StringUtils.isNullOrEmpty(rapids.id))
        throw new H2OIllegalArgumentException("Field RapidsSchemaV3.id is deprecated and should not be set " + rapids.id);
    if (StringUtils.isNullOrEmpty(rapids.ast))
        return rapids;
    if (StringUtils.isNullOrEmpty(rapids.session_id))
        rapids.session_id = "_specialSess";
    Session ses = RapidsHandler.SESSIONS.get(rapids.session_id);
    if (ses == null) {
        ses = new Session(rapids.session_id);
        RapidsHandler.SESSIONS.put(rapids.session_id, ses);
    }
    Val val;
    try {
        // This call is synchronized on the session instance
        val = Rapids.exec(rapids.ast, ses);
    } catch (IllegalArgumentException e) {
        throw e;
    } catch (Throwable e) {
        Log.err(e);
        e.printStackTrace();
        throw e;
    }
    switch(val.type()) {
        case Val.NUM:
            return new RapidsNumberV3(val.getNum());
        case Val.NUMS:
            return new RapidsNumbersV3(val.getNums());
        case Val.ROW:
            return new RapidsNumbersV3(val.getRow());
        case Val.STR:
            return new RapidsStringV3(val.getStr());
        case Val.STRS:
            return new RapidsStringsV3(val.getStrs());
        case Val.FRM:
            return new RapidsFrameV3(val.getFrame());
        case Val.FUN:
            return new RapidsFunctionV3(val.getFun().toString());
        default:
            throw H2O.fail();
    }
}
Also used : Val(water.rapids.Val) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) H2OIllegalArgumentException(water.exceptions.H2OIllegalArgumentException) Session(water.rapids.Session)

Example 4 with Session

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

the class AstMomentTest method testMultiChunkedFrame.

/**
   * [PUBDEV-4044] Verify that {@code moment} function creates the same moments it's
   * supposed to create, even if the input frame has multiple chunks.
   */
@Test
public void testMultiChunkedFrame() {
    long seed = new Random().nextLong();
    Log.info("In testMultiChunkedFrame: using seed " + seed);
    Random rnd = new Random(seed);
    int N = 30000;
    long[] years = new long[N];
    long[] months = new long[N];
    long[] days = new long[N];
    for (int i = 0; i < N; i++) {
        years[i] = 1980 + rnd.nextInt(50);
        months[i] = 1 + rnd.nextInt(12);
        days[i] = 1 + rnd.nextInt(28);
    }
    int nchunks = 30;
    long[] layout = new long[nchunks];
    layout[0] = N;
    for (int i = 1; i < nchunks; i++) {
        layout[i] = N / nchunks;
        layout[0] -= layout[i];
    }
    Scope.enter();
    try {
        Session s = new Session();
        Frame f0 = new TestFrameBuilder().withName("$f0", s).withColNames("year", "month", "day").withDataForCol(0, years).withDataForCol(1, months).withDataForCol(2, days).withChunkLayout(layout).build();
        Scope.track(f0);
        Frame f1 = Rapids.exec("(moment (cols $f0 0) (cols $f0 1) (cols $f0 2) 0 0 0 0)->$f1", s).getFrame();
        Scope.track(f1);
        assertEquals(1, f1.numCols());
        assertEquals(N, f1.numRows());
        // having a global timezone setting is evil...
        ParseTime.setTimezone("UTC");
        Frame f1y = Rapids.exec("(year $f1)->$fy", s).getFrame();
        Frame f1m = Rapids.exec("(month $f1)->$fm", s).getFrame();
        Frame f1d = Rapids.exec("(day $f1)->$fd", s).getFrame();
        Scope.track(f1y, f1m, f1d);
        assertVecEquals(f0.vec(0), f1y.vec(0), 1e-10);
        assertVecEquals(f0.vec(1), f1m.vec(0), 1e-10);
        assertVecEquals(f0.vec(2), f1d.vec(0), 1e-10);
    } finally {
        Scope.exit();
    }
}
Also used : Frame(water.fvec.Frame) TestFrameBuilder(water.fvec.TestFrameBuilder) Random(java.util.Random) Session(water.rapids.Session) Test(org.junit.Test)

Example 5 with Session

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

the class AstMomentTest method testBadArguments.

@Test
public void testBadArguments() {
    Scope.enter();
    try {
        Session s = new Session();
        try {
            Rapids.exec("(moment 2000 1 1 0 0 0)->$f1", s);
            fail("Expected error: Wrong number of arguments");
        } catch (IllegalArgumentException ignored) {
        }
        try {
            Rapids.exec("(moment 2000 1 1 [0] 0 0 0)->$f2", s);
            fail("Expected error: A NumList is not allowed");
        } catch (IllegalArgumentException ignored) {
        }
        try {
            Rapids.exec("(moment '2000' 1 1 0 0 0 0)->$f3", s);
            fail("Expected error: A string is not allowed");
        } catch (IllegalArgumentException ignored) {
        }
        new TestFrameBuilder().withName("$test", s).withColNames("day", "month").withVecTypes(Vec.T_NUM, Vec.T_CAT).withDataForCol(0, ard(5, 10, 15)).withDataForCol(1, ar("April", "May", "June")).build();
        try {
            Rapids.exec("(moment 2010 1 $test 0 0 0 0)->$f4", s);
            fail("Expected error: frame with >1 columns passed");
        } catch (IllegalArgumentException ignored) {
        }
        try {
            Rapids.exec("(moment 2010 (cols $test 'month') 1 0 0 0 0)->$f5", s);
            fail("Expected error: non-numeric column used");
        } catch (IllegalArgumentException ignored) {
        }
        new TestFrameBuilder().withName("$frame0", s).withColNames("a").build();
        try {
            Rapids.exec("(moment 2010 1 $frame0 0 0 0 0)->$f6", s);
            fail("Expected error: 0-rows frame used");
        } catch (IllegalArgumentException ignored) {
        }
        new TestFrameBuilder().withName("$test2", s).withColNames("month").withDataForCol(0, ard(1, 2)).build();
        try {
            Rapids.exec("(moment 2010 (cols $test2 'month') (cols $test 'day') 0 0 0 0)->$f7", s);
            fail("Expected error: Incompatible vecs: 2 rows and 3 rows");
        } catch (IllegalArgumentException ignored) {
        }
    } finally {
        Scope.exit();
    }
}
Also used : TestFrameBuilder(water.fvec.TestFrameBuilder) Session(water.rapids.Session) Test(org.junit.Test)

Aggregations

Session (water.rapids.Session)9 Test (org.junit.Test)8 Frame (water.fvec.Frame)7 TestFrameBuilder (water.fvec.TestFrameBuilder)6 Val (water.rapids.Val)5 Env (water.rapids.Env)2 Random (java.util.Random)1 H2OIllegalArgumentException (water.exceptions.H2OIllegalArgumentException)1 ValFrame (water.rapids.vals.ValFrame)1