annotate binaryimage2points.py @ 1:f38f42d55813 draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
author imgteam
date Mon, 13 Nov 2023 22:10:37 +0000
parents cb1eaebdb4c4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
1 import argparse
1
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
2 import warnings
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
3
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
4 import numpy as np
0
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
5 import pandas as pd
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
6 import skimage.io
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
7
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
8
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
9 def binaryimage2points(input_file):
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
10 # 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
1
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
11 warnings.filterwarnings("ignore")
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
12
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
13 img_in = skimage.io.imread(input_file, plugin='tifffile')
0
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
14
1
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
15 # make label image
0
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
16 label = skimage.measure.label(img_in)
1
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
17
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
18 # amount of regions
0
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
19 amount_label = np.max(label)
1
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
20
0
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
21 # iterate over all regions in order to calc center of mass
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
22 center_mass = []
1
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
23 for i in range(1, amount_label + 1):
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
24 # get coordinates of region
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
25 coord = np.where(label == i)
0
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
26 # be carefull with x,y coordinates
1
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
27 center_mass.append([np.mean(coord[1]), np.mean(coord[0])])
0
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
28
1
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
29 # make data frame of detections
0
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
30 out_dataFrame = pd.DataFrame(center_mass)
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
31
1
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
32 # return
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
33 return out_dataFrame
0
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
34
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
35
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
36 if __name__ == "__main__":
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
37 parser = argparse.ArgumentParser()
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
38 parser.add_argument('input_file', help='input file')
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
39 parser.add_argument('out_file', help='out file (TSV)')
1
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
40
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
41 args = parser.parse_args()
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
42 input_file = args.input_file
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
43 out_file = args.out_file
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
44
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
45 # TOOL
0
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
46 out_dataFrame = binaryimage2points(input_file)
1
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
47
f38f42d55813 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/binaryimage2points/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents: 0
diff changeset
48 # Print to csv file
0
cb1eaebdb4c4 planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/binaryimage2points/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff changeset
49 out_dataFrame.to_csv(out_file, index=False, header=False, sep="\t")