Skip to content

Fix error with off by one indexing #291

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

Merged
merged 8 commits into from
Aug 26, 2020
Merged

Conversation

johnlees
Copy link
Contributor

Submission Checklist

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

Summary

There appears to be an off-by-one indexing issue in R vs C++. The default index of 0 found in the metadata is invalid for R lists which are 1-indexed. This leads to the following error, which is difficult for the user to comprehend:

read_cmdstan_csv('cmdstan.run0.csv')
Error in inv_metric[[metadata$id]] <- metadata$inv_metric :
  attempt to select less than one element in integerOneIndex

The right solution to me would seem to be to convert the index into R's 1-index form, where they are used in this manner.

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):
John Lees

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

@johnlees
Copy link
Contributor Author

There are some checks on the length of the MCMC failing. I'm not sure what the best way to resolve these are without just hacking them. The files included in tests/testthat/resources/csv/ all have correct indices for R's 1-based indexing, so perhaps this is only a problem when running cmdstan from the command line?

I think this is worth looking at, because running cmdstan as in the manual, followed by rcmdstan gives this error. Perhaps adding an 'index_adjust' option to read_cmdstan_csv() might be better?

@jgabry
Copy link
Member

jgabry commented Aug 24, 2020

Thanks for reporting this. I just opened an issue (#292) about this and I'll respond more over there (so we can have the discussion in a separate issue in case this particular PR doesn't end up being the solution).

@rok-cesnovar
Copy link
Member

@johnlees was there an issue if you dealt with it as a character? If some tests failed, those tests might need a revisit?

@johnlees
Copy link
Contributor Author

Yes, sorry I was getting:

  ── 2. Error: read_cmdstan_csv() reads adaptation step size correctly (@test-csv.
  subscript out of bounds
  Backtrace:
   1. testthat::expect_equal(csv_out$step_size[[2]], 0.672434)
   2. testthat::quasi_label(enquo(object), label, arg = "object")
   3. rlang::eval_bare(expr, quo_get_env(quo))
  
  ══ testthat results  ═══════════════════════════════════════════════════════════
  [ OK: 868 | SKIPPED: 3 | WARNINGS: 2 | FAILED: 2 ]
  1. Error: read_cmdstan_csv() returns correct diagonal of inverse mass matrix (@test-csv.R#165) 
  2. Error: read_cmdstan_csv() reads adaptation step size correctly (@test-csv.R#376) 

I think changing to csv_out$step_size[[as.character(2)]] would work. I will try that! (sorry for the CI use, I don't have a good setup available to me to run the tests locally)

@johnlees
Copy link
Contributor Author

@rok-cesnovar An error in the test coverage, but otherwise passes. Does this fix look like what you envisioned?

@rok-cesnovar
Copy link
Member

sorry for the CI use, I don't have a good setup available to me to run the tests locally

No problem at all! Its free Github instances :)

Thanks for working on this!

@rok-cesnovar
Copy link
Member

Does this fix look like what you envisioned?

I think so yes. Will take a look shortly.

@jgabry
Copy link
Member

jgabry commented Aug 25, 2020

It looks like the test coverage failure is due to the tests for install_cmdstan(), which is unrelated to this PR. @rok-cesnovar maybe this is because of the 2.24.1 patch release?

@rok-cesnovar
Copy link
Member

Argh, yes. https://github.com/stan-dev/cmdstanr/blob/master/tests/testthat/helper-envvars-and-paths.R#L18 needs to be updated. Want to push to this PR or master @jgabry? I wont be at a computer till tomorrow morning. Can do it then.

@jgabry
Copy link
Member

jgabry commented Aug 25, 2020

Yeah, no problem, I can fix that now.

@codecov-commenter
Copy link

codecov-commenter commented Aug 25, 2020

Codecov Report

Merging #291 into master will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #291   +/-   ##
=======================================
  Coverage   90.11%   90.11%           
=======================================
  Files          12       12           
  Lines        2419     2419           
=======================================
  Hits         2180     2180           
  Misses        239      239           
Impacted Files Coverage Δ
R/read_csv.R 96.28% <100.00%> (ø)

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 35c7668...cdeccde. Read the comment docs.

Copy link
Member

@jgabry jgabry left a comment

Choose a reason for hiding this comment

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

@johnlees Looks good, thanks! I'm approving but will wait to merge in case @rok-cesnovar notices anything I missed.

@rok-cesnovar rok-cesnovar merged commit 4a218da into stan-dev:master Aug 26, 2020
@rok-cesnovar
Copy link
Member

Thanks @johnlees!

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.

4 participants