Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add annotation Module #175

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions AnnotationModule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import logging
from BaseImage import printMaskHelper
from skimage import io, img_as_ubyte
import os

import xml.etree.ElementTree as ET
import numpy as np
import cv2

def get_points(xml_fname):
"""Parses the xml file to get those annotations as lists of verticies"""
# create element tree object
tree = ET.parse(xml_fname)

# get root element
root = tree.getroot()

points = []

for annotation in root.findall('Annotation'):
for regions in annotation.findall('Regions'):
for region in regions.findall('Region'):
for vertices in region.findall('Vertices'):
points.append([None] * len(vertices.findall('Vertex')))
pjl54 marked this conversation as resolved.
Show resolved Hide resolved
for k, vertex in enumerate(vertices.findall('Vertex')):
points[-1][k] = (int(float(vertex.get('X'))), int(float(vertex.get('Y'))))

return points


def resize_points(points, resize_factor):
for k, pointSet in enumerate(points):
points[k] = [(int(p[0] * resize_factor), int(p[1] * resize_factor)) for p in pointSet]

return points.copy()

def mask_out_annotation(s,xml_fname):
"""Returns the mask of annotations"""

points = get_points(xml_fname)

resize_factor = np.shape(s["img_mask_use"])[1] / s["image_base_size"][0]

points = resize_points(points, resize_factor)

mask = np.zeros((np.shape(s["img_mask_use"])[0],np.shape(s["img_mask_use"])[1]),dtype=np.uint8)

for pointSet in points:
cv2.fillPoly(mask, [np.asarray(pointSet).reshape((-1, 1, 2))], 1)
pjl54 marked this conversation as resolved.
Show resolved Hide resolved

return mask

def xmlMask(s,params):
logging.info(f"{s['filename']} - \txmlMask")
mask = s["img_mask_use"]

xml_basepath = params.get("xml_filepath","")
pjl54 marked this conversation as resolved.
Show resolved Hide resolved
xml_suffix = params.get("xml_suffix", "")
if len(xml_basepath)==0:
xml_basepath = s["dir"]

xml_fname = xml_basepath + os.sep + s['filename'][:s['filename'].rindex('.')] + xml_suffix + '.xml'

logging.info(f"{s['filename']} - \tusing {xml_fname}")

annotationMask = mask_out_annotation(s,xml_fname) > 0
io.imsave(s["outdir"] + os.sep + s["filename"] + "_xmlMask.png", img_as_ubyte(annotationMask))
pjl54 marked this conversation as resolved.
Show resolved Hide resolved

prev_mask = s["img_mask_use"]
s["img_mask_use"] = prev_mask & annotationMask

s.addToPrintList("xmlMask",
printMaskHelper(params.get("mask_statistics", s["mask_statistics"]), prev_mask, s["img_mask_use"]))

if len(s["img_mask_use"].nonzero()[0]) == 0: # add warning in case the final tissue is empty
logging.warning(
f"{s['filename']} - After AnnotationModule.xmlMask NO tissue remains detectable! Downstream modules likely to be incorrect/fail")
s["warnings"].append(
f"After AnnotationModule.xmlMask NO tissue remains detectable! Downstream modules likely to be incorrect/fail")

return
2 changes: 2 additions & 0 deletions BaseImage.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ def __init__(self, fname, fname_outdir, params):
self["dir"] = os.path.dirname(fname)

self["os_handle"] = openslide.OpenSlide(fname)
self["image_base_size"] = self["os_handle"].dimensions
self["image_work_size"] = params.get("image_work_size", "1.25x")
self["mask_statistics"] = params.get("mask_statistics", "relative2mask")
self["base_mag"] = getMag(self, params)
pjl54 marked this conversation as resolved.
Show resolved Hide resolved
self.addToPrintList("base_mag", getMag(self, params))

mask_statistics_types = ["relative2mask", "absolute", "relative2image"]
Expand Down
33 changes: 33 additions & 0 deletions config_example_AnnotationModule.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[pipeline]
steps= BasicModule.getBasicStats
AnnotationModule.xmlMask
SaveModule.saveFinalMask
SaveModule.saveThumbnails
BasicModule.finalComputations


[BaseImage.BaseImage]
image_work_size = 1.25x

#three options: relative2mask, absolute, relative2image
mask_statistics = relative2mask

confirm_base_mag: False


[BasicModule.getBasicStats]
image_work_size = 1.25x


[AnnotationModule.xmlMask]
#leave blank to replacing image filename extension with "xml", as in: /path/to/file/base_filename_image.svs -> /path/to/file/base_filename_image.xml
xml_filepath =
#suffix is inserted before extension, as in: /path/to/file/base_filename_image.svs -> /path/to/file/base_filename_image{suffix}.xml
xml_suffix =

[SaveModule.saveFinalMask]
overlay: True

[SaveModule.saveThumbnails]
image_work_size: 1.25x
small_dim: 500