From a70b76b6bc0fd44dc80be3fe25415eb0482cad5a Mon Sep 17 00:00:00 2001
From: Laurent Guerard <laurent.guerard@unibas.ch>
Date: Thu, 13 Feb 2025 12:00:36 +0100
Subject: [PATCH] Fix resizing threshold and add log

---
 1_identify_fibers.py | 31 +++++++++++++++++++++++++------
 1 file changed, 25 insertions(+), 6 deletions(-)

diff --git a/1_identify_fibers.py b/1_identify_fibers.py
index cae2423..74d312b 100755
--- a/1_identify_fibers.py
+++ b/1_identify_fibers.py
@@ -47,7 +47,7 @@ import sys
 # python imports
 import time
 
-from ch.epfl.biop.ij2command import Labels2CompositeRois
+from ch.epfl.biop.ij2command import Labels2Rois
 
 # TrackMate imports
 from fiji.plugin.trackmate import Logger, Model, Settings, TrackMate
@@ -743,21 +743,25 @@ if __name__ == "__main__":
 
             # image (pre)processing and segmentation (-> ROIs)# imp, firstC, lastC, firstZ,
             # lastZ, firstT, lastT
+            misc.timed_log("Getting the membrane channel")
             membrane = Duplicator().run(
                 raw, membrane_channel, membrane_channel, 1, 1, 1, 1
             )
 
-            if (membrane.getWidth() * membrane.getHeight()) > 100000:
+            while (membrane.getWidth() * membrane.getHeight()) > 100000000:
                 misc.timed_log("Image is too large, resizing to speed up processing")
                 membrane = membrane.resize(
                     membrane.getWidth() / 2,
                     membrane.getHeight() / 2,
                     "none",
                 )
+
+            misc.timed_log("Pre processing the image for segmentation")
             imp_bgd_corrected = do_background_correction(membrane)
             IJ.run("Conversions...", "scale")
             IJ.run(imp_bgd_corrected, "16-bit", "")
 
+            misc.timed_log("Running the segmentation")
             imp_result = run_tm(
                 imp_bgd_corrected,
                 1,
@@ -769,25 +773,31 @@ if __name__ == "__main__":
                 perimeter_thresh=[minPer, maxPer],
             )
 
+            misc.timed_log("Resizing the segmentation results (if needed)")
             imp_result = imp_result.resize(
                 raw.getWidth(),
                 raw.getHeight(),
                 "none",
             )
 
+            misc.timed_log("Saving the segmentation results")
             IJ.saveAs(
                 imp_result,
                 "Tiff",
                 os.path.join(
                     output_dir,
-                    raw_image_title + "_" + str(serie_index) + "_all_fibers_binary",
+                    raw_image_title + "_" + str(serie_index) + "_all_fibers_labels",
                 ),
             )
 
-            command.run(Labels2CompositeRois, True, "rm", rm, "imp", imp_result).get()
+            misc.timed_log("Getting the ROIs")
+            command.run(Labels2Rois, True, "rm", rm, "imp", imp_result).get()
 
+            misc.timed_log("Enlarging and renumbering the ROIs")
             enlarge_all_rois(enlarge_radius, rm, raw_image_calibration.pixelWidth)
             renumber_rois(rm)
+
+            misc.timed_log("Saving the ROIs")
             save_all_rois(
                 rm,
                 os.path.join(
@@ -798,11 +808,12 @@ if __name__ == "__main__":
 
             # check for positive fibers
             if fiber_channel > 0:
+                misc.timed_log("Checking for positive fibers")
                 if min_fiber_intensity == 0:
                     min_fiber_intensity = get_threshold_from_method(
                         raw, fiber_channel, "Mean"
                     )[0]
-                    IJ.log("automatic intensity threshold detection: True")
+                    misc.timed_log("automatic intensity threshold detection: True")
 
                 IJ.log("fiber intensity threshold: " + str(min_fiber_intensity))
                 change_all_roi_color(rm, "blue")
@@ -810,6 +821,7 @@ if __name__ == "__main__":
                     raw, fiber_channel, rm, min_fiber_intensity
                 )
                 change_subset_roi_color(rm, positive_fibers, "magenta")
+                misc.timed_log("Saving positive fibers")
                 save_selected_rois(
                     rm,
                     positive_fibers,
@@ -823,6 +835,7 @@ if __name__ == "__main__":
                 )
 
             # measure size & shape, save
+            misc.timed_log("Measuring the ROIs")
             IJ.run(
                 "Set Measurements...",
                 "area perimeter shape feret's redirect=None decimal=4",
@@ -841,6 +854,7 @@ if __name__ == "__main__":
                 add_results(rt, "MHC Positive Fibers (magenta)", positive_fibers, "YES")
                 # print(rt.size())
 
+            misc.timed_log("Saving the measurements")
             rt.save(
                 os.path.join(
                     output_dir,
@@ -851,7 +865,11 @@ if __name__ == "__main__":
                 )
             )
             # print("saved the all_fibers_results.csv")
-            # dress up the original image, save a overlay-png, present original to the user
+            # dress up the original image, save a overlay-png, present original
+            # to the user
+            misc.timed_log(
+                "Improve the contrast and show the results to save an overlay image"
+            )
             rm.show()
             raw.show()
             show_all_rois_on_image(rm, raw)
@@ -877,6 +895,7 @@ if __name__ == "__main__":
             raw.setDisplayMode(IJ.GRAYSCALE)
             show_all_rois_on_image(rm, raw)
 
+            misc.timed_log("Save log")
             IJ.selectWindow("Log")
             IJ.saveAs(
                 "Text",
-- 
GitLab