From c2a6b684bdf245260d7092bf0388c3d6702ec9fd Mon Sep 17 00:00:00 2001
From: polarbean <harry.carey95@gmail.com>
Date: Fri, 26 May 2023 13:45:21 +0200
Subject: [PATCH] now saves objects to meshview as well" "

---
 PyNutil/coordinate_extraction.py | 10 +++++----
 PyNutil/main.py                  | 36 ++++++++++++++++++++------------
 testOOP.py                       |  2 +-
 3 files changed, 30 insertions(+), 18 deletions(-)

diff --git a/PyNutil/coordinate_extraction.py b/PyNutil/coordinate_extraction.py
index c220e82..d16ae6f 100644
--- a/PyNutil/coordinate_extraction.py
+++ b/PyNutil/coordinate_extraction.py
@@ -84,7 +84,7 @@ def transform_to_atlas_space(anchoring, y, x, reg_height, reg_width):
 # related to coordinate extraction
 # This function returns an array of points
 def folder_to_atlas_space(
-    folder, quint_alignment, pixel_id=[0, 0, 0], non_linear=True, method="all"
+    folder, quint_alignment, pixel_id=[0, 0, 0], non_linear=True, method="all", object_cutoff=0
 ):
     """Apply Segmentation to atlas space to all segmentations in a folder."""
 
@@ -118,6 +118,7 @@ def folder_to_atlas_space(
                 centroids_list,
                 index,
                 method,
+                object_cutoff
             ),
         )
         threads.append(x)
@@ -145,6 +146,7 @@ def segmentation_to_atlas_space(
     centroids_list=None,
     index=None,
     method="per_pixel",
+    object_cutoff=0
 ):
     """Combines many functions to convert a segmentation to atlas space. It takes care
     of deformations."""
@@ -170,7 +172,7 @@ def segmentation_to_atlas_space(
     centroids, points = None, None
     if method in ["per_object", "all"]:
         centroids, scaled_centroidsX, scaled_centroidsY = get_centroids(
-            segmentation, pixel_id, y_scale, x_scale
+            segmentation, pixel_id, y_scale, x_scale, object_cutoff
         )
         print("Number of objects: ", len(scaled_centroidsY))
     if method in ["per_pixel", "all"]:
@@ -212,10 +214,10 @@ def segmentation_to_atlas_space(
     centroids_list[index] = np.array(centroids)
 
 
-def get_centroids(segmentation, pixel_id, y_scale, x_scale):
+def get_centroids(segmentation, pixel_id, y_scale, x_scale, object_cutoff=0):
     binary_seg = segmentation == pixel_id
     binary_seg = np.all(binary_seg, axis=2)
-    centroids, area, coords = get_centroids_and_area(binary_seg, pixel_cut_off=0)
+    centroids, area, coords = get_centroids_and_area(binary_seg, pixel_cut_off=object_cutoff)
     centroidsY = centroids[:, 1]
     centroidsX = centroids[:, 0]
     scaled_centroidsY, scaled_centroidsX = scale_positions(
diff --git a/PyNutil/main.py b/PyNutil/main.py
index 2a2ea71..97ebdd4 100644
--- a/PyNutil/main.py
+++ b/PyNutil/main.py
@@ -132,7 +132,7 @@ class PyNutil:
         print("atlas labels loaded ✅")
         return atlas_volume, atlas_labels
 
-    def get_coordinates(self, non_linear=True, method="all"):
+    def get_coordinates(self, non_linear=True, method="all", object_cutoff=0):
         """Extracts pixel coordinates from the segmentation data.
 
         Parameters
@@ -142,6 +142,8 @@ class PyNutil:
         method : str, optional
             The method to use for extracting coordinates. Valid options are 'per_pixel', 'per_object', or 'all'.
             Default is 'all'.
+        object_cutoff : int, optional
+            The minimum number of pixels per object to be included in the analysis. Default is 1.
 
         Raises
         ------
@@ -164,6 +166,7 @@ class PyNutil:
             pixel_id=self.colour,
             non_linear=non_linear,
             method=method,
+            object_cutoff=object_cutoff,
         )
         self.pixel_points = pixel_points
         self.centroids = centroids
@@ -201,6 +204,7 @@ class PyNutil:
             labeled_points, labeled_points_centroids, self.atlas_labels
         )
         self.labeled_points = labeled_points
+        self.labeled_points_centroids = labeled_points_centroids
 
         print("quantification complete ✅")
 
@@ -224,22 +228,28 @@ class PyNutil:
 
         if not hasattr(self, "pixel_points"):
             raise ValueError("Please run get_coordinates before running save_analysis")
-
-        self.label_df.to_csv(
-            output_folder + "/counts.csv", sep=";", na_rep="", index=False
-        )
-
         if not hasattr(self, "label_df"):
             print("no quantification found so we will only save the coordinates")
             print(
                 "if you want to save the quantification please run quantify_coordinates"
             )
 
-        else:
-            write_points_to_meshview(
-                self.pixel_points,
-                self.labeled_points,
-                output_folder + "/pixels_meshview.json",
-                self.atlas_labels,
-            )
+        self.label_df.to_csv(
+            output_folder + "/counts.csv", sep=";", na_rep="", index=False
+        )
+
+ 
+ 
+        write_points_to_meshview(
+            self.pixel_points,
+            self.labeled_points,
+            output_folder + "/pixels_meshview.json",
+            self.atlas_labels,
+        )
+        write_points_to_meshview(
+            self.centroids,
+            self.labeled_points_centroids,
+            output_folder + "/objects_meshview.json",
+            self.atlas_labels,
+        )
         print("analysis saved ✅")
diff --git a/testOOP.py b/testOOP.py
index 617dcf7..fe5173a 100644
--- a/testOOP.py
+++ b/testOOP.py
@@ -3,7 +3,7 @@ from PyNutil import PyNutil
 pnt = PyNutil(settings_file=r"test/test4_2017.json")
 # pnt.build_quantifier()
 
-pnt.get_coordinates()
+pnt.get_coordinates(object_cutoff=0)
 
 pnt.quantify_coordinates()
 
-- 
GitLab