cmdstanr icon indicating copy to clipboard operation
cmdstanr copied to clipboard

cmdstanr::sample() saves incorrect path to csv files, Windows

Open AdamCSmithCWS opened this issue 6 months ago • 2 comments

When output_dir is set to a full directory path including the drive letter e.g., cmdstanr::sample(..., output_dir = "c:/output"), and using wsl (install_cmdstan(..., wsl = TRUE)), the function does not save the correct path. It seems to mangle the beginning of the path by adding "/mnt/" and dropping the colon separator.

To Reproduce Running the example from ?cmdstanr::sample().

library(cmdstanr)
This is cmdstanr version 0.9.0
- CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
- CmdStan path: //wsl$/ECCC-WSL-UBUNTU-24/home/acsmith/.cmdstan/cmdstan-2.36.0
- CmdStan version: 2.36.0

file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.stan")
mod <- cmdstan_model(file)
Model executable is up to date!
 
# Data as a named list (like RStan)
stan_data <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1))
 
# Running example model with output_dir set as full path to drive
  
drive <- "c:/gitHub"
 
dir.exists(drive)
[1] TRUE
 
fit_mcmc <- mod$sample(
+   data = stan_data,
+   seed = 123,
+   chains = 2,
+   parallel_chains = 2,
+   output_dir = drive
+ )
Running MCMC with 2 parallel chains...

Chain 1 Iteration:    1 / 2000 [  0%]  (Warmup) 
Chain 1 Iteration:  100 / 2000 [  5%]  (Warmup) 
Chain 1 Iteration:  200 / 2000 [ 10%]  (Warmup) 
Chain 1 Iteration:  300 / 2000 [ 15%]  (Warmup) 
Chain 1 Iteration:  400 / 2000 [ 20%]  (Warmup) 
Chain 1 Iteration:  500 / 2000 [ 25%]  (Warmup) 
Chain 1 Iteration:  600 / 2000 [ 30%]  (Warmup) 
Chain 1 Iteration:  700 / 2000 [ 35%]  (Warmup) 
Chain 1 Iteration:  800 / 2000 [ 40%]  (Warmup) 
Chain 1 Iteration:  900 / 2000 [ 45%]  (Warmup) 
Chain 1 Iteration: 1000 / 2000 [ 50%]  (Warmup) 
Chain 1 Iteration: 1001 / 2000 [ 50%]  (Sampling) 
Chain 1 Iteration: 1100 / 2000 [ 55%]  (Sampling) 
Chain 1 Iteration: 1200 / 2000 [ 60%]  (Sampling) 
Chain 1 Iteration: 1300 / 2000 [ 65%]  (Sampling) 
Chain 1 Iteration: 1400 / 2000 [ 70%]  (Sampling) 
Chain 1 Iteration: 1500 / 2000 [ 75%]  (Sampling) 
Chain 1 Iteration: 1600 / 2000 [ 80%]  (Sampling) 
Chain 2 Iteration:    1 / 2000 [  0%]  (Warmup) 
Chain 2 Iteration:  100 / 2000 [  5%]  (Warmup) 
Chain 2 Iteration:  200 / 2000 [ 10%]  (Warmup) 
Chain 2 Iteration:  300 / 2000 [ 15%]  (Warmup) 
Chain 2 Iteration:  400 / 2000 [ 20%]  (Warmup) 
Chain 2 Iteration:  500 / 2000 [ 25%]  (Warmup) 
Chain 2 Iteration:  600 / 2000 [ 30%]  (Warmup) 
Chain 2 Iteration:  700 / 2000 [ 35%]  (Warmup) 
Chain 2 Iteration:  800 / 2000 [ 40%]  (Warmup) 
Chain 2 Iteration:  900 / 2000 [ 45%]  (Warmup) 
Chain 2 Iteration: 1000 / 2000 [ 50%]  (Warmup) 
Chain 2 Iteration: 1001 / 2000 [ 50%]  (Sampling) 
Chain 2 Iteration: 1100 / 2000 [ 55%]  (Sampling) 
Chain 2 Iteration: 1200 / 2000 [ 60%]  (Sampling) 
Chain 2 Iteration: 1300 / 2000 [ 65%]  (Sampling) 
Chain 2 Iteration: 1400 / 2000 [ 70%]  (Sampling) 
Chain 1 Iteration: 1700 / 2000 [ 85%]  (Sampling) 
Chain 1 Iteration: 1800 / 2000 [ 90%]  (Sampling) 
Chain 1 Iteration: 1900 / 2000 [ 95%]  (Sampling) 
Chain 1 Iteration: 2000 / 2000 [100%]  (Sampling) 
Chain 2 Iteration: 1500 / 2000 [ 75%]  (Sampling) 
Chain 2 Iteration: 1600 / 2000 [ 80%]  (Sampling) 
Chain 2 Iteration: 1700 / 2000 [ 85%]  (Sampling) 
Chain 2 Iteration: 1800 / 2000 [ 90%]  (Sampling) 
Chain 1 finished in 0.3 seconds.
Chain 2 Iteration: 1900 / 2000 [ 95%]  (Sampling) 
Chain 2 Iteration: 2000 / 2000 [100%]  (Sampling) 
Chain 2 finished in 0.3 seconds.

Both chains finished successfully.
Mean chain execution time: 0.3 seconds.
Total execution time: 0.7 seconds.

 
output_file_saved <- fit_mcmc$output_files()
 file.exists(output_file_saved)
[1] FALSE FALSE
output_file_saved
[1] "/mnt/c/gitHub/bernoulli-202508281056-1-58343f.csv"
[2] "/mnt/c/gitHub/bernoulli-202508281056-2-58343f.csv"

Operating system Windows 10, sysname release version
"Windows" "10 x64" "build 26100"

CmdStanR version number

  • CmdStan path: //wsl$/ECCC-WSL-UBUNTU-24/home/acsmith/.cmdstan/cmdstan-2.36.0
  • CmdStan version: 2.36.0

Additional context Not sure if this is something new related to a recent Windows or WSL update. This error only started showing up for me recently. I have recreated this error on three machines, each running a different wsl version, but all running the same Windows 10 version.

AdamCSmithCWS avatar Aug 28 '25 15:08 AdamCSmithCWS

Thanks for reporting. Tagging @andrjohns, who knows a lot more about WSL than I do.

jgabry avatar Aug 28 '25 20:08 jgabry

One of my colleagues pointed out that it may be just a small bug in output_files().

save_object() still works.

Perhaps it's a simple as ensuring that when using wsl, output_files() needs to translate the path into a format suitable for Windows.

AdamCSmithCWS avatar Aug 29 '25 11:08 AdamCSmithCWS