Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.84 KB | None | 0 0
  1.    if (!is.null(condaenv) & !is.null(env)) {
  2.       stop("Only one argument from condaenv and env can be different from NULL")
  3.     }
  4.     if (!is.null(yml) &
  5.         is.null(condaenv) & .Platform$OS.type == "unix") {
  6.       stop("You have to provide condaenv parameter with yml when using unix-like OS")
  7.     }
  8.  
  9.     if (!is.null(yml)) {
  10.       # Extracting env name form file
  11.     name <- crete_env(yml, condaenv)
  12.     if()
  13.       tryCatch(
  14.         reticulate::use_condaenv(name, required = TRUE),
  15.         error = function(e) {
  16.           warning(e, call. = FALSE)
  17.           stop(
  18.             "reticulate is unable to set new environment due to already using other python.exe, please restart R session. See warnings() for original error",
  19.             call. = FALSE
  20.           )
  21.         }
  22.       )
  23.  
  24.     }
  25.  
  26.     if (!is.null(condaenv) & is.null(yml)) {
  27.       tryCatch(
  28.         reticulate::use_condaenv(condaenv, required = TRUE),
  29.         error = function(e) {
  30.           warning(e, call. = FALSE)
  31.           stop(
  32.             "reticulete is unable to set new environment due to already using other python.exe, please restart R session. See warnings() for original error",
  33.             call. = FALSE
  34.           )
  35.         }
  36.       )
  37.  
  38.  
  39.  
  40.     }
  41.  
  42.     if (!is.null(env)) {
  43.       tryCatch(
  44.         reticulate::use_virtualenv(env, required = TRUE),
  45.         error = function(e) {
  46.           warning(e, call. = FALSE)
  47.           stop(
  48.             "reticulete is unable to set new environment due to already using other python.exe, please restart R session. See warnings() for original error",
  49.             call. = FALSE
  50.           )
  51.         }
  52.       )
  53.     }
  54.  
  55.  
  56.  
  57.  
  58.     tryCatch(
  59.       model <- reticulate::py_load_object(path),
  60.  
  61.       error = function(e) {
  62.         if (grepl("UnicodeDecodeError", e)) {
  63.           warning(paste(e), call. = FALSE)
  64.           warning(
  65.             "There is a problem with encoding mismatch. You are probably using Python 3.x and are  trying to load Python 2.7 object.
  66.              Try to use virtual environment with matching encoding.",
  67.             call. = FALSE
  68.           )
  69.         }
  70.         else if (grepl("No module", e)) {
  71.           warning(paste(e), call. = FALSE)
  72.           warning(
  73.             "Your Python environment is missing some modules. Please install them using \n conda install name_of_missing_module \n or \n pip install name_of_missing_module " ,
  74.             call. = FALSE
  75.           )
  76.         }
  77.         else{
  78.           warning(paste(e), call. = FALSE)
  79.         }
  80.  
  81.         stop(
  82.           "Yours environment has to match environment where pickle file was created. It also includes encoding, python version, and libraries version. Specifying .yml file or path to virtual environment may help. For more information look warnings() and then ?scikitlearn_model",
  83.           call. = FALSE
  84.  
  85.         )
  86.       }
  87.  
  88.     )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement