changeset 4:aef9911c0d5c draft

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/points2labelimage/ commit 78614a9010c2ca0e1fa5973639c05ab74bcdb148
author imgteam
date Wed, 23 Apr 2025 14:37:42 +0000
parents de611b3b5ae8
children 4a49f74a3c14
files points2label.py points2label.xml test-data/input4.tsv test-data/output4.tif
diffstat 4 files changed, 54 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/points2label.py	Fri Sep 27 17:41:21 2024 +0000
+++ b/points2label.py	Wed Apr 23 14:37:42 2025 +0000
@@ -33,6 +33,17 @@
                 radius_list = [0] * len(pos_x_list)
 
             try:
+                width_column = giatools.pandas.find_column(df, ['width', 'WIDTH'])
+                height_column = giatools.pandas.find_column(df, ['height', 'HEIGHT'])
+                width_list = df[width_column]
+                height_list = df[height_column]
+                assert len(pos_x_list) == len(width_list)
+                assert len(pos_x_list) == len(height_list)
+            except KeyError:
+                width_list = [0] * len(pos_x_list)
+                height_list = [0] * len(pos_x_list)
+
+            try:
                 label_column = giatools.pandas.find_column(df, ['label', 'LABEL'])
                 label_list = df[label_column]
                 assert len(pos_x_list) == len(label_list)
@@ -45,7 +56,7 @@
             pos_x_list = df[0].round().astype(int)
             pos_y_list = df[1].round().astype(int)
             assert len(pos_x_list) == len(pos_y_list)
-            radius_list = [0] * len(pos_x_list)
+            radius_list, width_list, height_list = [[0] * len(pos_x_list)] * 3
             label_list = list(range(1, len(pos_x_list) + 1))
 
         # Optionally swap the coordinates
@@ -53,7 +64,9 @@
             pos_x_list, pos_y_list = pos_y_list, pos_x_list
 
         # Perform the rasterization
-        for y, x, radius, label in zip(pos_y_list, pos_x_list, radius_list, label_list):
+        for y, x, radius, width, height, label in zip(
+            pos_y_list, pos_x_list, radius_list, width_list, height_list, label_list,
+        ):
             if fg_value is not None:
                 label = fg_value
 
@@ -61,10 +74,23 @@
                 raise IndexError(f'The point x={x}, y={y} exceeds the bounds of the image (width: {shape[1]}, height: {shape[0]})')
 
             # Rasterize circle and distribute overlapping image area
-            if radius > 0:
-                mask = np.ones(shape, dtype=bool)
-                mask[y, x] = False
-                mask = (ndi.distance_transform_edt(mask) <= radius)
+            # Rasterize primitive geometry
+            if radius > 0 or (width > 0 and height > 0):
+
+                # Rasterize circle
+                if radius > 0:
+                    mask = np.ones(shape, dtype=bool)
+                    mask[y, x] = False
+                    mask = (ndi.distance_transform_edt(mask) <= radius)
+                else:
+                    mask = np.zeros(shape, dtype=bool)
+
+                # Rasterize rectangle
+                if width > 0 and height > 0:
+                    mask[
+                        y:min(shape[0], y + width),
+                        x:min(shape[1], x + height)
+                    ] = True
 
                 # Compute the overlap (pretend there is none if the rasterization is binary)
                 if fg_value is None:
--- a/points2label.xml	Fri Sep 27 17:41:21 2024 +0000
+++ b/points2label.xml	Wed Apr 23 14:37:42 2025 +0000
@@ -3,7 +3,7 @@
     <macros>
         <import>creators.xml</import>
         <import>tests.xml</import>
-        <token name="@TOOL_VERSION@">0.4</token>
+        <token name="@TOOL_VERSION@">0.4.1</token>
         <token name="@VERSION_SUFFIX@">0</token>
     </macros>
     <creator>
@@ -46,7 +46,7 @@
         <data name="output" format="tiff" />
     </outputs>
     <tests>
-        <!-- Binary / TSV without header -->
+        <!-- Binary / TSV without header / Points -->
         <test>
             <param name="input" value="input1.tsv" />
             <param name="shapex" value="30" />
@@ -56,7 +56,7 @@
             <param name="binary" value="true" />
             <expand macro="tests/binary_image_diff" name="output" value="output1_binary.tif" ftype="tiff" />
         </test>
-        <!-- Binary / TSV with header -->
+        <!-- Binary / TSV with header / Circles -->
         <test>
             <param name="input" value="input2.tsv" />
             <param name="shapex" value="205" />
@@ -66,7 +66,7 @@
             <param name="binary" value="true" />
             <expand macro="tests/binary_image_diff" name="output" value="output2_binary.tif" ftype="tiff" />
         </test>
-        <!-- Labeled / TSV with header -->
+        <!-- Labeled / TSV with header / Circles -->
         <test>
             <param name="input" value="input2.tsv" />
             <param name="shapex" value="205" />
@@ -76,7 +76,7 @@
             <param name="binary" value="false" />
             <expand macro="tests/label_image_diff" name="output" value="output2.tif" ftype="tiff" />
         </test>
-        <!-- Binary / TSV with header / TSV with labels -->
+        <!-- Binary / TSV with header / TSV with labels / Circles -->
         <test>
             <param name="input" value="input3.tsv" />
             <param name="shapex" value="200" />
@@ -86,7 +86,7 @@
             <param name="binary" value="true" />
             <expand macro="tests/binary_image_diff" name="output" value="output3_binary.tif" ftype="tiff" />
         </test>
-        <!-- Labeled / TSV with header / TSV with labels -->
+        <!-- Labeled / TSV with header / TSV with labels / Circles -->
         <test>
             <param name="input" value="input3.tsv" />
             <param name="shapex" value="200" />
@@ -96,6 +96,16 @@
             <param name="binary" value="false" />
             <expand macro="tests/label_image_diff" name="output" value="output3.tif" ftype="tiff" />
         </test>
+        <!-- Labeled / TSV with header / TSV with labels / Rectangles -->
+        <test>
+            <param name="input" value="input4.tsv" />
+            <param name="shapex" value="200" />
+            <param name="shapey" value="100" />
+            <param name="has_header" value="true" />
+            <param name="swap_xy" value="false" />
+            <param name="binary" value="false" />
+            <expand macro="tests/label_image_diff" name="output" value="output4.tif" ftype="tiff" />
+        </test>
     </tests>
     <help>
 
@@ -113,6 +123,8 @@
         - ``pos_y`` or ``POS_Y``: This column corresponds to the Y coordinates.
         - If a ``radius`` or ``RADIUS`` column is present, then the points will be rasterized as circles of the
           corresponding radii.
+        - If ``width`` or ``WIDTH`` and ``height`` or ``HEIGHT`` columns are present, then the points will be rasterized
+          as rectangles of the corresponding size.
         - If a ``label`` or ``LABEL`` column is present, then the corresponding labels will be used for rasterization
           (unless "Produce binary image" is activated). Different points are allowed to use the same label.
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-data/input4.tsv	Wed Apr 23 14:37:42 2025 +0000
@@ -0,0 +1,4 @@
+pos_x	pos_y	width	height	label
+20	20	40	40	1
+50	50	30	40	2
+150	50	60	40	3
Binary file test-data/output4.tif has changed