@Override public TileData<Double> deserialize(TileIndex index, InputStream rawData) throws IOException { ZipInputStream stream = new ZipInputStream(rawData); stream.getNextEntry(); ObjectInputStream ois = new ObjectInputStream(stream); try { double[] data = (double[]) ois.readObject(); List<Double> d = Doubles.asList(data); TileData<Double> tileData = new DenseTileData<Double>(index, d); return tileData; } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; }
@Override public <T> List<TileData<T>> readTiles (String pyramidId, TileSerializer<T> serializer, Iterable<TileIndex> tiles) throws IOException { List<TileData<T>> results = new ArrayList<>(); for (TileIndex rawIndex: tiles) { TileIndex index = new TileIndex(rawIndex.getLevel(), rawIndex.getX(), rawIndex.getY(), 1, 1); TileData<T> tile = new DenseTileData<>(index); results.add(tile); } return results; }
@Override public <T> InputStream getTileStream (String pyramidId, TileSerializer<T> serializer, TileIndex rawIndex) throws IOException { TileIndex index = new TileIndex(rawIndex.getLevel(), rawIndex.getX(), rawIndex.getY(), 1, 1); ByteArrayOutputStream baos = new ByteArrayOutputStream(); serializer.serialize(new DenseTileData<T>(index), baos); return new ByteArrayInputStream(baos.toByteArray()); }
public TileData<Map<String, List<Pair<String,Long>>>> getRawData() { List< Map<String, List<Pair<String,Long>>> > rawBins = AnnotationBin.convertToRaw( getData() ); return new DenseTileData<>( getDefinition(), rawBins ); }
@Test public void testUnmodifiableBinReplacement () { List<Integer> binVal = new ArrayList<>(); binVal.add(3); binVal.add(4); binVal.add(5); TileData<List<Integer>> base = new DenseTileData<List<Integer>>(new TileIndex(0, 0, 0, 1, 1)); base.setBin(0, 0, Collections.unmodifiableList(binVal)); TileData<List<Integer>> slice = new DenseTileMultiSliceView<Integer>(base, Arrays.asList(2, 0)); slice.setBin(0, 0, Arrays.asList(6, 2)); Assert.assertEquals(2, base.getBin(0, 0).get(0).intValue()); Assert.assertEquals(4, base.getBin(0, 0).get(1).intValue()); Assert.assertEquals(6, base.getBin(0, 0).get(2).intValue()); Assert.assertEquals(3, binVal.get(0).intValue()); Assert.assertEquals(4, binVal.get(1).intValue()); Assert.assertEquals(5, binVal.get(2).intValue()); }
private TileData<Double> createRandomTile () { TileIndex index = new TileIndex(0, 0, 0); TileData<Double> datum = new DenseTileData<>(index); for (int x=0; x<index.getXBins(); ++x) { for (int y=0; y<index.getYBins(); ++y) { datum.setBin(x, y, Math.random()); } } return datum; } private TileData<Double> addTiles (TileData<Double> a, TileData<Double> b) {
@Test public void testModifiableBinReplacement () { List<Integer> binVal = new ArrayList<>(); binVal.add(3); binVal.add(4); binVal.add(5); TileData<List<Integer>> base = new DenseTileData<List<Integer>>(new TileIndex(0, 0, 0, 1, 1)); base.setBin(0, 0, binVal); TileData<List<Integer>> slice = new DenseTileMultiSliceView<Integer>(base, Arrays.asList(2, 0)); slice.setBin(0, 0, Arrays.asList(6, 2)); Assert.assertEquals(2, base.getBin(0, 0).get(0).intValue()); Assert.assertEquals(4, base.getBin(0, 0).get(1).intValue()); Assert.assertEquals(6, base.getBin(0, 0).get(2).intValue()); Assert.assertEquals(2, binVal.get(0).intValue()); Assert.assertEquals(4, binVal.get(1).intValue()); Assert.assertEquals(6, binVal.get(2).intValue()); }
@Test public void testNullBinReplacement () { TileData<List<Integer>> base = new DenseTileData<List<Integer>>(new TileIndex(0, 0, 0, 1, 1), (List<Integer>) null); TileData<List<Integer>> slice = new DenseTileMultiSliceView<Integer>(base, Arrays.asList(2, 0)); slice.setBin(0, 0, Arrays.asList(4, 2)); Assert.assertEquals(2, base.getBin(0, 0).get(0).intValue()); Assert.assertNull( base.getBin(0, 0).get(1)); Assert.assertEquals(4, base.getBin(0, 0).get(2).intValue()); } @Test
@Test public void testCompression () throws IOException { TileData<Double> data = new DenseTileData<Double>(new TileIndex(0, 0, 0), 1.1); TileSerializer<Double> serializer = new KryoSerializer<Double>(new TypeDescriptor(Double.class)); ByteArrayOutputStream output = new ByteArrayOutputStream(); serializer.serialize(data, output); output.flush(); output.close(); byte[] buffer = output.toByteArray(); Assert.assertTrue(buffer.length < 256*256); }
@Test public void testBinRetrieval () { TileData<List<Integer>> base = new DenseTileData(new TileIndex(0, 0, 0, 1, 1)); base.setBin(0, 0, Arrays.asList(-0, -1, -2, -3, -4, -5, -6, -7)); TileData<List<Integer>> slice1 = new DenseTileMultiSliceView<Integer>(base, Arrays.asList(1, 3, 5, 7)); TileData<List<Integer>> slice2 = new DenseTileMultiSliceView<Integer>(base, Arrays.asList(6, 4, 2, 0)); List<Integer> b1 = slice1.getBin(0, 0); Assert.assertEquals(4, b1.size()); Assert.assertEquals(-1, b1.get(0).intValue()); Assert.assertEquals(-3, b1.get(1).intValue()); Assert.assertEquals(-5, b1.get(2).intValue()); Assert.assertEquals(-7, b1.get(3).intValue()); List<Integer> b2 = slice2.getBin(0, 0); Assert.assertEquals(4, b2.size()); Assert.assertEquals(-6, b2.get(0).intValue()); Assert.assertEquals(-4, b2.get(1).intValue()); Assert.assertEquals(-2, b2.get(2).intValue()); Assert.assertEquals(-0, b2.get(3).intValue()); } }
private TileData<Double> addTiles (TileData<Double> a, TileData<Double> b) { int xBins = a.getDefinition().getXBins(); int yBins = a.getDefinition().getYBins(); List<Double> dc = new ArrayList<>(); for (int y = 0; y < yBins; ++y) { for (int x = 0; x < xBins; ++x) { dc.add(a.getBin(x, y) + b.getBin(x, y)); } } return new DenseTileData<Double>(a.getDefinition(), dc); }
public void testBackwardCompatbilitySerialize() throws IOException{ TestPyramidIO io = new TestPyramidIO(); TileSerializer<Double> serializer = new com.oculusinfo.binning.io.serialization.impl.BackwardCompatibilitySerializer(); TileIndex index = new TileIndex(0, 0, 0, 1, 1); TileData<Double> tile = new DenseTileData<Double>(index); tile.setBin(0, 0, 5.0); io.initializeForWrite("backwardsCompatibilityTest"); io.writeTiles("backwardsCompatibilityTest", serializer, Collections.singleton(tile)); List<TileData<Double>> tiles = io.readTiles("backwardsCompatibilityTest", serializer, Collections.singleton(index)); TileData<Double> tileOut = tiles.get(0); double dataOut = tileOut.getBin(0, 0); Assert.assertEquals(5.0, dataOut); }
@Test(expected = IllegalArgumentException.class) public void testBadRelativeLevel() { TileData<Integer> source = new DenseTileData<>(new TileIndex(3, 0, 0)); SubTileDataView.fromSourceAbsolute(source, new TileIndex(2, 1, 1)); }
@Test(expected = IllegalArgumentException.class) public void testBadRelativeIndex () { TileData<Integer> source = new DenseTileData<>(new TileIndex(1, 0, 0)); SubTileDataView.fromSourceAbsolute(source, new TileIndex(2, 2, 1)); }
@Test public void testUnrequestedCacheRemovalTimeout () throws InterruptedException { int i; for (i=0; i<_N; ++i) { TileData<Integer> data = new DenseTileData<Integer>(_indices[i], i); // provide and listen to each tile checkRequest(_indices[i], true); _cache.provideTile(data); } Thread.sleep(260); // Make another request checkRequest(_indices[i], true); // Make sure all requests so far are still there for (i=1; i<_N+1; ++i) { checkRequest(_indices[i], false); } checkRequest(_indices[0], true); }
@Test public void testUnrequestedCacheRemoval () { int i; for (i=0; i<_N; ++i) { TileData<Integer> data = new DenseTileData<Integer>(_indices[i], i); // provide and listen to each tile checkRequest(_indices[i], true); _cache.provideTile(data); } // Make another request checkRequest(_indices[i], true); // Make sure all requests so far are still there for (i=0; i<_N+1; ++i) { checkRequest(_indices[i], false); } }
@Before public void setup () { _index = new TileIndex(0, 0, 0, 2, 2); _tile = new DenseTileData<>(_index); _tile.setBin(0, 0, 1.0); _tile.setBin(0, 1, 2.0); _tile.setBin(1, 0, 3.0); _tile.setBin(1, 1, 4.0); _tile.setMetaData("a", "abc"); _tile.setMetaData("b", "bcd"); }
@Test public void testAverageTileBucketView () { // since we modify the tile, we won't use a static class tile TileData<List<Double>> sourceListTile = new DenseTileData<>(new TileIndex(1, 1, 1, 2, 2), Arrays.asList(Arrays.asList( 1.0, 2.0, 3.0, 4.0), Arrays.asList( 2.0, 3.0, 4.0, 1.0), Arrays.asList( 3.0, 4.0, 1.0, 2.0), Arrays.asList( 4.0, 3.0, 2.0, 1.0))); AverageTileBucketView<Double> underTest = new AverageTileBucketView<Double>(sourceListTile, 0, 3); Assert.assertEquals(1, underTest.getDefinition().getLevel()); Assert.assertEquals(1, underTest.getDefinition().getX()); Assert.assertEquals(1, underTest.getDefinition().getY()); Assert.assertEquals(2, underTest.getDefinition().getXBins()); Assert.assertEquals(2, underTest.getDefinition().getYBins()); for (int y=0; y<underTest.getDefinition().getYBins(); y++) { for (int x=0; x<underTest.getDefinition().getXBins(); x++) { Assert.assertEquals(2.5, underTest.getBin(x,y).get(0).doubleValue(), 0.01); // value '-' average } } }
/** * Test if two serializers are equal, by creating a tile data, and making sure each reads the * output of the other. */ public static <T> void assertSerializersEqual (TileSerializer<T> expected, TileSerializer<T> actual, DataSource<T> random) throws Exception { int nX = 8; int nY = 8; TileIndex index = new TileIndex(4, 3, 2, nX, nY); TileData<T> data = new DenseTileData<>(index); for (int x=0; x<nX; ++x) { for (int y=0; y<nY; ++y) { data.setBin(x, y, random.create()); } } assertTilesEqual(data, roundTrip(data, expected, actual)); assertTilesEqual(data, roundTrip(data, actual, expected)); }
@Test public void testSimpleCacheRemoval () { CacheRequestCallback<Integer> callback = new NoOpCacheRequestCallback(); int i; for (i=0; i<_N; ++i) { TileData<Integer> data = new DenseTileData<Integer>(_indices[i], i); // provide and listen to each tile checkRequest(_indices[i], true); _cache.requestTile(_indices[i], callback); _cache.provideTile(data); } // Make another request checkRequest(_indices[i], true); // Make sure all but our first request is still there for (i=1; i<_N; ++i) { checkRequest(_indices[i], false); } // Make sure our first request is gone checkRequest(_indices[0], true); }