File: NMFStrategyFunction-class.R

package info (click to toggle)
r-cran-nmf 0.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,344 kB
  • sloc: cpp: 680; ansic: 7; makefile: 2
file content (69 lines) | stat: -rw-r--r-- 2,472 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#' @include NMFStrategy-class.R
NULL

#' Interface for Single Function NMF Strategies 
#'
#' This class implements the virtual interface \code{\link{NMFStrategy}} for 
#' NMF algorithms that are implemented by a single workhorse R function. 
#' 
#' @slot algorithm a function that implements an NMF algorithm.
#' It must have signature \code{(y='matrix', x='NMFfit')}, where \code{y} is the 
#' target matrix to approximate and \code{x} is the NMF model assumed to be  
#' seeded with an appropriate initial value -- as it is done internally by 
#' function \code{\link{nmf}}.
#' 
#' Note that argument names currently do not matter, but it is recommended to 
#' name them as specified above.
#' 
setClass('NMFStrategyFunction'
	, representation(
		algorithm = 'function' # the function that implements the algorithm				
	)
	, contains = 'NMFStrategy'
)
#' Runs the NMF algorithms implemented by the single R function -- and stored in slot \code{'algorithm'} 
#' of \code{object}, on the data object \code{y}, using \code{x} as starting point.
#' It is equivalent to calling \code{object@@algorithm(y, x, ...)}.
#' 
#' This method is usually not called directly, but only via the function \code{\link{nmf}}, which 
#' takes care of many other details such as seeding the computation, handling RNG settings, or setting up 
#' parallelisation.
#' 
#' @rdname NMFStrategy
setMethod('run', signature(object='NMFStrategyFunction', y='matrix', x='NMFfit'),
	function(object, y, x, ...){
		if( !is.function(fun <- algorithm(object)) )  
			stop("NMFStrategyFunction '", name(object), "': algorithm is not defined.")
		
		# run the function that defines the algorithm and return the result
		fun(y, x, ...)
	}
)

#' @export
nmfFormals.NMFStrategyFunction <- function(x, ...){
	args <- formals(x@algorithm)
	args[-(1:2)]
}
	

#' Returns the single R function that implements the NMF algorithm -- as stored in 
#' slot \code{algorithm}.
setMethod('algorithm', signature(object='NMFStrategyFunction'),
	function(object){
		slot(object, 'algorithm')
	}
)
#setReplaceMethod('algorithm', signature(object='NMFStrategyFunction', value='character'),
#	function(object, value){
#		slot(object, 'algorithm') <- value
#		object
#	}
#)
#' Sets the function that implements the NMF algorithm, stored in slot \code{algorithm}. 
setReplaceMethod('algorithm', signature(object='NMFStrategyFunction', value='function'),
	function(object, value){
		slot(object, 'algorithm') <- value
		object
	}
)