Mercurial > repos > imgteam > superdsm
comparison run-superdsm.py @ 3:7fd8dba15bd3 draft
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/superdsm/ commit fea6c8161c4b3e6394fe035b12b69b73e6fa7d75
| author | imgteam |
|---|---|
| date | Thu, 16 Nov 2023 12:29:41 +0000 |
| parents | 244f67290d28 |
| children | dc5f72f6b1e9 |
comparison
equal
deleted
inserted
replaced
| 2:244f67290d28 | 3:7fd8dba15bd3 |
|---|---|
| 5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | 5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
| 6 | 6 |
| 7 """ | 7 """ |
| 8 | 8 |
| 9 import argparse | 9 import argparse |
| 10 import csv | |
| 10 import imghdr | 11 import imghdr |
| 11 import os | 12 import os |
| 12 import pathlib | 13 import pathlib |
| 13 import shutil | 14 import shutil |
| 14 import tempfile | 15 import tempfile |
| 43 if value is not None: | 44 if value is not None: |
| 44 cfg[key] = value | 45 cfg[key] = value |
| 45 return cfg | 46 return cfg |
| 46 | 47 |
| 47 | 48 |
| 49 def flatten_dict(d, sep='/'): | |
| 50 result = {} | |
| 51 for key, val in d.items(): | |
| 52 if isinstance(val, dict): | |
| 53 for sub_key, sub_val in flatten_dict(val, sep=sep).items(): | |
| 54 result[f'{key}{sep}{sub_key}'] = sub_val | |
| 55 else: | |
| 56 result[key] = val | |
| 57 return result | |
| 58 | |
| 59 | |
| 48 if __name__ == "__main__": | 60 if __name__ == "__main__": |
| 49 parser = argparse.ArgumentParser(description='Segmentation of cell nuclei in 2-D fluorescence microscopy images') | 61 parser = argparse.ArgumentParser(description='Segmentation of cell nuclei in 2-D fluorescence microscopy images') |
| 50 parser.add_argument('image', help='Path to the input image') | 62 parser.add_argument('image', type=str, help='Path to the input image') |
| 51 parser.add_argument('cfg', help='Path to the file containing the configuration') | |
| 52 parser.add_argument('masks', help='Path to the file containing the segmentation masks') | |
| 53 parser.add_argument('overlay', help='Path to the file containing the overlay of the segmentation results') | |
| 54 parser.add_argument('seg_border', type=int) | |
| 55 parser.add_argument('slots', type=int) | 63 parser.add_argument('slots', type=int) |
| 64 parser.add_argument('--do-masks', type=str, default=None, help='Path to the file containing the segmentation masks') | |
| 65 parser.add_argument('--do-cfg', type=str, default=None, help='Path to the file containing the configuration') | |
| 66 parser.add_argument('--do-overlay', type=str, default=None, help='Path to the file containing the overlay of the segmentation results') | |
| 67 parser.add_argument('--do-overlay-border', type=int) | |
| 56 for key, ptype in hyperparameters: | 68 for key, ptype in hyperparameters: |
| 57 parser.add_argument('--' + get_param_name(key), type=ptype, default=None) | 69 parser.add_argument('--' + get_param_name(key), type=ptype, default=None) |
| 58 args = parser.parse_args() | 70 args = parser.parse_args() |
| 59 | 71 |
| 60 if args.slots >= 2: | 72 if args.slots >= 2: |
| 81 shutil.copy(str(args.image), img_filepath) | 93 shutil.copy(str(args.image), img_filepath) |
| 82 | 94 |
| 83 pipeline = superdsm.pipeline.create_default_pipeline() | 95 pipeline = superdsm.pipeline.create_default_pipeline() |
| 84 cfg = create_config(args) | 96 cfg = create_config(args) |
| 85 img = superdsm.io.imread(img_filepath) | 97 img = superdsm.io.imread(img_filepath) |
| 86 data, cfg, _ = superdsm.automation.process_image(pipeline, cfg, img) | |
| 87 | 98 |
| 88 with open(args.cfg, 'w') as fp: | 99 if args.do_cfg: |
| 89 cfg.dump_json(fp) | 100 print(f'Writing config to: {args.do_cfg}') |
| 101 cfg, _ = superdsm.automation.create_config(pipeline, cfg, img) | |
| 102 with open(args.do_cfg, 'w') as fp: | |
| 103 tsv_out = csv.writer(fp, delimiter='\t') | |
| 104 tsv_out.writerow(['Hyperparameter', 'Value']) | |
| 105 for key, value in flatten_dict(cfg.entries).items(): | |
| 106 tsv_out.writerow([key, value]) | |
| 90 | 107 |
| 91 overlay = superdsm.render.render_result_over_image(data, border_width=args.seg_border, normalize_img=False) | 108 if args.do_overlay or args.do_masks: |
| 92 superdsm.io.imwrite(args.overlay, overlay) | 109 print('Performing segmentation') |
| 110 data, cfg, _ = pipeline.process_image(img, cfg) | |
| 93 | 111 |
| 94 masks = superdsm.render.rasterize_labels(data) | 112 if args.do_overlay: |
| 95 superdsm.io.imwrite(args.masks, masks) | 113 print(f'Writing overlay to: {args.do_overlay}') |
| 114 overlay = superdsm.render.render_result_over_image(data, border_width=args.do_overlay_border, normalize_img=False) | |
| 115 superdsm.io.imwrite(args.do_overlay, overlay) | |
| 116 | |
| 117 if args.do_masks: | |
| 118 print(f'Writing masks to: {args.do_masks}') | |
| 119 masks = superdsm.render.rasterize_labels(data) | |
| 120 superdsm.io.imwrite(args.do_masks, masks) |
