From c7e1cb93bda8d0edbf689a10dd823e728d543576 Mon Sep 17 00:00:00 2001
From: Laurent Guerard <laurent.guerard@unibas.ch>
Date: Wed, 8 Jan 2025 14:40:04 +0100
Subject: [PATCH] Remove some filtering measurements and change order of input
 and imports

---
 1_identify_fibers.py | 81 ++++++++++++++++++++++++++++++--------------
 1 file changed, 56 insertions(+), 25 deletions(-)

diff --git a/1_identify_fibers.py b/1_identify_fibers.py
index 6017fda..6c3dc3e 100755
--- a/1_identify_fibers.py
+++ b/1_identify_fibers.py
@@ -1,24 +1,56 @@
+# @ String (visibility=MESSAGE, value="<html><b> Welcome to Myosoft - identify fibers! </b></html>") msg1
+# @ File (label="Select folder with your images", description="select folder with your images", style="directory") src_dir
+# @ String(label="Extension for the images to look for", value="czi") filename_filter
+# @ File (label="Select directory for output", style="directory") output_dir
+# @ File(label="Cellpose environment folder", style="directory", description="Folder with the cellpose env") cellpose_dir
+# @ Boolean (label="close image after processing", description="tick this box when using batch mode", value=False) close_raw
+# @ String (visibility=MESSAGE, value="<html><b> Morphometric Gates </b></html>") msg2
+# @ Integer (label="Min Area [um²]", value=10) minAr
+# @ Integer (label="Max Area [um²]", value=6000) maxAr
+# @ Double (label="Min Circularity", value=0.5) minCir
+# @ Double (label="Max Circularity", value=1) maxCir
+# @ Integer (label="Min perimeter [um]", value=5) minPer
+# @ Integer (label="Max perimeter [um]", value=300) maxPer
+# @ String (visibility=MESSAGE, value="<html><b> Expand ROIS to match fibers </b></html>") msg3
+# @ Double (label="ROI expansion [microns]", value=1) enlarge_radius
+# @ String (visibility=MESSAGE, value="<html><b> channel positions in the hyperstack </b></html>") msg5
+# @ Integer (label="Membrane staining channel number", style="slider", min=1, max=5, value=1) membrane_channel
+# @ Integer (label="Fiber staining (MHC) channel number (0=skip)", style="slider", min=0, max=5, value=3) fiber_channel
+# @ Integer (label="minimum fiber intensity (0=auto)", description="0 = automatic threshold detection", value=0) min_fiber_intensity
+# @ CommandService command
+
+# @ RoiManager rm
+# @ ResultsTable rt
+
+
 # this is a python rewrite of the original ijm published at
 # https://github.com/Hyojung-Choo/Myosoft/blob/Myosoft-hub/Scripts/central%20nuclei%20counter.ijm
 
+# ─── Requirements ─────────────────────────────────────────────────────────────
+
+# List of update sites needed for the code
+# * TrackMate-Cellpose
+# * IMCF
+# * PTBIOP
+# * CLIJ-CLIJ2
+
+# ─── Imports ──────────────────────────────────────────────────────────────────
+
 # IJ imports
 # TODO: are the imports RoiManager and ResultsTable needed when using the services?
-from ij import IJ, WindowManager as wm
-from ij.plugin import Duplicator, RoiEnlarger, RoiScaler
-
-from ij.measure import ResultsTable
+import os
+import sys
 
 # Bio-formats imports
-from loci.plugins import BF
-from loci.plugins.in import ImporterOptions
-
+# from loci.plugins import BF
+# from loci.plugins.in import ImporterOptions
 # python imports
 import time
-import os
-import sys
+
+from ch.epfl.biop.ij2command import Labels2CompositeRois
 
 # TrackMate imports
-from fiji.plugin.trackmate import Logger, Model, SelectionModel, Settings, TrackMate
+from fiji.plugin.trackmate import Logger, Model, Settings, TrackMate
 from fiji.plugin.trackmate.action import LabelImgExporter
 from fiji.plugin.trackmate.cellpose import CellposeDetectorFactory
 from fiji.plugin.trackmate.cellpose.CellposeSettings import PretrainedModel
@@ -153,7 +185,7 @@ def get_threshold_from_method(imp, channel, method):
     list
         the upper and the lower threshold (integer values)
     """
-    imp.setC(channel) # starts at 1
+    imp.setC(channel)  # starts at 1
     ip = imp.getProcessor()
     ip.setAutoThreshold(method + " dark")
     lower_thr = ip.getMinThreshold()
@@ -266,18 +298,17 @@ def run_tm(
     if any(quality_thresh):
         settings = set_trackmate_filter(settings, "QUALITY", quality_thresh)
     if any(intensity_thresh):
-        settings = set_trackmate_filter(settings, "MEAN_INTENSITY_CH" + str(channel_seg), intensity_thresh)
+        settings = set_trackmate_filter(
+            settings, "MEAN_INTENSITY_CH" + str(channel_seg), intensity_thresh
+        )
     if any(circularity_thresh):
         settings = set_trackmate_filter(settings, "CIRCULARITY", circularity_thresh)
     if any(area_thresh):
         settings = set_trackmate_filter(settings, "AREA", area_thresh)
     if any(perimeter_thresh):
         settings = set_trackmate_filter(settings, "PERIMETER", perimeter_thresh)
-    if any(feret_thresh):
-        settings = set_trackmate_filter(settings, "FERET", feret_thresh)
 
-
-    print(settings)
+    # print(settings)
 
     # Configure tracker
     settings.trackerFactory = SparseLAPTrackerFactory()
@@ -674,15 +705,15 @@ measure_in_all_rois( raw, membrane_channel, rm )
 rt = ResultsTable.getResultsTable("Results")
 
 print rt.size()
-
-if fiber_channel > 0:
-    print rt.size()
-    preset_results_column( rt, "MHC Positive Fibers (magenta)", "NO" )
-    print rt.size()
-    add_results( rt, "MHC Positive Fibers (magenta)", positive_fibers, "YES")
-    print rt.size()
-
-rt.save(output_dir + "/" + raw_image_title + "_all_fibers_results.csv")
+            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 -- ")
 print "saved the all_fibers_results.csv"
 # dress up the original image, save a overlay-png, present original to the user
 rm.show()
-- 
GitLab