Mercurial > repos > imgteam > points2labelimage
annotate points2label.py @ 6:22bb32eae6a1 draft default tip
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
| author | imgteam |
|---|---|
| date | Thu, 06 Nov 2025 09:59:34 +0000 |
| parents | 4a49f74a3c14 |
| children |
| rev | line source |
|---|---|
|
0
dcc8c1d6af48
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
1 import argparse |
|
5
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
2 import json |
|
2
30ca5d5d03ec
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
3 import warnings |
|
5
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
4 from typing import ( |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
5 Any, |
|
5
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
6 Dict, |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
7 Optional, |
|
5
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
8 Tuple, |
|
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
9 ) |
|
2
30ca5d5d03ec
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
10 |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
11 import giatools.pandas |
|
0
dcc8c1d6af48
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
12 import numpy as np |
|
5
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
13 import numpy.typing as npt |
|
2
30ca5d5d03ec
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
14 import pandas as pd |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
15 import scipy.ndimage as ndi |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
16 import skimage.draw |
|
0
dcc8c1d6af48
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
17 import skimage.io |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
18 import skimage.segmentation |
|
2
30ca5d5d03ec
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
19 |
|
0
dcc8c1d6af48
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
20 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
21 def get_list_depth(nested_list: Any) -> int: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
22 if isinstance(nested_list, list): |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
23 if len(nested_list) > 0: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
24 return 1 + max(map(get_list_depth, nested_list)) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
25 else: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
26 return 1 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
27 else: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
28 return 0 |
|
5
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
29 |
|
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
30 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
31 class AutoLabel: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
32 """ |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
33 Creates a sequence of unique labels (non-negative values). |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
34 """ |
|
5
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
35 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
36 def __init__(self, reserved_labels): |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
37 self.reserved_labels = reserved_labels |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
38 self.next_autolabel = 0 |
|
5
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
39 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
40 def next(self): |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
41 """ |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
42 Retrieve the next auto-label (post-increment). |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
43 """ |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
44 # Fast-forward `next_autolabel` to the first free label |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
45 while self.next_autolabel in self.reserved_labels: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
46 self.next_autolabel += 1 |
|
5
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
47 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
48 # Return the free label, then advance `next_autolabel` |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
49 try: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
50 return self.next_autolabel |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
51 finally: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
52 self.next_autolabel += 1 |
|
5
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
53 |
|
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
54 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
55 def get_feature_label(feature: Dict) -> Optional[int]: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
56 """ |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
57 Get the label of a GeoJSON feature, or `None` if there is no proper label. |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
58 """ |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
59 label = feature.get('properties', {}).get('name', None) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
60 if label is None: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
61 return None |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
62 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
63 # If the `label` is given as a string, try to parse as integer |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
64 if isinstance(label, str): |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
65 try: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
66 label = int(label) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
67 except ValueError: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
68 pass |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
69 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
70 # Finally, if `label` is an integer, only use it if it is non-negative |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
71 if isinstance(label, int) and label >= 0: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
72 return label |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
73 else: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
74 return None |
|
5
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
75 |
|
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
76 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
77 def rasterize( |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
78 geojson: Dict, |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
79 shape: Tuple[int, int], |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
80 bg_value: int = 0, |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
81 fg_value: Optional[int] = None, |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
82 ) -> npt.NDArray: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
83 """ |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
84 Rasterize GeoJSON into a pixel image, that is returned as a NumPy array. |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
85 """ |
|
0
dcc8c1d6af48
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
86 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
87 # Determine which labels are reserved (not used by auto-label) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
88 reserved_labels = [bg_value] |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
89 if fg_value is None: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
90 for feature in geojson['features']: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
91 label = get_feature_label(feature) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
92 if label is not None: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
93 reserved_labels.append(label) |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
94 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
95 # Convert `reserved_labels` into a `set` for faster look-ups |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
96 reserved_labels = frozenset(reserved_labels) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
97 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
98 # Define routine to retrieve the next auto-label |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
99 autolabel = AutoLabel(reserved_labels) |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
100 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
101 # Rasterize the image |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
102 img = np.full(shape, dtype=np.uint16, fill_value=bg_value) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
103 for feature in geojson['features']: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
104 geom_type = feature['geometry']['type'].lower() |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
105 coords = feature['geometry']['coordinates'] |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
106 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
107 # Rasterize a `mask` separately for each feature |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
108 if geom_type == 'polygon': |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
109 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
110 # Normalization: Let there always be a list of polygons |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
111 if get_list_depth(coords) == 2: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
112 coords = [coords] |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
113 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
114 # Rasterize each polygon separately, then join via XOR |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
115 mask = np.zeros(shape, dtype=bool) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
116 for polygon_coords in coords: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
117 polygon_mask = skimage.draw.polygon2mask( |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
118 shape, |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
119 [point[::-1] for point in polygon_coords], |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
120 ) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
121 mask = np.logical_xor(mask, polygon_mask) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
122 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
123 elif geom_type == 'point': |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
124 mask = np.zeros(shape, dtype=bool) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
125 mask[coords[1], coords[0]] = True |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
126 radius = feature.get('properties', {}).get('radius', 0) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
127 if radius > 0: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
128 mask = (ndi.distance_transform_edt(~mask) <= radius) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
129 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
130 else: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
131 raise ValueError( |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
132 f'Unsupported geometry type: "{feature["geometry"]["type"]}"', |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
133 ) |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
134 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
135 # Determine the `label` for the current `mask` |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
136 if fg_value is None: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
137 label = get_feature_label(feature) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
138 if label is None: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
139 label = autolabel.next() |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
140 else: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
141 label = fg_value |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
142 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
143 # Blend the current `mask` with the rasterized image |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
144 img[mask] = label |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
145 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
146 # Return the rasterized image |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
147 return img |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
148 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
149 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
150 def convert_tabular_to_geojson( |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
151 tabular_file: str, |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
152 has_header: bool, |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
153 ) -> dict: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
154 """ |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
155 Read a tabular file and convert it to GeoJSON. |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
156 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
157 The GeoJSON data is returned as a dictionary. |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
158 """ |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
159 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
160 # Read the tabular file with information from the header |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
161 if has_header: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
162 df = pd.read_csv(tabular_file, delimiter='\t') |
|
4
aef9911c0d5c
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
imgteam
parents:
3
diff
changeset
|
163 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
164 pos_x_column = giatools.pandas.find_column(df, ['pos_x', 'POS_X']) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
165 pos_y_column = giatools.pandas.find_column(df, ['pos_y', 'POS_Y']) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
166 pos_x_list = df[pos_x_column].round().astype(int) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
167 pos_y_list = df[pos_y_column].round().astype(int) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
168 assert len(pos_x_list) == len(pos_y_list) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
169 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
170 try: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
171 radius_column = giatools.pandas.find_column(df, ['radius', 'RADIUS']) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
172 radius_list = df[radius_column] |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
173 assert len(pos_x_list) == len(radius_list) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
174 except KeyError: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
175 radius_list = [0] * len(pos_x_list) |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
176 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
177 try: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
178 width_column = giatools.pandas.find_column(df, ['width', 'WIDTH']) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
179 height_column = giatools.pandas.find_column(df, ['height', 'HEIGHT']) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
180 width_list = df[width_column] |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
181 height_list = df[height_column] |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
182 assert len(pos_x_list) == len(width_list) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
183 assert len(pos_x_list) == len(height_list) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
184 except KeyError: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
185 width_list = [0] * len(pos_x_list) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
186 height_list = [0] * len(pos_x_list) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
187 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
188 try: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
189 label_column = giatools.pandas.find_column(df, ['label', 'LABEL']) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
190 label_list = df[label_column] |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
191 assert len(pos_x_list) == len(label_list) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
192 except KeyError: |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
193 label_list = list(range(1, len(pos_x_list) + 1)) |
|
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
194 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
195 # Read the tabular file without header |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
196 else: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
197 df = pd.read_csv(tabular_file, header=None, delimiter='\t') |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
198 pos_x_list = df[0].round().astype(int) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
199 pos_y_list = df[1].round().astype(int) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
200 assert len(pos_x_list) == len(pos_y_list) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
201 radius_list, width_list, height_list = [[0] * len(pos_x_list)] * 3 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
202 label_list = list(range(1, len(pos_x_list) + 1)) |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
203 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
204 # Convert to GeoJSON |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
205 features = [] |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
206 geojson = { |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
207 'type': 'FeatureCollection', |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
208 'features': features, |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
209 } |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
210 for y, x, radius, width, height, label in zip( |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
211 pos_y_list, pos_x_list, radius_list, width_list, height_list, label_list, |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
212 ): |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
213 if radius > 0 and width > 0 and height > 0: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
214 raise ValueError('Ambiguous shape type (circle or rectangle)') |
|
0
dcc8c1d6af48
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
215 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
216 # Create a rectangle |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
217 if width > 0 and height > 0: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
218 geom_type = 'Polygon' |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
219 coords = [ |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
220 [x, y], |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
221 [x + width - 1, y], |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
222 [x + width - 1, y + height - 1], |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
223 [x, y + height - 1], |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
224 ] |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
225 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
226 # Create a point or circle |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
227 else: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
228 geom_type = 'Point' |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
229 coords = [x, y] |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
230 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
231 # Create a GeoJSON feature |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
232 feature = { |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
233 'type': 'Feature', |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
234 'geometry': { |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
235 'type': geom_type, |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
236 'coordinates': coords, |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
237 }, |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
238 'properties': { |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
239 'name': label, |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
240 }, |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
241 } |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
242 if radius > 0: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
243 feature['properties']['radius'] = radius |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
244 feature['properties']['subType'] = 'Circle' |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
245 features.append(feature) |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
246 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
247 # Return the GeoJSON object |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
248 return geojson |
|
0
dcc8c1d6af48
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
249 |
|
2
30ca5d5d03ec
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 2286a6c9da88596349ed9d967c51541409c0a7bf
imgteam
parents:
1
diff
changeset
|
250 |
|
5
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
251 if __name__ == '__main__': |
|
0
dcc8c1d6af48
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
252 parser = argparse.ArgumentParser() |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
253 parser.add_argument('in_ext', type=str, help='Input file format') |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
254 parser.add_argument('in_file', type=str, help='Input file path (tabular or GeoJSON)') |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
255 parser.add_argument('out_file', type=str, help='Output file path (TIFF)') |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
256 parser.add_argument('shapex', type=int, help='Output image width') |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
257 parser.add_argument('shapey', type=int, help='Output image height') |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
258 parser.add_argument('--has_header', dest='has_header', default=False, help='Set True if tabular file has a header') |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
259 parser.add_argument('--swap_xy', dest='swap_xy', default=False, help='Swap X and Y coordinates') |
|
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
260 parser.add_argument('--binary', dest='binary', default=False, help='Produce binary image') |
|
0
dcc8c1d6af48
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
261 args = parser.parse_args() |
|
dcc8c1d6af48
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tools/points2labelimage/ commit c3f4b766f03770f094fda6bda0a5882c0ebd4581
imgteam
parents:
diff
changeset
|
262 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
263 # Validate command-line arguments |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
264 assert args.in_ext in ('tabular', 'geojson'), ( |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
265 f'Unexpected input file format: {args.in_ext}' |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
266 ) |
|
5
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
267 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
268 # Load the GeoJSON data (if the input file is tabular, convert to GeoJSON) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
269 if args.in_ext == 'tabular': |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
270 geojson = convert_tabular_to_geojson(args.in_file, args.has_header) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
271 else: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
272 with open(args.in_file) as f: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
273 geojson = json.load(f) |
|
5
4a49f74a3c14
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 9a40a5d1e1008c26cc327c6d163df2a1af22a1a0
imgteam
parents:
4
diff
changeset
|
274 |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
275 # Rasterize the image from GeoJSON |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
276 shape = (args.shapey, args.shapex) |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
277 img = rasterize( |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
278 geojson, |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
279 shape if not args.swap_xy else shape[::-1], |
|
3
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
280 fg_value=0xffff if args.binary else None, |
|
de611b3b5ae8
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 6fc9ab8db9ef72ac7ded30d7373768feeae9390d
imgteam
parents:
2
diff
changeset
|
281 ) |
|
6
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
282 if args.swap_xy: |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
283 img = img.T |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
284 |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
285 # Write the rasterized image as TIFF |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
286 with warnings.catch_warnings(): |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
287 warnings.simplefilter('ignore') |
|
22bb32eae6a1
planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit edac062b00490276ef00d094e0594abdb3a3f23c
imgteam
parents:
5
diff
changeset
|
288 skimage.io.imsave(args.out_file, img, plugin='tifffile') # otherwise we get problems with the .dat extension |
