annotate run-superdsm.py @ 3:c2412cb5fc2e draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 2c079a99cf5426a8970b656ef35eb1fc6e4dd4a9
author imgteam
date Thu, 18 Dec 2025 08:36:17 +0000
parents 7fcd96cf0d52
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
1 """
55b3a064638a 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.
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
3
55b3a064638a 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.
55b3a064638a 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
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
6
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
7 """
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
8
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
9 import argparse
2
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
10 import csv
0
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
11 import imghdr
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
12 import os
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
13 import pathlib
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
14 import shutil
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
15 import tempfile
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
16
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
17
1
680b866ba043 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
18 hyperparameters = [
680b866ba043 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
19 ('AF_scale', float),
2
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
20 ('c2f-region-analysis/min_atom_radius', float),
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
21 ('c2f-region-analysis/min_norm_energy_improvement', float),
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
22 ('c2f-region-analysis/max_atom_norm_energy', float),
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
23 ('c2f-region-analysis/max_cluster_marker_irregularity', float),
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
24 ('dsm/alpha', float),
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
25 ('dsm/AF_alpha', float),
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
26 ('global-energy-minimization/pruning', str),
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
27 ('global-energy-minimization/beta', float),
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
28 ('global-energy-minimization/AF_beta', float),
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
29 ('postprocess/mask_max_distance', int),
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
30 ('postprocess/mask_stdamp', float),
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
31 ('postprocess/max_norm_energy', float),
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
32 ('postprocess/min_contrast', float),
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
33 ('postprocess/min_object_radius', float),
1
680b866ba043 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
34 ]
680b866ba043 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
35
680b866ba043 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
36
680b866ba043 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):
2
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
38 return key.replace('/', '_').replace('-', '_')
1
680b866ba043 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
39
680b866ba043 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
40
680b866ba043 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):
680b866ba043 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()
680b866ba043 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:
680b866ba043 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))
680b866ba043 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:
680b866ba043 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
46 cfg[key] = value
680b866ba043 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
47 return cfg
680b866ba043 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
48
680b866ba043 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
49
2
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
50 def flatten_dict(d, sep='/'):
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
51 result = {}
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
52 for key, val in d.items():
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
53 if isinstance(val, dict):
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
54 for sub_key, sub_val in flatten_dict(val, sep=sep).items():
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
55 result[f'{key}{sep}{sub_key}'] = sub_val
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
56 else:
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
57 result[key] = val
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
58 return result
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
59
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
60
0
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
61 if __name__ == "__main__":
55b3a064638a 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')
2
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
63 parser.add_argument('image', type=str, help='Path to the input image')
0
55b3a064638a 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)
2
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
65 parser.add_argument('--do-masks', type=str, default=None, help='Path to the file containing the segmentation masks')
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
66 parser.add_argument('--do-cfg', type=str, default=None, help='Path to the file containing the configuration')
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
67 parser.add_argument('--do-overlay', type=str, default=None, help='Path to the file containing the overlay of the segmentation results')
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
68 parser.add_argument('--do-overlay-border', type=int)
1
680b866ba043 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:
680b866ba043 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
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
71 args = parser.parse_args()
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
72
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
73 if args.slots >= 2:
55b3a064638a 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
55b3a064638a 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
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
76 else:
55b3a064638a 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
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
78 num_processes = 1
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
79
55b3a064638a 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)
55b3a064638a 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
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
82
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
83 import giatools.io
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
84 import ray
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
85 import superdsm.automation
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
86 import superdsm.io
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
87 import superdsm.render
0
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
88
2
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
89 # The explicit `dir` and `prefix` is to avoid breaking the 107 byte limit for socket paths in Biocontainers
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
90 # See for details: https://github.com/BMCV/galaxy-image-analysis/pull/178
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
91 with tempfile.TemporaryDirectory(dir='/tmp', prefix='superdsm') as tmpdirname:
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
92 tmpdir = pathlib.Path(tmpdirname)
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
93 ray.init(num_cpus=num_processes, log_to_driver=True, _temp_dir=str(tmpdir / 'ray'))
0
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
94
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
95 img_ext = imghdr.what(args.image)
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
96 img_filepath = tmpdir / f'input.{img_ext}'
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
97 shutil.copy(str(args.image), img_filepath)
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
98
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
99 pipeline = superdsm.pipeline.create_default_pipeline()
1
680b866ba043 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 7db4b765fa5a1f526cc94cd1f65d963d1e047b27
imgteam
parents: 0
diff changeset
100 cfg = create_config(args)
2
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
101 img = giatools.io.imread(img_filepath, impl=superdsm.io.imread)
0
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
102
2
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
103 # Create configuration if it is required:
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
104 if args.do_cfg or args.do_overlay or args.do_masks:
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
105 cfg, _ = superdsm.automation.create_config(pipeline, cfg, img)
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
106
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
107 # Perform segmentation if it is required:
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
108 if args.do_overlay or args.do_masks:
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
109 print('Performing segmentation')
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
110 data, cfg, _ = pipeline.process_image(img, cfg)
0
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
111
2
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
112 # Write configuration used for segmentation, or the automatically created one, otherwise:
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
113 if args.do_cfg:
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
114 print(f'Writing config to: {args.do_cfg}')
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
115 with open(args.do_cfg, 'w') as fp:
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
116 tsv_out = csv.writer(fp, delimiter='\t')
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
117 tsv_out.writerow(['Hyperparameter', 'Value'])
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
118 rows = sorted(flatten_dict(cfg.entries).items(), key=lambda item: item[0])
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
119 for key, value in rows:
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
120 tsv_out.writerow([key, value])
0
55b3a064638a planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
imgteam
parents:
diff changeset
121
2
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
122 # Write the overlay image:
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
123 if args.do_overlay:
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
124 print(f'Writing overlay to: {args.do_overlay}')
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
125 overlay = superdsm.render.render_result_over_image(data, border_width=args.do_overlay_border, normalize_img=False)
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
126 superdsm.io.imwrite(args.do_overlay, overlay)
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
127
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
128 # Write the label map:
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
129 if args.do_masks:
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
130 print(f'Writing masks to: {args.do_masks}')
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
131 masks = superdsm.render.rasterize_labels(data)
7fcd96cf0d52 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit 597cb3dc531963d02c5db0e4fc2596fcb06728dd
imgteam
parents: 1
diff changeset
132 superdsm.io.imwrite(args.do_masks, masks)