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