Skip to content

How to use custom annotation parser

Takumi Ando edited this page Oct 13, 2021 · 1 revision

Available Annotation Files

Currently, wsiprocess can parse ASAP, SlideRunner, QuPath -styled annotations. These 3 are available by default, but the other styled annotations can be added by creating a customized annotation file parser. Here I show an example of a customized parser.

Extract Coordinates and Classes

wsiprocess uses the information of the coordinates and the classes in the annotation file. BaseParser has the smallest requirements. Parser must have strings of the defined classes in the classes attribute, lists of the defined coorinates in the mask_coords attribute, and additionally, the path to the annotation file.

class ExampleParser(BaseParser):

    def __init__(self, path):
        # path is the location of the annotation file.
        super().__init__(path)

        # Here do something to get classes data and coordinates data.
        # For example, by running read_classes function and read_coordinates
        # function to get the data.
        # Parsing functions must be finished in the __init__.
        self.read_classes()
        self.read_coordinates()

    def read_classes(self):
        # do something to get the class names.
        self.classes.append("something")

    def read_coordinates(self):
        # do something to get the coordinates data per each class.
        coord1 = [
            [x1, y1],
            [x2, y2],
            [x3, y3],
            [x4, y4]
        ]
        coord2 = [
            [x5, y5],
            [x6, y6],
            [x7, y7],
            [x8, y8]
        ]
        self.mask_coords["something"] = [
            [coord1, coord2]
        ]

Where to add the customized parser

When ExampleParser class is defined like above,

  1. place it in a py file example_parser.py under wsiprocess/annotationparser.
  2. add ExampleParser in wsiprcess/annotaionparser/init.py
  3. add parsed = parsers.ExampleParser(self.path) to read_annotation function.