How to transit python code to julia

Hi,
I have python codes for generate 3D tissue of plant, and the value of each point decides the color of the point(sphere). The values are from odes of the plant system.(So the python code is complex)

Do you know any way to do this?

Thanks!

Well, the first step is to check witch Python libraries you used and find equivalents in Julia (if needed, you do not need any replacement for Numpy, because array operations are part of Julia itself, or perhaps the standard library LinAlgebra).

Then start to translate the code…
I usually create a .jl file and put all the Python code in commented form into that file and then start to translate piece by piece…

It helps if you have unit tests for your Python code which you should translate to Julia first. If you do not have unit tests you need to write them to ensure the results of your Python code are identical to the results of your Julia code.

At some point search-and-replace also becomes useful, e.g. replace radians with deg2rad, ** with ^, np.zeros with zeros etc…

When the code works correctly you can start to improve the performance by benchmarking your functions, trying to get rid of allocations, using concrete types for struct members etc.

In general a Python class translates to a Julia struct plus functions that have an instance of this struct as first parameter (instead of self).

8 Likes

Thanks! I am trying to use pycall in julia. However, when I use ‘plt = pyimport(“matplotlib.pyplot”)’, it showed the error:
"The Python package matplotlib.pyplot could not be imported by pyimport. Usually this means
that you did not install matplotlib.pyplot in the Python version being used by PyCall.

PyCall is currently configured to use the Python version at:

D:\anaconda\python.exe

and you should use whatever mechanism you usually use (apt-get, pip, conda,
etcetera) to install the Python package containing the matplotlib.pyplot module."

I installed matplotlib.pyplot later via ‘conda install matplotlib’, but it still shows the problem. Could you help me with this? THANKS!

If your goal is to translate your Python code to Julia, I would not recommend trying to call your Python code in Julia. This is just adding an extra step. I would only use PyCall if you don’t intend on translating some code but you want to integrate it into your Julia code or something.

I would run your Python code in your usual Python editor for reference and translate your code into Julia just as @ufechner7 suggests. Work step by step and make each small part work correctly.

I also came from Python and moved an entire electrodynamics simulation to Julia. It’s much easier to work directly in Julia or directly in Python because then you won’t confuse the two languages as much. In fact, my Julia code looks different from my Python code because l took advantage of the unique features of Julia.

As you translate your code, you can ask specific questions about how to do something in Julia.
I’m sure very few people know how to generate 3d tissue of a plant (in any language!), so breaking your project into smaller pieces and asking more specific questions as you go will be more helpful to you, and more people will be able to help you. :wink:

2 Likes

If you are installing PyCall and PyPlot for the first time, just do ENV["PYTHON"]="" before running Pkg.add("PyPlot"). Otherwise, you can reconfigure PyCall to use Conda via:

ENV["PYTHON"]=""
Pkg.build("PyCall")

The next time you import PyPlot, it will tell Conda to install Matplotlib.

1 Like

Thanks!
I returned to the python and had the 3D tissue done using the Mayavi.

For translate python code to julia, I have one main problem, that is how to translate tellurium in python to julia, do you know how to do it?

Thanks! I got this!

See the post by tamasgal

3 Likes

As far as I see Tellurium is a Python library for simulating biological systems. There are a lot of libraries in Julia you could use for this task, so you have to be more specific what exactly you want to simulate…

1 Like