Skip to content

Commit

Permalink
Merge pull request #74 from jiajic/py_patch
Browse files Browse the repository at this point in the history
Py patch
  • Loading branch information
mattobny authored Nov 20, 2023
2 parents 4bcb787 + 6dfe84b commit 55c32d9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
2 changes: 1 addition & 1 deletion R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ utils::globalVariables(
"extract_layered_data","set_adg_nn","find_NN_keys",
"extract_NN_connectivities","extract_NN_distances",
"extract_NN_info", "align_network_data", "extract_SN_connectivities",
"extract_SN_distances", "set_adg_sn"
"extract_SN_distances", "set_adg_sn", 'create_Anndata'
)
)

70 changes: 35 additions & 35 deletions R/python_environment.R
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,8 @@ installGiottoEnvironment = function(packages_to_install = c('pandas==1.5.1',
if (is.null(mini_install_path)){
conda_path = reticulate::miniconda_path()
} else if (!dir.exists(mini_install_path)) {
stop(GiottoUtils::wrap_msg(paste0(" Unable to install miniconda in ",
mini_install_path,
stop(GiottoUtils::wrap_msg(paste0(" Unable to install miniconda in ",
mini_install_path,
"\nPlease ensure the directory has been created and provided as a string.")))
} else {
conda_path = mini_install_path
Expand Down Expand Up @@ -551,31 +551,31 @@ py_install_prompt <- function (package = NULL,

#' @title Install Package from GitHub Link
#' @name install_github_link_pip
#' @param link link to github repository containing a python package,
#' @param link link to github repository containing a python package,
#' e.g. `git+https://github.com/TencentAILabHealthcare/pysodb.git`
#' @param env conda environment to which `link` will be installed via pip
#' @description
#' Installs `link` to python `env`
#' @keywords internal
install_github_link_pip <- function(link = NULL,
env = NULL){
# Guard
env = NULL) {
# Guard
if (is.null(link) | is.null(env)) stop(GiottoUtils::wrap_txt("Incorrect Usage.", errWidth = TRUE))

config <- reticulate::py_discover_config(use_environment=env)
# system commands return 0 if ran successfully, 1 otherwise
successful_install = !as.logical(system2(config$python,
c("-m",
successful_install = !as.logical(system2(config$python,
c("-m",
"pip",
"install",
link)))
if (successful_install) {
return(TRUE)
} else {
git_url_err_msg = "Provided GitHub URL `"
git_url_err_msg = paste0(git_url_err_msg, github_package_URL, "`")
git_url_err_msg = paste0(git_url_err_msg, link, "`")
git_url_err_msg = paste0(git_url_err_msg, " Could not be installed.\n")
git_url_err_msg = paste0(git_url_err_msg,
git_url_err_msg = paste0(git_url_err_msg,
"Please try again with a different URL.")
stop(GiottoUtils::wrap_txt(git_url_err_msg, errWidth = TRUE))
}
Expand All @@ -584,20 +584,20 @@ install_github_link_pip <- function(link = NULL,
#' @title Install Python Package with Reticulate
#' @name install_py_pkg_reticulate
#' @param package name of python package
#' @param env name of the environment into which the python
#' @param env name of the environment into which the python
#' package should be installed.
#' @details
#' @details
#' Installs `package` to python `env` after prompting user.
#' Installation is done via `py_install` from the
#' Installation is done via `py_install` from the
#' `reticulate` package.
#' @keywords internal
install_py_pkg_reticulate <- function(package = NULL,
env = NULL){
resp = py_install_prompt(package = package,
env = env)
if(resp != 0){
try_install = tryCatch(expr = {
reticulate::py_install(package = package,
try_install = tryCatch(expr = {
reticulate::py_install(package = package,
envname = env)
return(TRUE)

Expand Down Expand Up @@ -625,46 +625,46 @@ install_py_pkg_reticulate <- function(package = NULL,
#' a python package that may be installed with pip,
#' e.g. `git+https://github.com/TencentAILabHealthcare/pysodb.git`;
#' see details.
#' @param env_to_use name of the environment into which the python
#' @param env_to_use name of the environment into which the python
#' package should be installed.
#' @description checks python environment for a
#' @description checks python environment for a
#' provided package, installs if it is not found.
#' @details
#' Parameter `github_package_url` takes precedent over
#' Parameter `github_package_url` takes precedent over
#' `package_name`, i.e. if both are provided, only the github
#' URL will be installed. This function should only be provided
#' one parameter, or the other.
#'
#' one parameter, or the other.
#'
#' @keywords export
checkPythonPackage <- function (package_name = NULL,
github_package_url = NULL,
env_to_use = "giotto_env"){
# Guard clauses
if (is.null(package_name) & is.null(github_package_url)){
null_input_err_msg = "A python package name must be provided, e.g. `scanpy==1.9.0`"
null_input_err_msg = paste0(null_input_err_msg,
null_input_err_msg = paste0(null_input_err_msg,
"\nAlternatively, provide a github package URL, ")
null_input_err_msg = paste0(null_input_err_msg,
null_input_err_msg = paste0(null_input_err_msg,
"e.g. `git+https://github.com/TencentAILabHealthcare/pysodb.git` ")
stop(GiottoUtils::wrap_txt(null_input_err_msg,
stop(GiottoUtils::wrap_txt(null_input_err_msg,
errWidth = TRUE))
}
# Find path to currently initialized python env
path_to_env = reticulate::py_config()$pythonhome
if (!grepl(env_to_use, path_to_env)){
env_err_msg = paste0("Provided python environment `",
env_err_msg = paste0("Provided python environment `",
env_to_use,"` is not initialized.")
env_err_msg = paste0(env_err_msg,
"\nThe following python environment is in use: `",
env_err_msg = paste0(env_err_msg,
"\nThe following python environment is in use: `",
path_to_env,"`")
env_err_msg = paste0(env_err_msg,
env_err_msg = paste0(env_err_msg,
"\nTo initialize `",env_to_use,"`, you must ",
"restart your R session.")
stop(GiottoUtils::wrap_txt(env_err_msg,
errWidth = TRUE))
}
env_str_location = stringr::str_locate(path_to_env, env_to_use)[2]
# Change env_to_use from name of environment
# Change env_to_use from name of environment
# to the full environment path
env_to_use = substr(path_to_env, 1, env_str_location)

Expand All @@ -678,7 +678,7 @@ checkPythonPackage <- function (package_name = NULL,
if (resp != 0){
install_status = install_github_link_pip(link = github_package_url,
env = env_to_use)
return(install_status)
return(install_status)
} else {
stop(GiottoUtils::wrap_txt("Package not installed.\n", errWidth = TRUE))
}
Expand All @@ -687,16 +687,16 @@ checkPythonPackage <- function (package_name = NULL,
package_config = reticulate::py_list_packages()
pkgs_in_py_env = package_config$package
versions = package_config$version

# package installed, right version --> exit
# package installed, but wrong version --> prompt for install
# package not installed --> prompt for install

version_number = NULL

contains_version_number = grepl("==", package_name)
contains_version_number = grepl("==", package_name)
if (contains_version_number){
split_package_version = stringr::str_split(package_name,
split_package_version = stringr::str_split(package_name,
pattern = "==")
package_name = split_package_version[[1]][1]
version_number = split_package_version[[1]][2]
Expand All @@ -705,8 +705,8 @@ checkPythonPackage <- function (package_name = NULL,
if (package_name %in% pkgs_in_py_env){
if (!contains_version_number){
# If a version number is not provided,
# and the package exists within the
# reticulate package list, exit,
# and the package exists within the
# reticulate package list, exit,
# since it is already installed.
return(TRUE)

Expand All @@ -719,7 +719,7 @@ checkPythonPackage <- function (package_name = NULL,
# if the versions match, the right version
# is installed
return(TRUE)
} else{
} else{
# Otherwise, install the provided version
inst_result = install_py_pkg_reticulate(package = paste0(package_name,
"==",
Expand Down

0 comments on commit 55c32d9

Please sign in to comment.