annotate concat_channels.py @ 2:ad1caf2331c6 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
author imgteam
date Sun, 07 Dec 2025 16:15:54 +0000
parents 31c8c700d98d
children 01c1d5af33be
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
24d64e0a405f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
1 import argparse
2
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
2
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
3 import giatools
0
24d64e0a405f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
4 import numpy as np
24d64e0a405f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
5 import skimage.io
24d64e0a405f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
6 import skimage.util
24d64e0a405f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
7
2
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
8
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
9 normalized_axes = 'QTZYXC'
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
10
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
11
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
12 def concat_channels(
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
13 input_image_paths: list[str],
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
14 output_image_path: str,
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
15 axis: str,
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
16 preserve_values: bool,
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
17 ):
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
18 # Create list of arrays to be concatenated
0
24d64e0a405f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
19 images = []
24d64e0a405f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
20 for image_path in input_image_paths:
2
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
21
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
22 img = giatools.Image.read(image_path, normalize_axes=normalized_axes)
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
23 arr = img.data
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
24
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
25 # Preserve values: Convert to `float` dtype without changing the values
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
26 if preserve_values:
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
27 arr = arr.astype(float)
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
28
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
29 # Preserve brightness: Scale values to 0..1
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
30 else:
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
31 arr = skimage.util.img_as_float(arr)
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
32
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
33 images.append(arr)
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
34
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
35 # Do the concatenation
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
36 axis_pos = normalized_axes.index(axis)
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
37 arr = np.concatenate(images, axis_pos)
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
38 res = giatools.Image(arr, normalized_axes)
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
39
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
40 # Squeeze singleton axes and save
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
41 squeezed_axes = ''.join(np.array(list(res.axes))[np.array(arr.shape) > 1])
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
42 res = res.squeeze_like(squeezed_axes)
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
43 res.write(output_image_path, backend='tifffile')
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
44
0
24d64e0a405f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
45
24d64e0a405f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
46 if __name__ == "__main__":
24d64e0a405f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
47 parser = argparse.ArgumentParser()
2
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
48 parser.add_argument('input_files', type=str, nargs='+')
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
49 parser.add_argument('out_file', type=str)
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
50 parser.add_argument('axis', type=str)
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
51 parser.add_argument('--preserve_values', default=False, action='store_true')
0
24d64e0a405f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
52 args = parser.parse_args()
24d64e0a405f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
53
2
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
54 concat_channels(
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
55 args.input_files,
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
56 args.out_file,
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
57 args.axis,
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
58 args.preserve_values,
ad1caf2331c6 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents: 1
diff changeset
59 )