annotate points2binaryimage.py @ 0:74b946cdba21 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
author thomaswollmann
date Wed, 12 Dec 2018 04:51:33 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
1 import argparse
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
2 import sys
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
3 import numpy as np
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
4 import skimage.io
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
5 import pandas as pd
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
6 import os
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
7 import warnings
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
8
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
9
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
10 def points2binaryimage(point_file, out_file, shape=[500, 500], has_header=False, invert_xy=False):
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
11
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
12 img = np.zeros(shape, dtype=np.int16)
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
13 if os.path.exists(point_file) and os.path.getsize(point_file) > 0:
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
14 if has_header:
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
15 df = pd.read_csv(point_file, skiprows=1, header=None)
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
16 else:
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
17 df = pd.read_csv(point_file, header=None)
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
18
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
19 for i in range(0, len(df)):
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
20 a_row = df.iloc[i]
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
21 if int(a_row[0]) < 0 or int(a_row[1]) < 0:
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
22 raise IndexError("Point {},{} is out of image with bounds {},{}.".format(int(a_row[0]), int(a_row[1]), shape[0], shape[1]))
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
23
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
24 if invert_xy:
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
25 if img.shape[0]<=int(a_row[0]) or img.shape[1]<=int(a_row[1]):
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
26 raise IndexError("Point {},{} is out of image with bounds {},{}.".format(int(a_row[0]), int(a_row[1]), shape[0], shape[1]))
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
27 else:
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
28 img[int(a_row[1]), int(a_row[0])] = 32767
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
29 else:
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
30 if img.shape[0]<=int(a_row[1]) or img.shape[1]<=int(a_row[0]):
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
31 raise IndexError("Point {},{} is out of image with bounds {},{}.".format(int(a_row[1]), int(a_row[0]), shape[0], shape[1]))
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
32 else:
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
33 img[int(a_row[0]), int(a_row[1])] = 32767
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
34 else:
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
35 raise Exception("{} is empty or does not exist.".format(point_file)) # appropriate built-in error?
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
36
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
37 with warnings.catch_warnings():
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
38 warnings.simplefilter("ignore")
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
39 skimage.io.imsave(out_file, img, plugin='tifffile') # otherwise we get problems with the .dat extension
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
40
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
41 if __name__ == "__main__":
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
42 parser = argparse.ArgumentParser()
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
43 parser.add_argument('point_file', type=argparse.FileType('r'), help='label file')
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
44 parser.add_argument('out_file', type=str, help='out file (TIFF)')
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
45 parser.add_argument('shapex', type=int, help='shapex')
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
46 parser.add_argument('shapey', type=int, help='shapey')
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
47 parser.add_argument('--has_header', dest='has_header', default=False, help='set True if CSV has header')
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
48 parser.add_argument('--invert_xy', dest='invert_xy', default=False, help='invert x and y in CSV')
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
49
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
50 args = parser.parse_args()
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
51
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
52 #TOOL
74b946cdba21 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2binaryimage/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
53 points2binaryimage(args.point_file.name, args.out_file, [args.shapey, args.shapex], has_header=args.has_header, invert_xy=args.invert_xy)