Mercurial > repos > imgteam > segmetrics
annotate run-segmetrics.py @ 4:ef65ecafa3ef draft default tip
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
author | imgteam |
---|---|
date | Tue, 20 Jun 2023 21:40:19 +0000 |
parents | 48dcf95aebc7 |
children |
rev | line source |
---|---|
0
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
1 """ |
4
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
2 Copyright 2022-2023 Leonid Kostrykin, Biomedical Computer Vision Group, Heidelberg University. |
0
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
3 |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
4 Distributed under the MIT license. |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
6 |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
7 """ |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
8 |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
9 import argparse |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
10 import pathlib |
4
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
11 import subprocess |
0
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
12 import tempfile |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
13 import zipfile |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
14 |
4
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
15 import pandas as pd |
0
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
16 |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
17 |
4
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
18 def process_batch(seg_dir, seg_file, gt_file, tsv_output_file, recursive, gt_unique, seg_unique, measures): |
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
19 with tempfile.NamedTemporaryFile() as csv_output_file: |
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
20 cmd = ['python', '-m', 'segmetrics.cli', str(seg_dir), str(seg_file), str(gt_file), str(csv_output_file.name), '--semicolon'] |
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
21 if recursive: |
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
22 cmd.append('--recursive') |
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
23 if gt_unique: |
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
24 cmd.append('--gt-unique') |
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
25 if seg_unique: |
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
26 cmd.append('--seg-unique') |
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
27 cmd += measures |
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
28 subprocess.run(cmd, check=True) |
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
29 df = pd.read_csv(csv_output_file.name, sep=';') |
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
30 df.to_csv(str(tsv_output_file), sep='\t', index=False) |
0
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
31 |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
32 |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
33 if __name__ == "__main__": |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
34 parser = argparse.ArgumentParser(description='Image segmentation and object detection performance measures for 2-D image data') |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
35 parser.add_argument('input_seg', help='Path to the segmented image or image archive (ZIP)') |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
36 parser.add_argument('input_gt', help='Path to the ground truth image or image archive (ZIP)') |
4
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
37 parser.add_argument('results', help='Path to the results file (TSV)') |
0
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
38 parser.add_argument('-unzip', action='store_true') |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
39 parser.add_argument('-seg_unique', action='store_true') |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
40 parser.add_argument('-gt_unique', action='store_true') |
4
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
41 parser.add_argument('measures', nargs='+', type=str, help='list of performance measures') |
0
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
42 args = parser.parse_args() |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
43 |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
44 if args.unzip: |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
45 zipfile_seg = zipfile.ZipFile(args.input_seg) |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
46 zipfile_gt = zipfile.ZipFile(args.input_gt) |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
47 with tempfile.TemporaryDirectory() as tmpdir: |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
48 basepath = pathlib.Path(tmpdir) |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
49 gt_path, seg_path = basepath / 'gt', basepath / 'seg' |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
50 zipfile_seg.extractall(str(seg_path)) |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
51 zipfile_gt.extractall(str(gt_path)) |
4
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
52 process_batch(seg_dir=seg_path, seg_file=rf'^{seg_path}/(.+\.(?:png|PNG|tif|TIF|tiff|TIFF))$', gt_file=gt_path / r'\1', tsv_output_file=args.results, recursive=True, gt_unique=args.gt_unique, seg_unique=args.seg_unique, measures=args.measures) |
0
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
53 |
6cbb36364b82
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff
changeset
|
54 else: |
4
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
55 seg_path = pathlib.Path(args.input_seg) |
ef65ecafa3ef
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents:
3
diff
changeset
|
56 process_batch(seg_dir=seg_path.parent, seg_file=seg_path, gt_file=args.input_gt, tsv_output_file=args.results, recursive=False, gt_unique=args.gt_unique, seg_unique=args.seg_unique, measures=args.measures) |