From 7b12b0d3c4ca087103243e3fbdcf047dfb05c4f8 Mon Sep 17 00:00:00 2001 From: Becky Reamy Date: Fri, 11 Feb 2022 16:19:21 -0500 Subject: [PATCH 1/2] KPMP-3104: Calculating overlay values --- docker/wsi-worker/extract_metadata.py | 71 +++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/docker/wsi-worker/extract_metadata.py b/docker/wsi-worker/extract_metadata.py index e8d60db..ba470f7 100644 --- a/docker/wsi-worker/extract_metadata.py +++ b/docker/wsi-worker/extract_metadata.py @@ -1,8 +1,71 @@ import openslide import sys import json +import math + +def getCurrentLetter(n): + string = "" + while n > 0: + n, remainder = divmod(n - 1, 26) + string = chr(65 + remainder) + string + return string + +def calculateGrids(metadata): + verticalSize = 500 + horizontalSize = 500 + lineThickness = 13 + vertical = verticalSize / float(metadata['openslide']['mpp_y']); + horizontal = horizontalSize / float(metadata['openslide']['mpp_y']); + overlay = [] + overlayLabel = [] + if ((metadata != None) and metadata['aperio'] and metadata['aperio']['OriginalHeight'] != None and metadata['aperio']['OriginalWidth'] != None): + width = int(metadata['aperio']['OriginalWidth']) + height = int(metadata['aperio']['OriginalHeight']) + gridLineLengthForHeight = (math.ceil(((height + horizontal) / horizontal)) - 1) * horizontal; + gridLineLengthForWidth = (math.ceil(((width + horizontal) / horizontal)) - 1) * horizontal; + + i = 0.0 + while (i <= (width + vertical)): + overlay.append({ + 'px': 0, + 'py': i, + 'width': lineThickness, + 'height': gridLineLengthForHeight, + 'className': 'gridline' + }) + i += vertical + j = 0.0 + while (j <= (height + horizontal)): + overlay.append({ + 'px': 0, + 'py': j, + 'width': gridLineLengthForWidth, + 'height': lineThickness, + 'className': 'gridline' + }) + j += horizontal + + currentNumber = 0 + yy = 0 + while (yy < (height)): + currentLetterCount = 1 + currentLetter = getCurrentLetter(currentLetterCount) + i=0 + while (i < (width)): + overlayLabel.append(currentLetter + str(currentNumber)) + overlay.append({ + 'id': 'labelOverlay-' + currentLetter + str(currentNumber), + 'px': 0 + (i/vertical * vertical + lineThickness), + 'py': 0 + (yy/horizontal * horizontal + lineThickness), + }) + i+= vertical + currentLetterCount += 1 + currentLetter = getCurrentLetter(currentLetterCount) + yy += vertical + currentNumber += 1 + print(overlayLabel) + print(overlay) -print("\n ".join(sys.argv)) svsfile = sys.argv[1] metadata_out = sys.argv[2] @@ -21,8 +84,10 @@ else: metadata[mainKey] = {subkey: slide.properties[properties]} -print('metadata_out', metadata_out) +# print('metadata_out', metadata_out) with open(metadata_out, 'w') as metadata_file: metadata_file.write(json.dumps(metadata)) -slide.close() +slide.close(); +calculateGrids(metadata) + From 111fb71f20ad5b1e8ecbd37b4b292f586849a149 Mon Sep 17 00:00:00 2001 From: Becky Reamy Date: Mon, 14 Feb 2022 12:58:17 -0500 Subject: [PATCH 2/2] KPMP-3104: Add calculations of grids --- docker/wsi-worker/extract_metadata.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/docker/wsi-worker/extract_metadata.py b/docker/wsi-worker/extract_metadata.py index ba470f7..337a1da 100644 --- a/docker/wsi-worker/extract_metadata.py +++ b/docker/wsi-worker/extract_metadata.py @@ -18,22 +18,26 @@ def calculateGrids(metadata): horizontal = horizontalSize / float(metadata['openslide']['mpp_y']); overlay = [] overlayLabel = [] + # Make sure we have the necessary information for the slide before we calculate if ((metadata != None) and metadata['aperio'] and metadata['aperio']['OriginalHeight'] != None and metadata['aperio']['OriginalWidth'] != None): width = int(metadata['aperio']['OriginalWidth']) height = int(metadata['aperio']['OriginalHeight']) gridLineLengthForHeight = (math.ceil(((height + horizontal) / horizontal)) - 1) * horizontal; gridLineLengthForWidth = (math.ceil(((width + horizontal) / horizontal)) - 1) * horizontal; + # determine where to put the vertical gridlines i = 0.0 while (i <= (width + vertical)): overlay.append({ - 'px': 0, - 'py': i, + 'px': i, + 'py': 0, 'width': lineThickness, 'height': gridLineLengthForHeight, 'className': 'gridline' }) i += vertical + + # determine where to put the horizontal gridlines j = 0.0 while (j <= (height + horizontal)): overlay.append({ @@ -47,11 +51,11 @@ def calculateGrids(metadata): currentNumber = 0 yy = 0 - while (yy < (height)): + while (yy < (height)): # loop through the rows currentLetterCount = 1 currentLetter = getCurrentLetter(currentLetterCount) i=0 - while (i < (width)): + while (i < (width)): # loop through the columns overlayLabel.append(currentLetter + str(currentNumber)) overlay.append({ 'id': 'labelOverlay-' + currentLetter + str(currentNumber), @@ -63,15 +67,15 @@ def calculateGrids(metadata): currentLetter = getCurrentLetter(currentLetterCount) yy += vertical currentNumber += 1 - print(overlayLabel) - print(overlay) + metadata['overlay'] = overlay + metadata['overlayLabel'] = overlayLabel + svsfile = sys.argv[1] metadata_out = sys.argv[2] slide = openslide.OpenSlide(svsfile) - metadata = {} for properties in slide.properties: if '.' in properties: @@ -83,11 +87,11 @@ def calculateGrids(metadata): metadata[mainKey][subkey] = slide.properties[properties] else: metadata[mainKey] = {subkey: slide.properties[properties]} - -# print('metadata_out', metadata_out) + +calculateGrids(metadata) with open(metadata_out, 'w') as metadata_file: metadata_file.write(json.dumps(metadata)) slide.close(); -calculateGrids(metadata) +