There are currently two ways to call Julia from Python and vice versa, which are described below. When converting from Julia to Python, there are two most important differences:
- Julia is column-major, while Python is row-major.
- Julia adopts by default 1-based indexing, while Python adopts 0-based indexing.
Vlasiator.jl can be called from Python via JuliaCall. JuliaCall will link to the first Julia version in the system path. If Vlasiator.jl has been installed, we can use it directly; otherwise we need to state it in the juliacalldeps.json file.
from juliacall import Main as jl jl.seval("using Vlasiator") file = "bulk.1d.vlsv" meta = jl.load(file)
Matplotlib can then be used to visualize the data.
from matplotlib import pyplot as plt import numpy as np rho = jl.readvariable(meta, "proton/vg_rho") x = np.arange(meta.coordmin, meta.coordmax, meta.dcoord) plt.plot(x, rho) plt.show()
There is an issue that JuliaCall may decide to check pkg installation every time for a new session. We need to first make sure that PythonCall is installed in Julia; then make sure your
PYTHONPATH is properly set. See this issue for more information.
# Handling initialization issue for Conda from julia.api import Julia jl = Julia(compiled_modules=False) from julia import Vlasiator file = "bulk1.0001000.vlsv" meta = Vlasiator.load(file) var = "proton/vg_rho" data = Vlasiator.readvariable(meta, var)
To run a Julia script in Python,
# Handling initialization issue for Conda from julia.api import Julia jl = Julia(compiled_modules=False) jl.eval('include("examples/demo_2dplot_pyplot.jl")') import matplotlib.pyplot as plt plt.show()
This approach is for you to have a taste of the package with a Python frontend. The workaround shown above for handling the static python libraries makes it slow for regular use. An alternative solution would be creating system images, but as of Julia 1.6 the user experience is not smooth. For better integrated experience with its full power, it is recommended to use the package inside Julia.