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