From 32b72ad7407da79981dd3a0fd9590e7646288d97 Mon Sep 17 00:00:00 2001
From: Guillaume Cerutti <guillaume.cerutti@inria.fr>
Date: Thu, 7 Mar 2024 16:09:24 +0100
Subject: [PATCH] enable label properties in mesh -> cell transfer

---
 .../surfaceMeshCellProperty.py                     | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/src/gnomon_package_tissueimagemesh/algorithm/cellImageQuantification/surfaceMeshCellProperty.py b/src/gnomon_package_tissueimagemesh/algorithm/cellImageQuantification/surfaceMeshCellProperty.py
index f8b6c36..16ee8f3 100644
--- a/src/gnomon_package_tissueimagemesh/algorithm/cellImageQuantification/surfaceMeshCellProperty.py
+++ b/src/gnomon_package_tissueimagemesh/algorithm/cellImageQuantification/surfaceMeshCellProperty.py
@@ -35,6 +35,7 @@ class surfaceMeshCellProperty(gnomonAbstractCellImageQuantification):
 
         self._parameters = {}
         self._parameters['attribute_names'] = d_inliststringlist("Attributes", [""], [""], "List of vertex attributes to transfer from the surface mesh")
+        self._parameters['label_threshold'] = d_int("Max Label Number", 64, 0, 1e4, "Maximal number of different values for an attribute to be considered a label")
 
     def refreshParameters(self):
         if len(self.surface_topomesh)>0:
@@ -42,6 +43,8 @@ class surfaceMeshCellProperty(gnomonAbstractCellImageQuantification):
 
             attribute_names = self['attribute_names']
             property_names = list(surface_topomesh.wisp_property_names(0))
+            property_names = [p for p in property_names if surface_topomesh.has_wisp_property(p, 0, is_computed=True)]
+            property_names = [p for p in property_names if surface_topomesh.wisp_property(p, 0).values().dtype != np.dtype('O')]
             if len(property_names) == 0:
                 property_names = [""]
             attribute_names = [p for p in attribute_names if p in property_names]
@@ -92,9 +95,20 @@ class surfaceMeshCellProperty(gnomonAbstractCellImageQuantification):
                 vertex_property = surface_topomesh.wisp_property(property_name, 0).values(list(surface_topomesh.wisps(0)))
                 property_values = np.unique(vertex_property)
                 property_is_binary = vertex_property.ndim == 1 and len(property_values) <= 2 and (0 in property_values or 1 in property_values)
+                property_is_label = vertex_property.ndim == 1 and len(property_values) <= self['label_threshold']
                 cell_property = None
                 if property_is_binary:
                     cell_property = (nd.sum(vertex_property, vertex_cells, index=cell_labels) > 0).astype(int)
+                elif property_is_label:
+                    def most_represented(array):
+                        values, counts = np.unique(array, return_counts=True)
+                        return values[np.argmax(counts)]
+                    dtype = property_values.dtype
+                    default = np.iinfo(dtype).min if np.issubdtype(dtype, np.integer) else np.nan
+                    cell_property = nd.labeled_comprehension(
+                        vertex_property, vertex_cells, index=cell_labels,
+                        func=most_represented, out_dtype=dtype, default=default
+                    )
                 else:
                     if vertex_property.ndim == 1:
                         cell_property = nd.mean(vertex_property, vertex_cells, index=cell_labels)
-- 
GitLab