annotate run-segmetrics.py @ 3:c496306c1cba draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit baee438adb94f6e8b6c3e45e8413c36f9d1dadee
author imgteam
date Sat, 08 Oct 2022 21:54:40 +0000
parents c90b52773d2e
children 7989264b5780
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 """
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
2 Copyright 2022 Leonid Kostrykin, Biomedical Computer Vision Group, Heidelberg University.
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 csv
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
11 import itertools
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
12 import pathlib
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
13 import tempfile
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
14 import zipfile
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
15
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
16 import numpy as np
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
17 import segmetrics as sm
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
18 import skimage.io
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
19
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
20
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
21 measures = [
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
22 ('dice', 'Dice', sm.regional.Dice()),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
23 ('seg', 'SEG', sm.regional.ISBIScore()),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
24 ('jc', 'Jaccard coefficient', sm.regional.JaccardSimilarityIndex()),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
25 ('ji', 'Jaccard index', sm.regional.JaccardIndex()),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
26 ('ri', 'Rand index', sm.regional.RandIndex()),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
27 ('ari', 'Adjusted Rand index', sm.regional.AdjustedRandIndex()),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
28 ('hsd_sym', 'HSD (sym)', sm.boundary.Hausdorff('sym')),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
29 ('hsd_e2a', 'HSD (e2a)', sm.boundary.Hausdorff('e2a')),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
30 ('hsd_a2e', 'HSD (a2e)', sm.boundary.Hausdorff('a2e')),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
31 ('nsd', 'NSD', sm.boundary.NSD()),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
32 ('o_hsd_sym', 'Ob. HSD (sym)', sm.boundary.ObjectBasedDistance(sm.boundary.Hausdorff('sym'))),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
33 ('o_hsd_e2a', 'Ob. HSD (e2a)', sm.boundary.ObjectBasedDistance(sm.boundary.Hausdorff('e2a'))),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
34 ('o_hsd_a2e', 'Ob. HSD (a2e)', sm.boundary.ObjectBasedDistance(sm.boundary.Hausdorff('a2e'))),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
35 ('o_nsd', 'Ob. NSD', sm.boundary.ObjectBasedDistance(sm.boundary.NSD())),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
36 ('fs', 'Split', sm.detection.FalseSplit()),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
37 ('fm', 'Merge', sm.detection.FalseMerge()),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
38 ('fp', 'Spurious', sm.detection.FalsePositive()),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
39 ('fn', 'Missing', sm.detection.FalseNegative()),
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
40 ]
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
41
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
42
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
43 def process_batch(study, gt_filelist, seg_filelist, namelist, gt_is_unique, seg_is_unique):
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
44 for gt_filename, seg_filename, name in zip(gt_filelist, seg_filelist, namelist):
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
45 img_ref = skimage.io.imread(gt_filename)
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
46 img_seg = skimage.io.imread(seg_filename)
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
47 study.set_expected(img_ref, unique=gt_is_unique)
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
48 study.process(img_seg, unique=seg_is_unique, chunk_id=name)
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
49
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
50
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
51 def aggregate(measure, values):
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
52 fnc = np.sum if measure.ACCUMULATIVE else np.mean
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
53 return fnc(values)
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
54
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
55
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
56 def is_zip_filepath(filepath):
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
57 return filepath.lower().endswith('.zip')
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
58
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
59
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
60 def is_image_filepath(filepath):
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
61 suffixes = ['png', 'tif', 'tiff']
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
62 return any((filepath.lower().endswith(f'.{suffix}') for suffix in suffixes))
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
63
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
64
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
65 if __name__ == "__main__":
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
66 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
67 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
68 parser.add_argument('input_gt', help='Path to the ground truth 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
69 parser.add_argument('results', help='Path to the results file (CSV)')
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
70 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
71 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
72 parser.add_argument('-gt_unique', action='store_true')
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
73 for measure in measures:
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
74 parser.add_argument(f'-measure-{measure[0]}', action='store_true', help=f'Include {measure[1]}')
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
75
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
76 args = parser.parse_args()
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
77 study = sm.study.Study()
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
78
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
79 used_measures = []
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
80 for measure in measures:
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
81 if getattr(args, f'measure_{measure[0]}'):
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
82 used_measures.append(measure)
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
83 study.add_measure(measure[2], measure[1])
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
84
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
85 if args.unzip:
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
86 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
87 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
88 namelist = [filepath for filepath in zipfile_seg.namelist() if is_image_filepath(filepath) and filepath in zipfile_gt.namelist()]
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
89 print('namelist:', namelist)
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
90 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
91 basepath = pathlib.Path(tmpdir)
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
92 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
93 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
94 zipfile_gt.extractall(str(gt_path))
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
95 gt_filelist, seg_filelist = list(), list()
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
96 for filepath in namelist:
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
97 seg_filelist.append(str(seg_path / filepath))
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
98 gt_filelist.append(str(gt_path / filepath))
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
99 process_batch(study, gt_filelist, seg_filelist, namelist, args.gt_unique, args.seg_unique)
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
100
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
101 else:
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
102 namelist = ['']
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
103 process_batch(study, [args.input_gt], [args.input_seg], namelist, args.gt_unique, args.seg_unique)
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
104
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
105 # define header
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
106 rows = [[''] + [measure[1] for measure in used_measures]]
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
107
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
108 # define rows
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
109 if len(namelist) > 1:
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
110 for chunk_id in namelist:
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
111 row = [chunk_id]
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
112 for measure in used_measures:
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
113 measure_name = measure[1]
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
114 measure = study.measures[measure_name]
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
115 chunks = study.results[measure_name]
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
116 row += [aggregate(measure, chunks[chunk_id])]
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
117 rows.append(row)
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
118
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
119 # define footer
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
120 rows.append([''])
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
121 for measure in used_measures:
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
122 measure_name = measure[1]
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
123 measure = study.measures[measure_name]
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
124 chunks = study.results[measure_name]
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
125 values = list(itertools.chain(*[chunks[chunk_id] for chunk_id in chunks]))
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
126 val = aggregate(measure, values)
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
127 rows[-1].append(val)
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
128
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
129 # write results
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
130 with open(args.results, 'w', newline='') as fout:
3
c496306c1cba planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit baee438adb94f6e8b6c3e45e8413c36f9d1dadee
imgteam
parents: 2
diff changeset
131 csv_writer = csv.writer(fout, delimiter='\t', quotechar='"', quoting=csv.QUOTE_MINIMAL)
0
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
132 for row in rows:
0729657d9e4e planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/segmetrics/ commit 3b911df716a7b42115c6cd773f666bc90a2bb10f
imgteam
parents:
diff changeset
133 csv_writer.writerow(row)