Browse code

"Revert "remove fast option, fixes #9"

This reverts commit 075b94b19cff41f601075610f3e7ad0f1f7192f2.

alexq authored on 01/08/2024 06:14:02
Showing 1 changed files

  • R/LISA.R index 223659c..f160ffe 100644
... ...
@@ -7,9 +7,13 @@
7 7
 #' @param window Should the window around the regions be 'square', 'convex' or 'concave'.
8 8
 #' @param window.length A tuning parameter for controlling the level of concavity
9 9
 #' when estimating concave windows.
10
+#' @param whichParallel Should the function use parallization on the imageID or
11
+#' the cellType.
10 12
 #' @param sigma A numeric variable used for scaling when filting inhomogeneous L-curves.
11 13
 #' @param lisaFunc Either "K" or "L" curve.
12 14
 #' @param minLambda  Minimum value for density for scaling when fitting inhomogeneous L-curves.
15
+#' @param fast A logical describing whether to use a fast approximation of the
16
+#' inhomogeneous local L-curves.
13 17
 #' @param spatialCoords The columns which contain the x and y spatial coordinates.
14 18
 #' @param cellType The column which contains the cell types.
15 19
 #' @param imageID The column which contains image identifiers.
... ...
@@ -55,9 +59,11 @@ lisa <-
55 59
            BPPARAM = BiocParallel::SerialParam(),
56 60
            window = "convex",
57 61
            window.length = NULL,
62
+           whichParallel = "imageID",
58 63
            sigma = NULL,
59 64
            lisaFunc = "K",
60 65
            minLambda = 0.05,
66
+           fast = TRUE,
61 67
            spatialCoords = c("x", "y"),
62 68
            cellType = "cellType",
63 69
            imageID = "imageID") {
... ...
@@ -72,28 +78,52 @@ lisa <-
72 78
       Rs <- c(20, 50, 100)
73 79
     }
74 80
 
75
-    BPimage <- BPPARAM
76
-
77
-    message("Generating local L-curves.")
78
-
79
-    curveList <-
80
-      BiocParallel::bplapply(
81
-        cellSummary,
82
-        inhomLocalK,
83
-        Rs = Rs,
84
-        sigma = sigma,
85
-        window = window,
86
-        window.length = window.length,
87
-        minLambda = minLambda,
88
-        lisaFunc = lisaFunc,
89
-        BPPARAM = BPimage
90
-      )
81
+    BPimage <- BPcellType <- BiocParallel::SerialParam()
82
+    if (whichParallel == "imageID") {
83
+      BPimage <- BPPARAM
84
+    }
85
+    if (whichParallel == "cellType") {
86
+      BPcellType <- BPPARAM
87
+    }
88
+
89
+    if (!fast) {
90
+      message("Generating local L-curves. ")
91
+      if (identical(BPimage, BPcellType)) {
92
+        message("You might like to consider setting BPPARAM to run the calculations in parallel.")
93
+      }
94
+      curveList <-
95
+        BiocParallel::bplapply(
96
+          cellSummary,
97
+          generateCurves,
98
+          Rs = Rs,
99
+          window = window,
100
+          window.length = window.length,
101
+          BPcellType = BPcellType,
102
+          BPPARAM = BPimage,
103
+          sigma = sigma
104
+        )
105
+    }
106
+
107
+    if (fast) {
108
+      message("Generating local L-curves. If you run out of memory, try 'fast = FALSE'.")
109
+
110
+      curveList <-
111
+        BiocParallel::bplapply(
112
+          cellSummary,
113
+          inhomLocalK,
114
+          Rs = Rs,
115
+          sigma = sigma,
116
+          window = window,
117
+          window.length = window.length,
118
+          minLambda = minLambda,
119
+          lisaFunc = lisaFunc,
120
+          BPPARAM = BPimage
121
+        )
122
+    }
91 123
 
92 124
     curvelist <- lapply(curveList, as.data.frame)
93 125
     curves <- as.matrix(dplyr::bind_rows(curvelist))
94
-    rownames(curves) <- as.character(
95
-      unlist(lapply(cellSummary, function(x) x$cellID))
96
-    )
126
+    rownames(curves) <- as.character(unlist(lapply(cellSummary, function(x) x$cellID)))
97 127
 
98 128
     curves[is.na(curves)] <- 0
99 129
     return(curves)