comparison 2d_feature_extraction.py @ 4:0a53256b48c6 draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_feature_extraction/ commit 6b687746bdb3d5d1fb11ecffd6dd1bf42dc2c38d
author imgteam
date Fri, 10 Nov 2023 14:23:12 +0000
parents 5791a7f65275
children 2436a8807ad1
comparison
equal deleted inserted replaced
3:5791a7f65275 4:0a53256b48c6
1 import argparse 1 import argparse
2
2 import numpy as np 3 import numpy as np
3 import pandas as pd 4 import pandas as pd
5 import skimage.feature
4 import skimage.io 6 import skimage.io
5 import skimage.measure 7 import skimage.measure
6 import skimage.feature 8 import skimage.morphology
7 import skimage.segmentation 9 import skimage.segmentation
8 import skimage.morphology
9 10
10 #TODO make importable by python script 11 # TODO make importable by python script
11 12
12 parser = argparse.ArgumentParser(description='Extract Features 2D') 13 parser = argparse.ArgumentParser(description='Extract Features 2D')
13 14
14 #TODO create factory for boilerplate code 15 # TODO create factory for boilerplate code
15 features = parser.add_argument_group('compute features') 16 features = parser.add_argument_group('compute features')
16 features.add_argument('--all', dest='all_features', action='store_true') 17 features.add_argument('--all', dest='all_features', action='store_true')
17 features.add_argument('--label', dest='add_label', action='store_true') 18 features.add_argument('--label', dest='add_label', action='store_true')
18 features.add_argument('--patches', dest='add_roi_patches', action='store_true') 19 features.add_argument('--patches', dest='add_roi_patches', action='store_true')
19 features.add_argument('--max_intensity', dest='max_intensity', action='store_true') 20 features.add_argument('--max_intensity', dest='max_intensity', action='store_true')
20 features.add_argument('--mean_intensity', dest='mean_intensity', action='store_true') 21 features.add_argument('--mean_intensity', dest='mean_intensity', action='store_true')
21 features.add_argument('--min_intensity', dest='min_intensity', action='store_true') 22 features.add_argument('--min_intensity', dest='min_intensity', action='store_true')
22 features.add_argument('--moments_hu', dest='moments_hu', action='store_true') 23 features.add_argument('--moments_hu', dest='moments_hu', action='store_true')
23 features.add_argument('--centroid', dest='centroid', action='store_true') 24 features.add_argument('--centroid', dest='centroid', action='store_true')
24 features.add_argument('--bbox', dest='bbox', action='store_true') 25 features.add_argument('--bbox', dest='bbox', action='store_true')
25 features.add_argument('--area', dest='area', action='store_true') 26 features.add_argument('--area', dest='area', action='store_true')
26 features.add_argument('--filled_area', dest='filled_area', action='store_true') 27 features.add_argument('--filled_area', dest='filled_area', action='store_true')
27 features.add_argument('--convex_area', dest='convex_area', action='store_true') 28 features.add_argument('--convex_area', dest='convex_area', action='store_true')
39 features.add_argument('--convexity', dest='convexity', action='store_true') 40 features.add_argument('--convexity', dest='convexity', action='store_true')
40 41
41 parser.add_argument('--label_file_binary', dest='label_file_binary', action='store_true') 42 parser.add_argument('--label_file_binary', dest='label_file_binary', action='store_true')
42 43
43 parser.add_argument('--raw', dest='raw_file', type=argparse.FileType('r'), 44 parser.add_argument('--raw', dest='raw_file', type=argparse.FileType('r'),
44 help='Original input file', required=False) 45 help='Original input file', required=False)
45 parser.add_argument('label_file', type=argparse.FileType('r'), 46 parser.add_argument('label_file', type=argparse.FileType('r'),
46 help='Label input file') 47 help='Label input file')
47 parser.add_argument('output_file', type=argparse.FileType('w'), 48 parser.add_argument('output_file', type=argparse.FileType('w'),
48 help='Tabular output file') 49 help='Tabular output file')
49 args = parser.parse_args() 50 args = parser.parse_args()
50 51
51 label_file_binary = args.label_file_binary 52 label_file_binary = args.label_file_binary
52 label_file = args.label_file.name 53 label_file = args.label_file.name
53 out_file = args.output_file.name 54 out_file = args.output_file.name
54 add_patch = args.add_roi_patches 55 add_patch = args.add_roi_patches
55 56
68 69
69 if add_patch: 70 if add_patch:
70 df['image'] = df['it'].map(lambda ait: regions[ait].image.astype(np.float).tolist()) 71 df['image'] = df['it'].map(lambda ait: regions[ait].image.astype(np.float).tolist())
71 df['intensity_image'] = df['it'].map(lambda ait: regions[ait].intensity_image.astype(np.float).tolist()) 72 df['intensity_image'] = df['it'].map(lambda ait: regions[ait].intensity_image.astype(np.float).tolist())
72 73
73 #TODO no matrix features, but split in own rows? 74 # TODO no matrix features, but split in own rows?
74 if args.add_label or args.all_features: 75 if args.add_label or args.all_features:
75 df['label'] = df['it'].map(lambda ait: regions[ait].label) 76 df['label'] = df['it'].map(lambda ait: regions[ait].label)
76 77
77 if raw_image is not None: 78 if raw_image is not None:
78 if args.max_intensity or args.all_features: 79 if args.max_intensity or args.all_features:
117 if args.moments or args.all_features: 118 if args.moments or args.all_features:
118 df['moments'] = df['it'].map(lambda ait: regions[ait].moments) 119 df['moments'] = df['it'].map(lambda ait: regions[ait].moments)
119 if args.convexity or args.all_features: 120 if args.convexity or args.all_features:
120 perimeter = df['it'].map(lambda ait: regions[ait].perimeter) 121 perimeter = df['it'].map(lambda ait: regions[ait].perimeter)
121 area = df['it'].map(lambda ait: regions[ait].area) 122 area = df['it'].map(lambda ait: regions[ait].area)
122 df['convexity'] = area/(perimeter*perimeter) 123 df['convexity'] = area / (perimeter * perimeter)
123 124
124 del df['it'] 125 del df['it']
125 df.to_csv(out_file, sep='\t', line_terminator='\n', index=False) 126 df.to_csv(out_file, sep='\t', line_terminator='\n', index=False)