Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mrgsolve/drake Incompatibility? #471

Closed
billdenney opened this issue Mar 20, 2019 · 4 comments
Closed

mrgsolve/drake Incompatibility? #471

billdenney opened this issue Mar 20, 2019 · 4 comments

Comments

@billdenney
Copy link
Contributor

When using drake with mrgsolve, I just got the following error:

There was a problem accessing the model shared object.
  Either the model object is corrupted or  
  the model was not properly compiled and/or loaded.
Check mrgsolve:::funset(mod) for more information.
> mrgsolve::funset(readd(model))
Error: 'funset' is not an exported object from 'namespace:mrgsolve'
> mrgsolve:::funset(readd(model))
$`symbols`
  name loaded
1 <NA>  FALSE
2 <NA>  FALSE
3 <NA>  FALSE
4 <NA>  FALSE

$shlib
            package version compiled
1 pku-so-3549ba2a85   0.9.0     TRUE

> 

Both mrgsolve and drake do magical things with R, and it appears their magic is incompatible in at least this instance.

To reproduce this requires more complexity than a standard reproducible example.

  1. Start R
  2. source("run1.txt")
  3. Quit R
  4. Start R
  5. source("run2.txt")

run1.txt
run2.txt

@kylebaron
Copy link
Collaborator

Try this:

library(mrgsolve)
options(mrgsolve.soloc = "i_love_drake")
mod <- mread_cache("my_model_file.cpp")

I don't know how to use drake; but set the option and use mread_cache rather than mread ... or mcode_cache rather than mcode. I use this in my everyday (drake-less) workflow now.

If that still doesn't work try adding loadso(mod) after the mread_cache call; that will force the loading of the shared object.

@billdenney
Copy link
Contributor Author

loadso() fixes it, if I manually run it. drake works to only re-run code that has changed while tracking dependencies, so mread() or mcode() isn't run if the model is already current (i.e. the model code hasn't changed). But, I changed my dataset and resimulated, so mrgsim was called in a session where mcode() wasn't called.

Would it be reasonable to add an attempt to load the shared object, if not already loaded, before throwing the error here:

mrgsolve/R/funset.R

Lines 71 to 76 in ea18191

pointers <- function(x) {
if(!funs_loaded(x)) stop(FUNSET_ERROR__)
what <- funs(x)
ans <- getNativeSymbolInfo(what,PACKAGE=dllname(x))
setNames(lapply(ans, "[[","address"),names(what))
}

(And anywhere else that may be an issue)

This code appeared to work:

library(mrgsolve)
library(drake)

model_text <- "
$PARAM @annotated
CL   :  1 : Clearance (volume/time)
V    : 20 : Central volume (volume)
KA   :  1 : Absorption rate constant (1/time)

$CMT  @annotated
EV   : Extravascular compartment
CENT : Central compartment

$GLOBAL
#define CP (CENT/V)

$PKMODEL ncmt = 1, depot = TRUE

$CAPTURE @annotated
CP : Plasma concentration (mass/volume)
"

mrgsim_load <- function(x, ...) {
  loadso(x)
  mrgsim(x, ...)
}

my_plan <-
  drake_plan(
    model=mcode_cache(model_text, model="pk1"),
    my_idata=data.frame(ID=2),
    sim_result=mrgsim_load(model, idata=my_idata),
    strings_in_dots="literals"
  )

make(my_plan)
as.data.frame(readd(sim_result))

@billdenney
Copy link
Contributor Author

Also, drake is one of my favorite new (to me) R tools. Especially for long-running projects (such as model fitting or larger mrgsolve simulations), it lets me iterate on the reporting side without rebuilding everything from scratch or worrying about dependencies or stale outputs (like saved files).

https://ropensci.org/technotes/2019/03/18/drake-700/

@kylebaron
Copy link
Collaborator

Thanks for the report. I'll open tickets to fix the funset output (not sure why the names are not showing up there). Also, will add pointer to try loadso when the model functions are not loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants