Mercurial > repos > imgteam > concat_channels
annotate concat_channels.py @ 6:999c5941a6f0 draft default tip
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
| author | imgteam |
|---|---|
| date | Fri, 12 Dec 2025 21:15:56 +0000 |
| parents | 8d50a0a9e4af |
| children |
| 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 |
|
6
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
2 from typing import Any |
|
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
|
3 |
|
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
|
4 import giatools |
|
0
30517f733f7b
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
5 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
|
6 import skimage.io |
|
30517f733f7b
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
7 import skimage.util |
|
30517f733f7b
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
8 |
|
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
|
9 |
|
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
|
10 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
|
11 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
|
12 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
|
13 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
|
14 preserve_values: bool, |
|
6
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
15 sort_by: str | None, |
|
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
|
16 ): |
|
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 # Create list of arrays to be concatenated |
|
6
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
18 images = list() |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
19 metadata = dict() |
|
0
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 |
|
6
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
22 img = giatools.Image.read(image_path, normalize_axes=giatools.default_normalized_axes) |
|
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
|
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 |
|
6
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
33 # Record the metadata |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
34 for metadata_key, metadata_value in img.metadata.items(): |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
35 metadata.setdefault(metadata_key, list()) |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
36 metadata[metadata_key].append(metadata_value) |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
37 |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
38 # Record the image data |
|
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
|
39 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
|
40 |
|
6
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
41 # Perform sorting, if requested |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
42 if sort_by is not None: |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
43 |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
44 # Validate that `sort_by` is available as metadata for all images |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
45 sort_keys = list( |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
46 filter( |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
47 lambda value: value is not None, |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
48 metadata.get(sort_by, list()), |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
49 ), |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
50 ) |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
51 if len(sort_keys) != len(images): |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
52 raise ValueError( |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
53 f'Requested to sort by "{sort_by}", ' |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
54 f'but this is not available for all {len(images)} images' |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
55 f' (available for only {len(sort_keys)} images)' |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
56 ) |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
57 |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
58 # Sort images by the corresponding `sort_key` metadata value |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
59 sorted_indices = sorted(range(len(images)), key=lambda i: sort_keys[i]) |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
60 images = [images[i] for i in sorted_indices] |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
61 |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
62 # Determine consensual metadata |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
63 # TODO: Convert metadata of images with different units of measurement into a common unit |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
64 final_metadata = dict() |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
65 for metadata_key, metadata_values in metadata.items(): |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
66 if (metadata_value := reduce_metadata(metadata_values)) is not None: |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
67 final_metadata[metadata_key] = metadata_value |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
68 |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
69 # Update the `z_spacing` metadata, if concatenating along the Z-axis and `z_position` is available for all images |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
70 if axis == 'Z' and len(images) >= 2 and len(z_positions := metadata.get('z_position', list())) == len(images): |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
71 z_positions = sorted(z_positions) # don't mutate the `metadata` dictionary for easier future code maintenance |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
72 final_metadata['z_spacing'] = abs(np.subtract(z_positions[1:], z_positions[:-1]).mean()) |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
73 |
|
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
|
74 # Do the concatenation |
|
6
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
75 axis_pos = giatools.default_normalized_axes.index(axis) |
|
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
|
76 arr = np.concatenate(images, axis_pos) |
|
6
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
77 res = giatools.Image( |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
78 data=arr, |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
79 axes=giatools.default_normalized_axes, |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
80 metadata=final_metadata, |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
81 ) |
|
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
|
82 |
|
8d50a0a9e4af
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit 4573c7c050968a40f6377a95727694105fbd69c7
imgteam
parents:
4
diff
changeset
|
83 # Squeeze singleton axes and save |
|
6
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
84 res = res.squeeze() |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
85 print('Output TIFF shape:', res.data.shape) |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
86 print('Output TIFF axes:', res.axes) |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
87 print('Output TIFF', metadata_to_str(final_metadata)) |
|
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
|
88 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
|
89 |
|
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
|
90 |
|
6
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
91 def reduce_metadata(values: list[Any]) -> Any | None: |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
92 non_none_values = list(filter(lambda value: value is not None, values)) |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
93 |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
94 # Reduction is not possible if more than one type is involved (or none) |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
95 value_types = [type(value) for value in non_none_values] |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
96 if len(frozenset(value_types)) != 1: |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
97 return None |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
98 else: |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
99 value_type = value_types[0] |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
100 |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
101 # For floating point types, reduce via arithmetic average |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
102 if np.issubdtype(value_type, np.floating): |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
103 return np.mean(non_none_values) |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
104 |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
105 # For integer types, reduce via the median |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
106 if np.issubdtype(value_type, np.integer): |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
107 return int(np.median(non_none_values)) |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
108 |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
109 # For all other types, reduction is only possible if the values are identical |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
110 if len(frozenset(non_none_values)) == 1: |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
111 return non_none_values[0] |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
112 else: |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
113 return None |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
114 |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
115 |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
116 def metadata_to_str(metadata: dict) -> str: |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
117 tokens = list() |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
118 for key in sorted(metadata.keys()): |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
119 value = metadata[key] |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
120 if isinstance(value, tuple): |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
121 value = '(' + ', '.join([f'{val}' for val in value]) + ')' |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
122 tokens.append(f'{key}: {value}') |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
123 return ', '.join(tokens) |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
124 |
|
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
125 |
|
0
30517f733f7b
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
126 if __name__ == "__main__": |
|
30517f733f7b
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
127 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
|
128 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
|
129 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
|
130 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
|
131 parser.add_argument('--preserve_values', default=False, action='store_true') |
|
6
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
132 parser.add_argument('--sort_by', type=str, default=None) |
|
0
30517f733f7b
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/concat_channels/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
133 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
|
134 |
|
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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 args.preserve_values, |
|
6
999c5941a6f0
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/concat_channels/ commit a94f04c109c545a9f892a6ce7a5ffef152253201
imgteam
parents:
5
diff
changeset
|
140 args.sort_by, |
|
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
|
141 ) |
