annotate binaryimage2points.py @ 0:c983b2d2cdd7 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
author thomaswollmann
date Wed, 12 Dec 2018 04:49:24 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
1 import argparse
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
2 import sys
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
3 import pandas as pd
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
4 import skimage.io
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
5 from skimage.measure import label
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
6 from skimage.data import checkerboard
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
7 import numpy as np
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
8 import warnings
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
9
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
10
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
11
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
12 def binaryimage2points(input_file):
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
13 # ignore warnings that arise when importing a package that was compiled against an older version of numpy than installed; https://github.com/numpy/numpy/pull/432
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
14 warnings.filterwarnings("ignore")
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
15
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
16 img_in = skimage.io.imread(input_file, plugin='tifffile')
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
17
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
18 #make label image
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
19 label = skimage.measure.label(img_in)
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
20
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
21 #amount of regions
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
22 amount_label = np.max(label)
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
23
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
24 # iterate over all regions in order to calc center of mass
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
25 center_mass = []
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
26 for i in range(1,amount_label+1):
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
27 #get coordinates of region
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
28 coord = np.where(label==i)
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
29 # be carefull with x,y coordinates
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
30 center_mass.append([np.mean(coord[1]),np.mean(coord[0])])
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
31
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
32 #make data frame of detections
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
33 out_dataFrame = pd.DataFrame(center_mass)
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
34
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
35
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
36 #return
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
37 return(out_dataFrame)
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
38
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
39
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
40
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
41
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
42 if __name__ == "__main__":
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
43 parser = argparse.ArgumentParser()
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
44 parser.add_argument('input_file', help='input file')
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
45 parser.add_argument('out_file', help='out file (CSV)')
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
46
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
47 args = parser.parse_args()
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
48 input_file = args.input_file
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
49 out_file = args.out_file
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
50
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
51 #TOOL
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
52 out_dataFrame = binaryimage2points(input_file)
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
53
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
54 #Print to csv file
c983b2d2cdd7 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit 27a6350188f687411bdd6bfe0d569c0803389ca0
thomaswollmann
parents:
diff changeset
55 out_dataFrame.to_csv(out_file,index=False,header=False)