annotate scale_image.py @ 4:5122286b700e draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
author imgteam
date Sat, 13 Dec 2025 22:11:13 +0000
parents ba2b1a6f1b84
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
1 import argparse
4
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
2 import json
0
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
3 import sys
4
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
4 from typing import (
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
5 Any,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
6 Literal,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
7 )
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
8
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
9 import giatools.io
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
10 import numpy as np
0
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
11 import skimage.io
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
12 import skimage.transform
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
13 import skimage.util
4
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
14
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
15
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
16 def get_uniform_scale(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
17 img: giatools.Image,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
18 axes: Literal['all', 'spatial'],
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
19 factor: float,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
20 ) -> tuple[float, ...]:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
21 """
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
22 Determine a tuple of `scale` factors for uniform or spatially uniform scaling.
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
23
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
24 Axes, that are not present in the original image data, are ignored.
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
25 """
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
26 ignored_axes = [
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
27 axis for axis_idx, axis in enumerate(img.axes)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
28 if axis not in img.original_axes or (
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
29 factor < 1 and img.data.shape[axis_idx] == 1
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
30 )
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
31 ]
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
32 match axes:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
33
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
34 case 'all':
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
35 return tuple(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
36 [
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
37 (factor if axis not in ignored_axes else 1)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
38 for axis in img.axes if axis != 'C'
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
39 ]
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
40 )
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
41
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
42 case 'spatial':
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
43 return tuple(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
44 [
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
45 (factor if axis in 'YXZ' and axis not in ignored_axes else 1)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
46 for axis in img.axes if axis != 'C'
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
47 ]
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
48 )
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
49
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
50 case _:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
51 raise ValueError(f'Unknown axes for uniform scaling: "{axes}"')
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
52
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
53
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
54 def get_scale_for_isotropy(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
55 img: giatools.Image,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
56 sample: Literal['up', 'down'],
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
57 ) -> tuple[float, ...]:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
58 """
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
59 Determine a tuple of `scale` factors to establish spatial isotropy.
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
60
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
61 The `sample` parameter governs whether to up-sample or down-sample the image data.
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
62 """
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
63 scale = [1] * (len(img.axes) - 1) # omit the channel axis
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
64 z_axis, y_axis, x_axis = [
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
65 img.axes.index(axis) for axis in 'ZYX'
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
66 ]
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
67
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
68 # Determine the pixel size of the image
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
69 if 'resolution' in img.metadata:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
70 pixel_size = np.divide(1, img.metadata['resolution'])
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
71 else:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
72 sys.exit('Resolution information missing in image metadata')
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
73
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
74 # Define unified transformation of pixel/voxel sizes to scale factors
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
75 def voxel_size_to_scale(voxel_size: np.ndarray) -> list:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
76 match sample:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
77 case 'up':
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
78 return (voxel_size / voxel_size.min()).tolist()
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
79 case 'down':
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
80 return (voxel_size / voxel_size.max()).tolist()
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
81 case _:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
82 raise ValueError(f'Unknown value for sample: "{sample}"')
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
83
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
84 # Handle the 3-D case
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
85 if img.data.shape[z_axis] > 1:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
86
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
87 # Determine the voxel depth of the image
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
88 if (voxel_depth := img.metadata.get('z_spacing', None)) is None:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
89 sys.exit('Voxel depth information missing in image metadata')
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
90
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
91 # Determine the XYZ scale factors
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
92 scale[x_axis], scale[y_axis], scale[z_axis] = (
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
93 voxel_size_to_scale(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
94 np.array([*pixel_size, voxel_depth]),
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
95 )
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
96 )
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
97
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
98 # Handle the 2-D case
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
99 else:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
100
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
101 # Determine the XY scale factors
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
102 scale[x_axis], scale[y_axis] = (
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
103 voxel_size_to_scale(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
104 np.array(pixel_size),
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
105 )
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
106 )
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
107
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
108 return tuple(scale)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
109
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
110
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
111 def get_aa_sigma_by_scale(scale: float) -> float:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
112 """
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
113 Determine the optimal size of the Gaussian filter for anti-aliasing.
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
114
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
115 See for details: https://scikit-image.org/docs/0.25.x/api/skimage.transform.html#skimage.transform.rescale
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
116 """
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
117 return (1 / scale - 1) / 2 if scale < 1 else 0
0
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
118
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
119
4
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
120 def get_new_metadata(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
121 old: giatools.Image,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
122 scale: float | tuple[float, ...],
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
123 arr: np.ndarray,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
124 ) -> dict[str, Any]:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
125 """
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
126 Determine the result metadata (copy and adapt).
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
127 """
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
128 metadata = dict(old.metadata)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
129 scales = (
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
130 [scale] * (len(old.axes) - 1) # omit the channel axis
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
131 if isinstance(scale, float) else scale
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
132 )
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
133
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
134 # Determine the original pixel size
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
135 old_pixel_size = (
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
136 np.divide(1, old.metadata['resolution'])
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
137 if 'resolution' in old.metadata else (1, 1)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
138 )
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
139
4
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
140 # Determine the new pixel size and update metadata
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
141 new_pixel_size = np.divide(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
142 old_pixel_size,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
143 (
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
144 scales[old.axes.index('X')],
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
145 scales[old.axes.index('Y')],
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
146 ),
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
147 )
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
148 metadata['resolution'] = tuple(1 / new_pixel_size)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
149
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
150 # Update the metadata for the new voxel depth
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
151 old_voxel_depth = old.metadata.get('z_spacing', 1)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
152 metadata['z_spacing'] = old_voxel_depth / scales[old.axes.index('Z')]
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
153
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
154 return metadata
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
155
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
156
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
157 def metadata_to_str(metadata: dict) -> str:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
158 tokens = list()
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
159 for key in sorted(metadata.keys()):
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
160 value = metadata[key]
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
161 if isinstance(value, tuple):
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
162 value = '(' + ', '.join([f'{val}' for val in value]) + ')'
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
163 tokens.append(f'{key}: {value}')
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
164 if len(metadata_str := ', '.join(tokens)) > 0:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
165 return metadata_str
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
166 else:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
167 return 'has no metadata'
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
168
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
169
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
170 def write_output(filepath: str, img: giatools.Image):
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
171 """
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
172 Validate that the output file format is suitable for the image data, then write it.
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
173 """
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
174 print('Output shape:', img.data.shape)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
175 print('Output axes:', img.axes)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
176 print('Output', metadata_to_str(img.metadata))
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
177
4
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
178 # Validate that the output file format is suitable for the image data
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
179 if filepath.lower().endswith('.png'):
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
180 if not frozenset(img.axes) <= frozenset('YXC'):
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
181 sys.exit(f'Cannot write PNG file with axes "{img.axes}"')
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
182
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
183 # Write image data to the output file
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
184 img.write(filepath)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
185
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
186
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
187 def scale_image(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
188 input_filepath: str,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
189 output_filepath: str,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
190 mode: Literal['uniform', 'explicit', 'isotropy'],
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
191 order: int,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
192 anti_alias: bool,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
193 **cfg,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
194 ):
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
195 img = giatools.Image.read(input_filepath)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
196 print('Input axes:', img.original_axes)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
197 print('Input', metadata_to_str(img.metadata))
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
198
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
199 # Determine `scale` for scaling
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
200 match mode:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
201
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
202 case 'uniform':
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
203 scale = get_uniform_scale(img, cfg['axes'], cfg['factor'])
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
204
4
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
205 case 'explicit':
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
206 scale = tuple(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
207 [cfg.get(f'factor_{axis.lower()}', 1) for axis in img.axes if axis != 'C']
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
208 )
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
209
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
210 case 'isotropy':
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
211 scale = get_scale_for_isotropy(img, cfg['sample'])
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
212
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
213 case _:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
214 raise ValueError(f'Unknown mode: "{mode}"')
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
215
4
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
216 # Assemble remaining `rescale` parameters
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
217 rescale_kwargs = dict(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
218 scale=scale,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
219 order=order,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
220 preserve_range=True,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
221 channel_axis=img.axes.index('C'),
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
222 )
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
223 if (anti_alias := anti_alias and (np.array(scale) < 1).any()):
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
224 rescale_kwargs['anti_aliasing'] = anti_alias
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
225 rescale_kwargs['anti_aliasing_sigma'] = tuple(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
226 [
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
227 get_aa_sigma_by_scale(s) for s in scale
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
228 ] + [0] # `skimage.transform.rescale` also expects a value for the channel axis
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
229 )
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
230 else:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
231 rescale_kwargs['anti_aliasing'] = False
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
232
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
233 # Re-sample the image data to perform the scaling
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
234 for key, value in rescale_kwargs.items():
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
235 print(f'{key}: {value}')
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
236 arr = skimage.transform.rescale(img.data, **rescale_kwargs)
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
237
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
238 # Preserve the `dtype` so that both brightness and range of values is preserved
4
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
239 if arr.dtype != img.data.dtype:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
240 if np.issubdtype(img.data.dtype, np.integer):
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
241 arr = arr.round()
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
242 arr = arr.astype(img.data.dtype)
3
ba2b1a6f1b84 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit c55311bbe4c3d7e0039c77785509a150864bb272
imgteam
parents: 2
diff changeset
243
4
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
244 # Determine the result metadata and save result
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
245 metadata = get_new_metadata(img, scale, arr)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
246 write_output(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
247 output_filepath,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
248 giatools.Image(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
249 data=arr,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
250 axes=img.axes,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
251 metadata=metadata,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
252 ).squeeze()
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
253 )
0
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
254
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
255
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
256 if __name__ == "__main__":
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
257 parser = argparse.ArgumentParser()
4
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
258 parser.add_argument('input', type=str)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
259 parser.add_argument('output', type=str)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
260 parser.add_argument('params', type=str)
0
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
261 args = parser.parse_args()
8b187dfeb541 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/scale_image/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
262
4
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
263 # Read the config file
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
264 with open(args.params) as cfgf:
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
265 cfg = json.load(cfgf)
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
266
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
267 # Perform scaling
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
268 scale_image(
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
269 args.input,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
270 args.output,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
271 **cfg,
5122286b700e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/scale_image/ commit cd908933bd7bd8756c213af57ea6343a90effc12
imgteam
parents: 3
diff changeset
272 )