ML7331 SetUp

Author

JessiTMcP

Create Environment

In Bash (Use mamba or conda depending on system setup)

mamba create -n ml7331 python=3.9 r-base=4.4.1
mamba activate ml7331 #conda activate ml7331 etc if conda
mamba install ipykernel
python -m ipykernel install --user --name ml7331 --display-name "Python (ml7331)"
# (restart VSCode or source ~/.bashrc)
# always check nano~/.bashrc and /.bash_profile
#mamba init / conda inti - modify condarc or bashrc if need be and echo path
mamba activate ml7331
mamba install tzlocal
mamba install plotly 
pip install rpy2
pip install seaborn
pip install jupyter
pip install matplotlib
pip install scipy
pip install scikit-learn
pip install pillow
pip install tensorflow
pip install notebook
pip install folium
pip install geopandas
pip install PyGithub
pip install github3.py
pip install python-gitlab
pip check

In R

.libPaths(c("C:/Users/jessi/mambaforge/envs/ml7331/Lib/R/library", .libPaths())) 
install.packages("reticulate")
install.packages('arules')
install.packages('arulesViz')
install.packages('mlbench')
install.packages("tidyverse")
install.packages("caret")
#matlab,keras,tidyr,tidyverse,dlpyr,rmarkdown,quarto,tinytex,torch & whatever else 

Verify Installs:

library(arules)
library(arulesViz)
library(mlbench)
library(reticulate)

Save the Environment:

mamba env export -n ml7331 > ml7331_environment.yml
# If updated:
mamba env export -n ml7331 > ml7331_environment_updated.yml
conda clean --all

In R Studio:

library(reticulate)
use_condaenv("ml7331", required = TRUE)

Run a Simple Python Command:

py_run_string("x = 10")
py_run_string("y = x + 5")
py$x  # Access the Python variable in R
py$y  # Access another Python variable in R

Install an R Package Using Mamba:

system("mamba install -c conda-forge r-ggplot2")

Install numpy Using Mamba:

system("mamba install -c conda-forge numpy")

Alternatively Use PIP:

# Install numpy using pip
py_install("numpy", method = "pip")

Verify Installs:

Check TensorFlow:

tensorflow <- import("tensorflow")
print(tensorflow$__version__)

Check Keras (if needed):

keras <- import("keras")
print(keras$__version__)

Check Installed Packages:

system("mamba list")
system("pip list")

In R Markdown, Notebook, or Quarto:

x = 10
y = x + 5
print(y)
15

Notes:

  • rpy2 is used to run R within Python (install with pip).
  • reticulate is used to run Python within R (install within R).

Additional Setup Notes for August 20, 2024

Set Python Path in R Studio:

library(reticulate)
use_python("C:/Users/jessi/mambaforge/envs/ml7331/python.exe", required = TRUE)

Configure Quarto to Use the Correct Python Environment:

  • Bash:

    export QUARTO_PYTHON="C:/Users/jessi/mambaforge/envs/ml7331/python.exe"
  • Command Prompt:

    set QUARTO_PYTHON="C:/Users/jessi/mambaforge/envs/ml7331/python.exe"
  • R:

    file.edit("~/.Renviron")
  • TinyTeX Installation:

    tinytex::install_tinytex(bundle = 'TinyTeX-2')

Check Versions and Paths:

  • Get R Path:

    R.home()
  • Get Python Version:

    python --version

Example Python Code to Set R_HOME in Python:

import os  

# Set R_HOME to the full directory path  
os.environ['R_HOME'] = r'C:\Program Files\R\R-4.4.1'  

import rpy2.robjects as ro  
# Test R integration  
print(ro.r('R.version.string'))
[1] "R version 4.4.1 (2024-06-14 ucrt)"

Install Specific Package Versions: (if conflicts arise these are compatible)

mamba install \
    numpy=1.23.5 \
    pandas=2.2.2 \
    scikit-learn=1.5.1 \
    matplotlib=3.9.1 \
    seaborn=0.13.2 \
    tensorflow=2.10.0 \
    jupyterlab=4.2.4
mamba install \
    scipy=1.13.1 \
    pytorch=2.3.1 \
    torch-geometric=2.5.3 \
    transformers=4.37.2

GitHub Integration Example Code:

  • Using PyGithub:

    #from github import Github  
    
    #g = Github("your_personal_access_token")  
    
    #user = g.get_user("Texas...(username)")  
    
    #for repo in user.get_repos():  
        #print(repo.name)
  • Using GitLab:

    #import gitlab  
    
    #gl = gitlab.Gitlab('https://gitlab.com', private_token='your_private_access_token')  
    
    #projects = gl.projects.list()  
    #for project in projects:  
        #print(project.name)

GraphLab Environment Setup:

Create and Activate GraphLab Environment:

mamba create -n graphlab-env python=2.7 r-base=3.6.1
mamba activate graphlab-env
mamba install anaconda rpy2 tzlocal plotly pillow
pip install tornado==4.5.3

In R:

.libPaths(c("C:/Users/jessi/mambaforge/envs/graphlab-env/Lib/R/library", .libPaths()))
install.packages("reticulate")
install.packages("ggplot2")

Save the Environment:

mamba env export -n graphlab-env > graphlab-env.yml

Reopen the Environment in R Studio:

library(reticulate)
use_condaenv("graphlab-env", required = TRUE)

Just for Reference If I’m a moron & Forget

  • List Environments:

    mamba env list
    conda env list
    pip check
    pip list
    install pip tree depd if need detailed package checks
  • Remove Environment:

    mamba remove -n environment_name --all