annotate 2d_filter_segmentation_by_features.py @ 0:e576b73a2e2f draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
author imgteam
date Mon, 22 Jul 2019 05:00:03 -0400
parents
children 6ad1d3cfdea1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
1 import argparse
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
2 import sys
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
3 import skimage.io
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
4 import skimage.util
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
5 import pandas as pd
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
6
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
7 if __name__ == "__main__":
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
8 parser = argparse.ArgumentParser(description='Filter segmentation by features')
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
9 parser.add_argument('input_file', type=argparse.FileType('r'), default=sys.stdin, help='input file')
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
10 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file (TIFF)')
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
11 parser.add_argument('feature_file', type=argparse.FileType('r'), default=sys.stdin, help='feature file (cols: label, f1, f2)')
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
12 parser.add_argument('rule_file', type=argparse.FileType('r'), default=sys.stdin, help='file with rules per feature (cols: ,f1,2, rows: feature_name, min, max)')
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
13 args = parser.parse_args()
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
14
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
15 img_in = skimage.io.imread(args.input_file.name)
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
16 features = pd.read_csv(args.feature_file, delimiter="\t")
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
17 rules = pd.read_csv(args.rule_file, delimiter="\t")
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
18
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
19 cols = [a for a in rules.columns if not 'Unnamed' in a]
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
20 for a_c in cols:
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
21 a_min = rules[rules.ix[:, 0] == 'min'][a_c]
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
22 a_max = rules[rules.ix[:, 0] == 'max'][a_c]
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
23 for a_l in features.label:
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
24 a_val = float(features[features['label'] == a_l][a_c])
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
25 if a_val < float(a_min) or a_val > float(a_max):
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
26 img_in[img_in == int(a_l)] = 0
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
27
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
28 res = skimage.util.img_as_uint(img_in)
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
29 skimage.io.imsave(args.out_file.name, res, plugin="tifffile")