annotate run-superdsm.py @ 6:79ec3263686a draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
author imgteam
date Wed, 24 Apr 2024 08:13:34 +0000
parents dc5f72f6b1e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
1 """
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
2 Copyright 2023 Leonid Kostrykin, Biomedical Computer Vision Group, Heidelberg University.
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
3
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
4 Distributed under the MIT license.
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
6
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
7 """
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
8
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
9 import argparse
3
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
10 import csv
0
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
11 import imghdr
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
12 import os
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
13 import pathlib
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
14 import shutil
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
15 import tempfile
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
16
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
17
1
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
18 hyperparameters = [
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
19 ('AF_scale', float),
4
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
20 ('c2f-region-analysis/min_atom_radius', float),
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
21 ('c2f-region-analysis/min_norm_energy_improvement', float),
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
22 ('c2f-region-analysis/max_atom_norm_energy', float),
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
23 ('c2f-region-analysis/max_cluster_marker_irregularity', float),
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
24 ('dsm/alpha', float),
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
25 ('dsm/AF_alpha', float),
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
26 ('global-energy-minimization/pruning', str),
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
27 ('global-energy-minimization/beta', float),
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
28 ('global-energy-minimization/AF_beta', float),
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
29 ('postprocess/mask_max_distance', int),
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
30 ('postprocess/mask_stdamp', float),
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
31 ('postprocess/max_norm_energy', float),
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
32 ('postprocess/min_contrast', float),
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
33 ('postprocess/min_object_radius', float),
1
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
34 ]
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
35
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
36
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
37 def get_param_name(key):
4
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
38 return key.replace('/', '_').replace('-', '_')
1
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
39
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
40
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
41 def create_config(args):
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
42 cfg = superdsm.config.Config()
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
43 for key, _ in hyperparameters:
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
44 value = getattr(args, get_param_name(key))
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
45 if value is not None:
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
46 cfg[key] = value
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
47 return cfg
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
48
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
49
3
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
50 def flatten_dict(d, sep='/'):
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
51 result = {}
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
52 for key, val in d.items():
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
53 if isinstance(val, dict):
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
54 for sub_key, sub_val in flatten_dict(val, sep=sep).items():
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
55 result[f'{key}{sep}{sub_key}'] = sub_val
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
56 else:
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
57 result[key] = val
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
58 return result
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
59
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
60
0
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
61 if __name__ == "__main__":
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
62 parser = argparse.ArgumentParser(description='Segmentation of cell nuclei in 2-D fluorescence microscopy images')
3
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
63 parser.add_argument('image', type=str, help='Path to the input image')
0
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
64 parser.add_argument('slots', type=int)
3
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
65 parser.add_argument('--do-masks', type=str, default=None, help='Path to the file containing the segmentation masks')
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
66 parser.add_argument('--do-cfg', type=str, default=None, help='Path to the file containing the configuration')
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
67 parser.add_argument('--do-overlay', type=str, default=None, help='Path to the file containing the overlay of the segmentation results')
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
68 parser.add_argument('--do-overlay-border', type=int)
1
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
69 for key, ptype in hyperparameters:
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
70 parser.add_argument('--' + get_param_name(key), type=ptype, default=None)
0
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
71 args = parser.parse_args()
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
72
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
73 if args.slots >= 2:
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
74 num_threads_per_process = 2
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
75 num_processes = args.slots // num_threads_per_process
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
76 else:
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
77 num_threads_per_process = 1
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
78 num_processes = 1
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
79
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
80 os.environ['MKL_NUM_THREADS'] = str(num_threads_per_process)
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
81 os.environ['OPENBLAS_NUM_THREADS'] = str(num_threads_per_process)
2
244f67290d28 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
82
6
79ec3263686a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 4
diff changeset
83 import giatools.io
2
244f67290d28 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
84 import ray
244f67290d28 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
85 import superdsm.automation
244f67290d28 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
86 import superdsm.io
244f67290d28 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 1
diff changeset
87 import superdsm.render
0
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
88
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
89 ray.init(num_cpus=num_processes, log_to_driver=True)
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
90
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
91 with tempfile.TemporaryDirectory() as tmpdirname:
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
92 tmpdir = pathlib.Path(tmpdirname)
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
93 img_ext = imghdr.what(args.image)
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
94 img_filepath = tmpdir / f'input.{img_ext}'
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
95 shutil.copy(str(args.image), img_filepath)
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
96
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
97 pipeline = superdsm.pipeline.create_default_pipeline()
1
700ae37e5c69 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
98 cfg = create_config(args)
6
79ec3263686a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 4
diff changeset
99 img = giatools.io.imread(img_filepath, impl=superdsm.io.imread)
0
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
100
4
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
101 # Create configuration if it is required:
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
102 if args.do_cfg or args.do_overlay or args.do_masks:
3
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
103 cfg, _ = superdsm.automation.create_config(pipeline, cfg, img)
0
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
104
4
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
105 # Perform segmentation if it is required:
3
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
106 if args.do_overlay or args.do_masks:
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
107 print('Performing segmentation')
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
108 data, cfg, _ = pipeline.process_image(img, cfg)
0
1b0fc671187f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
109
4
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
110 # Write configuration used for segmentation, or the automatically created one, otherwise:
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
111 if args.do_cfg:
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
112 print(f'Writing config to: {args.do_cfg}')
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
113 with open(args.do_cfg, 'w') as fp:
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
114 tsv_out = csv.writer(fp, delimiter='\t')
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
115 tsv_out.writerow(['Hyperparameter', 'Value'])
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
116 rows = sorted(flatten_dict(cfg.entries).items(), key=lambda item: item[0])
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
117 for key, value in rows:
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
118 tsv_out.writerow([key, value])
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
119
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
120 # Write the overlay image:
3
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
121 if args.do_overlay:
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
122 print(f'Writing overlay to: {args.do_overlay}')
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
123 overlay = superdsm.render.render_result_over_image(data, border_width=args.do_overlay_border, normalize_img=False)
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
124 superdsm.io.imwrite(args.do_overlay, overlay)
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
125
4
dc5f72f6b1e9 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit b0b09d6788778541d1c0b89ca96101fc57d60e22
imgteam
parents: 3
diff changeset
126 # Write the label map:
3
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
127 if args.do_masks:
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
128 print(f'Writing masks to: {args.do_masks}')
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
129 masks = superdsm.render.rasterize_labels(data)
7fd8dba15bd3 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
imgteam
parents: 2
diff changeset
130 superdsm.io.imwrite(args.do_masks, masks)