Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
print('>> Reading input image')
im_input = skimage.io.imread(args.inputImageFile)[:, :, :3]
#
# Perform color deconvolution -unnormalized
#
print('>> Performing color deconvolution')
stain_color_1 = stain_color_map[args.stain_1]
stain_color_2 = stain_color_map[args.stain_2]
stain_color_3 = stain_color_map[args.stain_3]
w = np.array([stain_color_1, stain_color_2, stain_color_3]).T
im_stains = htk_cdeconv.color_deconvolution(im_input, w).Stains
im_membrane_stain = im_stains[:, :, 2].astype(np.float)
#
# Perform membrane detection
#
print('>> Performing membrane detection')
Label, Split, Branches = htk_seg.membranes.membrane_detection(
im_membrane_stain, min_sigma=args.filter_sigma_min,
max_sigma=args.filter_sigma_max, beta=args.filter_beta,
c=args.filter_c, f_threshold=args.foreground_threshold,
min_lsize=args.min_labelsize, b_dilation=args.branch_merge_dilation,
b_split_dilation=args.branch_split_dilation
)
**it_kwargs)
# get tile image
im_tile = tile_info['tile'][:, :, :3]
# perform color normalization
im_nmzd = htk_cnorm.reinhard(im_tile,
args.reference_mu_lab,
args.reference_std_lab,
src_mu=src_mu_lab,
src_sigma=src_sigma_lab)
# perform color decovolution
w = cli_utils.get_stain_matrix(args)
im_stains = htk_cdeconv.color_deconvolution(im_nmzd, w).Stains
im_nuclei_stain = im_stains[:, :, 0].astype(np.float)
# segment nuclear foreground
im_nuclei_fgnd_mask = im_nuclei_stain < args.foreground_threshold
# segment nuclei
im_nuclei_seg_mask = htk_nuclear.detect_nuclei_kofahi(
im_nuclei_stain,
im_nuclei_fgnd_mask,
args.min_radius,
args.max_radius,
args.min_nucleus_area,
args.local_max_search_radius
)
im_nmzd = (resize(
im_nmzd, (int(im_nmzd.shape[0] / scale), int(im_nmzd.shape[1] / scale)),
mode='reflect') * 255).astype(np.uint8)
# get red and green channels
im_red = im_nmzd[:, :, 0]
im_green = im_nmzd[:, :, 1]
# get foreground mask simply using numpy.spacing, a generalization of EPS
im_ratio = im_red / (im_green + np.spacing(1))
im_fgnd_mask = im_ratio > args.rg_ratio_superpixel
# perform color decovolution
w = cli_utils.get_stain_matrix(args)
im_stains = htk_cdeconv.color_deconvolution(im_nmzd, w).Stains
# detect real stains
dict_stains = {}
# compare w with stain_color_map
w = w.T
for i in range(w.shape[0]):
for j in htk_cdeconv.stain_color_map:
if len(set(w[i]) & set(htk_cdeconv.stain_color_map[j])) == 3:
dict_stains[j] = i
# compute the number of super-pixels
im_width, im_height = im_nmzd.shape[:2]
n_superpixels = (im_width/args.patchSize) * (im_height/args.patchSize)
print(">> Starting Dask cluster and sampling pixels")
utils.create_dask_client(args.dask)
sample = utils.sample_pixels(args.sample)
# Create stain matrix
print('>> Creating stain matrix')
args.snmf.w_init = utils.get_stain_matrix(args.stains, 2)
print(args.snmf.w_init)
# Perform color deconvolution
print('>> Performing color deconvolution')
w_est = htk_cdeconv.rgb_separate_stains_xu_snmf(sample.T, **vars(args.snmf))
w_est = htk_cdeconv.complement_stain_matrix(w_est)
with open(args.returnParameterFile, 'w') as f:
for i, stain in enumerate(w_est.T):
f.write('stainColor_{} = {}\n'.format(i+1, ','.join(map(str, stain))))
)
# perform color decovolution
if args.deconv_method == 'ruifrok':
w = cli_utils.get_stain_matrix(args)
im_stains = htk_cdeconv.color_deconvolution(
im_nmzd, w).Stains.astype(np.float)[:, :, :2]
elif args.deconv_method == 'macenko':
w_est = htk_cdeconv.rgb_separate_stains_macenko_pca(im_tile, 255)
im_stains = htk_cdeconv.color_deconvolution(
im_tile, w_est, 255).Stains.astype(np.float)
ch1 = htk_cdeconv.find_stain_index(
htk_cdeconv.stain_color_map[args.stain_1], w_est)
ch2 = htk_cdeconv.find_stain_index(
htk_cdeconv.stain_color_map[args.stain_2], w_est)
im_stains = im_stains[:, :, [ch1, ch2]]
else:
raise ValueError('Invalid deconvolution method parameter.')
# =========================================================================
# ====================== Fuse the stain1 & stain2 pix======================
# =========================================================================
# compute nuclear foreground mask
im_fgnd_mask_stain_1 = im_stains[
:, :, 0] < threshold_yen(im_stains[:, :, 0])
im_fgnd_mask_stain_2 = im_stains[
im_fgnd_mask = im_ratio > args.rg_ratio_superpixel
# perform color decovolution
w = cli_utils.get_stain_matrix(args)
im_stains = htk_cdeconv.color_deconvolution(im_nmzd, w).Stains
# detect real stains
dict_stains = {}
# compare w with stain_color_map
w = w.T
for i in range(w.shape[0]):
for j in htk_cdeconv.stain_color_map:
if len(set(w[i]) & set(htk_cdeconv.stain_color_map[j])) == 3:
dict_stains[j] = i
# compute number of super-pixels
im_width, im_height = im_nmzd.shape[:2]
n_superpixels = (im_width/args.patchSize) * (im_height/args.patchSize)
#
# Perform a superpixel algorithm (SLIC)
# In SLIC, compactness controls image space proximity.
# Higher compactness will make the shape of superpixels more square.
#
im_label = slic(im_nmzd, n_segments=n_superpixels,
compactness=args.compactness) + 1
region_props = regionprops(im_label)
print(">> Starting Dask cluster and sampling pixels")
utils.create_dask_client(args.dask)
sample = utils.sample_pixels(args.sample)
# Create stain matrix
print('>> Creating stain matrix')
args.snmf.w_init = utils.get_stain_matrix(args.stains, 2)
print(args.snmf.w_init)
# Perform color deconvolution
print('>> Performing color deconvolution')
w_est = htk_cdeconv.rgb_separate_stains_xu_snmf(sample.T, **vars(args.snmf))
w_est = htk_cdeconv.complement_stain_matrix(w_est)
with open(args.returnParameterFile, 'w') as f:
for i, stain in enumerate(w_est.T):
f.write('stainColor_{} = {}\n'.format(i+1, ','.join(map(str, stain))))
def get_stain_vector(args, index):
"""Get the stain corresponding to args.stain_$index and
args.stain_$index_vector. If the former is not "custom", all the
latter's elements must be -1.
"""
args = args._asdict() if hasattr(args, '_asdict') else vars(args)
stain = args['stain_' + str(index)]
stain_vector = args['stain_' + str(index) + '_vector']
if all(x == -1 for x in stain_vector): # Magic default value
if stain == 'custom':
raise ValueError('If "custom" is chosen for a stain, '
'a stain vector must be provided.')
return htk_cdeconv.stain_color_map[stain]
else:
if stain == 'custom':
return stain_vector
raise ValueError('Unless "custom" is chosen for a stain, '
'no stain vector may be provided.')
print(args.inputImageFile)
im_input = skimage.io.imread(args.inputImageFile)[:, :, :3]
# Create stain matrix
print('>> Creating stain matrix')
w_init = np.array([args.stainColor_1, args.stainColor_2]).T
print w_init
# Perform color deconvolution
print('>> Performing color deconvolution')
res = htk_cdeconv.sparse_color_deconvolution(
im_input, w_init, args.beta)
w_est = np.concatenate((res.Wc, np.zeros((3, 1))), 1)
res = htk_cdeconv.color_deconvolution(im_input, w_est)
# write stain images to output
print('>> Outputting individual stain images')
print args.outputStainImageFile_1
skimage.io.imsave(args.outputStainImageFile_1, res.Stains[:, :, 0])
print args.outputStainImageFile_2
skimage.io.imsave(args.outputStainImageFile_2, res.Stains[:, :, 1])
print args.outputStainImageFile_3
skimage.io.imsave(args.outputStainImageFile_3, res.Stains[:, :, 2])
im_ratio = im_red / (im_green + np.spacing(1))
im_fgnd_mask = im_ratio > args.rg_ratio_superpixel
# perform color decovolution
w = cli_utils.get_stain_matrix(args)
im_stains = htk_cdeconv.color_deconvolution(im_nmzd, w).Stains
# detect real stains
dict_stains = {}
# compare w with stain_color_map
w = w.T
for i in range(w.shape[0]):
for j in htk_cdeconv.stain_color_map:
if len(set(w[i]) & set(htk_cdeconv.stain_color_map[j])) == 3:
dict_stains[j] = i
# compute the number of super-pixels
im_width, im_height = im_nmzd.shape[:2]
n_superpixels = (im_width/args.patchSize) * (im_height/args.patchSize)
#
# Generate labels using a superpixel algorithm (SLIC)
# In SLIC, compactness controls image space proximity.
# Higher compactness will make the shape of superpixels more square.
#
im_label = slic(im_nmzd, n_segments=n_superpixels,
compactness=args.compactness) + 1
region_props = regionprops(im_label)