Search in sources :

Example 6 with TestFrameBuilder

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

use of water.fvec.TestFrameBuilder 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)

Example 8 with TestFrameBuilder

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

the class AstMomentTest method vectimeTest.

@Test
public void vectimeTest() {
    Scope.enter();
    try {
        Session session = new Session();
        new TestFrameBuilder().withName("$fr", session).withColNames("day", "hour", "min").withDataForCol(0, ard(1, 1.1, 1.2, 2, 3)).withDataForCol(1, ard(0, Double.NaN, 11, 13, 15)).withDataForCol(2, ar(0, 0, 30, 0, 0)).build();
        Val result = Rapids.exec("(moment 2016 12 (cols $fr 'day') 0 0 0 0)->$res1", session);
        assertTrue(result.isFrame());
        Frame fr = result.getFrame();
        Scope.track(fr);
        assertEquals(1, fr.numCols());
        assertEquals(5, fr.numRows());
        assertEquals(Vec.T_TIME, fr.vec(0).get_type());
        long t0 = (long) fr.vec(0).at(0);
        long t1 = (long) fr.vec(0).at(1);
        long t2 = (long) fr.vec(0).at(2);
        long t3 = (long) fr.vec(0).at(3);
        long t4 = (long) fr.vec(0).at(4);
        assertEquals(0, t0 - t1);
        assertEquals(0, t1 - t2);
        assertEquals(24 * 3600 * 1000, t3 - t2);
        assertEquals(24 * 3600 * 1000, t4 - t3);
        result = Rapids.exec("(moment 2016 12 1 (cols $fr 'hour') (cols $fr 'min') 0 0)->$res2", session);
        assertTrue(result.isFrame());
        fr = result.getFrame();
        Scope.track(fr);
        assertEquals(1, fr.numCols());
        assertEquals(5, fr.numRows());
        assertEquals(Vec.T_TIME, fr.vec(0).get_type());
        double d0 = fr.vec(0).at(0);
        double d1 = fr.vec(0).at(1);
        double d2 = fr.vec(0).at(2);
        double d3 = fr.vec(0).at(3);
        double d4 = fr.vec(0).at(4);
        assertTrue("d1 should have been NaN, got " + d1 + " instead", Double.isNaN(d1));
        assertEquals((11 * 60 + 30) * 60 * 1000, (long) (d2 - d0));
        assertEquals((13 * 60) * 60 * 1000, (long) (d3 - d0));
        assertEquals((15 * 60) * 60 * 1000, (long) (d4 - d0));
    } 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 9 with TestFrameBuilder

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

the class AstMomentTest method naTest.

@Test
public void naTest() {
    Scope.enter();
    try {
        Val result = Rapids.exec("(moment 2000 1 1 0 0 0 NaN)->$f1");
        assertTrue(result.isFrame());
        Frame fr = result.getFrame();
        Scope.track(fr);
        assertEquals(1, fr.numCols());
        assertEquals(1, fr.numRows());
        assertEquals(Vec.T_TIME, fr.vec(0).get_type());
        assertTrue(Double.isNaN(fr.vec(0).at(0)));
        Session s = new Session();
        new TestFrameBuilder().withName("$year", s).withColNames("year").withDataForCol(0, ard(2000, 2004, 2008)).build();
        result = Rapids.exec("(moment $year 1 1 0 0 NaN 0)->$f2", s);
        assertTrue(result.isFrame());
        fr = result.getFrame();
        Scope.track(fr);
        assertEquals(1, fr.numCols());
        assertEquals(3, 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)));
        assertTrue(Double.isNaN(fr.vec(0).at(2)));
        new TestFrameBuilder().withName("$day", s).withColNames("day").withDataForCol(0, ard(28, 29, 30)).build();
        result = Rapids.exec("(moment 2001 2 $day 0 0 0 0)->$f3", s);
        assertTrue(result.isFrame());
        fr = result.getFrame();
        Scope.track(fr);
        assertEquals(1, fr.numCols());
        assertEquals(3, 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)));
        assertTrue(Double.isNaN(fr.vec(0).at(2)));
    } 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 10 with TestFrameBuilder

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

the class AstCumuTest method testCumuSimple.

@Test
public void testCumuSimple() {
    Scope.enter();
    try {
        Session sess = new Session();
        new TestFrameBuilder().withName("$fr", sess).withColNames("c1", "c2", "c3", "c4").withDataForCol(0, ard(1, 1)).withDataForCol(1, ard(2, 2)).withDataForCol(2, ard(3, 3)).withDataForCol(3, ard(4, 4)).build();
        Val val = Rapids.exec("(cumsum $fr 1)", sess);
        Assert.assertTrue(val instanceof ValFrame);
        Frame res = Scope.track(val.getFrame());
        Assert.assertEquals(res.vec(0).at8(0L), 1);
        Assert.assertEquals(res.vec(1).at8(0L), 3);
        Assert.assertEquals(res.vec(2).at8(0L), 6);
        Assert.assertEquals(res.vec(3).at8(0L), 10);
        val = Rapids.exec("(cumsum $fr 0)", sess);
        Assert.assertTrue(val instanceof ValFrame);
        res = Scope.track(val.getFrame());
        Assert.assertEquals(res.vec(0).at8(1L), 2);
        Assert.assertEquals(res.vec(1).at8(1L), 4);
        Assert.assertEquals(res.vec(2).at8(1L), 6);
        Assert.assertEquals(res.vec(3).at8(1L), 8);
        val = Rapids.exec("(cummax $fr 1)", sess);
        Assert.assertTrue(val instanceof ValFrame);
        res = Scope.track(val.getFrame());
        Assert.assertEquals(res.vec(0).at8(0L), 1);
        Assert.assertEquals(res.vec(1).at8(0L), 2);
        Assert.assertEquals(res.vec(2).at8(0L), 3);
        Assert.assertEquals(res.vec(3).at8(0L), 4);
    } finally {
        Scope.exit();
    }
}
Also used : Val(water.rapids.Val) ValFrame(water.rapids.vals.ValFrame) ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) TestFrameBuilder(water.fvec.TestFrameBuilder) Session(water.rapids.Session) Test(org.junit.Test)

Aggregations

TestFrameBuilder (water.fvec.TestFrameBuilder)12 Frame (water.fvec.Frame)9 Test (org.junit.Test)8 Session (water.rapids.Session)6 Val (water.rapids.Val)4 Random (java.util.Random)3 BufferedString (water.parser.BufferedString)3 IOException (java.io.IOException)1 ByteChannel (java.nio.channels.ByteChannel)1 Vec (water.fvec.Vec)1 ValFrame (water.rapids.vals.ValFrame)1 IcedLong (water.util.IcedLong)1