DIAGNijmegen / pathology-whole-slide-data

A package for working with whole-slide data including a fast batch iterator that can be used to train deep learning models.
https://diagnijmegen.github.io/pathology-whole-slide-data/
Apache License 2.0
86 stars 24 forks source link

Iterating over classes in WholeSlideAnnotation objects #25

Closed leandervaneekelen closed 2 years ago

leandervaneekelen commented 2 years ago

Hi @martvanrijthoven,

I was wondering if there is existing code within whole-slide-data to do something similar to what I write below:

wsa = WholeSlideAnnotation('foo.xml')

# Iterate over classes in WSA like with a dict.items()
for label, annos in wsa.annotations_per_label():
    # Do stuff here

I am aware of the existence of sampling annotations, but these don't really fit this use case. I would have to instantiate a separate wsa object with its own sampling_annotation for every class (and I would first have to read these classes if didn't know them a priori).

If something like that doesn't exist, what do you think of making it a property/method in WholeSlideAnnotations? It wouldn't be very hard:


@property
def annotations_per_label(self) -> Dict
    annos_per_label = dict(zip(self.labels.names, [[] for _ in self.labels.names]))
    for anno in self.annotations:
        annos_per_label[anno.label.name].append(anno)
    return annos_per_label
martvanrijthoven commented 2 years ago

Hi Leander,

Nice suggestion, Thanks!

I added this feature via annotations_per_label

and sampling_annotations_per_label

leandervaneekelen commented 2 years ago

Hi Mart,

Looks great, thanks a lot for the quick feature update!