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

pass stan code from CmdStanArgs -> RunSet -> CmdStanFit #575

Merged
merged 7 commits into from Oct 19, 2021

Conversation

jgabry
Copy link
Member

@jgabry jgabry commented Oct 13, 2021

Submission Checklist

  • Run unit tests
  • Declare copyright holder and agree to license (see below)

Summary

closes #175

Adds $code() method to fitted model objects for returning Stan code. This is accomplished by first passing the Stan code via CmdStanArgs to RunSet and then from RunSet to CmdStanFit.

Copyright and Licensing

Please list the copyright holder for the work you are submitting
(this will be you or your assignee, such as a university or company):
Columbia University

By submitting this pull request, the copyright holder is agreeing to
license the submitted work under the following licenses:

Comment on lines +255 to +256
warning("'$code()' will return NULL because the 'CmdStanModel' was not created with a Stan file.", call. = FALSE)
return(NULL)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed this from an error to a warning and returning NULL. This makes it easier to pass around the Stan code without having to use try() to avoid errors.

#' cat(fit$code(), sep = "\n") # pretty print
#' }
#'
code <- function() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I named the method code() because that's the same name as the CmdStanModel method for returning the code. Alternatively we could use fit$stan_code() instead of fit$code(), but then I would want to do the same for CmdStanModel and deprecate mod$code() in favor of mod$stan_code(). Any preference? I'm open to either (or some other name).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stan_code is unambiguous,. would anyone expect to get c++ code?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think anyone would expect c++ code, so code() is probably fine. That said, stan_code() is definitely unambiguous so I'm open to that too. @rok-cesnovar do you have a preference?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No preference. I dont think anyone would expect C++ honestly, maybe no need to change atm.

code <- function() {
stan_code <- self$runset$stan_code()
if (is.null(stan_code)) {
warning("'$code()' will return NULL because the 'CmdStanModel' was not created with a Stan file.", call. = FALSE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you reconstitute fit object from CSV files, will also not get Stan code, unless you get fancy w/ saving the stan file along with the CSV files.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I don't think we should get too fancy and store the Stan file with the CSV files. I can just add an error message for that case saying that this method isn't available (there are already a few other methods unavailable when recreating from CSV files).

Copy link
Member

@mitzimorris mitzimorris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (note, not really fluent in R)

@codecov-commenter
Copy link

codecov-commenter commented Oct 13, 2021

Codecov Report

Merging #575 (6a9686e) into master (3b3c02f) will increase coverage by 1.16%.
The diff coverage is 93.10%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #575      +/-   ##
==========================================
+ Coverage   91.93%   93.10%   +1.16%     
==========================================
  Files          12       12              
  Lines        3163     3233      +70     
==========================================
+ Hits         2908     3010     +102     
+ Misses        255      223      -32     
Impacted Files Coverage Δ
R/model.R 93.05% <90.90%> (+0.28%) ⬆️
R/args.R 98.63% <100.00%> (+<0.01%) ⬆️
R/csv.R 98.66% <100.00%> (+0.44%) ⬆️
R/fit.R 98.25% <100.00%> (+0.02%) ⬆️
R/run.R 95.73% <100.00%> (+1.65%) ⬆️
R/utils.R 90.27% <0.00%> (+1.38%) ⬆️
R/install.R 70.02% <0.00%> (+5.34%) ⬆️
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b10f2e3...6a9686e. Read the comment docs.

@rok-cesnovar
Copy link
Member

Looks good to me.

@jgabry
Copy link
Member Author

jgabry commented Oct 19, 2021

Ok I think we can go ahead and merge this. Will do that now.

@jgabry jgabry merged commit 622fa94 into master Oct 19, 2021
@jgabry jgabry deleted the save-stan-code-in-fit branch October 19, 2021 19:18
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

Successfully merging this pull request may close these issues.

Include Stan code in resulting fit object
4 participants