annotate split.py @ 2:227e8928af6e draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
author imgteam
date Fri, 12 Dec 2025 21:02:25 +0000
parents 4b7940d0c051
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
5deaa390ca62 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
5deaa390ca62 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
2
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
3 import os
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
4 import pathlib
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
5 import shutil
0
5deaa390ca62 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
6
5deaa390ca62 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
7 import giatools
5deaa390ca62 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
8 import numpy as np
5deaa390ca62 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
9 import tifffile
5deaa390ca62 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
10
5deaa390ca62 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
11
2
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
12 class OutputWriter:
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
13
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
14 def __init__(self, dir_path: pathlib.Path, num_images: int, squeeze: bool, verbose: bool):
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
15 print(f'Writing {num_images} image(s)')
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
16 decimals = math.ceil(math.log10(1 + num_images))
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
17 self.output_filepath_pattern = str(dir_path / f'%0{decimals}d.tiff')
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
18 self.last_idx = 0
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
19 self.squeeze = squeeze
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
20 self.verbose = verbose
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
21
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
22 def write(self, img: giatools.Image):
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
23 self.last_idx += 1
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
24 if self.squeeze:
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
25 img = img.squeeze()
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
26 if self.last_idx == 1 or self.verbose:
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
27 prefix = f'Output {self.last_idx}' if self.verbose else 'Output'
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
28 print(f'{prefix} axes:', img.axes)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
29 print(f'{prefix} shape:', img.data.shape)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
30 img.write(self.output_filepath_pattern % self.last_idx)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
31
0
5deaa390ca62 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
32
2
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
33 if __name__ == '__main__':
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
34
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
35 parser = argparse.ArgumentParser()
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
36 parser.add_argument('input', type=pathlib.Path)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
37 parser.add_argument('axis', type=str, choices=list(giatools.default_normalized_axes) + ['S', ''])
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
38 parser.add_argument('output', type=pathlib.Path)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
39 parser.add_argument('--squeeze', action='store_true', default=False)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
40 args = parser.parse_args()
0
5deaa390ca62 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
41
2
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
42 # If splitting a file that contains multiple images...
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
43 if args.axis == '':
0
5deaa390ca62 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
44
2
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
45 # Peek the number of series in the input file (if it is a TIFF)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
46 try:
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
47 with tifffile.TiffFile(args.input) as tiff:
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
48 num_tiff_series = len(tiff.series)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
49 print(f'Found TIFF with {num_tiff_series} series')
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
50 except tifffile.TiffFileError:
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
51 num_tiff_series = 0 # not a TIFF file
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
52 print('Not a TIFF file')
0
5deaa390ca62 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
53
2
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
54 # If the file is a multi-series TIFF, extract the individual series
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
55 # (for consistency, also accept only a single series if squeezing is requested)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
56 if num_tiff_series >= 2 or (num_tiff_series == 1 and args.squeeze):
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
57 output = OutputWriter(
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
58 dir_path=args.output,
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
59 num_images=num_tiff_series,
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
60 squeeze=args.squeeze,
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
61 verbose=True,
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
62 )
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
63 for series in range(num_tiff_series):
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
64 img = giatools.Image.read(args.input, series=series)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
65 output.write(
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
66 img.squeeze_like(img.original_axes),
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
67 )
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
68
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
69 # Otherwise, there is nothing to be split (or squeeze)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
70 # (the input is either a single-series TIFF or not a TIFF at all)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
71 elif num_tiff_series == 1: # input is a single-series TIFF (output = input)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
72 try:
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
73 os.symlink(args.input, args.output / '1.tiff')
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
74 except OSError:
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
75 shutil.copyfile(args.input, args.output / '1.tiff')
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
76 else: # input is not a TIFF, conversion needed
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
77 img = giatools.Image.read(args.input)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
78 OutputWriter(
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
79 dir_path=args.output,
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
80 num_images=1,
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
81 squeeze=args.squeeze,
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
82 verbose=False,
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
83 ).write(
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
84 img.squeeze_like(img.original_axes),
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
85 )
0
5deaa390ca62 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
86
2
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
87 # If splitting along an image axes...
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
88 else:
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
89
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
90 # Validate and normalize input parameters
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
91 axis = args.axis.replace('S', 'C')
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
92
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
93 # Read input image with normalized axes
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
94 img_in = giatools.Image.read(args.input)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
95 print('Input image axes:', img_in.original_axes)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
96 print('Input image shape:', img_in.squeeze_like(img_in.original_axes).data.shape)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
97
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
98 # Determine the axis to split along
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
99 axis_pos = img_in.axes.index(axis)
1
4b7940d0c051 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
100
2
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
101 # Perform the splitting
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
102 arr = np.moveaxis(img_in.data, axis_pos, 0)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
103 output = OutputWriter(
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
104 dir_path=args.output,
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
105 num_images=arr.shape[0],
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
106 squeeze=args.squeeze,
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
107 verbose=False,
1
4b7940d0c051 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 6152e9fafda27f76c1e96e533143f86fe605e2da
imgteam
parents: 0
diff changeset
108 )
2
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
109 for img_idx, img in enumerate(arr):
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
110 img = np.moveaxis(img[None], 0, axis_pos)
0
5deaa390ca62 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit f1052360d410594fa1795cf0e02576b8fa4d4135
imgteam
parents:
diff changeset
111
2
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
112 # Construct the output image, remove axes added by normalization
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
113 img_out = giatools.Image(
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
114 data=img,
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
115 axes=img_in.axes,
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
116 metadata=img_in.metadata,
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
117 ).squeeze_like(
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
118 img_in.original_axes,
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
119 )
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
120
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
121 # Save the result (write stdout during first iteration)
227e8928af6e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/split_image/ commit 5e452f10eb88f0fa8a420eec66c6c97e3060e433
imgteam
parents: 1
diff changeset
122 output.write(img_out)