From 0d6fdcc104c95266fbea4ba1569c2e15b197adde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksandar=20Toma=C5=A1evi=C4=87?= Date: Wed, 15 Nov 2023 13:38:59 -0500 Subject: [PATCH 1/2] Fixed #5, new named conda environment --- R/setup_miniconda.R | 48 +++++++++++++++++++++++++++++++++++------- R/setup_modules.R | 2 +- man/setup_miniconda.Rd | 7 +++--- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/R/setup_miniconda.R b/R/setup_miniconda.R index d2ffeef..cba624f 100644 --- a/R/setup_miniconda.R +++ b/R/setup_miniconda.R @@ -1,15 +1,16 @@ -#' Install Miniconda +#' Install Miniconda and activate the transforEmotion environment #' -#' @description Installs miniconda +#' @description Installs miniconda and activates the transforEmotion environment #' -#' @details Installs miniconda using \code{\link[reticulate]{install_miniconda}} +#' @details Installs miniconda using \code{\link[reticulate]{install_miniconda}} and activates the transforEmotion environment using \code{\link[reticulate]{use_condaenv}}. If the transforEmotion environment does not exist, it will be created using \code{\link[reticulate]{conda_create}}. #' #' @author Alexander P. Christensen +#' Aleksandar Tomasevic #' #' @export #' # Install miniconda -# Updated 13.04.2022 +# Updated 15.11.2023 setup_miniconda <- function() { @@ -22,9 +23,40 @@ setup_miniconda <- function() # Check for try-error if(any(class(path_to_miniconda) != "try-error")){ - # Give user the deets - message("\nTo uninstall miniconda, use `reticulate::miniconda_uninstall()`") + # Give user the deets + message("\nTo uninstall miniconda, use `reticulate::miniconda_uninstall()`") } - -} + + # Create transformEmotion enviroment if it doesn't exist + + if (sum(grepl("transforEmotion", reticulate::conda_list()$name)) == 0){ + path_to_env <- try( + reticulate::conda_create("transforEmotion"), + silent = TRUE + ) + + # Check for try-error + if(any(class(path_to_env) != "try-error")){ + + # Give user the deets + message("\nNew Python virtual environment created. To remove it, use: \n `reticulate::conda_remove(\"transforEmotion\")`") + } + + } + + # Activate the environment + + Sys.unsetenv("RETICULATE_PYTHON") + reticulate::use_condaenv("transforEmotion", required = TRUE) + + # Check if the enviroment is activated + + if (grepl("transforEmotion", reticulate::py_config()$python)){ + message("\ntransforEmotion Python virtual environment activated") + } else { + # throw an error if the environment is not activated + stop("Not able to setup and activate transforEmotion Python virtual environemnt") + } + +} \ No newline at end of file diff --git a/R/setup_modules.R b/R/setup_modules.R index 6b5a08f..9811ec5 100644 --- a/R/setup_modules.R +++ b/R/setup_modules.R @@ -32,7 +32,7 @@ setup_modules <- function() Sys.sleep(1) # one second pause before the console explodes with text # Actually install the modules - reticulate::conda_install( + reticulate::conda_install("transforEmotion", packages = c( "torch", "torchvision", "torchaudio", "tensorflow", diff --git a/man/setup_miniconda.Rd b/man/setup_miniconda.Rd index e022d90..1eb2d4e 100644 --- a/man/setup_miniconda.Rd +++ b/man/setup_miniconda.Rd @@ -2,16 +2,17 @@ % Please edit documentation in R/setup_miniconda.R \name{setup_miniconda} \alias{setup_miniconda} -\title{Install Miniconda} +\title{Install Miniconda and activate the transforEmotion environment} \usage{ setup_miniconda() } \description{ -Installs miniconda +Installs miniconda and activates the transforEmotion environment } \details{ -Installs miniconda using \code{\link[reticulate]{install_miniconda}} +Installs miniconda using \code{\link[reticulate]{install_miniconda}} and activates the transforEmotion environment using \code{\link[reticulate]{use_condaenv}}. If the transforEmotion environment does not exist, it will be created using \code{\link[reticulate]{conda_create}}. } \author{ Alexander P. Christensen + Aleksandar Tomasevic } From 4e46871ebb424aa63fa32adac41aa6b9e5ebe18c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksandar=20Toma=C5=A1evi=C4=87?= Date: Wed, 15 Nov 2023 14:31:10 -0500 Subject: [PATCH 2/2] Fixed error messages, added 'torch' check in module setup --- R/setup_miniconda.R | 6 ++++-- R/transformer_scores.R | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/R/setup_miniconda.R b/R/setup_miniconda.R index cba624f..2697c6c 100644 --- a/R/setup_miniconda.R +++ b/R/setup_miniconda.R @@ -30,7 +30,7 @@ setup_miniconda <- function() # Create transformEmotion enviroment if it doesn't exist - if (sum(grepl("transforEmotion", reticulate::conda_list()$name)) == 0){ + if (!(reticulate::condaenv_exists("transforEmotion"))){ path_to_env <- try( reticulate::conda_create("transforEmotion"), silent = TRUE @@ -56,7 +56,9 @@ setup_miniconda <- function() message("\ntransforEmotion Python virtual environment activated") } else { # throw an error if the environment is not activated - stop("Not able to setup and activate transforEmotion Python virtual environemnt") + print("Your active Python environment is:") + print(reticulate::py_config()$python) + stop("Please activate the transforEmotion Python environment instead: `reticulate::use_condaenv(\"transforEmotion\", required = TRUE)`") } } \ No newline at end of file diff --git a/R/transformer_scores.R b/R/transformer_scores.R index f30d181..a1751a5 100644 --- a/R/transformer_scores.R +++ b/R/transformer_scores.R @@ -180,8 +180,8 @@ transformer_scores <- function( # Run setup for miniconda setup_miniconda() - # Check if 'transformers' module is available - if(!reticulate::py_module_available("transformers")){ + # Check if 'torch' and 'transformers' module are available + if(!(reticulate::py_module_available("transformers") & reticulate::py_module_available("torch"))){ # Run setup for modules setup_modules()