This notebook demos how to use Python and Julia in the same notebook using SoS as the super kernel that communicates with the Python and Julia kernels, and exchange variables between them. Note that SoS is by itself based on Python 3 so it is good enough to have SoS and Julia for the features demonstrated in this notebook.
The following are versions of Julia and Python used in this notebook, but a %sessioninfo
magic shown at the end of this notebook is recommended for this purpose.
VERSION
v"0.6.3"
import sys
sys.version
'3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29) \n[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]'
Let us start from the example of this notebook, which allows calling Python functions from Julia in a very nice way. It is almost painstaking to achieve the same effect in SoS, but let us do it in the SoS way anyway:
So we first create an array in Julia. Because SoS cannot yet handle the StepRangeLen
type of Julia, let us create an Array
t = Array(linspace(0, 2*pi,1000)); # use the julia `linspace` and `pi`
We then transfer this Array
to Python2 as a numpy.array
,
%get t --from Julia
import numpy as np
s = 3*t + 4*np.cos(2*t) # use the numpy cosine
Then back to Julia to apply sin.
%get s --from Python3
s = sin.(s)
1000-element Array{Float64,1}: -0.756802 -0.768798 -0.780133 -0.790831 -0.800913 -0.810404 -0.819324 -0.827696 -0.835542 -0.842882 -0.849738 -0.856128 -0.862072 ⋮ -0.575016 -0.595685 -0.615473 -0.634393 -0.652464 -0.669704 -0.68613 -0.701765 -0.716627 -0.73074 -0.744124 -0.756802
Then to Python3 to plot
%get s --from Julia
%matplotlib inline
import matplotlib.pyplot as plt
fig = plt.gcf() # **** WATCH THIS VARIABLE ****
plt.plot(t, s, color="red", linewidth=2.0, linestyle="--", label="sin(3t+4.cos(2t))")
[<matplotlib.lines.Line2D at 0x1163995f8>]
The SoS way is less convenient but it has the advantage of smoother learning curve. Basically, you are using two authentic Jupyter kernels with no additional syntax or library to learn, other than the %get
magic to exchange variables between the two kernels.
Just to demonstrate the transfer of dataframes between kernels, let us create a DataFrame
in Julia
using DataFrames
df = DataFrame(A = 1:.02:10, B= sin.(1:.02:10))
A | B | |
---|---|---|
1 | 1.0 | 0.841471 |
2 | 1.02 | 0.852108 |
3 | 1.04 | 0.862404 |
4 | 1.06 | 0.872355 |
5 | 1.08 | 0.881958 |
6 | 1.1 | 0.891207 |
7 | 1.12 | 0.9001 |
8 | 1.14 | 0.908633 |
9 | 1.16 | 0.916803 |
10 | 1.18 | 0.924606 |
11 | 1.2 | 0.932039 |
12 | 1.22 | 0.939099 |
13 | 1.24 | 0.945784 |
14 | 1.26 | 0.95209 |
15 | 1.28 | 0.958016 |
16 | 1.3 | 0.963558 |
17 | 1.32 | 0.968715 |
18 | 1.34 | 0.973485 |
19 | 1.36 | 0.977865 |
20 | 1.38 | 0.981854 |
21 | 1.4 | 0.98545 |
22 | 1.42 | 0.988652 |
23 | 1.44 | 0.991458 |
24 | 1.46 | 0.993868 |
25 | 1.48 | 0.995881 |
26 | 1.5 | 0.997495 |
27 | 1.52 | 0.99871 |
28 | 1.54 | 0.999526 |
29 | 1.56 | 0.999942 |
30 | 1.58 | 0.999958 |
⋮ | ⋮ | ⋮ |
We can pass it to the SoS kernel where a %preview
magic can be used to display the dataframe in a searchable and sortable table,
%preview -n df
%get df --from Julia
A | B | |
---|---|---|
0 | 1.00 | 0.841471 |
1 | 1.02 | 0.852108 |
2 | 1.04 | 0.862404 |
3 | 1.06 | 0.872355 |
4 | 1.08 | 0.881958 |
5 | 1.10 | 0.891207 |
6 | 1.12 | 0.900100 |
7 | 1.14 | 0.908633 |
8 | 1.16 | 0.916803 |
9 | 1.18 | 0.924606 |
10 | 1.20 | 0.932039 |
11 | 1.22 | 0.939099 |
12 | 1.24 | 0.945784 |
13 | 1.26 | 0.952090 |
14 | 1.28 | 0.958016 |
15 | 1.30 | 0.963558 |
16 | 1.32 | 0.968715 |
17 | 1.34 | 0.973485 |
18 | 1.36 | 0.977865 |
19 | 1.38 | 0.981854 |
20 | 1.40 | 0.985450 |
21 | 1.42 | 0.988652 |
22 | 1.44 | 0.991458 |
23 | 1.46 | 0.993868 |
24 | 1.48 | 0.995881 |
25 | 1.50 | 0.997495 |
26 | 1.52 | 0.998710 |
27 | 1.54 | 0.999526 |
28 | 1.56 | 0.999942 |
29 | 1.58 | 0.999958 |
30 | 1.60 | 0.999574 |
31 | 1.62 | 0.998790 |
32 | 1.64 | 0.997606 |
33 | 1.66 | 0.996024 |
34 | 1.68 | 0.994043 |
35 | 1.70 | 0.991665 |
36 | 1.72 | 0.988890 |
37 | 1.74 | 0.985719 |
38 | 1.76 | 0.982154 |
39 | 1.78 | 0.978197 |
40 | 1.80 | 0.973848 |
41 | 1.82 | 0.969109 |
42 | 1.84 | 0.963983 |
43 | 1.86 | 0.958471 |
44 | 1.88 | 0.952576 |
45 | 1.90 | 0.946300 |
46 | 1.92 | 0.939645 |
47 | 1.94 | 0.932615 |
48 | 1.96 | 0.925212 |
49 | 1.98 | 0.917438 |
50 | 2.00 | 0.909297 |
51 | 2.02 | 0.900793 |
52 | 2.04 | 0.891929 |
53 | 2.06 | 0.882707 |
54 | 2.08 | 0.873133 |
55 | 2.10 | 0.863209 |
56 | 2.12 | 0.852940 |
57 | 2.14 | 0.842330 |
58 | 2.16 | 0.831383 |
59 | 2.18 | 0.820104 |
60 | 2.20 | 0.808496 |
61 | 2.22 | 0.796565 |
62 | 2.24 | 0.784316 |
63 | 2.26 | 0.771753 |
64 | 2.28 | 0.758881 |
65 | 2.30 | 0.745705 |
66 | 2.32 | 0.732231 |
67 | 2.34 | 0.718465 |
68 | 2.36 | 0.704411 |
69 | 2.38 | 0.690075 |
70 | 2.40 | 0.675463 |
71 | 2.42 | 0.660581 |
72 | 2.44 | 0.645435 |
73 | 2.46 | 0.630031 |
74 | 2.48 | 0.614374 |
75 | 2.50 | 0.598472 |
76 | 2.52 | 0.582331 |
77 | 2.54 | 0.565956 |
78 | 2.56 | 0.549355 |
79 | 2.58 | 0.532535 |
80 | 2.60 | 0.515501 |
81 | 2.62 | 0.498262 |
82 | 2.64 | 0.480823 |
83 | 2.66 | 0.463191 |
84 | 2.68 | 0.445375 |
85 | 2.70 | 0.427380 |
86 | 2.72 | 0.409214 |
87 | 2.74 | 0.390885 |
88 | 2.76 | 0.372399 |
89 | 2.78 | 0.353764 |
90 | 2.80 | 0.334988 |
91 | 2.82 | 0.316078 |
92 | 2.84 | 0.297041 |
93 | 2.86 | 0.277886 |
94 | 2.88 | 0.258619 |
95 | 2.90 | 0.239249 |
96 | 2.92 | 0.219784 |
97 | 2.94 | 0.200230 |
98 | 2.96 | 0.180596 |
99 | 2.98 | 0.160890 |
100 | 3.00 | 0.141120 |
101 | 3.02 | 0.121293 |
102 | 3.04 | 0.101418 |
103 | 3.06 | 0.081502 |
104 | 3.08 | 0.061554 |
105 | 3.10 | 0.041581 |
106 | 3.12 | 0.021591 |
107 | 3.14 | 0.001593 |
108 | 3.16 | -0.018406 |
109 | 3.18 | -0.038398 |
110 | 3.20 | -0.058374 |
111 | 3.22 | -0.078327 |
112 | 3.24 | -0.098249 |
113 | 3.26 | -0.118131 |
114 | 3.28 | -0.137966 |
115 | 3.30 | -0.157746 |
116 | 3.32 | -0.177462 |
117 | 3.34 | -0.197108 |
118 | 3.36 | -0.216675 |
119 | 3.38 | -0.236155 |
120 | 3.40 | -0.255541 |
121 | 3.42 | -0.274825 |
122 | 3.44 | -0.293998 |
123 | 3.46 | -0.313054 |
124 | 3.48 | -0.331985 |
125 | 3.50 | -0.350783 |
126 | 3.52 | -0.369441 |
127 | 3.54 | -0.387951 |
128 | 3.56 | -0.406306 |
129 | 3.58 | -0.424498 |
130 | 3.60 | -0.442520 |
131 | 3.62 | -0.460366 |
132 | 3.64 | -0.478027 |
133 | 3.66 | -0.495497 |
134 | 3.68 | -0.512769 |
135 | 3.70 | -0.529836 |
136 | 3.72 | -0.546691 |
137 | 3.74 | -0.563327 |
138 | 3.76 | -0.579738 |
139 | 3.78 | -0.595917 |
140 | 3.80 | -0.611858 |
141 | 3.82 | -0.627554 |
142 | 3.84 | -0.642999 |
143 | 3.86 | -0.658186 |
144 | 3.88 | -0.673111 |
145 | 3.90 | -0.687766 |
146 | 3.92 | -0.702146 |
147 | 3.94 | -0.716246 |
148 | 3.96 | -0.730058 |
149 | 3.98 | -0.743579 |
150 | 4.00 | -0.756802 |
151 | 4.02 | -0.769723 |
152 | 4.04 | -0.782336 |
153 | 4.06 | -0.794636 |
154 | 4.08 | -0.806618 |
155 | 4.10 | -0.818277 |
156 | 4.12 | -0.829609 |
157 | 4.14 | -0.840609 |
158 | 4.16 | -0.851273 |
159 | 4.18 | -0.861597 |
160 | 4.20 | -0.871576 |
161 | 4.22 | -0.881206 |
162 | 4.24 | -0.890484 |
163 | 4.26 | -0.899405 |
164 | 4.28 | -0.907967 |
165 | 4.30 | -0.916166 |
166 | 4.32 | -0.923998 |
167 | 4.34 | -0.931461 |
168 | 4.36 | -0.938551 |
169 | 4.38 | -0.945266 |
170 | 4.40 | -0.951602 |
171 | 4.42 | -0.957558 |
172 | 4.44 | -0.963131 |
173 | 4.46 | -0.968319 |
174 | 4.48 | -0.973119 |
175 | 4.50 | -0.977530 |
176 | 4.52 | -0.981550 |
177 | 4.54 | -0.985178 |
178 | 4.56 | -0.988411 |
179 | 4.58 | -0.991249 |
180 | 4.60 | -0.993691 |
181 | 4.62 | -0.995735 |
182 | 4.64 | -0.997381 |
183 | 4.66 | -0.998628 |
184 | 4.68 | -0.999476 |
185 | 4.70 | -0.999923 |
186 | 4.72 | -0.999971 |
187 | 4.74 | -0.999619 |
188 | 4.76 | -0.998867 |
189 | 4.78 | -0.997715 |
190 | 4.80 | -0.996165 |
191 | 4.82 | -0.994216 |
192 | 4.84 | -0.991869 |
193 | 4.86 | -0.989125 |
194 | 4.88 | -0.985986 |
195 | 4.90 | -0.982453 |
196 | 4.92 | -0.978526 |
197 | 4.94 | -0.974208 |
198 | 4.96 | -0.969501 |
199 | 4.98 | -0.964405 |
even generate a scatterplot with tooltips (move your cursor to the points to see):
%preview df -n -s scatterplot A B
You can plot the dataframe in SoS using matplotlib
(remember, SoS is based on Python3)
import matplotlib.pyplot as plt
plt.plot(df['A'], df['B'])
[<matplotlib.lines.Line2D at 0x11b05ad30>]
Or transfer to the Python3 kernel if you wish:
%get df
plt.plot(df['A'], df['B'])
[<matplotlib.lines.Line2D at 0x118257320>]
As a good practice, all SoS Notebook should ends with a %sessioninfo
magic to show the session information of all kernels used in the notebook, and/or a %revisions
magic to show revisions of the notebook.
%sessioninfo
SoS
SoS Version | 0.16.12 |
---|
Julia
Kernel | julia-0.6 |
---|---|
Language | Julia |
Julia Version 0.6.3 Commit d55cadc350 (2018-05-28 20:20 UTC) Platform Info: OS: macOS (x86_64-apple-darwin14.5.0) CPU: Intel(R) Xeon(R) CPU E5-1650 v2 @ 3.50GHz WORD_SIZE: 64 uname: Darwin 17.4.0 Darwin Kernel Version 17.4.0: Sun Dec 17 09:19:54 PST 2017; root:xnu-4570.41.2~1/RELEASE_X86_64 x86_64 i386 Memory: 32.0 GB (541.734375 MB free) Uptime: 946297.0 sec Load Avg: 3.634765625 3.09228515625 3.03857421875 Intel(R) Xeon(R) CPU E5-1650 v2 @ 3.50GHz: speed user nice sys idle irq #1-12 3500 MHz 3216700 s 0 s 3255575 s 107086561 s 0 s BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge) LAPACK: libopenblas64_ LIBM: libopenlibm LLVM: libLLVM-3.9.1 (ORCJIT, ivybridge) Environment: LDFLAGS = -Wl,-pie -Wl,-headerpad_max_install_names -Wl,-dead_strip_dylibs TERM = xterm-color CPPFLAGS = -D_FORTIFY_SOURCE=2 -mmacosx-version-min=10.9 CDPATH = .:/Users/bpeng1:/Users/bpeng1/research/Projects CXXFLAGS = -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -stdlib=libc++ -fvisibility-inlines-hidden -std=c++14 -fmessage-length=0 PATH = /Applications/Julia-0.6.app/Contents/Resources/julia/bin:/Applications/Julia-0.6.app/Contents/Resources/julia/bin:/Users/bpeng1/anaconda3/envs/sos/bin:/usr/local/opt/icu4c/sbin:/usr/local/opt/icu4c/bin:/Users/bpeng1/.npm-packages/bin:/Applications/MacVim.app/Contents/bin:/Applications/Julia-0.6.app/Contents/Resources/julia/bin:/Applications/Octave.app/Contents/Resources/usr/bin/:/Users/bpeng1/perl5/bin:/usr/local/bin:/Users/bpeng1/anaconda3/envs/sos/bin:/usr/local/opt/icu4c/sbin:/usr/local/opt/icu4c/bin:/Users/bpeng1/.npm-packages/bin:/Applications/MacVim.app/Contents/bin:/Applications/Julia-0.6.app/Contents/Resources/julia/bin:/Applications/Octave.app/Contents/Resources/usr/bin/:/Users/bpeng1/perl5/bin:/usr/local/bin:/bin:/sbin:/usr/bin:/usr/sbin DEBUG_CXXFLAGS = -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -stdlib=libc++ -fvisibility-inlines-hidden -std=c++14 -fmessage-length=0 -Og -g -Wall -Wextra DEBUG_FFLAGS = -march=nocona -mtune=core2 -ftree-vectorize -fPIC -fstack-protector -O2 -pipe -march=nocona -mtune=core2 -ftree-vectorize -fPIC -fstack-protector -O2 -pipe -Og -g -Wall -Wextra -fcheck=all -fbacktrace -fimplicit-none -fvar-tracking-assignments XPC_FLAGS = 0x0 HOME = /Users/bpeng1 FORTRANFLAGS = -march=nocona -mtune=core2 -ftree-vectorize -fPIC -fstack-protector -O2 -pipe CFLAGS = -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe DEBUG_CFLAGS = -march=core2 -mtune=haswell -mssse3 -ftree-vectorize -fPIC -fPIE -fstack-protector-strong -O2 -pipe -Og -g -Wall -Wextra LDFLAGS_LD = -pie -headerpad_max_install_names -dead_strip_dylibs FFLAGS = -march=nocona -mtune=core2 -ftree-vectorize -fPIC -fstack-protector -O2 -pipe FONTCONFIG_PATH = /Applications/Julia-0.6.app/Contents/Resources/julia/etc/fonts Package Directory: /Users/bpeng1/.julia/v0.6 5 required packages: - DataFrames 0.11.7 - Feather 0.4.0 - IJulia 1.10.0 - NamedArrays 0.7.0 - RDatasets 0.4.0 32 additional packages: - Arrow 0.1.2 - BinaryProvider 0.3.3 - CSV 0.2.5 - CategoricalArrays 0.3.13 - CodecZlib 0.4.4 - Combinatorics 0.6.0 - Compat 1.2.0 - Conda 1.0.2 - DataStreams 0.3.6 - DataStructures 0.8.4 - FileIO 0.9.1 - FlatBuffers 0.3.2 - InternedStrings 0.6.2 - IterTools 0.2.1 - JSON 0.17.2 - MbedTLS 0.5.13 - Missings 0.2.10 - Mocking 0.5.7 - NamedTuples 4.0.2 - Nullables 0.0.8 - Polynomials 0.4.0 - RData 0.4.0 - Reexport 0.1.0 - SHA 0.5.7 - SoftGlobalScope 1.0.7 - SortingAlgorithms 0.2.1 - StatsBase 0.23.1 - TimeZones 0.8.0 - TranscodingStreams 0.5.4 - VersionParsing 1.1.3 - WeakRefStrings 0.4.7 - ZMQ 0.6.4 |
Python3
Kernel | python3 |
---|---|
Language | Python3 |
Version | 3.6.6 |Anaconda, Inc.| (default, Jun 28 2018, 11:07:29) [GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)] |
numpy | 1.15.0 |
%revisions
Revision | Author | Date | Message |
---|---|---|---|
8fdd03a | Chris Holdgraf | 2018-10-09 | adding SOS files |