APPENDIX D: Bridging Julia with Other Platforms

Appendix004.jpg

Jumping from one programming language to another is usually a hassle, particularly if you are not an expert in one of the languages. The situation can get even more challenging when you are dealing with peculiar data that must be transferred to the other platform in order to process or visualize it. This issue is often tackled by putting everything into a data file that both platforms can access. However, this still requires considerable time, especially if you are dealing with large datasets.

Bridging two platforms together is usually the best way to resolve this matter, which is made possible with some specialized packages. Julia can be bridged with various platforms, the most relevant of which to data science are Python and R. It is also possible to bridge Julia with C, C++, and Fortran, but this is something beyond the scope of this book. What’s more, there are packages in Python and R that can make this a two-way street, making Julia even more relevant. In this appendix we’ll look into how this is done.

Bridging Julia with R

Running a Julia script in R

Although R is great for plots and running statistical models, it is not the best platform for scripts that involve loops, which are often encountered in data engineering. Fortunately you can outsource this part of the data science pipeline to Julia using the rjulia package (which has the devtools package as a dependency). Depending on your operating system, you can make use of this as follows:

Installation of rjulia package:

Linux OS:

install.packages(“devtools”) # if you haven’t installed the devtools package already

install_github(“armgong/rjulia”, ref=”master”)

Windows OS:

install.packages(“devtools”) # if you haven’t installed the devtools package already

installeddevtools::install_github(“armgong/rjulia”, ref=”master”, args =  “--no-multiarch”)

Here is how you can apply the rjulia package in R:

library(rjulia)

julia_init() # the rjulia package will find your julia home folder automatically

julia_eval(“2 + 2”) # a very basic example

The output of this should be 4 and be in a data type native to the R environment.

Running an R script in Julia

In the rare case where you would want to use an R code snippet while in Julia, you can summon it using the RCall package. Here is the code you need to use to get this up and running:

In[1]: Pkg.add(“RCall”)

In[2]: using(RCall)

If everything is running smoothly you should see a confirmation message like the following:

R installation found at C:Program FilesRR-3.2.2

Since it is unlikely that you will ever need to make use of this package, we won’t elaborate on this.

Bridging Julia with Python

Running a Julia script in Python

Although Python often poses as a true programming language, in essence it is a scripting language with OOP capabilities. If you want to run a long loop in this language, you may face a lengthy wait time. That’s one of the reasons it is often useful to have Julia on the side: to do all the stuff that Python wouldn’t ever be able to handle in a reasonable time frame. To make this happen you need to use the Julia module, as follows:

At the command prompt / shell type:

pip install Julia

While in Python type the following:

import Julia

J = julia.Julia()

J.run(“1 + 1”)

This should yield the output 2 and be in a data type that is native to Python.

Running a Python script in Julia

Although unlikely, it is possible that you may need to run a Python script while in Julia (probably to make use of some package that hasn’t been developed in Julia yet). You can accomplish this by making use of the PyCall package, just like in the following example:

In[1]: using PyCall

In[2]: @pyimport numpy.random as nr

In[3]: nr.rand(3,4)

Out[3]: 3x4 Array{Float64,2}:

    0.564096 0.12906  0.828137 0.238906

    0.783359 0.682929 0.929377 0.155438

    0.511939 0.713345 0.182735 0.453748

The output values you see are bound to be different than these. However, there is something else in the output that is particularly important: the data type of the output. Even though this came from a function in the numpy package of Python, the output is still a Julia data type, fully compatible with other data structures in the environment this is run on.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset