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

data = list() causes expecting JSON error #401

Closed
bob-carpenter opened this issue Dec 14, 2020 · 6 comments · Fixed by #403
Closed

data = list() causes expecting JSON error #401

bob-carpenter opened this issue Dec 14, 2020 · 6 comments · Fixed by #403
Labels
bug Something isn't working

Comments

@bob-carpenter
Copy link
Contributor

Describe the bug

Empty data list causes JSON error.

To Reproduce

race.stan:

functions {
  real foo(int[] y, int start, int end) {
    print("hello, start = ", start, ";  end = ", end);
    return 0;
  }
}
transformed data {
  int N = 1000000;
  int y[N] = rep_array(1, N);
  int granularity = 1;
}
parameters {
  real<lower = 0, upper = 1> theta;
}
model {
  target += reduce_sum(foo, y, granularity);
}

race.R:

library('cmdstanr')
m <- cmdstan_model('race.stan',
                   cpp_options = list(stan_threads = TRUE))
fit <- m$sample(data = list(a = 5),
                chains = 1, threads_per_chain = 1,
                iter_warmup = 100, iter_sampling = 100)

In R:

> source('race.R')
...
Running MCMC with 1 chain, with 1 thread(s) per chain...

Chain 1 expecting JSON object, found array 
Warning: Chain 1 finished unexpectedly!

Warning message:
No chains finished successfully. Unable to retrieve the fit. 

If I change the data line in the script to data = list(a = 1) for a non-existent data item then it runs just fine.

Expected behavior

It treats an empty list as an empty list rather than trying to convert to JSON.

Operating system

> sessionInfo()
R version 4.0.1 (2020-06-06)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] cmdstanr_0.2.2

loaded via a namespace (and not attached):
[1] processx_3.4.5  compiler_4.0.1  backports_1.1.8 R6_2.4.1       
[5] tools_4.0.1     checkmate_2.0.0 jsonlite_1.6.1  ps_1.3.3     
@bob-carpenter bob-carpenter added the bug Something isn't working label Dec 14, 2020
@rok-cesnovar
Copy link
Member

Thanks. This is because jsonlite, which we use to write the json file, saves [ ] to a file when you pass it an empty list.

For cmdstan to work it should be an empty file, no file or { }. Cmdstan's input parser does not accept [].

jsonlite::write_json(
    list(),
    path = file,
    auto_unbox = TRUE,
    factor = "integer",
    digits = NA,
    pretty = TRUE
  )

stores
[ ]

@jgabry
Copy link
Member

jgabry commented Dec 14, 2020

@bob-carpenter I think the code you shared is actually the code that runs fine, not the code that produces the warning because you included list(a=5) instead of list(), right? Or am I confused?

# this code you had doesn't result in the error/warning you showed below it
fit <- m$sample(data = list(a = 5),
                chains = 1, threads_per_chain = 1,
                iter_warmup = 100, iter_sampling = 100)

But if I go ahead and change your example to use list() instead of list(a=5) then I can reproduce it.

I think I just assumed that if a model had no data the data argument would be omitted (which works fine), but I definitely agree that it should still work if it's specified as an empty list. Should be an easy fix (@rok-cesnovar we can just immediately convert an empty list to NULL and it should work as if the argument had been omitted).

@rok-cesnovar
Copy link
Member

we can just immediately convert an empty list to NULL

Agree.

@bob-carpenter
Copy link
Contributor Author

I think the code you shared is actually the code that runs fine

Yup. My bad. I meant data = list() causes problems.

@jgabry
Copy link
Member

jgabry commented Dec 14, 2020

I meant data = list() causes problems.

Ok yeah I can definitely fix that today. Thanks for pointing it out.

jgabry added a commit that referenced this issue Dec 14, 2020
@jgabry jgabry mentioned this issue Dec 14, 2020
2 tasks
@jgabry
Copy link
Member

jgabry commented Dec 14, 2020

Ok this should be fixed by #403 as soon as it's merged. Thanks again for reporting this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants