forked from choosehappy/HistoQC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeconvolutionModule.py
More file actions
72 lines (55 loc) · 2.59 KB
/
DeconvolutionModule.py
File metadata and controls
72 lines (55 loc) · 2.59 KB
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
72
import logging
import os
import sys
import numpy as np
from skimage import io, color, img_as_ubyte
from skimage.exposure import rescale_intensity
from skimage.color import separate_stains
from skimage.color import hed_from_rgb, hdx_from_rgb, fgx_from_rgb, bex_from_rgb, rbd_from_rgb
from skimage.color import gdx_from_rgb, hax_from_rgb, bro_from_rgb, bpx_from_rgb, ahx_from_rgb, \
hpx_from_rgb # need to load all of these in case the user selects them
from distutils.util import strtobool
import matplotlib.pyplot as plt
def seperateStains(s, params):
logging.info(f"{s['filename']} - \tseperateStains")
stain = params.get("stain", "")
use_mask = strtobool(params.get("use_mask", "True"))
if stain == "":
logging.error(f"{s['filename']} - stain not set in DeconvolutionModule.seperateStains")
sys.exit(1)
return
stain_matrix = getattr(sys.modules[__name__], stain, "")
if stain_matrix == "":
logging.error(f"{s['filename']} - Unknown stain matrix specified in DeconolutionModule.seperateStains")
sys.exit(1)
return
mask = s["img_mask_use"]
if use_mask and len(mask.nonzero()[0]) == 0: #-- lets just error check at the top if mask is empty and abort early
for c in range(3):
s.addToPrintList(f"deconv_c{c}_std", str(-100))
s.addToPrintList(f"deconv_c{c}_mean", str(-100))
io.imsave(s["outdir"] + os.sep + s["filename"] + f"_deconv_c{c}.png", img_as_ubyte(np.zeros(mask.shape)))
logging.warning(f"{s['filename']} - DeconvolutionModule.seperateStains: NO tissue "
f"remains detectable! Saving Black images")
s["warnings"].append(f"DeconvolutionModule.seperateStains: NO tissue "
f"remains detectable! Saving Black images")
return
img = s.getImgThumb(s["image_work_size"])
dimg = separate_stains(img, stain_matrix)
for c in range(0, 3):
dc = dimg[:, :, c]
if use_mask:
dc_sub = dc[mask]
dc_min = dc_sub.min()
dc_max = dc_sub.max()
s.addToPrintList(f"deconv_c{c}_mean", str(dc_sub.mean()))
s.addToPrintList(f"deconv_c{c}_std", str(dc_sub.std()))
else:
mask = 1.0
dc_min = dc.min()
dc_max = dc.max()
s.addToPrintList(f"deconv_c{c}_mean", str(dc.mean()))
s.addToPrintList(f"deconv_c{c}_std", str(dc.std()))
dc = (dc - dc_min) / float(dc_max - dc_min) * mask
io.imsave(s["outdir"] + os.sep + s["filename"] + f"_deconv_c{c}.png", img_as_ubyte(dc))
return