annotate run-segmetrics.py @ 4:7989264b5780 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
author imgteam
date Tue, 20 Jun 2023 21:40:31 +0000
parents c496306c1cba
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
1 """
4
7989264b5780 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
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
3
0729657d9e4e 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.
0729657d9e4e 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
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
6
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
7 """
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
8
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
9 import argparse
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
10 import pathlib
4
7989264b5780 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents: 3
diff changeset
11 import subprocess
0
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
12 import tempfile
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
13 import zipfile
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
14
4
7989264b5780 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
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
16
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
17
4
7989264b5780 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):
7989264b5780 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:
7989264b5780 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']
7989264b5780 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents: 3
diff changeset
21 if recursive:
7989264b5780 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents: 3
diff changeset
22 cmd.append('--recursive')
7989264b5780 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents: 3
diff changeset
23 if gt_unique:
7989264b5780 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')
7989264b5780 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents: 3
diff changeset
25 if seg_unique:
7989264b5780 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')
7989264b5780 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 075271cee9cb9c2625c04dbefd903cdea6e74724
imgteam
parents: 3
diff changeset
27 cmd += measures
7989264b5780 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)
7989264b5780 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=';')
7989264b5780 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
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
31
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
32
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
33 if __name__ == "__main__":
0729657d9e4e 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')
0729657d9e4e 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)')
0729657d9e4e 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
7989264b5780 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
0729657d9e4e 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')
0729657d9e4e 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')
0729657d9e4e 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
7989264b5780 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
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
42 args = parser.parse_args()
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
43
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
44 if args.unzip:
0729657d9e4e 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)
0729657d9e4e 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)
0729657d9e4e 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:
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
48 basepath = pathlib.Path(tmpdir)
0729657d9e4e 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'
0729657d9e4e 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))
0729657d9e4e 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
7989264b5780 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
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
53
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
54 else:
4
7989264b5780 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)
7989264b5780 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)