comparison 2d_feature_extraction.py @ 0:96909b9d1df1 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_feature_extraction/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
author imgteam
date Sat, 09 Feb 2019 14:28:26 -0500
parents
children f03b4da203d0
comparison
equal deleted inserted replaced
-1:000000000000 0:96909b9d1df1
1 import argparse
2 import numpy as np
3 import pandas as pd
4 import tifffile
5 import skimage.io
6 import skimage.measure
7 import skimage.feature
8 import skimage.segmentation
9 import skimage.morphology
10
11 #TODO make importable by python script
12
13 parser = argparse.ArgumentParser(description='Extract Features 2D')
14
15 #TODO create factory for boilerplate code
16 features = parser.add_argument_group('compute features')
17 features.add_argument('--all', dest='all_features', action='store_true')
18 features.add_argument('--label', dest='add_label', action='store_true')
19 features.add_argument('--patches', dest='add_roi_patches', action='store_true')
20 features.add_argument('--max_intensity', dest='max_intensity', action='store_true')
21 features.add_argument('--mean_intensity', dest='mean_intensity', action='store_true')
22 features.add_argument('--min_intensity', dest='min_intensity', action='store_true')
23 features.add_argument('--moments_hu', dest='moments_hu', action='store_true')
24 features.add_argument('--centroid', dest='centroid', action='store_true')
25 features.add_argument('--bbox', dest='bbox', action='store_true')
26 features.add_argument('--area', dest='area', action='store_true')
27 features.add_argument('--filled_area', dest='filled_area', action='store_true')
28 features.add_argument('--convex_area', dest='convex_area', action='store_true')
29 features.add_argument('--perimeter', dest='perimeter', action='store_true')
30 features.add_argument('--extent', dest='extent', action='store_true')
31 features.add_argument('--eccentricity', dest='eccentricity', action='store_true')
32 features.add_argument('--equivalent_diameter', dest='equivalent_diameter', action='store_true')
33 features.add_argument('--euler_number', dest='euler_number', action='store_true')
34 features.add_argument('--inertia_tensor_eigvals', dest='inertia_tensor_eigvals', action='store_true')
35 features.add_argument('--major_axis_length', dest='major_axis_length', action='store_true')
36 features.add_argument('--minor_axis_length', dest='minor_axis_length', action='store_true')
37 features.add_argument('--orientation', dest='orientation', action='store_true')
38 features.add_argument('--solidity', dest='solidity', action='store_true')
39 features.add_argument('--moments', dest='moments', action='store_true')
40 features.add_argument('--convexity', dest='convexity', action='store_true')
41
42 parser.add_argument('--label_file_binary', dest='label_file_binary', action='store_true')
43
44 parser.add_argument('--raw', dest='raw_file', type=argparse.FileType('r'),
45 help='Original input file', required=False)
46 parser.add_argument('label_file', type=argparse.FileType('r'),
47 help='Label input file')
48 parser.add_argument('output_file', type=argparse.FileType('w'),
49 help='Tabular output file')
50 args = parser.parse_args()
51
52 label_file_binary = args.label_file_binary
53 label_file = args.label_file.name
54 out_file = args.output_file.name
55 add_patch = args.add_roi_patches
56
57 raw_image = None
58 if args.raw_file is not None:
59 raw_image = skimage.io.imread(args.raw_file.name)
60
61 raw_label_image = skimage.io.imread(label_file)
62
63 df = pd.DataFrame()
64 if label_file_binary:
65 raw_label_image = skimage.measure.label(raw_label_image)
66 regions = skimage.measure.regionprops(raw_label_image, intensity_image=raw_image)
67
68 df['it'] = np.arange(len(regions))
69
70 if add_patch:
71 df['image'] = df['it'].map(lambda ait: regions[ait].image.astype(np.float).tolist())
72 df['intensity_image'] = df['it'].map(lambda ait: regions[ait].intensity_image.astype(np.float).tolist())
73
74 #TODO no matrix features, but split in own rows?
75 if args.add_label or args.all_features:
76 df['label'] = df['it'].map(lambda ait: regions[ait].label)
77
78 if raw_image is not None:
79 if args.max_intensity or args.all_features:
80 df['max_intensity'] = df['it'].map(lambda ait: regions[ait].max_intensity)
81 if args.mean_intensity or args.all_features:
82 df['mean_intensity'] = df['it'].map(lambda ait: regions[ait].mean_intensity)
83 if args.min_intensity or args.all_features:
84 df['min_intensity'] = df['it'].map(lambda ait: regions[ait].min_intensity)
85 if args.moments_hu or args.all_features:
86 df['moments_hu'] = df['it'].map(lambda ait: regions[ait].moments_hu)
87
88 if args.centroid or args.all_features:
89 df['centroid'] = df['it'].map(lambda ait: regions[ait].centroid)
90 if args.bbox or args.all_features:
91 df['bbox'] = df['it'].map(lambda ait: regions[ait].bbox)
92 if args.area or args.all_features:
93 df['area'] = df['it'].map(lambda ait: regions[ait].area)
94 if args.filled_area or args.all_features:
95 df['filled_area'] = df['it'].map(lambda ait: regions[ait].filled_area)
96 if args.convex_area or args.all_features:
97 df['convex_area'] = df['it'].map(lambda ait: regions[ait].convex_area)
98 if args.perimeter or args.all_features:
99 df['perimeter'] = df['it'].map(lambda ait: regions[ait].perimeter)
100 if args.extent or args.all_features:
101 df['extent'] = df['it'].map(lambda ait: regions[ait].extent)
102 if args.eccentricity or args.all_features:
103 df['eccentricity'] = df['it'].map(lambda ait: regions[ait].eccentricity)
104 if args.equivalent_diameter or args.all_features:
105 df['equivalent_diameter'] = df['it'].map(lambda ait: regions[ait].equivalent_diameter)
106 if args.euler_number or args.all_features:
107 df['euler_number'] = df['it'].map(lambda ait: regions[ait].euler_number)
108 if args.inertia_tensor_eigvals or args.all_features:
109 df['inertia_tensor_eigvals'] = df['it'].map(lambda ait: regions[ait].inertia_tensor_eigvals)
110 if args.major_axis_length or args.all_features:
111 df['major_axis_length'] = df['it'].map(lambda ait: regions[ait].major_axis_length)
112 if args.minor_axis_length or args.all_features:
113 df['minor_axis_length'] = df['it'].map(lambda ait: regions[ait].minor_axis_length)
114 if args.orientation or args.all_features:
115 df['orientation'] = df['it'].map(lambda ait: regions[ait].orientation)
116 if args.solidity or args.all_features:
117 df['solidity'] = df['it'].map(lambda ait: regions[ait].solidity)
118 if args.moments or args.all_features:
119 df['moments'] = df['it'].map(lambda ait: regions[ait].moments)
120 if args.convexity or args.all_features:
121 df['convexity'] = df.area/(df.perimeter*df.perimeter)
122
123 del df['it']
124 df.to_csv(out_file, sep='\t', line_terminator='\n', index=False)