OHDSI Home | Forums | Wiki | Github

Installing PatientLevelPrediction module on mac

Hi, I’m having a problem with installing the PatientLevelPrediction R package on my mac. My default python is Python 3.6.3 |Anaconda custom (64-bit). I confirm this version by confirming that python I’m running R version 3.4.3.I’ve updated numpy, scikit-learn, and tensorflow. When I check the installation with PatientLevelPrediction::checkPlpInstallation(), I get the following output:
Checking R population
Generating covariates
Generating cohorts
Generating outcomes
Generating exclusion

  • Done

Checking Models
Patient-Level Prediction Package version 2.0.4
AnalysisID: 20180524091809

Cohort size: 2000
Covariates: 33801

  • Ok

Initialize Python Version 2.7.10 (default, Jul 15 2017, 17:16:57)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]

Patient-Level Prediction Package version 2.0.4
AnalysisID: 20180524091809

Cohort size: 2000
Covariates: 33801

R is already connected to Python!
Traceback (most recent call last):

File “”, line 1, in

ImportError
:
No module named sklearn
Patient-Level Prediction Package version 2.0.4
AnalysisID: 20180524091809

Cohort size: 2000
Covariates: 33801

Patient-Level Prediction Package version 2.0.4
AnalysisID: 20180524091809

Cohort size: 2000
Covariates: 33801

Patient-Level Prediction Package version 2.0.4
AnalysisID: 20180524091809

Cohort size: 2000
Covariates: 33801

Patient-Level Prediction Package version 2.0.4
AnalysisID: 20180524091809

Cohort size: 2000
Covariates: 33801

  • Done

Checking support for large data objects

  • Done

PatientLevelPrediction installation check completed…

Response code: 247357937827

The output after running the command: PatientLevelPrediction::interpretInstallCode(247357937827) is:
Issue with createStudyPopulation()
Issue with lasso logistic regression
Issue with random forest
Issue with mlp
Issue with ada boost
Issue with decison tree
Issue with naive bayes
Issue with knn
Issue with gradient boosting machine
NULL

I think the problem is that PythonInR is using Python Version 2.7.10. When I try installing PythonInR through anaconda RStudio the package fails to compile. Can anyone suggest an easy solution to fix for this, or should I try installing with the broadsea-methodslibrary docker?

@jreps

I have a similar issue on a Mac. Does PLP run in Broadsea given the Python dependency? @Rijnbeek would you have some recommendations? Thanks.

Hi, I’m currently installing all the OHDSI stuff on a mac I’ve gotten hold of to see what issues I get and how to fix them, I’ll hopefully have a full guide before the end of the day.

The problem with the creating a study population means it isn’t just python that is an issue.

Ok, so the first problem is that the createStudyPopualtion code in github had cut off the last two inputs, so I’ve update that now and that should fix the non-python issues - this was just an issue with the latest PLP github code, so running:
devtools::install_github(‘ohdsi/PatientLevelPrediction’) will fix things, you can check by running:
Library(PatientLevelPrediction)
checkPlpInstallation(python=F)

I’m going to install anaconda now and will provide information on how to get that set up on mac with PLP asap.

Update: I’ve installed anconda and that is my default python in terminal (as anaconda was added to the path using .bash_profile) but R doesn’t get the updated PATH variable, so still tries to use the default python - this can be tested by typing in R:
Sys.getenv(‘PATH’)

and typing in the terminal
echo $PATH

they differ as the R PATH doesn’t get updated. I’m going to see how I can get R to use the updated PATH and then that should solve all the issues.

Ok, so I decided I dislike this R package. The only way it seems we can get it to use python 3.6 is to specify python 3.6 when you install the PythonInR package by compiling it, something like:
library(devtools)
Sys.setenv(‘USESPECIALPYTHONVERSION’=‘python3.6’)
install_github(“cran/PythonInR”)

but you need to have tools to compile the code by running in the terminal:
xcode-select --install

However, when I tried it on my mac it has a bug in the code to compile it, so it seems impossible to set it up for me.

There is a different R package available now that does the same as PythonInR and it seems you can specify the python version in windows and on macs, so I’m going to convert the code to use that in the future.

Alternatively, it is possible to manually install the required python libraries on the default python and that should make the majority of the python classifiers available.

The thing I was able to solve earlier was related to setting it up to use the Anaconda python version instead of the default.
This allowed the package to work fine on Mac when we were still using Python 2.7.
The problem was related to the Path used in R in Rstudio is equal to the path on the system (apparently it takes this from somewhere else).

This could be fixed by adding the path you want in the ~/.REnviron file which is used by Rstudio.

If you then get the path you correctly get:

system(“python --version”)
Python 3.6.3 :: Anaconda, Inc.

However, to use python3+ you indeed as mention by Jenna have to recompile PythonInR.
I tried this (after making sure the path was right) but I got this error:

clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I/Users/Rijnbeek/anaconda3/include/python3.6m -I/Users/Rijnbeek/anaconda3/include/python3.6m -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -pedantic -I/Users/Rijnbeek/anaconda3/include -arch x86_64 -I/Users/Rijnbeek/anaconda3/include -arch x86_64 -D PYTHONLIBXY=libpython3.6m.so -D PYTHON_IN_R_NO_EXPLICIT_LINKING -fPIC -Wall -g -O2 -c init.c -o init.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/usr/local/lib -o PythonInR.so CToR.o CastPyObjects.o CastRObjects.o GetPyObjects.o PyCall.o PyRunString.o PythonInR.o Python_Explicit_Linking.o SetPyObjects.o init.o -lpython3.6m -ldl -framework CoreFoundation -Wl,-stack_size,1000000 -framework CoreFoundation -F/Library/Frameworks/R.framework/… -framework R -Wl,-framework -Wl,CoreFoundation
ld: library not found for -lpython3.6m
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [PythonInR.so] Error 1
ERROR: compilation failed for package ‘PythonInR’

  • removing ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/PythonInR’
  • restoring previous ‘/Library/Frameworks/R.framework/Versions/3.4/Resources/library/PythonInR’
    Installation failed: Command failed (1)

I have posted an issue on their BitBucket:

I agree with Jenna that the flexibility of PythonInR is not optimal and very recently a new package by the Rstudio team has been developed which is good to have a look at:

https://blog.rstudio.com/2018/03/26/reticulate-r-interface-to-python/

This one is much better at switching python versions.

Also I like the Rstudio integration they have build so you can run native python code in RStudio etc.

Will update here if I learn more from the PythonInR developers.

Thank you for your email. I am out of office. I will respond to your email
upon return.

For matters related to Research IT, please contact us at:
https://med.stanford.edu/researchit/contact-us.html

Hi I figured using Mac it is easier to just install the python manually rather than using anaconda (the PythonInR problem is specific to anaconda). Here are the steps I’ve used to set everything up:
Setting Up Non-Anaconda Python 3.6 for Mac Users

Step 1: Install python 3.6:

This should automatically update your PATH variable and you should be able to open python by typing into a new terminal:
python3

If this doesn’t work then you need to make sure your python 3 is in the path (you can inspect by trying in the terminal:)
echo $PATH
If it isn’t present, then you need to edit the bash profile to add it by running in the terminal:
touch ~/.bash_profile; open ~/.bash_profile;

Once python3 works, you can then install the dependancies by running in terminal:

pip3 install --upgrade pip
pip3 install -U NumPy
pip3 install -U SciPy
pip3 install -U scikit-learn
pip3 install --upgrade tensorflow
pip3 install keras

Step 2 Getting PythonInR to use python3:
In a new Rstudio session run this to open the environment file:
install.packages(‘usethis’)
usethis::edit_r_environ()::edit_r_environ()

In the file that opens add and save:
PATH= {The path containing the python 3}
USESPECIALPYTHONVERSION=“python3.6"

For example mine was:
PATH="/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin"
USESPECIALPYTHONVERSION=“python3.6”

Restart the R session and now compile the PythonInR (this requires adding the max os command line tools):
Sys.setenv(‘USESPECIALPYTHONVERSION’=‘python3.6’)
install_bitbucket(“Floooo/PythonInR”)

If all this runs you have python set up for the package. Now test to confirm. Open a new R session:
devtools::install_github(‘ohdsi/PatientLevelPrediction’)
library(PatientLevelPrediction)
PatientLevelPrediction::checkPlpInstallation(python=T)

Step 3 (optional) adding deep learning keras:
For Keras deep learning do this:
devtools::install_github(“rstudio/keras”)
library(keras)
install_keras()

This is all pretty quick to do.

Thanks Jenna! This solution works! Looks like PythonInR is referencing python 3.6 and the models are trained. I am still seeing a response code not equal to one (Response code: 2800733). Is that okay?

1 Like
t