forked from choosehappy/HistoQC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicModule.py
71 lines (53 loc) · 3.11 KB
/
BasicModule.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import logging
import os
from BaseImage import printMaskHelper
from skimage.morphology import remove_small_objects, binary_opening, disk
from skimage import io, color, img_as_ubyte
import matplotlib.pyplot as plt
def getBasicStats(s, params):
logging.info(f"{s['filename']} - \tgetBasicStats")
osh = s["os_handle"]
s.addToPrintList("type", osh.properties.get("openslide.vendor", "NA"))
s.addToPrintList("levels", osh.properties.get("openslide.level-count", "NA"))
s.addToPrintList("height", osh.properties.get("openslide.level[0].height", "NA"))
s.addToPrintList("width", osh.properties.get("openslide.level[0].width", "NA"))
s.addToPrintList("mpp_x", osh.properties.get("openslide.mpp-x", "NA"))
s.addToPrintList("mpp_y", osh.properties.get("openslide.mpp-y", "NA"))
s.addToPrintList("comment", osh.properties.get("openslide.comment", "NA").replace("\n", " ").replace("\r", " "))
return
def finalComputations(s, params):
mask = s["img_mask_use"]
s.addToPrintList("pixels_to_use", str(len(mask.nonzero()[0])))
def finalProcessingSpur(s, params):
logging.info(f"{s['filename']} - \tfinalProcessingSpur")
disk_radius = int(params.get("disk_radius", "25"))
selem = disk(disk_radius)
mask = s["img_mask_use"]
mask_opened = binary_opening(mask, selem)
mask_spur = ~mask_opened & mask
io.imsave(s["outdir"] + os.sep + s["filename"] + "_spur.png", img_as_ubyte(mask_spur))
prev_mask = s["img_mask_use"]
s["img_mask_use"] = mask_opened
s.addToPrintList("spur_pixels",
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 BasicModule.finalProcessingSpur NO tissue remains detectable! Downstream modules likely to be incorrect/fail")
s["warnings"].append(
f"After BasicModule.finalProcessingSpur NO tissue remains detectable! Downstream modules likely to be incorrect/fail")
def finalProcessingArea(s, params):
logging.info(f"{s['filename']} - \tfinalProcessingArea")
area_thresh = int(params.get("area_threshold", "1000"))
mask = s["img_mask_use"]
mask_opened = remove_small_objects(mask, min_size=area_thresh)
mask_removed_area = ~mask_opened & mask
io.imsave(s["outdir"] + os.sep + s["filename"] + "_areathresh.png", img_as_ubyte(mask_removed_area))
prev_mask = s["img_mask_use"]
s["img_mask_use"] = mask_opened > 0
s.addToPrintList("areaThresh",
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 BasicModule.finalProcessingArea NO tissue remains detectable! Downstream modules likely to be incorrect/fail")
s["warnings"].append(
f"After BasicModule.finalProcessingArea NO tissue remains detectable! Downstream modules likely to be incorrect/fail")