annotate color_deconvolution.py @ 4:5bd113d38acc draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
author imgteam
date Thu, 06 Mar 2025 18:12:27 +0000
parents 612aa1478fe1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
1 import argparse
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
2 import sys
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
3 import warnings
2
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
4
4
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
5 import giatools.io
0
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
6 import numpy as np
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
7 import skimage.color
2
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
8 import skimage.io
0
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
9 import skimage.util
4
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
10 import tifffile
2
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
11 from sklearn.decomposition import FactorAnalysis, FastICA, NMF, PCA
0
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
12
4
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
13 # Stain separation matrix for H&E color deconvolution, extracted from ImageJ/FIJI
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
14 rgb_from_he = np.array([
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
15 [0.64431860, 0.7166757, 0.26688856],
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
16 [0.09283128, 0.9545457, 0.28324000],
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
17 [0.63595444, 0.0010000, 0.77172660],
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
18 ])
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
19
0
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
20 convOptions = {
4
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
21 # General color space conversion operations
2
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
22 'hed2rgb': lambda img_raw: skimage.color.hed2rgb(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
23 'hsv2rgb': lambda img_raw: skimage.color.hsv2rgb(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
24 'lab2lch': lambda img_raw: skimage.color.lab2lch(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
25 'lab2rgb': lambda img_raw: skimage.color.lab2rgb(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
26 'lab2xyz': lambda img_raw: skimage.color.lab2xyz(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
27 'lch2lab': lambda img_raw: skimage.color.lch2lab(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
28 'luv2rgb': lambda img_raw: skimage.color.luv2rgb(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
29 'luv2xyz': lambda img_raw: skimage.color.luv2xyz(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
30 'rgb2hed': lambda img_raw: skimage.color.rgb2hed(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
31 'rgb2hsv': lambda img_raw: skimage.color.rgb2hsv(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
32 'rgb2lab': lambda img_raw: skimage.color.rgb2lab(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
33 'rgb2luv': lambda img_raw: skimage.color.rgb2luv(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
34 'rgb2rgbcie': lambda img_raw: skimage.color.rgb2rgbcie(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
35 'rgb2xyz': lambda img_raw: skimage.color.rgb2xyz(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
36 'rgbcie2rgb': lambda img_raw: skimage.color.rgbcie2rgb(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
37 'xyz2lab': lambda img_raw: skimage.color.xyz2lab(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
38 'xyz2luv': lambda img_raw: skimage.color.xyz2luv(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
39 'xyz2rgb': lambda img_raw: skimage.color.xyz2rgb(img_raw),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
40
4
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
41 # Color deconvolution operations
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
42 'hed_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hed_from_rgb),
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
43 'hdx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hdx_from_rgb),
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
44 'fgx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.fgx_from_rgb),
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
45 'bex_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bex_from_rgb),
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
46 'rbd_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.rbd_from_rgb),
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
47 'gdx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.gdx_from_rgb),
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
48 'hax_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hax_from_rgb),
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
49 'bro_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bro_from_rgb),
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
50 'bpx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.bpx_from_rgb),
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
51 'ahx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.ahx_from_rgb),
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
52 'hpx_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, skimage.color.hpx_from_rgb),
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
53
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
54 # Recomposition operations (reverse color deconvolution)
2
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
55 'rgb_from_hed': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hed),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
56 'rgb_from_hdx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hdx),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
57 'rgb_from_fgx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_fgx),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
58 'rgb_from_bex': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_bex),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
59 'rgb_from_rbd': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_rbd),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
60 'rgb_from_gdx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_gdx),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
61 'rgb_from_hax': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hax),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
62 'rgb_from_bro': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_bro),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
63 'rgb_from_bpx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_bpx),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
64 'rgb_from_ahx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_ahx),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
65 'rgb_from_hpx': lambda img_raw: skimage.color.combine_stains(img_raw, skimage.color.rgb_from_hpx),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
66
4
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
67 # Custom color deconvolution and recomposition operations
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
68 'rgb_from_he': lambda img_raw: skimage.color.combine_stains(img_raw, rgb_from_he),
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
69 'he_from_rgb': lambda img_raw: skimage.color.separate_stains(img_raw, np.linalg.inv(rgb_from_he)),
2
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
70
4
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
71 # Unsupervised machine learning-based operations
2
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
72 'pca': lambda img_raw: np.reshape(PCA(n_components=3).fit_transform(np.reshape(img_raw, [-1, img_raw.shape[2]])),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
73 [img_raw.shape[0], img_raw.shape[1], -1]),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
74 'nmf': lambda img_raw: np.reshape(NMF(n_components=3, init='nndsvda').fit_transform(np.reshape(img_raw, [-1, img_raw.shape[2]])),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
75 [img_raw.shape[0], img_raw.shape[1], -1]),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
76 'ica': lambda img_raw: np.reshape(FastICA(n_components=3).fit_transform(np.reshape(img_raw, [-1, img_raw.shape[2]])),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
77 [img_raw.shape[0], img_raw.shape[1], -1]),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
78 'fa': lambda img_raw: np.reshape(FactorAnalysis(n_components=3).fit_transform(np.reshape(img_raw, [-1, img_raw.shape[2]])),
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
79 [img_raw.shape[0], img_raw.shape[1], -1])
0
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
80 }
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
81
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
82 parser = argparse.ArgumentParser()
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
83 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file')
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
84 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)')
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
85 parser.add_argument('conv_type', choices=convOptions.keys(), help='conversion type')
4
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
86 parser.add_argument('--isolate_channel', type=int, help='set all other channels to zero (1-3)', default=0)
2
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
87 args = parser.parse_args()
0
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
88
4
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
89 # Read and normalize the input image as TZYXC
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
90 img_in = giatools.io.imread(args.input_file.name)
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
91
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
92 # Verify input image
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
93 assert img_in.shape[0] == 1, f'Image must have 1 frame (it has {img_in.shape[0]} frames)'
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
94 assert img_in.shape[1] == 1, f'Image must have 1 slice (it has {img_in.shape[1]} slices)'
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
95 assert img_in.shape[4] == 3, f'Image must have 3 channels (it has {img_in.shape[4]} channels)'
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
96
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
97 # Normalize the image from TZYXC to YXC
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
98 img_in = img_in.squeeze()
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
99 assert img_in.ndim == 3
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
100
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
101 # Apply channel isolation
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
102 if args.isolate_channel:
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
103 for ch in range(3):
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
104 if ch + 1 != args.isolate_channel:
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
105 img_in[:, :, ch] = 0
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
106
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
107 result = convOptions[args.conv_type](img_in)
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
108
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
109 # It is sufficient to store 32bit floating point data, the precision loss is tolerable
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
110 if result.dtype == np.float64:
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
111 result = result.astype(np.float32)
0
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
112
8aa6e767f737 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/color-deconvolution/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
113 with warnings.catch_warnings():
2
612aa1478fe1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color-deconvolution/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
114 warnings.simplefilter('ignore')
4
5bd113d38acc planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/color_deconvolution/ commit f546b3cd5cbd3a8613cd517975c7ad1d1f83514e
imgteam
parents: 2
diff changeset
115 tifffile.imwrite(args.out_file.name, result)