Skip to content

Commit c5292a1

Browse files
authored
uncompiled functionality of nimble models (#95)
* add model dollar sign to uncompiled version of calculate methods * update predefined nClasses used for models * clean up predefined Hincludes and some testing * generate full set of calc_one, sim_one, calcDiff_one, getLogProb_one for both stochastic and deterministic * clean up test-nimbleModel for current status
1 parent 8d951ab commit c5292a1

53 files changed

Lines changed: 714 additions & 328 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

nCompiler/NAMESPACE

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export(argType2Cpp)
99
export(build_compiled_nClass)
1010
export(calcInputList_to_calcInstrList)
1111
export(calcInstr_nClass)
12-
export(calcInstrList_nC)
12+
export(calcInstrList_nClass)
1313
export(cloglog)
1414
export(check_Rcpp_for_nCompiler)
1515
export(compileNimble)
@@ -59,7 +59,8 @@ export(logfact)
5959
export(loggam)
6060
export(logit)
6161
export(makeModel_nClass)
62-
export(make_node_fun)
62+
export(make_model_from_nimbleModel)
63+
export(make_node_nClass)
6364
export(method)
6465
export(modelBase_nClass)
6566
export(new.loadedObjectEnv) ## needed for Rcpp::Function access in loadedObjectEnv.h

nCompiler/R/NC_LoadedObjectEnv.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ setup_DLLenv <- function(compiledFuns,
266266
move_funs_from_list_to_env <- function(funNames, funList, env) {
267267
keep <- rep(TRUE, length(funList))
268268
for(funName in funNames) {
269-
found <- grepl(funName, names(funList))
269+
found <- funName == names(funList) #grepl(funName, names(funList))
270270
if(any(found)) {
271271
i <- which(found)
272272
if(length(i) != 1)

nCompiler/R/all_utils.R

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## labelFunctionMetaCreator is only called once, immediately below, to create labelFunctionCreator
44
## The outer layer allows allLabelFunctionCreators to be in the closure of every function returned
55
## by labelFunctionCreator. Each of those functions is registered as an element of allLableFunctionCreators.
6-
##
6+
##
77
## This scheme allows the function resetLabelFunctionCreators below to work simply,
88
## resetting the count to 1 for all of the label generators.
99
##
@@ -48,9 +48,9 @@ modelLabelCreator <- labelFunctionCreator("model")
4848

4949
# no longer documented in Rd
5050
# Generates a valid C++ name from an R Name
51-
#
51+
#
5252
# replaces [ ( $ and a few other symbols with underscores, and removes ] ) and spaces in a string
53-
#
53+
#
5454
# @param rName A String
5555
# @return returns a string representing the modified rName
5656
# @author Jagadish Babu
@@ -74,9 +74,9 @@ Rname2CppName <- function(rName, colonsOK = TRUE) {
7474
paste(rName[grepl(':', rName)], collapse=', ')))
7575
}
7676
rName <- gsub(' ', '', rName)
77-
rName <- gsub('\\.', '_dot_', rName)
77+
rName <- gsub('\\.', '_dot_', rName)
7878
rName <- gsub("\"", "_quote_", rName)
79-
rName <- gsub(',', '_comma_', rName)
79+
rName <- gsub(',', '_comma_', rName)
8080
rName <- gsub("`", "_backtick_" , rName)
8181
rName <- gsub('\\[', '_oB', rName)
8282
rName <- gsub('\\]', '_cB', rName)
@@ -105,7 +105,7 @@ Rname2CppName <- function(rName, colonsOK = TRUE) {
105105
rName <- gsub('\\^', '_tothe_', rName)
106106
rName <- gsub('^_+', '', rName) # remove leading underscores. can arise from (a+b), for example
107107
rName <- gsub('^([[:digit:]])', 'd\\1', rName) # if begins with a digit, add 'd' in front
108-
rName
108+
rName
109109
}
110110

111111
## This takes a character vector as the first argument and length-1
@@ -138,7 +138,7 @@ pasteSemicolon <- function(x, indent = '') {
138138
stop(paste0('Error, pasteSemicolon called for object of class ',
139139
class(x),
140140
'. Must be character or list.'),
141-
call. = FALSE)
141+
call. = FALSE)
142142
}
143143

144144
#' Write unlisted code generated from.nCompiler cpp definitions.
@@ -183,3 +183,36 @@ is.blank <- function(arg) {
183183
if(is.null(arg)) return(FALSE)
184184
return(identical(arg, quote(x[])[[3]]))
185185
}
186+
187+
188+
# Modified from nimble, including comments
189+
# simply adds width.cutoff = 500 as the default to deal with creation of long variable names from expressions
190+
# The control list is the default plus "digits17", which is the only one done in nimble.
191+
# We need to deparse lists (e.g. in build_compiled_nClass) and have the names in the deparsed result.
192+
# I think "niceNames" does that, possibly "showAttributes" too.
193+
deparse <- function(...) {
194+
control <- c("keepNA", "keepInteger", "niceNames", "showAttributes", "digits17")
195+
if("width.cutoff" %in% names(list(...))) {
196+
base::deparse(..., control = control)
197+
} else {
198+
base::deparse(..., width.cutoff = 500L, control = control)
199+
}
200+
}
201+
202+
## This version of deparse avoids splitting into multiple lines, which generally would lead to
203+
## problems. We keep the original nimble:::deparse above as deparse is widely used and there
204+
## are cases where not modifying the nlines behavior may be best.
205+
safeDeparse <- function(..., warn = FALSE) {
206+
out <- deparse(...)
207+
if(isTRUE(get_nOption('useSafeDeparse'))) {
208+
dotArgs <- list(...)
209+
if("nlines" %in% names(dotArgs))
210+
nlines <- dotArgs$nlines else nlines <- 1L
211+
if(nlines != -1L && length(out) > nlines) {
212+
if(warn)
213+
message(" [Note] safeDeparse: truncating deparse output to ", nlines, " line", if(nlines>1) "s" else "")
214+
out <- out[1:nlines]
215+
}
216+
}
217+
return(out)
218+
}

nCompiler/R/compile_aaa_operatorLists.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,15 @@ assignOperatorDef(
507507
updateOperatorDef('max', 'cppOutput', 'cppString', 'maximum')
508508
updateOperatorDef('min', 'cppOutput', 'cppString', 'minimum')
509509

510+
assignOperatorDef(
511+
'invisible',
512+
list(
513+
simpleTransformations = list(
514+
handler = 'RemoveLayer'
515+
)
516+
)
517+
)
518+
510519
assignOperatorDef(
511520
c('pairmin', 'pairmax'),
512521
list(

nCompiler/R/compile_simpleTransformations.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@ simpleTransformationsEnv$minMax <-
5555
if(length(code$args) == 2) code$name <- paste0('pair',code$name)
5656
}
5757

58+
## Used e.g. for invisible(foo(x)) --> foo(x)
59+
simpleTransformationsEnv$RemoveLayer <-
60+
function(code, symTab, auxEnv, info) {
61+
removeExprClassLayer(code)
62+
}
63+
64+
5865
simpleTransformationsEnv$replace <-
5966
function(code, symTab, auxEnv, info) {
6067
repl <- info$replacement

0 commit comments

Comments
 (0)