From aa9f80b0857ab2857a6f779d4c16bac2e560a064 Mon Sep 17 00:00:00 2001
From: Laurent Guerard <laurent.guerard@unibas.ch>
Date: Wed, 12 Feb 2025 13:53:41 +0100
Subject: [PATCH] Add rescaling if image is too big for Cellpose even on Naboo

---
 1_identify_fibers.py | 52 ++++++++++----------------------------------
 1 file changed, 11 insertions(+), 41 deletions(-)

diff --git a/1_identify_fibers.py b/1_identify_fibers.py
index ffb0f5f..cae2423 100755
--- a/1_identify_fibers.py
+++ b/1_identify_fibers.py
@@ -747,8 +747,13 @@ if __name__ == "__main__":
                 raw, membrane_channel, membrane_channel, 1, 1, 1, 1
             )
 
-        if not os.path.exists(str(output_dir)):
-            os.makedirs(str(output_dir))
+            if (membrane.getWidth() * membrane.getHeight()) > 100000:
+                misc.timed_log("Image is too large, resizing to speed up processing")
+                membrane = membrane.resize(
+                    membrane.getWidth() / 2,
+                    membrane.getHeight() / 2,
+                    "none",
+                )
             imp_bgd_corrected = do_background_correction(membrane)
             IJ.run("Conversions...", "scale")
             IJ.run(imp_bgd_corrected, "16-bit", "")
@@ -764,46 +769,11 @@ if __name__ == "__main__":
                 perimeter_thresh=[minPer, maxPer],
             )
 
-        # update the log for the user
-        misc.timed_log("Now working on " + str(raw_image_title))
-        if raw_image_calibration.scaled() is False:
-            IJ.log(
-                "Your image is not spatially calibrated! Size measurements are only possible in [px]."
+            imp_result = imp_result.resize(
+                raw.getWidth(),
+                raw.getHeight(),
+                "none",
             )
-        # Only print it once since we'll use the same settings everytime
-        if index == 0:
-            IJ.log(" -- settings used -- ")
-            IJ.log("area = " + str(minAr) + "-" + str(maxAr))
-            IJ.log("perimeter = " + str(minPer) + "-" + str(maxPer))
-            IJ.log("circularity = " + str(minCir) + "-" + str(maxCir))
-            IJ.log("ROI expansion [microns] = " + str(enlarge_radius))
-            IJ.log("Membrane channel = " + str(membrane_channel))
-            IJ.log("MHC positive fiber channel = " + str(fiber_channel))
-            # IJ.log("sub-tiling = " + str(tiling_factor))
-            IJ.log(" -- settings used -- ")
-
-        # image (pre)processing and segmentation (-> ROIs)# imp, firstC, lastC, firstZ,
-        # lastZ, firstT, lastT
-        membrane = Duplicator().run(raw, membrane_channel, membrane_channel, 1, 1, 1, 1)
-        imp_bgd_corrected = do_background_correction(membrane)
-        IJ.run("Conversions...", "scale")
-        IJ.run(imp_bgd_corrected, "16-bit", "")
-
-        imp_result = run_tm(
-            imp_bgd_corrected,
-            1,
-            cellpose_dir.getPath(),
-            PretrainedModel.CYTO2,
-            30.0,
-            area_thresh=[minAr, maxAr],
-            circularity_thresh=[minCir, maxCir],
-            perimeter_thresh=[minPer, maxPer],
-        )
-        IJ.saveAs(
-            imp_result,
-            "Tiff",
-            os.path.join(output_dir, raw_image_title + "_all_fibers_binary"),
-        )
 
             IJ.saveAs(
                 imp_result,
-- 
GitLab