Reticulate in R

R Python

Read log well .las file with Reticulate in R

Rigoberto Chandomi Vazquez https://www.linkedin.com/in/rigoberto-chandomi-v%C3%A1zquez-79038495/
06-19-2021

The reticulate package provides a comprehensive set of tools for interoperability between Python and R. The package includes facilities for:

Calling Python from R in a variety of ways including R Markdown, sourcing Python scripts, importing Python modules, and using Python interactively within an R session.

Translation between R and Python objects (for example, between R and Pandas data frames, or between R matrices and NumPy arrays).

Flexible binding to different versions of Python including virtual environments and Conda environments.

Load reticulate and dplyr packages to use Python libraries and manipulate data. We can specify the python path o use default path variable

#install.packages("reticulate")
library(reticulate)
library(dplyr)
use_python("C:/Python37")

Load python libraries; welly to read .las file and numpy for manage arrays and dataframes. In the import() function the module parameter is the the name of the Python module, convert parameter indicate if Python objects be automatically converted to their R equivalent.

wl <- import(module = "welly",convert = FALSE)
np <- import(module = "numpy",convert = FALSE)

Loading .las data, in this case is log data from a Volve field well, taked from public dataset. We use $ operator to call functions from python libraries

w1.las =  wl$Well$from_las('159-19A_LFP.las')

After read LAS file we can convert it in pandas dataframe, then in a R equivalente object with py_to_r() function

#convert to pandas dataframe
w1.pyDF = wl$Well$df(w1.las)

#Convert to R object
w1.rDF <- py_to_r(w1.pyDF)

Now we can manipulate log data in R envairoment. Using select() function from dplyr package, get only 5 variables (columns) from dataframe

#Select 5 columns
W1.log <- w1.rDF %>%
       select(LFP_GR,LFP_DT,LFP_NPHI,LFP_RHOB,LFP_RT)

#Set columns names
colnames(W1.log) <- c("GR","DT","NPHI","RHOB","RT")

head(W1.log)
                GR       DT      NPHI     RHOB       RT
3500.0183 36.62100 76.72920 0.1542000 2.460200 1.791000
3500.1707 36.37400 77.24730 0.1694000 2.468000 1.756000
3500.3231 30.74738 77.84653 0.1775994 2.472983 1.719984
3500.4755 29.79562 78.35677 0.1767006 2.447117 1.696016
3500.6279 27.34600 78.65600 0.1662000 2.446000 1.697000
3500.7803 24.99700 78.74900 0.1716000 2.449000 1.738000
summary(W1.log)
       GR                DT              NPHI         
 Min.   :   0.00   Min.   : 58.60   Min.   : 0.06075  
 1st Qu.:  23.86   1st Qu.: 72.26   1st Qu.: 0.13980  
 Median :  37.22   Median : 77.53   Median : 0.17592  
 Mean   :  54.16   Mean   : 80.92   Mean   : 0.22887  
 3rd Qu.:  66.33   3rd Qu.: 84.82   3rd Qu.: 0.23339  
 Max.   :1567.09   Max.   :131.95   Max.   :61.88736  
 NA's   :56                                           
      RHOB             RT          
 Min.   :1.991   Min.   :   0.075  
 1st Qu.:2.362   1st Qu.:   1.559  
 Median :2.467   Median :   2.081  
 Mean   :2.448   Mean   :   7.327  
 3rd Qu.:2.551   3rd Qu.:   4.500  
 Max.   :3.019   Max.   :1920.751  
 NA's   :4                         

Files

Citation

For attribution, please cite this work as

Vazquez (2021, June 19). Chato Solutions: Reticulate in R. Retrieved from https://www.chatosolutions.com/posts/2021-06-19-reticulateex/

BibTeX citation

@misc{vazquez2021reticulate,
  author = {Vazquez, Rigoberto Chandomi},
  title = {Chato Solutions: Reticulate in R},
  url = {https://www.chatosolutions.com/posts/2021-06-19-reticulateex/},
  year = {2021}
}