comparison detection_viz.py @ 4:99433164b593 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/detection_viz/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
author imgteam
date Mon, 13 Nov 2023 22:11:04 +0000
parents cb2d9de888a1
children
comparison
equal deleted inserted replaced
3:cb2d9de888a1 4:99433164b593
1 import argparse 1 import argparse
2 import sys
3 import os
4 import csv 2 import csv
5 3
6 import matplotlib 4 import matplotlib
5 import matplotlib.pyplot as plt
6 import skimage.io
7
8
7 matplotlib.use('Agg') 9 matplotlib.use('Agg')
8 import matplotlib.pyplot as plt
9 10
10 import skimage.io
11 11
12 def plot_circles(file_name, ax, color, stroke_size, radius): 12 def plot_circles(file_name, ax, color, stroke_size, radius):
13 resfile = open(file_name, 'rb') 13 resfile = open(file_name, 'rb')
14 rd = csv.reader(resfile, delimiter=',') 14 rd = csv.reader(resfile, delimiter=',')
15 for row in rd: 15 for row in rd:
16 circ = plt.Circle((int(row[1]), int(row[0])), lw=stroke_size, radius=radius, color=color, fill=False) 16 circ = plt.Circle((int(row[1]), int(row[0])), lw=stroke_size, radius=radius, color=color, fill=False)
17 ax.add_patch(circ) 17 ax.add_patch(circ)
18 resfile.close() 18 resfile.close()
19 19
20
20 def detection_viz(input_file, output_file, tp=None, fn=None, fp=None, stroke_size=3, circle_radius=50): 21 def detection_viz(input_file, output_file, tp=None, fn=None, fp=None, stroke_size=3, circle_radius=50):
21 img = skimage.io.imread(input_file) 22 img = skimage.io.imread(input_file)
22 23
23 fig = plt.figure(figsize=(40, 40)) 24 fig = plt.figure(figsize=(40, 40))
24 ax = fig.add_axes([0, 0, 1, 1]) 25 ax = fig.add_axes([0, 0, 1, 1])
25 ax.axis('off') 26 ax.axis('off')
26 27
27 plt.imshow(img) 28 plt.imshow(img)
28 if tp is not None: 29 if tp is not None:
29 plot_circles(tp, ax, '#00FF00', stroke_size, circle_radius) 30 plot_circles(tp, ax, '#00FF00', stroke_size, circle_radius)
32 if fp is not None: 33 if fp is not None:
33 plot_circles(fp, ax, 'darkorange', stroke_size, circle_radius) 34 plot_circles(fp, ax, 'darkorange', stroke_size, circle_radius)
34 35
35 fig.canvas.print_png(output_file, dpi=1800) 36 fig.canvas.print_png(output_file, dpi=1800)
36 37
38
37 if __name__ == "__main__": 39 if __name__ == "__main__":
38 parser = argparse.ArgumentParser() 40 parser = argparse.ArgumentParser()
39 parser.add_argument('input_file', type=argparse.FileType('r'), help='original file') 41 parser.add_argument('input_file', type=argparse.FileType('r'), help='original file')
40 # output file should not be of type argparse.FileType('w') sine it is created immediately in this case which leads to an error in renaming 42 # output file should not be of type argparse.FileType('w') sine it is created immediately in this case which leads to an error in renaming
41 parser.add_argument('out_file_str', type=str, help='string of output file name') 43 parser.add_argument('out_file_str', type=str, help='string of output file name')
42 parser.add_argument('--tp', dest='input_tp_file', type=argparse.FileType('r'), help='input TP file') 44 parser.add_argument('--tp', dest='input_tp_file', type=argparse.FileType('r'), help='input TP file')
43 parser.add_argument('--fn', dest='input_fn_file', type=argparse.FileType('r'), help='input FN file') 45 parser.add_argument('--fn', dest='input_fn_file', type=argparse.FileType('r'), help='input FN file')
44 parser.add_argument('--fp', dest='input_fp_file', type=argparse.FileType('r'), help='input FP file') 46 parser.add_argument('--fp', dest='input_fp_file', type=argparse.FileType('r'), help='input FP file')
45 parser.add_argument('--stroke_size', dest='thickness', default=3, type=float, help='stroke thickness') 47 parser.add_argument('--stroke_size', dest='thickness', default=3, type=float, help='stroke thickness')
46 parser.add_argument('--circle_radius', dest='circle_radius', type=float, default=50, help='circle radius') 48 parser.add_argument('--circle_radius', dest='circle_radius', type=float, default=50, help='circle radius')
47 args = parser.parse_args() 49 args = parser.parse_args()
48 50
49 tp=None 51 tp = None
50 if args.input_tp_file: 52 if args.input_tp_file:
51 tp=args.input_tp_file.name 53 tp = args.input_tp_file.name
52 fn=None 54 fn = None
53 if args.input_fn_file: 55 if args.input_fn_file:
54 fn=args.input_fn_file.name 56 fn = args.input_fn_file.name
55 fp=None 57 fp = None
56 if args.input_fp_file: 58 if args.input_fp_file:
57 fp=args.input_fp_file.name 59 fp = args.input_fp_file.name
58 60
59 detection_viz(args.input_file.name, args.out_file_str, tp=tp, fn=fn, fp=fp, stroke_size=args.thickness, circle_radius=args.circle_radius) 61 detection_viz(args.input_file.name, args.out_file_str, tp=tp, fn=fn, fp=fp, stroke_size=args.thickness, circle_radius=args.circle_radius)