Пример #1
0
    def getNavigationViewOverlay(self, image: Image):

        file_path_navigator = \
            Path(os.path.join(Path(image.path()).parent, Path(image.path()).stem + self.navigation_ext) + ".png")

        if file_path_navigator.exists():
            return PIL_Image.open(str(file_path_navigator))
        # create new image
        else:
            self.updateNavigationViewOverlay(image)
            return PIL_Image.open(str(file_path_navigator))
Пример #2
0
    def getNavigationViewOverlayStatus(self, image: Image):

        file_path_navigator = \
            Path(os.path.join(Path(image.path()).parent, Path(image.path()).stem + self.navigation_ext) + ".png")

        slide = self.slide_cache.get(image.path())
        if slide._osr.level_dimensions[0][0] < 10000:
            return NavigationViewOverlayStatus.ERROR
        elif file_path_navigator.exists() is False or datetime.utcfromtimestamp(os.stat(file_path_navigator).st_mtime) \
                < image.annotations.latest('last_edit_time').last_edit_time.replace(tzinfo=None):
            return NavigationViewOverlayStatus.NEEDS_UPDATE
        else:
            return NavigationViewOverlayStatus.UP_TO_DATE
Пример #3
0
    def updateNavigationViewOverlay(self, image: Image):

        file_path = image.path()

        file_path_navigator = \
            Path(os.path.join(Path(image.path()).parent, Path(image.path()).stem + "_navigator") + ".png")
        # check if navigator overlay image exist
        # and was calculated after the annotation change

        if file_path_navigator.exists() is False or datetime.utcfromtimestamp(os.stat(file_path_navigator).st_mtime) \
                < image.annotations.latest('last_edit_time').last_edit_time.replace(tzinfo=None):
            slide = self.slide_cache.get(file_path)

            tile = np.array(slide._osr.get_thumbnail((self.width, self.hight)))

            x_steps = range(0, slide._osr.level_dimensions[0][0] - 2 * self.headmap_resolution,
                            int(self.headmap_resolution / 2))
            y_steps = range(0, slide._osr.level_dimensions[0][1] - 2 * self.headmap_resolution,
                            int(self.headmap_resolution / 2))

            gt_image = np.zeros(shape=(len(x_steps) + 1, len(y_steps) + 1))
            annotations = np.array(
                [[a.vector['x1'], a.vector['y1'], a.vector['x2'], a.vector['y2'], int(a.annotation_type.name)]
                 for a in image.annotations.filter(annotation_type__active=True, deleted=False).exclude(vector__isnull=True).all()])

            # image.annotations.filter(vector__x1__gte=x_min, vector__y1__gte=y_min, vector__x2__lte=x_max,
            # vector__y2__lte=y_max).annotate(name_as_int=Cast('annotation_type__name', FloatField()))
            # .aggregate(Avg('name_as_int'))['name_as_int__avg']

            if image.annotations.filter(annotation_type__active=True, deleted=False).exists():
                x_index = 0
                for x in x_steps:
                    y_index = 0
                    for y in y_steps:
                        ids = ((annotations[:, 1]) > x) \
                              & ((annotations[:, 0]) > y) \
                              & ((annotations[:, 3]) < x + self.headmap_resolution) \
                              & ((annotations[:, 2]) < y + self.headmap_resolution)

                        score = np.mean(annotations[ids, 4]) if np.count_nonzero(ids) > 1 else 0
                        gt_image[x_index, y_index] = score

                        y_index += 1
                    x_index += 1
                gt_image = np.expand_dims(gt_image * (255. / 4), axis=2).astype(np.uint8)
                overlay = cv2.applyColorMap(gt_image, cv2.COLORMAP_JET)[::-1]
                # Mask overlay
                overlay[np.array(gt_image == 0)[:, :, [0, 0, 0]]] = [255]
                overlay = cv2.resize(overlay, tile.shape[:2][::-1])

                PIL_Image.fromarray(overlay).save(str(file_path_navigator))
Пример #4
0
    def getPluginStatisticsElements(self,
                                    image: Image,
                                    user: get_user_model(),
                                    options={}):

        tab_id = "EIPH_Statistics"

        slide = self.slide_cache.get(image.path())

        x_min = int(options.get("min_x", 0))
        x_max = int(options.get("max_x", slide._osr.level_dimensions[0][0]))
        y_min = int(options.get("min_y", 0))
        y_max = int(options.get("max_y", slide._osr.level_dimensions[0][1]))

        if image.image_set.collaboration_type == ImageSet.CollaborationTypes.COMPETITIVE:
            annotation_types = AnnotationType.objects.filter(
                annotation__image=image,
                active=True,
                annotation__deleted=False,
                annotation__user=user,
                annotation__vector__x1__gte=x_min,
                annotation__vector__y1__gte=y_min,
                annotation__vector__x2__lte=x_max,
                annotation__vector__y2__lte=y_max)
        else:
            annotation_types = AnnotationType.objects.filter(
                annotation__image=image,
                active=True,
                annotation__deleted=False,
                annotation__vector__x1__gte=x_min,
                annotation__vector__y1__gte=y_min,
                annotation__vector__x2__lte=x_max,
                annotation__vector__y2__lte=y_max)

        annotation_types = annotation_types.distinct().filter(
            product__name__contains="EIPH").order_by('sort_order').annotate(
                count=Count('annotation'))

        doucet_score = 0
        annotations_total = annotation_types.aggregate(
            Sum('count'))['count__sum']
        if annotations_total is not None:
            doucet_score = sum([
                a['count'] / (annotations_total / 100) * int(a['name'])
                for a in annotation_types.values() if a['name'].isdigit()
            ])
            doucet_score = '{:f}'.format(doucet_score)

        rendering = render_to_string(
            'EIPH/EIPH_Statistics.html', {
                'tab_id': tab_id,
                'annotation_types': annotation_types,
                'doucet_score': doucet_score
            })

        return {
            'id': tab_id,
            'content': rendering,
            'update_policy': self.getStatisticsUpdatePolicy()
        }
Пример #5
0
    def getPluginStatisticsElements(self, image: Image, user: get_user_model(), options={}):

        tab_id = "Screening"


        image_width = image.width
        image_height = image.height

        screening = image.screening.filter(user=user).first()
        resolution_x, resolution_y = None, None
        # get resolution
        # first check if set by user
        # second load from database for that image
        # third load from dataset by current user
        # finally load from any user
        if "resolution_x" in options and "resolution_y" in options:
            resolution_x = int(options["resolution_x"])
            resolution_y = int(options["resolution_y"])
        elif screening:
            resolution_x = screening.x_resolution
            resolution_y = screening.y_resolution
        else:
            image_with_screening_result = image.image_set.images.filter(screening__user=user).first()
            if image_with_screening_result:
                temp_screening = image.image_set.images.filter(screening__user=user)\
                    .first().screening.filter(user=user).first()
                resolution_x = temp_screening.x_resolution
                resolution_y = temp_screening.y_resolution
            else:
                temp_screening = image.image_set.images.filter() \
                        .first().screening.filter().first()
                if temp_screening:
                    resolution_x = temp_screening.x_resolution
                    resolution_y = temp_screening.y_resolution

        if screening:
            # check if screening resolution has changed and need update
            if resolution_x is not None and resolution_y is not None and \
                    screening.x_resolution != resolution_x or screening.y_resolution != resolution_y:

                tile_dict, x_steps, y_steps = self._create_tiles(resolution_x, resolution_y, image_width, image_height)

                screening.x_steps = len(x_steps)
                screening.y_steps = len(y_steps)
                screening.x_resolution = resolution_x
                screening.y_resolution = resolution_y
                screening.screening_tiles = tile_dict
                screening.current_index = 0
                screening.save()
        elif resolution_x is not None and resolution_y is not None and \
                "resolution_x" in options and "resolution_y" in options:
            tile_dict, x_steps, y_steps = self._create_tiles(resolution_x, resolution_y, image_width, image_height)
            screening = ScreeningMode(image=image,
                                      user=user,
                                      screening_tiles=tile_dict,
                                      x_resolution=resolution_x,
                                      y_resolution=resolution_y,
                                      x_steps=len(x_steps),
                                      y_steps=len(y_steps))
            screening.save()

        img_str = None
        if screening:
            if "current_index" in options:
                screening.current_index = options['current_index']
                if screening.current_index in screening.screening_tiles:
                    screening.screening_tiles[screening.current_index]['Screened'] = True
                    screening.save()
                elif str(screening.current_index) in screening.screening_tiles:
                    screening.screening_tiles[str(screening.current_index)]['Screened'] = True
                    screening.save()

            slide = self.slide_cache.get(image.path())

            tile = slide._osr.get_thumbnail((self.thumbnail_size_x, self.thumbnail_size_y))

            scale_x, scale_y = image_width / self.thumbnail_size_x, image_height / self.thumbnail_size_y
            tile = self.draw_tiles(tile, screening.screening_tiles, scale_x, scale_y, screening.current_index)

            tile = PIL_Image.fromarray(tile)
            buf = PILBytesIO()
            tile.save(buf, 'png', quality=90)
            img_str = str(base64.b64encode(buf.getvalue()))[2:-1]

        rendering = render_to_string('Screening/Screening.html', {
            'image_id': image.id,
            'tab_id': tab_id,
            'image': img_str,
            'resolution_x': resolution_x,
            'resolution_y': resolution_y})

        return {
            'id': tab_id,
            'content':  rendering,
            'screening_tile_status': screening.screening_tiles if screening else {},
            'x_steps': screening.x_steps if screening else 0,
            'y_steps': screening.y_steps if screening else 0,
            'current_index': screening.current_index if screening else None,
            'update_policy': self.getStatisticsUpdatePolicy()
        }