Browse code

updates to Seq class

- yieldSize rather than length.out
- ANY fields so that class of yield() is consistent with from + by
- reset() method


git-svn-id: file:///home/git/hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/Streamer@68810 bc3139a8-67e5-0310-9ffc-ced21a209358

Martin Morgan authored on 25/08/2012 19:49:53
Showing 6 changed files

... ...
@@ -1,7 +1,7 @@
1 1
 Package: Streamer
2 2
 Type: Package
3 3
 Title: Enabling stream processing of large files
4
-Version: 1.3.7
4
+Version: 1.3.8
5 5
 Author: Martin Morgan, Nishant Gopalakrishnan
6 6
 Maintainer: Martin Morgan <mtmorgan@fhcrc.org>
7 7
 Description: Large data files can be difficult to work with in R,
... ...
@@ -1,39 +1,35 @@
1 1
 .Seq <- setRefClass("Seq",
2 2
     fields = list(
3
-      from="numeric", to="numeric", by="numeric", length.out="integer"),
3
+      .start="ANY",
4
+      from="ANY", to="ANY", by="ANY", yieldSize="integer"),
4 5
     contains="Producer",
5 6
     methods = list(
6
-      initialize = function(..., length.out) {
7
-          ## increment length.out to avoid repeated addition
8
-          callSuper(..., length.out=as.integer(length.out) + 1L)
7
+      initialize = function(..., yieldSize) {
8
+          ## increment yieldSize to avoid repeated addition
9
+          callSuper(..., yieldSize=as.integer(yieldSize) + 1L)
10
+      },
11
+      reset = function() {
12
+          from <<- .start
13
+          invisible(.self)
9 14
       },
10 15
       yield = function() {
11 16
           if ((from - to) * by > 0)
12 17
               return(numeric())
13
-          s <- seq(from, by=by, length.out=length.out)
14
-          from <<- as.numeric(s[length(s)])
18
+          s <- seq(from, by=by, length.out=yieldSize)
19
+          from <<- s[length(s)]
15 20
           s <- s[-length(s)]
16 21
           s[s <= to]
17 22
       },
18 23
       show = function() {
19 24
           cat("from:", from, "\nto:", to, "\nby:", by,
20
-              "\nlength.out:", length.out - 1L, "\n")
25
+              "\nyieldSize:", yieldSize - 1L, "\n")
21 26
       }))
22 27
 
23 28
 Seq <-
24
-    function(from=1L, to, by=1L, length.out=1L, ...)
29
+    function(from=1L, to=.Machine$integer.max, by=1L, yieldSize=1L,
30
+             ...)
25 31
 {
26
-    if (missing(to)) {
27
-        to <-
28
-            if (is.integer(from)) {
29
-                .Machine$integer.max
30
-            } else Inf
31
-        if (by < 0)
32
-            to <- -to
33
-    }
34
-
35
-    if (from > to)
36
-        stop("'from' must be less than or equal to 'to'")
37
-    length.out <- as.integer(length.out)
38
-    .Seq$new(from=from, to=to, by=by, length.out=length.out, ...)
32
+    yieldSize <- as.integer(yieldSize)
33
+    .Seq$new(.start=from, from=from, to=to, by=by,
34
+             yieldSize=yieldSize, ...)
39 35
 }
... ...
@@ -4,20 +4,20 @@ test_lapply_Producer <- function()
4 4
     exp <- unname(lapply(vals, mean))
5 5
 
6 6
     ## function
7
-    obs <- lapply(Seq(to=47, length.out=7), mean)
7
+    obs <- lapply(Seq(to=47, yieldSize=7), mean)
8 8
     checkIdentical(exp, obs)
9 9
 
10 10
     ## anonymous function
11
-    obs <- lapply(Seq(to=47, length.out=7), function(x) mean(x))
11
+    obs <- lapply(Seq(to=47, yieldSize=7), function(x) mean(x))
12 12
     checkIdentical(exp, obs)
13 13
 
14 14
     ## ... args
15
-    obs <- lapply(Seq(to=47, length.out=7), function(x, z) mean(z), z=1:10)
15
+    obs <- lapply(Seq(to=47, yieldSize=7), function(x, z) mean(z), z=1:10)
16 16
     checkIdentical(mean(1:10), unique(unlist(obs)))
17 17
 
18 18
     ## env
19 19
     ZZZ <- 1:10
20
-    res <- lapply(Seq(to=47, length.out=7), function(x) mean(ZZZ))
20
+    res <- lapply(Seq(to=47, yieldSize=7), function(x) mean(ZZZ))
21 21
     checkIdentical(mean(ZZZ), unique(unlist(res)))
22 22
 
23 23
     ## error
... ...
@@ -25,10 +25,10 @@ test_lapply_Producer <- function()
25 25
     checkException(lapply(Seq(to=5), fun), silent=TRUE)
26 26
 
27 27
     ## error partial results
28
-    res <- tryCatch(sapply(Seq(to=5), fun), error = function(err) {
28
+    res <- tryCatch(sapply(Seq(to=5L), fun), error = function(err) {
29 29
         err$partialResult
30 30
     })
31
-    checkIdentical(c(1, 2), res)
31
+    checkIdentical(1:2, res)
32 32
 
33 33
     ## trigger re-allocation
34 34
     ## res <- lapply(Seq(to=4096*4),
... ...
@@ -96,7 +96,7 @@
96 96
 showClass("Producer")
97 97
 showMethods(class="Producer", where="package:Streamer")
98 98
 
99
-sapply(Seq(to=47, length.out=7), function(elt) {
99
+sapply(Seq(to=47, yieldSize=7), function(elt) {
100 100
     c(n = length(elt), xbar = mean(elt))
101 101
 })
102 102
 
... ...
@@ -13,21 +13,23 @@
13 13
 
14 14
 }
15 15
 
16
-\usage{Seq(from = 1L, to, by = 1L, length.out=1L, ...)}
16
+\usage{
17
+Seq(from = 1L, to=.Machine$integer.max, by = 1L, yieldSize=1L,
18
+    ...)
19
+}
17 20
 
18 21
 \arguments{
19 22
 
20
-  \item{from}{A \code{numeric(1)} starting value.}
21
-
22
-  \item{to}{A \code{numeric(1)} ending value. If missing, it is set to
23
-    the maximum (or minimum, depending on \code{by}) value representable
24
-    for the class of \code{from}, creating an (effectively) infinite
25
-    stream.}
23
+  \item{from}{A starting value of any type (e.g., \code{integer},
24
+    \code{numeric} supported by \code{base::seq}.}
26 25
 
27
-  \item{by}{A \code{numeric(1)} indicating the increment between
28
-    successive numbers in the sequence.}
26
+  \item{to}{An ending value, typically of the same type as \code{from}.}
27
+  
28
+  \item{by}{A value, typically of the same class as \code{from},
29
+    indicating the increment between successive numbers in the
30
+    sequence. \code{by = 0} can create an infinite stream.}
29 31
 
30
-  \item{length.out}{A \code{integer(1)} indicating the length of the
32
+  \item{yieldSize}{A \code{integer(1)} indicating the length of the
31 33
     output sequence each time \code{yield()} is invoked.}
32 34
 
33 35
   \item{...}{Additional arguments passed to \code{\linkS4class{Producer}}.}
... ...
@@ -38,24 +40,15 @@
38 40
   Use \code{Seq} to construct instances of this class.
39 41
 }
40 42
 
41
-\section{Fields}{
42
-  Fields of this class are
43
-  \describe{
44
-    \item{\code{from}:}{\code{numeric(1)} (current) sequence starting value.}
45
-    \item{\code{to}:}{\code{numeric(1)} sequence ending value.}
46
-    \item{\code{by}:}{\code{numeric(1)} sequence increment value.}
47
-    \item{\code{length.out}:}{\code{integer(1)} sequence length on yield.}
48
-  }
49
-}
43
+\section{Internal Class Fields and Methods}{
50 44
 
51
-\section{Methods}{
52
-  Methods of this class are
53
-  \describe{
45
+  Internal fields of this class are are described with
46
+  \code{getRefClass("Seq")$fields}.
54 47
 
55
-    \item{\code{yield()}:}{A sequence as specified by current
56
-      \code{from}, \code{to}, \code{by}, and \code{length.out} values.}
48
+  Internal methods of this class are described with
49
+  \code{getRefClass("Seq")$methods()} and
50
+  \code{getRefClass("Seq")$help()}.
57 51
 
58
-  }
59 52
 }
60 53
 
61 54
 \author{Martin Morgan \url{mtmorgan@fhcrc.org}}
... ...
@@ -63,7 +56,7 @@
63 56
 \seealso{\code{\link{stream}}}
64 57
 
65 58
 \examples{
66
-s <- Seq(1, 10, length.out=5)
59
+s <- Seq(1, 10, yieldSize=5)
67 60
 while(length(y <- yield(s)))
68 61
     print(y)
69 62
 }
... ...
@@ -110,7 +110,7 @@
110 110
 \examples{
111 111
 if (.Platform$OS.type != "windows") {
112 112
     team <- Team(function(x) { Sys.sleep(1); mean(x) }, size=5)
113
-    s <- stream(Seq(to=50, length.out=5), team)
113
+    s <- stream(Seq(to=50, yieldSize=5), team)
114 114
     system.time({while(length(y <- yield(s)))
115 115
         print(y)
116 116
     })