annotate 2d_filter_segmentation_by_features.py @ 3:9d47aabda459 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_filter_segmentation_by_features/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
author imgteam
date Wed, 24 Apr 2024 08:11:47 +0000
parents 6ad1d3cfdea1
children
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
1
6ad1d3cfdea1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_filter_segmentation_by_features/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
3
3
9d47aabda459 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_filter_segmentation_by_features/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 1
diff changeset
4 import giatools.io
1
6ad1d3cfdea1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_filter_segmentation_by_features/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
5 import pandas as pd
6ad1d3cfdea1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_filter_segmentation_by_features/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
6 import skimage.io
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
7 import skimage.util
1
6ad1d3cfdea1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_filter_segmentation_by_features/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
8
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
9
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 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
11 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
12 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
13 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
14 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
15 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
16 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
17
3
9d47aabda459 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_filter_segmentation_by_features/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 1
diff changeset
18 img_in = giatools.io.imread(args.input_file.name)
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
19 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
20 rules = pd.read_csv(args.rule_file, delimiter="\t")
1
6ad1d3cfdea1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_filter_segmentation_by_features/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
21
6ad1d3cfdea1 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_filter_segmentation_by_features/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
22 cols = [a for a in rules.columns if 'Unnamed' not in a]
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
23 for a_c in cols:
3
9d47aabda459 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_filter_segmentation_by_features/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 1
diff changeset
24 a_min = rules[rules.iloc[:, 0] == 'min'][a_c]
9d47aabda459 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_filter_segmentation_by_features/ commit c86a1b93cb7732f7331a981d13465653cc1a2790
imgteam
parents: 1
diff changeset
25 a_max = rules[rules.iloc[:, 0] == 'max'][a_c]
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
26 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
27 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
28 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
29 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
30
e576b73a2e2f planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/2d_filter_segmentation_by_features/ commit b2acc1845a25828181597fe5b6982fe116a7796d
imgteam
parents:
diff changeset
31 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
32 skimage.io.imsave(args.out_file.name, res, plugin="tifffile")