Skip to content

Bug with saving model object to a network drive with UNC path #1026

Closed
@bschneidr

Description

@bschneidr

Describe the bug
I'm working in a fairly common situation where I have to use a Windows machine and store data on a network drive accessed by UNC paths (e.g. using paths such as "\\westat.com\projects\my-project-folder\data.csv").

I'm able to call model_fit$save_output_files() to save my CSV files of MCMC draws to the network folder. But when I try to save the model object (class CmdStanMCMC) using model_fit$save_object(), I get an error message saying that the CSV files don't exist.

I suspect that cmdstanr/cmdstan are having trouble with the two slashes at the beginning of the filepath.

Likely related to #1 (comment).

To Reproduce

Create a fitted model object with the usual example:

library(cmdstanr)
fit_schools_mcmc <- cmdstanr::cmdstanr_example()

Save the CSV files to a network drive:

fit_schools_mcmc$save_output_files(
  dir = "\\\\westat.com\\projects\\my-project-folder", 
  basename = "schools-fit", timestamp = FALSE, random = FALSE
)
#> Moved 3 files and set internal paths to new locations:
#> - /westat.com/projects/my-project-folder/schools-fit-1.csv
#> - /westat.com/projects/my-project-folder/schools-fit-2.csv
#> - /westat.com/projects/my-project-folder/schools-fit-3.csv

These CSV files do save successfully, all though it's a bit suspicious that the beginnings of the filepaths shown here only contain one slash.

Then try to save the fitted model object to the network drive:

fit_schools_mcmc$save_object(file.path("\\\\westat.com\\projects\\my-project-folder", "schools-fit.rds"))
#> Error in read_cmdstan_csv(files = self$output_files(include_failed = FALSE),  : 
#>  Assertion on 'files' failed: File does not exist: '/westat.com/projects/my-project-folder/schools-fit-1.csv'

I suspect there's an issue to do with the double slashes at the beginning of the filepath. See for example the following R output:

file.exists('/westat.com/projects/my-project-folder/schools-fit-1.csv')
#> [1] FALSE

file.exists('//westat.com/projects/my-project-folder/schools-fit-1.csv')
#> [1] TRUE

Expected behavior
I expected the model object to save without issue, since the CSV files were successfully copied.

Operating system
Windows 10 Enterprise

CmdStanR version number
Your CmdStanR version number (e.g. from packageVersion("cmdstanr")).

Additional context

I think the issue has to do with the internal cmdstanr utility functions repair_path() and absolute_path():

cmdstanr/R/utils.R

Lines 125 to 136 in f2e152b

repair_path <- function(path) {
if (!length(path) || !is.character(path)) {
return(path)
}
path <- path.expand(path)
path <- gsub("\\\\", "/", path)
# WSL cmdstan path is a network path and needs the leading //
path <- gsub("//(?!wsl)", "/", path, perl = TRUE)
# remove trailing "/"
path <- gsub("/$","", path)
path
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions