Files
Julia_Academy/Data_Science/11__From_other_languages.ipynb
2021-06-26 18:42:18 +02:00

10 KiB

Using other languages

Often, I hear that the biggest challenge of moving from another language to Julia is giving up all the codes you have written in other languages or your favorite packages from other languages. This notebook is not about data science, but it's about your next data science project (if you're working on a data science project in Julia and you want to use functionality from other langages). Here, we will specifically cover Python, R, and C.

Python

In [1]:
using PyCall

You can import any python package...

In [2]:
math = pyimport("math")
math.sin(math.pi / 4) # returns ≈ 1/√2 = 0.70710678...
Out[2]:
0.7071067811865475

Hint: you may need to do:

julia> using Conda
julia> Conda.add("networkx")

for the line below to work.

In [3]:
python_networkx = pyimport("networkx")
Out[3]:
PyObject <module 'networkx' from '/opt/conda/lib/python3.9/site-packages/networkx/__init__.py'>

You can also write your own Python code as follows

In [4]:
py"""
import numpy
def find_best_fit_python(xvals,yvals):
    meanx = numpy.mean(xvals)
    meany = numpy.mean(yvals)
    stdx = numpy.std(xvals)
    stdy = numpy.std(yvals)
    r = numpy.corrcoef(xvals,yvals)[0][1]
    a = r*stdy/stdx
    b = meany - a*meanx
    return a,b
"""
In [5]:
xvals = repeat(1:0.5:10, inner=2)
yvals = 3 .+ xvals .+ 2 .* rand(length(xvals)) .-1
find_best_fit_python = py"find_best_fit_python"
a,b = find_best_fit_python(xvals,yvals)
Out[5]:
(0.9590099079443583, 3.3945823140627924)

If the above python code was in a file called fit_linear.py, you can call it as follows:

python_linear_fit = pyimport("fit_linear") 
python_linear_fit.find_best_fit_python(xvals,yvals)```

R code

In [6]:
using RCall

$ can switch to an R REPL from julia's REPL. We'll take a look...

In [7]:
# we can use the rcall function
r = rcall(:sum, Float64[1.0, 4.0, 6.0])
Out[7]:
RObject{RealSxp}
[1] 11
In [8]:
typeof(r[1])
Out[8]:
Float64

The @rput allows you to put julia variable in the R context.

In [9]:
z = 1
@rput z
Out[9]:
1
In [10]:
r = R"z+z"
Out[10]:
RObject{IntSxp}
[1] 2
In [11]:
r[1]
Out[11]:
2
In [12]:
x = randn(10)
Out[12]:
10-element Vector{Float64}:
 -0.8375683420545423
  0.7936207892163949
 -0.9384437083801473
  1.3499189652001669
  0.9703398099979935
 -0.6405138798150218
 -1.5560041013786536
 -0.5666575019004383
 -0.21398352496411505
  2.059761076234788

You can apply R functions on julia variables

In [13]:
@rimport base as rbase
rbase.sum([1, 2, 3])
Out[13]:
RObject{IntSxp}
[1] 6

Hint: for the code below to work, you will need to type $ in the REPL followed by:

install.packages("boot")

the $ will enter you into the R REPL mode.

In [15]:
R"t.test($x)"
Out[15]:
RObject{VecSxp}

	One Sample t-test

data:  `#JL`$x
t = 0.11328, df = 9, p-value = 0.9123
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
 -0.7975999  0.8816938
sample estimates:
 mean of x 
0.04204696 

The equivalent in Julia would be

In [16]:
using HypothesisTests
OneSampleTTest(x)
Out[16]:
One sample t-test
-----------------
Population details:
    parameter of interest:   Mean
    value under h_0:         0
    point estimate:          0.042047
    95% confidence interval: (-0.7976, 0.8817)

Test summary:
    outcome with 95% confidence: fail to reject h_0
    two-sided p-value:           0.9123

Details:
    number of observations:   10
    t-statistic:              0.11328193986719481
    degrees of freedom:       9
    empirical standard error: 0.3711708880068253

C code

Calling standard libraries is easy

In [17]:
t = ccall(:clock, Int32, ())
Out[17]:
57583336

Can look at Python and C/C++ examples here: https://github.com/xorJane/Excelling-at-Julia-Basics-and-Beyond/blob/master/JuliaCon2019_Huda/Julia%20Wrappers.ipynb

ccall((:hello_world_repeated,"hello_world_lib.dylib"),
    Int64,
    (Int64,),
    10)
    ```

Finally, I would say that this is the only off-topic notebook in this course, and it's a topic that can be covered on its own in a standalone tutorial... Nevertheless, the goal of this notebook is to tell you that porting your code from Python, R, and C should be easy and straight forward in Julia.

🥳 One cool finding

You can easily call Python, R, C, and Cpp code from Julia!