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