Skip to content
This repository
Browse code

Added regexp option for spotter and test_spotter to test coverage

  • Loading branch information...
commit 2d0260e9558229a4e7b50b9e830c44eb5ed76edc 1 parent 220201b
authored March 17, 2012
1  NAMESPACE
@@ -19,5 +19,6 @@ export(scrub_keds)
19 19
 export(sources)
20 20
 export(spotter)
21 21
 export(targets)
  22
+export(test_spotter)
22 23
 S3method(summary,eventdata)
23 24
 S3method(summary,eventscale)
57  R/events.R
... ...
@@ -1,19 +1,56 @@
1  
-##' Hands back a function to spot the items it was given in (\code{...})
2  
-##'
3  
-##' This is a convenience function for creates a function that returns
4  
-##' true for exact matches to its arguments.
5  
-##' 
6  
-##' @title Make a spotting function
7  
-##' @param ... The items for which the new function should return \code{TRUE}
8  
-##' @return A function 
  1
+##' Creates spotter function
  2
+##'
  3
+##' This is a convenience function that creates and returns a function which returns
  4
+##' TRUE when any of its arguments match one of the patterns given here. 
  5
+##' When \code{re} is TRUE,
  6
+##' arguments to this function are matched as regular expressions in the returned 
  7
+##' function using the base R function \code{grepl} with its default parameters.  
  8
+##' These currently imply no PERL-style regexps,
  9
+##' case sensitivity, and character rather than byte-based matching.
  10
+##' When \code{re} is FALSE any exact match is sufficient for the returned function
  11
+##' to return TRUE.  
  12
+##'
  13
+##' @title Make a spotter function
  14
+##' @param ... Patterns, matches to which the returned function should return \code{TRUE}
  15
+##' @return A spotter function 
9 16
 ##' @export
10 17
 ##' @author Will Lowe
11  
-spotter <- function(...){
  18
+spotter <- function(..., re=TRUE){
12 19
   lst <- unlist(list(...))
13  
-  f <- function(x){x %in% lst}
  20
+  if (re){
  21
+    f <- function(x){ any(sapply(lst, function(p){ grepl(p, x) })) } 
  22
+  } else {
  23
+    f <- function(x){ x %in% lst }
  24
+  }
14 25
   return(f)
15 26
 }
16 27
 
  28
+##' Tests the coverage of a spotter function
  29
+##'
  30
+##' This is a test function to runs a spotter function over a set of strings
  31
+##' and reports which strings are spotted and which are not.  It's mostly useful
  32
+##' to ensure that any regular expressions given to \code{spotter} (i.e. when
  33
+##' re=TRUE) have the 
  34
+##' right coverage before using the spotter function to select events from your data.
  35
+##' 
  36
+##' @title Test spotter function coverage
  37
+##' @param spotter a spotter function
  38
+##' @param data a vector of character test data
  39
+##' @param unspotted whether to also list strings to which the spotter returns FALSE
  40
+##' @return the elements of \code{data} that the spotter spots, i.e. returns TRUE to, and
  41
+##'         also optionally the elements which the spotter ignores, i.e. returns FALSE to.
  42
+##' @export
  43
+##' @author Will Lowe
  44
+test_spotter <- function(spotter, data, unspotted=TRUE){
  45
+  sp <- sapply(data, spotter)
  46
+  spotted <- data[sp]
  47
+  notspotted <- data[!sp]
  48
+
  49
+  cat("Spots:   ", paste(spotted[order(spotted)]), "\n")
  50
+  if (unspotted)
  51
+    cat("Ignores: ", paste(notspotted[order(notspotted)]), "\n") 
  52
+}
  53
+
17 54
 ##' Removes well-known noise from KEDS output files
18 55
 ##'
19 56
 ##' This function applies the regular expression based cleaning routine 
2  man/balkans.weis.Rd
@@ -16,5 +16,5 @@
16 16
   \url{http://web.ku.edu/~keds/data.dir/balk.html}
17 17
 }
18 18
 \keyword{data}
19  
-\keyword{dataset}
  19
+\keyword{datasets}
20 20
 
2  man/cameo.scale.Rd
@@ -18,5 +18,5 @@
18 18
   \url{http://web.ku.edu/~keds/}
19 19
 }
20 20
 \keyword{data}
21  
-\keyword{dataset}
  21
+\keyword{datasets}
22 22
 
25  man/spotter.Rd
... ...
@@ -1,23 +1,30 @@
1 1
 \name{spotter}
2 2
 \alias{spotter}
3  
-\title{Make a spotting function}
  3
+\title{Make a spotter function}
4 4
 \usage{
5  
-  spotter(...)
  5
+  spotter(..., re = TRUE)
6 6
 }
7 7
 \arguments{
8  
-  \item{...}{The items for which the new function should
9  
-  return \code{TRUE}}
  8
+  \item{...}{Patterns, matches to which the returned
  9
+  function should return \code{TRUE}}
10 10
 }
11 11
 \value{
12  
-  A function
  12
+  A spotter function
13 13
 }
14 14
 \description{
15  
-  Hands back a function to spot the items it was given in
16  
-  (\code{...})
  15
+  Creates spotter function
17 16
 }
18 17
 \details{
19  
-  This is a convenience function for creates a function
20  
-  that returns true for exact matches to its arguments.
  18
+  This is a convenience function that creates and returns a
  19
+  function which returns TRUE when any of its arguments
  20
+  match one of the patterns given here. When \code{re} is
  21
+  TRUE, arguments to this function are matched as regular
  22
+  expressions in the returned function using the base R
  23
+  function \code{grepl} with its default parameters. These
  24
+  currently imply no PERL-style regexps, case sensitivity,
  25
+  and character rather than byte-based matching. When
  26
+  \code{re} is FALSE any exact match is sufficient for the
  27
+  returned function to return TRUE.
21 28
 }
22 29
 \author{
23 30
   Will Lowe
34  man/test_spotter.Rd
... ...
@@ -0,0 +1,34 @@
  1
+\name{test_spotter}
  2
+\alias{test_spotter}
  3
+\title{Test spotter function coverage}
  4
+\usage{
  5
+  test_spotter(spotter, data, unspotted = TRUE)
  6
+}
  7
+\arguments{
  8
+  \item{spotter}{a spotter function}
  9
+
  10
+  \item{data}{a vector of character test data}
  11
+
  12
+  \item{unspotted}{whether to also list strings to which
  13
+  the spotter returns FALSE}
  14
+}
  15
+\value{
  16
+  the elements of \code{data} that the spotter spots, i.e.
  17
+  returns TRUE to, and also optionally the elements which
  18
+  the spotter ignores, i.e. returns FALSE to.
  19
+}
  20
+\description{
  21
+  Tests the coverage of a spotter function
  22
+}
  23
+\details{
  24
+  This is a test function to runs a spotter function over a
  25
+  set of strings and reports which strings are spotted and
  26
+  which are not.  It's mostly useful to ensure that any
  27
+  regular expressions given to \code{spotter} (i.e. when
  28
+  re=TRUE) have the right coverage before using the spotter
  29
+  function to select events from your data.
  30
+}
  31
+\author{
  32
+  Will Lowe
  33
+}
  34
+
2  man/weis.goldstein.scale.Rd
@@ -17,5 +17,5 @@
17 17
   \url{http://web.ku.edu/~keds/}
18 18
 }
19 19
 \keyword{data}
20  
-\keyword{dataset}
  20
+\keyword{datasets}
21 21
 

0 notes on commit 2d0260e

Please sign in to comment.
Something went wrong with that request. Please try again.