comparison run-superdsm.py @ 0:1b0fc671187f draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/superdsm/ commit 4d66ff6e8a2a842e44e8d0d7102dfb3ac78dca7e
author imgteam
date Sun, 25 Jun 2023 21:48:40 +0000
parents
children 700ae37e5c69
comparison
equal deleted inserted replaced
-1:000000000000 0:1b0fc671187f
1 """
2 Copyright 2023 Leonid Kostrykin, Biomedical Computer Vision Group, Heidelberg University.
3
4 Distributed under the MIT license.
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
6
7 """
8
9 import argparse
10 import imghdr
11 import os
12 import pathlib
13 import shutil
14 import tempfile
15
16 import ray
17 import superdsm.automation
18 import superdsm.io
19 import superdsm.render
20
21
22 if __name__ == "__main__":
23 parser = argparse.ArgumentParser(description='Segmentation of cell nuclei in 2-D fluorescence microscopy images')
24 parser.add_argument('image', help='Path to the input image')
25 parser.add_argument('cfg', help='Path to the file containing the configuration')
26 parser.add_argument('masks', help='Path to the file containing the segmentation masks')
27 parser.add_argument('overlay', help='Path to the file containing the overlay of the segmentation results')
28 parser.add_argument('seg_border', type=int)
29 parser.add_argument('slots', type=int)
30 args = parser.parse_args()
31
32 if args.slots >= 2:
33 num_threads_per_process = 2
34 num_processes = args.slots // num_threads_per_process
35 else:
36 num_threads_per_process = 1
37 num_processes = 1
38
39 os.environ['MKL_NUM_THREADS'] = str(num_threads_per_process)
40 os.environ['OPENBLAS_NUM_THREADS'] = str(num_threads_per_process)
41 os.environ['MKL_DEBUG_CPU_TYPE'] = '5'
42
43 ray.init(num_cpus=num_processes, log_to_driver=True)
44
45 with tempfile.TemporaryDirectory() as tmpdirname:
46 tmpdir = pathlib.Path(tmpdirname)
47 img_ext = imghdr.what(args.image)
48 img_filepath = tmpdir / f'input.{img_ext}'
49 shutil.copy(str(args.image), img_filepath)
50
51 pipeline = superdsm.pipeline.create_default_pipeline()
52 cfg = superdsm.config.Config()
53 img = superdsm.io.imread(img_filepath)
54 data, cfg, _ = superdsm.automation.process_image(pipeline, cfg, img)
55
56 with open(args.cfg, 'w') as fp:
57 cfg.dump_json(fp)
58
59 overlay = superdsm.render.render_result_over_image(data, border_width=args.seg_border, normalize_img=False)
60 superdsm.io.imwrite(args.overlay, overlay)
61
62 masks = superdsm.render.rasterize_labels(data)
63 superdsm.io.imwrite(args.masks, masks)