annotate split.py @ 1:73d7a4ffc03d draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
author imgteam
date Sat, 12 Apr 2025 15:05:29 +0000
parents 6b42bec75e69
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
1 import argparse
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
2 import math
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
3
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
4 import giatools
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
5 import giatools.util
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
6 import numpy as np
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
7 import tifffile
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
8
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
9
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
10 parser = argparse.ArgumentParser()
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
11 parser.add_argument('input', type=str)
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
12 parser.add_argument('axis', type=str)
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
13 parser.add_argument('output', type=str)
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
14 parser.add_argument('--squeeze', action='store_true', default=False)
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
15 args = parser.parse_args()
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
16
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
17 # Validate and normalize input parameters
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
18 assert len(args.axis) == 1, 'Axis input must be a single character.'
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
19 axis = args.axis.replace('S', 'C')
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
20
1
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
21 # Read input image with normalized axes
0
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
22 img_in = giatools.Image.read(args.input)
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
23
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
24 # Determine the axis to split along
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
25 axis_pos = img_in.axes.index(axis)
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
26
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
27 # Perform the splitting
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
28 arr = np.moveaxis(img_in.data, axis_pos, 0)
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
29 decimals = math.ceil(math.log10(1 + arr.shape[0]))
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
30 output_filename_pattern = f'{args.output}/%0{decimals}d.tiff'
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
31 for img_idx, img in enumerate(arr):
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
32 img = np.moveaxis(img[None], 0, axis_pos)
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
33
1
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
34 # Construct the output image, remove axes added by normalization
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
35 img_out = giatools.Image(
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
36 data=img,
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
37 axes=img_in.axes,
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
38 ).squeeze_like(
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
39 img_in.original_axes,
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
40 )
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
41
0
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
42 # Optionally, squeeze the image
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
43 if args.squeeze:
1
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
44 s = [
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
45 axis_pos for axis_pos in range(len(img_out.axes))
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
46 if img_out.data.shape[axis_pos] == 1 and img_out.axes[axis_pos] not in 'YX'
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
47 ]
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
48 img_out = img_out.squeeze_like(
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
49 giatools.util.str_without_positions(img_out.axes, s),
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
50 )
0
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
51
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
52 # Save the result
6b42bec75e69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
53 filename = output_filename_pattern % (img_idx + 1)
1
73d7a4ffc03d planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
54 tifffile.imwrite(filename, img_out.data, metadata=dict(axes=img_out.axes))