annotate points2label.py @ 0:14525b8c8d67 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
author thomaswollmann
date Wed, 12 Dec 2018 05:12:06 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
1 import argparse
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
2 import sys
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
3 import numpy as np
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
4 import skimage.io
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
5 import pandas as pd
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
6 import warnings
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
7
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
8 def points2label(labels, shape, output_file=None, has_header=False, is_TSV=False):
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
9 labelimg = np.zeros([shape[0], shape[1]], dtype=np.int32)
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
10
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
11 if is_TSV:
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
12 if has_header:
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
13 df = pd.read_csv(labels, sep='\t', skiprows=1, header=None)
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
14 else:
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
15 df = pd.read_csv(labels, sep='\t', header=None)
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
16 else:
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
17 if has_header:
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
18 df = pd.read_csv(labels, skiprows=1, header=None)
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
19 else:
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
20 df = pd.read_csv(labels, header=None)
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
21
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
22 for i in range(0, len(df)):
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
23 a_row = df.iloc[i]
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
24 labelimg[a_row[0], a_row[1]] = i+1
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
25
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
26 if output_file is not None:
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
27 with warnings.catch_warnings():
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
28 warnings.simplefilter("ignore")
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
29 skimage.io.imsave(output_file, labelimg, plugin='tifffile')
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
30 else:
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
31 return labelimg
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
32
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
33 if __name__ == "__main__":
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
34 parser = argparse.ArgumentParser()
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
35 parser.add_argument('label_file', type=argparse.FileType('r'), default=sys.stdin, help='label file')
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
36 parser.add_argument('out_file', type=argparse.FileType('w'), default=sys.stdin, help='out file')
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
37 parser.add_argument('org_file', type=argparse.FileType('r'), default=sys.stdin, help='input original file')
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
38 parser.add_argument('--has_header', dest='has_header', type=bool, default=False, help='label file has header')
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
39 parser.add_argument('--is_tsv', dest='is_tsv', type=bool, default=False, help='label file is TSV')
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
40 args = parser.parse_args()
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
41
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
42 original_shape = skimage.io.imread(args.org_file.name, plugin='tifffile').shape
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
43
14525b8c8d67 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit 356f2556bd629e5473d6711ec71372ea17d3e28f
thomaswollmann
parents:
diff changeset
44 points2label(args.label_file.name, original_shape, args.out_file.name, args.has_header, args.is_tsv)