Пример #1
0
    def testTileIteratorRetiling(self):
        from girder.plugins.large_image import tilesource
        from girder.plugins.large_image.models.image_item import ImageItem

        file = self._uploadFile(
            os.path.join(os.environ['LARGE_IMAGE_DATA'], 'sample_image.ptif'))
        itemId = str(file['itemId'])
        item = Item().load(itemId, user=self.admin)
        source = ImageItem().tileSource(item)

        # Test retiling to 500 x 400
        tileCount = 0
        for tile in source.tileIterator(scale={'magnification': 2.5},
                                        format=tilesource.TILE_FORMAT_PIL,
                                        tile_size={
                                            'width': 500,
                                            'height': 400
                                        }):
            tileCount += 1
            self.assertEqual(tile['tile'].size,
                             (tile['width'], tile['height']))
            self.assertEqual(tile['width'],
                             500 if tile['level_x'] < 7 else 148)
            self.assertEqual(tile['height'],
                             400 if tile['level_y'] < 1 else 368)
        self.assertEqual(tileCount, 16)

        # Test retiling to 300 x 275 with 50 x 40 pixels overlap with trimmed
        # edges
        tileCount = 0
        for tile in source.tileIterator(scale={'magnification': 2.5},
                                        format=tilesource.TILE_FORMAT_PIL,
                                        tile_size={
                                            'width': 300,
                                            'height': 275
                                        },
                                        tile_overlap={
                                            'x': 50,
                                            'y': 40,
                                            'edges': True
                                        }):
            tileCount += 1
            self.assertEqual(tile['tile'].size,
                             (tile['width'], tile['height']))
            self.assertEqual(
                tile['width'], 275 if not tile['level_x'] else
                300 if tile['level_x'] < 14 else 173)
            self.assertEqual(
                tile['height'], 255
                if not tile['level_y'] else 275 if tile['level_y'] < 3 else 83)
            self.assertEqual(tile['tile_overlap']['left'],
                             0 if not tile['level_x'] else 25)
            self.assertEqual(tile['tile_overlap']['right'],
                             25 if tile['level_x'] < 14 else 0)
            self.assertEqual(tile['tile_overlap']['top'],
                             0 if not tile['level_y'] else 20)
            self.assertEqual(tile['tile_overlap']['bottom'],
                             20 if tile['level_y'] < 3 else 0)
        self.assertEqual(tileCount, 60)

        # Test retiling to 300 x 275 with 50 x 40 pixels overlap without
        # trimmed edges
        tileCount = 0
        for tile in source.tileIterator(scale={'magnification': 2.5},
                                        format=tilesource.TILE_FORMAT_PIL,
                                        tile_size={
                                            'width': 300,
                                            'height': 275
                                        },
                                        tile_overlap={
                                            'x': 50,
                                            'y': 40
                                        }):
            tileCount += 1
            self.assertEqual(tile['tile'].size,
                             (tile['width'], tile['height']))
            self.assertEqual(tile['width'],
                             300 if tile['level_x'] < 14 else 148)
            self.assertEqual(tile['height'],
                             275 if tile['level_y'] < 3 else 63)
            self.assertEqual(tile['tile_overlap']['left'],
                             0 if not tile['level_x'] else 25)
            self.assertEqual(tile['tile_overlap']['right'],
                             25 if tile['level_x'] < 14 else 0)
            self.assertEqual(tile['tile_overlap']['top'],
                             0 if not tile['level_y'] else 20)
            self.assertEqual(tile['tile_overlap']['bottom'],
                             20 if tile['level_y'] < 3 else 0)
        self.assertEqual(tileCount, 60)

        # Test retiling to 300 x 275 with 51 x 41 pixels overlap with trimmed
        # edges
        tileCount = 0
        for tile in source.tileIterator(scale={'magnification': 2.5},
                                        format=tilesource.TILE_FORMAT_PIL,
                                        tile_size={
                                            'width': 300,
                                            'height': 275
                                        },
                                        tile_overlap={
                                            'x': 51,
                                            'y': 41,
                                            'edges': True
                                        }):
            tileCount += 1
            self.assertEqual(tile['tile'].size,
                             (tile['width'], tile['height']))
            self.assertEqual(
                tile['width'], 275 if not tile['level_x'] else
                300 if tile['level_x'] < 14 else 187)
            self.assertEqual(
                tile['height'], 255
                if not tile['level_y'] else 275 if tile['level_y'] < 3 else 86)
            self.assertEqual(tile['tile_overlap']['left'],
                             0 if not tile['level_x'] else 25)
            self.assertEqual(tile['tile_overlap']['right'],
                             26 if tile['level_x'] < 14 else 0)
            self.assertEqual(tile['tile_overlap']['top'],
                             0 if not tile['level_y'] else 20)
            self.assertEqual(tile['tile_overlap']['bottom'],
                             21 if tile['level_y'] < 3 else 0)
        self.assertEqual(tileCount, 60)

        # Test retiling to 300 x 275 with 51 x 41 pixels overlap without
        # trimmed edges
        tileCount = 0
        for tile in source.tileIterator(scale={'magnification': 2.5},
                                        format=tilesource.TILE_FORMAT_PIL,
                                        tile_size={
                                            'width': 300,
                                            'height': 275
                                        },
                                        tile_overlap={
                                            'x': 51,
                                            'y': 41
                                        }):
            tileCount += 1
            self.assertEqual(tile['tile'].size,
                             (tile['width'], tile['height']))
            self.assertEqual(tile['width'],
                             300 if tile['level_x'] < 14 else 162)
            self.assertEqual(tile['height'],
                             275 if tile['level_y'] < 3 else 66)
            self.assertEqual(tile['tile_overlap']['left'],
                             0 if not tile['level_x'] else 25)
            self.assertEqual(tile['tile_overlap']['right'],
                             26 if tile['level_x'] < 14 else 0)
            self.assertEqual(tile['tile_overlap']['top'],
                             0 if not tile['level_y'] else 20)
            self.assertEqual(tile['tile_overlap']['bottom'],
                             21 if tile['level_y'] < 3 else 0)
        self.assertEqual(tileCount, 60)
Пример #2
0
    def testTileIteratorSingleTile(self):
        from girder.plugins.large_image.models.image_item import ImageItem

        file = self._uploadFile(
            os.path.join(os.environ['LARGE_IMAGE_DATA'], 'sample_image.ptif'))
        itemId = str(file['itemId'])
        item = Item().load(itemId, user=self.admin)
        source = ImageItem().tileSource(item)

        # Test getting a single tile
        sourceRegion = {
            'width': 0.7,
            'height': 0.6,
            'left': 0.15,
            'top': 0.2,
            'units': 'fraction'
        }
        tileCount = 0
        for tile in source.tileIterator(region=sourceRegion,
                                        scale={'magnification': 5},
                                        tile_position=25):
            tileCount += 1
            self.assertEqual(tile['tile_position']['position'], 25)
            self.assertEqual(tile['tile_position']['level_x'], 8)
            self.assertEqual(tile['tile_position']['level_y'], 2)
            self.assertEqual(tile['tile_position']['region_x'], 4)
            self.assertEqual(tile['tile_position']['region_y'], 1)
            self.assertEqual(tile['iterator_range']['level_x_min'], 4)
            self.assertEqual(tile['iterator_range']['level_y_min'], 1)
            self.assertEqual(tile['iterator_range']['level_x_max'], 25)
            self.assertEqual(tile['iterator_range']['level_y_max'], 5)
            self.assertEqual(tile['iterator_range']['region_x_max'], 21)
            self.assertEqual(tile['iterator_range']['region_y_max'], 4)
            self.assertEqual(tile['iterator_range']['position'], 84)
        self.assertEqual(tileCount, 1)
        tiles = list(
            source.tileIterator(region=sourceRegion,
                                scale={'magnification': 5},
                                tile_position={'position': 25}))
        self.assertEqual(len(tiles), 1)
        self.assertEqual(tiles[0]['tile_position']['position'], 25)
        tiles = list(
            source.tileIterator(region=sourceRegion,
                                scale={'magnification': 5},
                                tile_position={
                                    'level_x': 8,
                                    'level_y': 2
                                }))
        self.assertEqual(len(tiles), 1)
        self.assertEqual(tiles[0]['tile_position']['position'], 25)
        tiles = list(
            source.tileIterator(region=sourceRegion,
                                scale={'magnification': 5},
                                tile_position={
                                    'region_x': 4,
                                    'region_y': 1
                                }))
        self.assertEqual(len(tiles), 1)
        self.assertEqual(tiles[0]['tile_position']['position'], 25)
        tiles = list(
            source.tileIterator(region=sourceRegion,
                                scale={'magnification': 5},
                                tile_position={'position': 90}))
        self.assertEqual(len(tiles), 0)
Пример #3
0
    def testTileIterator(self):
        from girder.plugins.large_image import tilesource
        from girder.plugins.large_image.models.image_item import ImageItem

        file = self._uploadFile(
            os.path.join(
                os.environ['LARGE_IMAGE_DATA'],
                'sample_jp2k_33003_TCGA-CV-7242-'
                '11A-01-TS1.1838afb1-9eee-4a70-9ae3-50e3ab45e242.svs'))
        itemId = str(file['itemId'])
        item = Item().load(itemId, user=self.admin)
        source = ImageItem().tileSource(item)
        tileCount = 0
        visited = {}
        for tile in source.tileIterator(format=tilesource.TILE_FORMAT_PIL,
                                        scale={'magnification': 5}):
            # Check that we haven't loaded the tile's image yet
            self.assertFalse(getattr(tile, 'loaded', None))
            visited.setdefault(tile['level_x'], {})[tile['level_y']] = True
            tileCount += 1
            self.assertEqual(tile['tile'].size,
                             (tile['width'], tile['height']))
            self.assertEqual(tile['width'],
                             256 if tile['level_x'] < 11 else 61)
            self.assertEqual(tile['height'],
                             256 if tile['level_y'] < 11 else 79)
            # Check that we have loaded the tile's image
            self.assertTrue(getattr(tile, 'loaded', None))
        self.assertEqual(tileCount, 144)
        self.assertEqual(len(visited), 12)
        self.assertEqual(len(visited[0]), 12)
        # Check with a non-native magnfication with exact=True
        tileCount = 0
        for tile in source.tileIterator(scale={
                'magnification': 4,
                'exact': True
        }):
            tileCount += 1
        self.assertEqual(tileCount, 0)
        # Check with a non-native (but factor of 2) magnfication with exact=True
        for tile in source.tileIterator(scale={
                'magnification': 2.5,
                'exact': True
        }):
            tileCount += 1
        self.assertEqual(tileCount, 0)
        # Check with a native magnfication with exact=True
        for tile in source.tileIterator(scale={
                'magnification': 5,
                'exact': True
        }):
            tileCount += 1
        self.assertEqual(tileCount, 144)
        # Check with a non-native magnfication without resampling
        tileCount = 0
        for tile in source.tileIterator(format=tilesource.TILE_FORMAT_PIL,
                                        scale={'magnification': 2}):
            tileCount += 1
            self.assertEqual(tile['tile'].size,
                             (tile['width'], tile['height']))
            self.assertEqual(tile['width'],
                             256 if tile['level_x'] < 11 else 61)
            self.assertEqual(tile['height'],
                             256 if tile['level_y'] < 11 else 79)
        self.assertEqual(tileCount, 144)
        # Check with a non-native magnfication with resampling
        tileCount = 0
        for tile in source.tileIterator(format=tilesource.TILE_FORMAT_PIL,
                                        scale={'magnification': 2},
                                        resample=True):
            tileCount += 1
            self.assertEqual(tile['tile'].size,
                             (tile['width'], tile['height']))
            self.assertEqual(tile['width'],
                             256 if tile['level_x'] < 4 else 126)
            self.assertEqual(tile['height'],
                             256 if tile['level_y'] < 4 else 134)
        self.assertEqual(tileCount, 25)

        # Ask for numpy array as results
        tileCount = 0
        for tile in source.tileIterator(scale={'magnification': 5}):
            tileCount += 1
            self.assertTrue(isinstance(tile['tile'], numpy.ndarray))
            self.assertEqual(tile['tile'].shape,
                             (256 if tile['level_y'] < 11 else 79,
                              256 if tile['level_x'] < 11 else 61, 4))
            self.assertEqual(tile['tile'].dtype, numpy.dtype('uint8'))
        self.assertEqual(tileCount, 144)
        # Ask for either PIL or IMAGE data, we should get PIL data
        tileCount = 0
        for tile in source.tileIterator(scale={'magnification': 5},
                                        format=(tilesource.TILE_FORMAT_PIL,
                                                tilesource.TILE_FORMAT_IMAGE),
                                        encoding='JPEG'):
            tileCount += 1
            self.assertTrue(isinstance(tile['tile'], PIL.Image.Image))
        self.assertEqual(tileCount, 144)
        # Ask for PNGs
        tileCount = 0
        for tile in source.tileIterator(scale={'magnification': 5},
                                        format=tilesource.TILE_FORMAT_IMAGE,
                                        encoding='PNG'):
            tileCount += 1
            self.assertFalse(isinstance(tile['tile'], PIL.Image.Image))
            self.assertEqual(tile['tile'][:len(common.PNGHeader)],
                             common.PNGHeader)
        self.assertEqual(tileCount, 144)

        # Use a ptif to test getting tiles as images
        file = self._uploadFile(
            os.path.join(os.environ['LARGE_IMAGE_DATA'], 'sample_image.ptif'))
        itemId = str(file['itemId'])
        item = Item().load(itemId, user=self.admin)
        source = ImageItem().tileSource(item)
        # Ask for either PIL or IMAGE data, we should get image data
        tileCount = 0
        jpegTileCount = 0
        for tile in source.tileIterator(scale={'magnification': 2.5},
                                        format=(tilesource.TILE_FORMAT_PIL,
                                                tilesource.TILE_FORMAT_IMAGE),
                                        encoding='JPEG'):
            tileCount += 1
            if not isinstance(tile['tile'], PIL.Image.Image):
                self.assertEqual(tile['tile'][:len(common.JPEGHeader)],
                                 common.JPEGHeader)
                jpegTileCount += 1
        self.assertEqual(tileCount, 45)
        self.assertGreater(jpegTileCount, 0)
        # Ask for PNGs
        tileCount = 0
        for tile in source.tileIterator(scale={'magnification': 2.5},
                                        format=tilesource.TILE_FORMAT_IMAGE,
                                        encoding='PNG'):
            tileCount += 1
            self.assertEqual(tile['tile'][:len(common.PNGHeader)],
                             common.PNGHeader)
        self.assertEqual(tileCount, 45)
        # Ask for TIFFs
        tileCount = 0
        for tile in source.tileIterator(scale={'magnification': 2.5},
                                        format=tilesource.TILE_FORMAT_IMAGE,
                                        encoding='TIFF'):
            tileCount += 1
            self.assertEqual(tile['tile'][:len(common.TIFFHeader)],
                             common.TIFFHeader)
        self.assertEqual(tileCount, 45)
        # Test some internal properties
        self.assertEqual(len(source._tiffDirectories), 9)
        info = source._tiffDirectories[-1]._tiffInfo
        self.assertEqual(info['bitspersample'], 8)
        self.assertEqual(info['compression'], 7)
        self.assertEqual(info['imagelength'], 12288)
        self.assertEqual(info['imagewidth'], 58368)
        self.assertEqual(info['istiled'], 1)
        self.assertEqual(info['jpegcolormode'], 0)
        self.assertEqual(info['jpegquality'], 75)
        self.assertEqual(info['jpegtablesmode'], 3)
        self.assertEqual(info['orientation'], 1)
        self.assertEqual(info['photometric'], 6)
        self.assertEqual(info['planarconfig'], 1)
        self.assertEqual(info['samplesperpixel'], 3)
        self.assertEqual(info['tilelength'], 256)
        self.assertEqual(info['tilewidth'], 256)