annotate slice_image.py @ 1:8856a7c85e4c draft

"planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit 3d389fdec0db29cf6fbd783c0501455bf624fa90"
author imgteam
date Wed, 18 Dec 2019 05:03:25 -0500
parents aacfbd7c4af6
children f312d414f234
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
1 import argparse
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
2 import sys
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
3 import warnings
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
4 import numpy as np
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
5 import random
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
6 import os.path
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
7 import skimage.io
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
8 import skimage.util
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
9 import skimage.feature
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
10 from scipy.stats import entropy as scipy_entropy
1
8856a7c85e4c "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit 3d389fdec0db29cf6fbd783c0501455bf624fa90"
imgteam
parents: 0
diff changeset
11
0
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
12 def slice_image(input_file, out_folder, label=None, label_out_folder=None, window_size=64,
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
13 stride=1, bg_thresh=1, limit_slices=False, n_thresh=5000, seed=None):
1
8856a7c85e4c "planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit 3d389fdec0db29cf6fbd783c0501455bf624fa90"
imgteam
parents: 0
diff changeset
14 #TODO NOT Implemented:process labels
0
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
15 # --> label and label_out_folder useless so far
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
16
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
17 # primarily for testing purposes:
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
18 if seed is not None:
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
19 random.seed(seed)
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
20
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
21 img_raw = skimage.io.imread(input_file)
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
22 if len(img_raw.shape) == 2:
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
23 img_raw = np.expand_dims(img_raw, 3)
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
24
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
25 with warnings.catch_warnings(): # ignore FutureWarning
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
26 warnings.simplefilter("ignore")
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
27 patches_raw = skimage.util.view_as_windows(img_raw, (window_size, window_size, img_raw.shape[2]), step=stride)
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
28 patches_raw = patches_raw.reshape([-1, window_size, window_size, img_raw.shape[2]])
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
29
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
30 new_path = os.path.join(out_folder, "%d.tiff")
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
31
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
32 #samples for thresholding the amount of slices
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
33 sample = random.sample(range(patches_raw.shape[0]), n_thresh)
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
34
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
35 for i in range(0, patches_raw.shape[0]):
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
36 # TODO improve
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
37 sum_image = np.sum(patches_raw[i], 2)/img_raw.shape[2]
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
38 total_entr = np.var(sum_image.reshape([-1]))
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
39
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
40 if bg_thresh > 0:
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
41 sum_image = skimage.util.img_as_uint(sum_image)
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
42 g = skimage.feature.greycomatrix(sum_image, [1,2], [0, np.pi/2], nnormed=True, symmetric=True)
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
43 hom = np.var(skimage.feature.greycoprops(g, prop='homogeneity'))
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
44 if hom > bg_thresh: #0.0005
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
45 continue
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
46
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
47 if limit_slices == True:
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
48 if i in sample:
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
49 res = skimage.util.img_as_uint(patches_raw[i]) #Attention: precision loss
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
50 skimage.io.imsave(new_path % i, res, plugin='tifffile')
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
51 else:
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
52 res = skimage.util.img_as_uint(patches_raw[i]) #Attention: precision loss
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
53 skimage.io.imsave(new_path % i, res, plugin='tifffile')
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
54
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
55
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
56 if __name__ == "__main__":
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
57 parser = argparse.ArgumentParser()
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
58 parser.add_argument('input_file', type=argparse.FileType('r'), help='input file')
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
59 parser.add_argument('out_folder', help='out folder')
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
60 parser.add_argument('--label', dest='label_file', default=None, help='auxiliary label file to split in the same way')
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
61 parser.add_argument('--label_out_folder', dest='label_out_folder', default=None, help='label out folder')
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
62 parser.add_argument('--stride', dest='stride', type=int, default=1, help='applied stride')
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
63 parser.add_argument('--window_size', dest='window_size', type=int, default=64, help='size of resulting patches')
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
64 parser.add_argument('--bg_thresh', dest='bg_thresh', type=float, default=0, help='skip patches without information using a treshold')
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
65 parser.add_argument('--limit_slices', dest='limit_slices', type=bool, default=False, help='limit amount of slices')
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
66 parser.add_argument('--n_thresh', dest='n_thresh', type=int, default=5000, help='amount of slices')
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
67 parser.add_argument('--seed', dest='seed', type=int, default=None, help='seed for random choice of limited slices')
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
68 args = parser.parse_args()
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
69
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
70 slice_image(args.input_file.name, args.out_folder,
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
71 label=args.label_file, label_out_folder=args.label_out_folder,
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
72 stride=args.stride, window_size=args.window_size, bg_thresh=args.bg_thresh,
aacfbd7c4af6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/slice_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
73 limit_slices=args.limit_slices, n_thresh=args.n_thresh, seed=args.seed)